#include "MeasureAnalysisParticleCountPlotView.h" #include "qcustomplot.h" #include "csv.h" #include #include #include QColor getDistinctColorForManyCurves(int curveIndex) { // 1. 定义基础色相(覆盖不同主色系,0-360度) const QList baseHues = { 0, // 红色 30, // 橙色 60, // 黄色 90, // 黄绿色 120, // 绿色 150, // 青绿色 180, // 青色 210, // 天蓝色 240, // 蓝色 270, // 紫色 300, // 洋红色 330 // 玫红色 }; // 2. 定义不同的饱和度/明度组合(避免颜色太暗/太灰) const QList> sVCombinations = { {85, 90}, // 高饱和、高明度 {70, 85}, // 中高饱和、中高明度 {85, 75}, // 高饱和、中明度 {60, 80}, // 中饱和、中高明度 {75, 70}, // 中高饱和、中明度 {90, 80} // 极高饱和、中高明度 }; // 3. 计算当前曲线对应的色相和饱和度/明度 int hueIndex = curveIndex % baseHues.size(); // 循环使用基础色相 int svIndex = (curveIndex / baseHues.size()) % sVCombinations.size(); // 循环使用饱和度/明度组合 // 4. 获取HSV参数(色相0-360,饱和度0-255,明度0-255) int hue = baseHues[hueIndex]; int saturation = sVCombinations[svIndex].first * 255 / 100; // 转换为0-255范围 int value = sVCombinations[svIndex].second * 255 / 100; // 转换为0-255范围 // 5. 生成并返回颜色(HSV转RGB) QColor color; color.setHsv(hue, saturation, value); // 额外优化:避免极浅/极暗的颜色(保证曲线可见) if (value < 50 * 255 / 100) { value = 50 * 255 / 100; color.setHsv(hue, saturation, value); } return color; } MeasureAnalysisParticleCountPlotView::MeasureAnalysisParticleCountPlotView(QWidget* parent) : MeasureAnalysisView { parent } { this->setAnalyzeViewType(MeasureAnalysisViewType::CountSpectrum); QHBoxLayout* layout = new QHBoxLayout(this); _custom_plot = new QCustomPlot(this); layout->addWidget(_custom_plot); setupCustomPlot(); } void MeasureAnalysisParticleCountPlotView::SetAnalyzeDataFilename(const QMap &data_files_set) { for (auto it = data_files_set.begin(); it != data_files_set.end(); ++it) { loadDataFromFile(it.key(), it.value().toString()); } _custom_plot->update(); this->update(); } void MeasureAnalysisParticleCountPlotView::setupCustomPlot() { _custom_plot->setNotAntialiasedElements(QCP::aeAll); _custom_plot->setNoAntialiasingOnDrag(true); _custom_plot->xAxis->setLabel(QString(QStringLiteral(u"道址"))); _custom_plot->xAxis->setVisible(true); _custom_plot->yAxis->setLabel(QString(QStringLiteral(u"计数"))); _custom_plot->yAxis->setVisible(true); _custom_plot->xAxis2->setVisible(true); _custom_plot->xAxis2->setTickLabels(false); _custom_plot->xAxis2->setTickLength(0); ; _custom_plot->yAxis2->setVisible(true); _custom_plot->yAxis2->setTickLabels(false); _custom_plot->yAxis2->setTickLength(0); ; _custom_plot->legend->setVisible(true); _custom_plot->legend->setSelectableParts(QCPLegend::spItems); } void MeasureAnalysisParticleCountPlotView::loadDataFromFile(const QString& data_name, const QString& filename) { std::string address_str = QString(QStringLiteral(u"道址")).toStdString(); std::string count_str = QString(QStringLiteral(u"计数")).toStdString(); io::CSVReader< 2, io::trim_chars<' ', '\t'>, io::double_quote_escape<',', '"'>, io::throw_on_overflow, io::empty_line_comment > reader(QStrToSysPath(filename)); reader.read_header(io::ignore_extra_column, address_str, count_str); int address; int particle_count; QVector x, y; while (reader.read_row(address, particle_count)) { x.push_back(address); y.push_back(particle_count); } auto graph = _custom_plot->addGraph(); graph->setName(data_name); graph->setPen(QPen(getDistinctColorForManyCurves(_custom_plot->graphCount()))); graph->setData(x, y, true); graph->rescaleAxes(); _custom_plot->replot(); }