123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299 |
- #include "PlotView.h"
- #include "qcustomplot/CustomHistogramBars.h"
- #include "qcustomplot/SmoothCurveGraph.h"
- #include <QPainter>
- PlotView::PlotView(PlotType t, const QVector<Data> &data, const QString &title, QWidget *parent)
- : QCustomPlot(parent), m_plotType(t), m_sourceData(data), m_title(title)
- {
- }
- void PlotView::updateType(PlotView::PlotType t)
- {
- m_plotType = t;
- plot();
- }
- void PlotView::updateData(const QVector<PlotView::Data> &data)
- {
- m_sourceData = data;
- plot();
- }
- QPixmap PlotView::toPixmap()
- {
- QPixmap pixmap(this->size());
- pixmap.fill(Qt::transparent);
- this->render(&pixmap);
- return pixmap;
- }
- void PlotView::plot()
- {
- this->clearGraphs();
- switch (m_plotType) {
- case Line:
- drawLine();
- break;
- case HistogramHorizontal:
- drawHistogram(true);
- break;
- case HistogramVertical:
- drawHistogram(false);
- break;
- case Curve:
- drawCurve();
- break;
- case Area:
- drawArea();
- break;
- default:
- break;
- }
- }
- void PlotView::drawLine()
- {
- this->legend->setVisible(true);
- this->legend->setFont(QFont("微软雅黑", 9));
- this->legend->setRowSpacing(-3);
- QVector<QCPScatterStyle::ScatterShape> shapes;
- shapes << QCPScatterStyle::ssDot;
- shapes << QCPScatterStyle::ssCross;
- shapes << QCPScatterStyle::ssPlus;
- shapes << QCPScatterStyle::ssCircle;
- shapes << QCPScatterStyle::ssDisc;
- shapes << QCPScatterStyle::ssSquare;
- shapes << QCPScatterStyle::ssDiamond;
- shapes << QCPScatterStyle::ssStar;
- shapes << QCPScatterStyle::ssTriangle;
- shapes << QCPScatterStyle::ssTriangleInverted;
- shapes << QCPScatterStyle::ssCrossSquare;
- shapes << QCPScatterStyle::ssPlusSquare;
- shapes << QCPScatterStyle::ssCrossCircle;
- shapes << QCPScatterStyle::ssPlusCircle;
- shapes << QCPScatterStyle::ssPeace;
- shapes << QCPScatterStyle::ssCustom;
- QPen pen;
- for (int i = 0; i < m_sourceData.size(); i++) {
- this->addGraph();
- pen.setColor(
- QColor(qSin(i * 0.3) * 100 + 100, qSin(i * 0.6 + 0.7) * 100 + 100, qSin(i * 0.4 + 0.6) * 100 + 100));
- // generate data:
- QVector<double> x, y;
- x << 0 << 1;
- y << 0 << m_sourceData.at(i).value;
- this->graph()->setData(x, y);
- this->graph()->rescaleAxes(true);
- this->graph()->setPen(pen);
- this->graph()->setName(m_sourceData.at(i).name);
- this->graph()->setLineStyle(QCPGraph::lsLine);
- // set scatter style:
- if (shapes.at(i % shapes.size()) != QCPScatterStyle::ssCustom) {
- this->graph()->setScatterStyle(QCPScatterStyle(shapes.at(i % shapes.size()), 10));
- } else {
- QPainterPath customScatterPath;
- for (int i = 0; i < 3; ++i)
- customScatterPath.cubicTo(qCos(2 * M_PI * i / 3.0) * 9, qSin(2 * M_PI * i / 3.0) * 9,
- qCos(2 * M_PI * (i + 0.9) / 3.0) * 9, qSin(2 * M_PI * (i + 0.9) / 3.0) * 9, 0,
- 0);
- this->graph()->setScatterStyle(
- QCPScatterStyle(customScatterPath, QPen(Qt::black, 0), QColor(40, 70, 255, 50), 10));
- }
- }
- // set blank axis lines:
- this->rescaleAxes();
- this->xAxis->setTicks(false);
- this->yAxis->setTicks(true);
- this->xAxis->setTickLabels(false);
- this->yAxis->setTickLabels(true);
- if (!m_title.isEmpty()) {
- this->xAxis->setLabel(m_title);
- }
- // set axis ranges to show all data:
- this->xAxis->setRange(0, 1.2);
- this->yAxis->setRange(0, 2);
- // make top right axes clones of bottom left axes:
- this->axisRect()->setupFullAxesBox();
- // make left and bottom axes always transfer their ranges to right and top axes:
- connect(this->xAxis, SIGNAL(rangeChanged(QCPRange)), this->xAxis2, SLOT(setRange(QCPRange)));
- connect(this->yAxis, SIGNAL(rangeChanged(QCPRange)), this->yAxis2, SLOT(setRange(QCPRange)));
- // Note: we could have also just called customPlot->rescaleAxes(); instead
- // Allow user to drag axis ranges with mouse, zoom with mouse wheel and select graphs by clicking:
- this->setInteractions(QCP::iRangeDrag | QCP::iRangeZoom | QCP::iSelectPlottables);
- this->replot();
- }
- void PlotView::drawHistogram(bool horizontal)
- {
- QCPAxis *keyAxis = nullptr;
- QCPAxis *valueAxis = nullptr;
- if (horizontal) {
- keyAxis = this->yAxis;
- valueAxis = this->xAxis;
- } else {
- keyAxis = this->xAxis;
- valueAxis = this->yAxis;
- }
- // QCPBars *fossil = new QCPBars(keyAxis, valueAxis); // 使用xAxis作为柱状图的key轴,yAxis作为value轴
- CustomHistogramBars *fossil = new CustomHistogramBars(keyAxis, valueAxis);
- fossil->setAntialiased(false); // 为了更好的边框效果,关闭抗齿锯
- fossil->setName("Fossil fuels"); // 设置柱状图的名字,可在图例中显示
- fossil->setPen(QPen(QColor(0, 168, 140).lighter(130))); // 设置柱状图的边框颜色
- fossil->setBrush(QColor(0, 168, 140)); // 设置柱状图的画刷颜色
- // 为柱状图设置一个文字类型的key轴,ticks决定了轴的范围,而labels决定了轴的刻度文字的显示
- QVector<double> ticks;
- QVector<QString> labels;
- QVector<double> fossilData;
- double maxValue = 0;
- for (int i = 0; i < m_sourceData.size(); ++i) {
- ticks << i + 1;
- labels << m_sourceData.at(i).name;
- fossilData << m_sourceData.at(i).value;
- maxValue = qMax(maxValue, m_sourceData.at(i).value);
- }
- QSharedPointer<QCPAxisTickerText> textTicker(new QCPAxisTickerText);
- textTicker->addTicks(ticks, labels);
- keyAxis->setTicker(textTicker); // 设置为文字轴
- keyAxis->setTickLabelRotation(horizontal ? 15 : 75); // 轴刻度文字旋转60度
- keyAxis->setSubTicks(false); // 不显示子刻度
- keyAxis->setTickLength(0, 4); // 轴内外刻度的长度分别是0,4,也就是轴内的刻度线不显示
- keyAxis->setRange(0, ticks.size() + 2); // 设置范围
- keyAxis->setUpperEnding(QCPLineEnding::esSpikeArrow);
- if (!m_title.isEmpty()) {
- keyAxis->setLabel(m_title);
- }
- valueAxis->setRange(0, 1.1 * maxValue);
- valueAxis->setPadding(35); // 轴的内边距,可以到QCustomPlot之开始(一)看图解
- valueAxis->setUpperEnding(QCPLineEnding::esSpikeArrow);
- fossil->setData(ticks, fossilData);
- this->replot();
- }
- void PlotView::drawCurve()
- {
- QCPAxis *keyAxis = this->xAxis;
- QCPAxis *valueAxis = this->yAxis;
- SmoothCurveGraph *scGraph = new SmoothCurveGraph(keyAxis, valueAxis);
- scGraph->setPen(QPen(Qt::blue)); // line color blue for first graph
- QVector<double> x, y;
- QVector<QString> labels;
- for (int i = 0; i < m_sourceData.size(); ++i) {
- x << i + 1;
- y << m_sourceData.at(i).value;
- labels << m_sourceData.at(i).name;
- }
- // configure right and top axis to show ticks but no labels:
- // (see QCPAxisRect::setupFullAxesBox for a quicker method to do this)
- this->xAxis2->setVisible(true);
- this->xAxis2->setTickLabels(false);
- this->yAxis2->setVisible(true);
- this->yAxis2->setTickLabels(false);
- // make left and bottom axes always transfer their ranges to right and top axes:
- connect(this->xAxis, SIGNAL(rangeChanged(QCPRange)), this->xAxis2, SLOT(setRange(QCPRange)));
- connect(this->yAxis, SIGNAL(rangeChanged(QCPRange)), this->yAxis2, SLOT(setRange(QCPRange)));
- // pass data points to graphs:
- scGraph->setData(x, y);
- scGraph->setScatterStyle(QCPScatterStyle(QCPScatterStyle::ssCross, QColor(Qt::red), QColor(Qt::white), 4));
- QSharedPointer<QCPAxisTickerText> textTicker(new QCPAxisTickerText);
- textTicker->addTicks(x, labels);
- keyAxis->setTicker(textTicker); // 设置为文字轴
- keyAxis->setTickLabelRotation(75); // 轴刻度文字旋转60度
- keyAxis->setSubTicks(false); // 不显示子刻度
- keyAxis->setTickLength(0, 4); // 轴内外刻度的长度分别是0,4,也就是轴内的刻度线不显示
- keyAxis->setRange(0, x.size() + 2); // 设置范围
- keyAxis->setUpperEnding(QCPLineEnding::esSpikeArrow);
- if (!m_title.isEmpty()) {
- keyAxis->setLabel(m_title);
- }
- // let the ranges scale themselves so graph 0 fits perfectly in the visible area:
- scGraph->rescaleAxes();
- // Note: we could have also just called customPlot->rescaleAxes(); instead
- // Allow user to drag axis ranges with mouse, zoom with mouse wheel and select graphs by clicking:
- this->setInteractions(QCP::iRangeDrag | QCP::iRangeZoom | QCP::iSelectPlottables);
- this->replot();
- }
- void PlotView::drawArea()
- {
- // create multiple graphs:
- for (int gi = 0; gi < m_sourceData.size(); ++gi) {
- this->addGraph();
- QColor color(20 + 200 / m_sourceData.size() * gi, 70 * (1.6 - gi / m_sourceData.size()), 150, 150);
- this->graph()->setLineStyle(QCPGraph::lsLine);
- this->graph()->setPen(QPen(color.lighter(200)));
- this->graph()->setBrush(QBrush(color));
- this->graph()->setName(m_sourceData.at(gi).name);
- this->graph()->setScatterStyle(
- QCPScatterStyle(QCPScatterStyle::ssCross, QColor(Qt::blue), QColor(Qt::white), 4));
- // generate random walk data:
- QVector<QCPGraphData> timeData(2);
- timeData[0].key = 0;
- timeData[0].value = 0;
- timeData[1].key = 1;
- timeData[1].value = m_sourceData.at(gi).value;
- this->graph()->data()->set(timeData);
- }
- // set axis labels:
- if (!m_title.isEmpty()) {
- this->xAxis->setLabel(m_title);
- }
- this->xAxis->setTicks(false);
- this->yAxis->setTicks(true);
- this->xAxis->setTickLabels(false);
- this->yAxis->setTickLabels(true);
- // set axis ranges to show all data:
- this->xAxis->setRange(0, 1.2);
- this->yAxis->setRange(0, 2);
- // make left and bottom axes always transfer their ranges to right and top axes:
- connect(this->xAxis, SIGNAL(rangeChanged(QCPRange)), this->xAxis2, SLOT(setRange(QCPRange)));
- connect(this->yAxis, SIGNAL(rangeChanged(QCPRange)), this->yAxis2, SLOT(setRange(QCPRange)));
- // show legend with slightly transparent background brush:
- this->legend->setVisible(true);
- this->legend->setFont(QFont("微软雅黑", 9));
- this->legend->setRowSpacing(-3);
- this->legend->setBrush(QColor(255, 255, 255, 150));
- // let the ranges scale themselves so graph 0 fits perfectly in the visible area:
- // this->rescaleAxes();
- // Note: we could have also just called customPlot->rescaleAxes(); instead
- // Allow user to drag axis ranges with mouse, zoom with mouse wheel and select graphs by clicking:
- this->setInteractions(QCP::iRangeDrag | QCP::iRangeZoom | QCP::iSelectPlottables);
- this->replot();
- }
|