EnergySpectrumAnalyer/src/MeasureAnalysisParticleCountPlotView.cpp

139 lines
4.5 KiB
C++
Raw Normal View History

#include "MeasureAnalysisParticleCountPlotView.h"
#include "csv.h"
#include <QVector>
#include <GlobalDefine.h>
#include <QHBoxLayout>
#include <QwtPlot>
#include <QwtPlotCurve>
#include <QwtLegend>
#include <QwtPlotCanvas>
#include <QPen>
QColor getDistinctColorForManyCurves(int curveIndex)
{
// 1. 定义基础色相覆盖不同主色系0-360度
const QList<int> baseHues = {
0, // 红色
30, // 橙色
60, // 黄色
90, // 黄绿色
120, // 绿色
150, // 青绿色
180, // 青色
210, // 天蓝色
240, // 蓝色
270, // 紫色
300, // 洋红色
330 // 玫红色
};
// 2. 定义不同的饱和度/明度组合(避免颜色太暗/太灰)
const QList<QPair<int, int>> 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);
_plot = new QwtPlot(this);
layout->addWidget(_plot);
setupPlot();
}
2026-03-04 16:17:04 +08:00
void MeasureAnalysisParticleCountPlotView::SetAnalyzeDataFilename(const QMap<QString, QVariant> &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::setupPlot()
{
_plot->setTitle(QString(QStringLiteral(u"粒子计数谱")));
_plot->setCanvasBackground(Qt::white);
QwtPlotCanvas* canvas = qobject_cast<QwtPlotCanvas*>(_plot->canvas());
canvas->setFrameStyle(QFrame::NoFrame);
_plot->setAxisTitle(QwtPlot::xBottom, QString(QStringLiteral(u"道址")));
_plot->setAxisTitle(QwtPlot::yLeft, QString(QStringLiteral(u"计数")));
// set axis auto scale
_plot->setAxisAutoScale(QwtPlot::xBottom, true);
_plot->setAxisAutoScale(QwtPlot::yLeft, true);
// 设置QWT图例
QwtLegend *legend = new QwtLegend();
legend->setDefaultItemMode(QwtLegendData::Clickable);
_plot->insertLegend(legend, QwtPlot::RightLegend);
}
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<float> x, y;
while (reader.read_row(address, particle_count)) {
x.push_back(address);
y.push_back(particle_count);
}
// 绘制曲线
QwtPlotCurve* curve = new QwtPlotCurve(data_name);
curve->setPen(QPen(getDistinctColorForManyCurves(0)));
curve->setSamples(x, y);
curve->attach(_plot);
// 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();
}