#include "PlotView.h" #include "qcustomplot/CustomHistogramBars.h" #include "qcustomplot/SmoothCurveGraph.h" #include PlotView::PlotView(PlotType t, const QVector &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 &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 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 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 ticks; QVector labels; QVector 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 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 x, y; QVector 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 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 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(); }