#ifndef PLOTVIEW_H #define PLOTVIEW_H #include /** * @例子 * QVector pd; pd << PlotView::Data { "经典1", 0.6 }; pd << PlotView::Data { "经典2", 0.2 }; pd << PlotView::Data { "经典3", 0.3 }; pd << PlotView::Data { "经典4", 0.61 }; pd << PlotView::Data { "经典5", 0.52 }; pd << PlotView::Data { "经典6", 0.233 }; pd << PlotView::Data { "经典7", 0.16 }; pd << PlotView::Data { "经典8", 0.22 }; pd << PlotView::Data { "经典9", 0.33 }; pd << PlotView::Data { "经典10", 0.161 }; pd << PlotView::Data { "经典11", 0.152 }; pd << PlotView::Data { "经典12", 0.133 }; pd << PlotView::Data { "经典13", 0.36 }; pd << PlotView::Data { "经典14", 0.72 }; pd << PlotView::Data { "经典15", 0.3 }; pd << PlotView::Data { "经典16", 0.861 }; pd << PlotView::Data { "经典17", 0.552 }; pd << PlotView::Data { "经典18", 0.93 }; PlotView *pv = new PlotView(PlotView::Area, pd, "标题"); */ class PlotView : public QCustomPlot { Q_OBJECT public: enum PlotType { Line, // 折线图 HistogramHorizontal, // 横向柱状图 HistogramVertical, // 竖向柱状图 // Pie, // QCustomPlot不支持饼图 Curve, // 曲线图 Area, // 面积图 }; Q_ENUM(PlotType) struct Data { QString name; double value; bool operator==(const Data &r) const { return (this->name == r.name) && (this->value == r.value); } }; explicit PlotView(PlotType t, const QVector &data, const QString &title = "", QWidget *parent = nullptr); void updateType(PlotType t); void updateData(const QVector &data); QPixmap toPixmap(); void plot(); signals: private: PlotType m_plotType; QVector m_sourceData; QString m_title; void drawLine(); void drawHistogram(bool horizontal = false); void drawCurve(); void drawArea(); }; #endif // PLOTVIEW_H