PlotView.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. #include "PlotView.h"
  2. #include "qcustomplot/CustomHistogramBars.h"
  3. #include "qcustomplot/SmoothCurveGraph.h"
  4. #include <QPainter>
  5. PlotView::PlotView(PlotType t, const QVector<Data> &data, const QString &title, QWidget *parent)
  6. : QCustomPlot(parent), m_plotType(t), m_sourceData(data), m_title(title)
  7. {
  8. }
  9. void PlotView::updateType(PlotView::PlotType t)
  10. {
  11. m_plotType = t;
  12. plot();
  13. }
  14. void PlotView::updateData(const QVector<PlotView::Data> &data)
  15. {
  16. m_sourceData = data;
  17. plot();
  18. }
  19. QPixmap PlotView::toPixmap()
  20. {
  21. QPixmap pixmap(this->size());
  22. pixmap.fill(Qt::transparent);
  23. this->render(&pixmap);
  24. return pixmap;
  25. }
  26. void PlotView::plot()
  27. {
  28. this->clearGraphs();
  29. switch (m_plotType) {
  30. case Line:
  31. drawLine();
  32. break;
  33. case HistogramHorizontal:
  34. drawHistogram(true);
  35. break;
  36. case HistogramVertical:
  37. drawHistogram(false);
  38. break;
  39. case Curve:
  40. drawCurve();
  41. break;
  42. case Area:
  43. drawArea();
  44. break;
  45. default:
  46. break;
  47. }
  48. }
  49. void PlotView::drawLine()
  50. {
  51. this->legend->setVisible(true);
  52. this->legend->setFont(QFont("微软雅黑", 9));
  53. this->legend->setRowSpacing(-3);
  54. QVector<QCPScatterStyle::ScatterShape> shapes;
  55. shapes << QCPScatterStyle::ssDot;
  56. shapes << QCPScatterStyle::ssCross;
  57. shapes << QCPScatterStyle::ssPlus;
  58. shapes << QCPScatterStyle::ssCircle;
  59. shapes << QCPScatterStyle::ssDisc;
  60. shapes << QCPScatterStyle::ssSquare;
  61. shapes << QCPScatterStyle::ssDiamond;
  62. shapes << QCPScatterStyle::ssStar;
  63. shapes << QCPScatterStyle::ssTriangle;
  64. shapes << QCPScatterStyle::ssTriangleInverted;
  65. shapes << QCPScatterStyle::ssCrossSquare;
  66. shapes << QCPScatterStyle::ssPlusSquare;
  67. shapes << QCPScatterStyle::ssCrossCircle;
  68. shapes << QCPScatterStyle::ssPlusCircle;
  69. shapes << QCPScatterStyle::ssPeace;
  70. shapes << QCPScatterStyle::ssCustom;
  71. QPen pen;
  72. for (int i = 0; i < m_sourceData.size(); i++) {
  73. this->addGraph();
  74. pen.setColor(
  75. QColor(qSin(i * 0.3) * 100 + 100, qSin(i * 0.6 + 0.7) * 100 + 100, qSin(i * 0.4 + 0.6) * 100 + 100));
  76. // generate data:
  77. QVector<double> x, y;
  78. x << 0 << 1;
  79. y << 0 << m_sourceData.at(i).value;
  80. this->graph()->setData(x, y);
  81. this->graph()->rescaleAxes(true);
  82. this->graph()->setPen(pen);
  83. this->graph()->setName(m_sourceData.at(i).name);
  84. this->graph()->setLineStyle(QCPGraph::lsLine);
  85. // set scatter style:
  86. if (shapes.at(i % shapes.size()) != QCPScatterStyle::ssCustom) {
  87. this->graph()->setScatterStyle(QCPScatterStyle(shapes.at(i % shapes.size()), 10));
  88. } else {
  89. QPainterPath customScatterPath;
  90. for (int i = 0; i < 3; ++i)
  91. customScatterPath.cubicTo(qCos(2 * M_PI * i / 3.0) * 9, qSin(2 * M_PI * i / 3.0) * 9,
  92. qCos(2 * M_PI * (i + 0.9) / 3.0) * 9, qSin(2 * M_PI * (i + 0.9) / 3.0) * 9, 0,
  93. 0);
  94. this->graph()->setScatterStyle(
  95. QCPScatterStyle(customScatterPath, QPen(Qt::black, 0), QColor(40, 70, 255, 50), 10));
  96. }
  97. }
  98. // set blank axis lines:
  99. this->rescaleAxes();
  100. this->xAxis->setTicks(false);
  101. this->yAxis->setTicks(true);
  102. this->xAxis->setTickLabels(false);
  103. this->yAxis->setTickLabels(true);
  104. if (!m_title.isEmpty()) {
  105. this->xAxis->setLabel(m_title);
  106. }
  107. // set axis ranges to show all data:
  108. this->xAxis->setRange(0, 1.2);
  109. this->yAxis->setRange(0, 2);
  110. // make top right axes clones of bottom left axes:
  111. this->axisRect()->setupFullAxesBox();
  112. // make left and bottom axes always transfer their ranges to right and top axes:
  113. connect(this->xAxis, SIGNAL(rangeChanged(QCPRange)), this->xAxis2, SLOT(setRange(QCPRange)));
  114. connect(this->yAxis, SIGNAL(rangeChanged(QCPRange)), this->yAxis2, SLOT(setRange(QCPRange)));
  115. // Note: we could have also just called customPlot->rescaleAxes(); instead
  116. // Allow user to drag axis ranges with mouse, zoom with mouse wheel and select graphs by clicking:
  117. this->setInteractions(QCP::iRangeDrag | QCP::iRangeZoom | QCP::iSelectPlottables);
  118. this->replot();
  119. }
  120. void PlotView::drawHistogram(bool horizontal)
  121. {
  122. QCPAxis *keyAxis = nullptr;
  123. QCPAxis *valueAxis = nullptr;
  124. if (horizontal) {
  125. keyAxis = this->yAxis;
  126. valueAxis = this->xAxis;
  127. } else {
  128. keyAxis = this->xAxis;
  129. valueAxis = this->yAxis;
  130. }
  131. // QCPBars *fossil = new QCPBars(keyAxis, valueAxis); // 使用xAxis作为柱状图的key轴,yAxis作为value轴
  132. CustomHistogramBars *fossil = new CustomHistogramBars(keyAxis, valueAxis);
  133. fossil->setAntialiased(false); // 为了更好的边框效果,关闭抗齿锯
  134. fossil->setName("Fossil fuels"); // 设置柱状图的名字,可在图例中显示
  135. fossil->setPen(QPen(QColor(0, 168, 140).lighter(130))); // 设置柱状图的边框颜色
  136. fossil->setBrush(QColor(0, 168, 140)); // 设置柱状图的画刷颜色
  137. // 为柱状图设置一个文字类型的key轴,ticks决定了轴的范围,而labels决定了轴的刻度文字的显示
  138. QVector<double> ticks;
  139. QVector<QString> labels;
  140. QVector<double> fossilData;
  141. double maxValue = 0;
  142. for (int i = 0; i < m_sourceData.size(); ++i) {
  143. ticks << i + 1;
  144. labels << m_sourceData.at(i).name;
  145. fossilData << m_sourceData.at(i).value;
  146. maxValue = qMax(maxValue, m_sourceData.at(i).value);
  147. }
  148. QSharedPointer<QCPAxisTickerText> textTicker(new QCPAxisTickerText);
  149. textTicker->addTicks(ticks, labels);
  150. keyAxis->setTicker(textTicker); // 设置为文字轴
  151. keyAxis->setTickLabelRotation(horizontal ? 15 : 75); // 轴刻度文字旋转60度
  152. keyAxis->setSubTicks(false); // 不显示子刻度
  153. keyAxis->setTickLength(0, 4); // 轴内外刻度的长度分别是0,4,也就是轴内的刻度线不显示
  154. keyAxis->setRange(0, ticks.size() + 2); // 设置范围
  155. keyAxis->setUpperEnding(QCPLineEnding::esSpikeArrow);
  156. if (!m_title.isEmpty()) {
  157. keyAxis->setLabel(m_title);
  158. }
  159. valueAxis->setRange(0, 1.1 * maxValue);
  160. valueAxis->setPadding(35); // 轴的内边距,可以到QCustomPlot之开始(一)看图解
  161. valueAxis->setUpperEnding(QCPLineEnding::esSpikeArrow);
  162. fossil->setData(ticks, fossilData);
  163. this->replot();
  164. }
  165. void PlotView::drawCurve()
  166. {
  167. QCPAxis *keyAxis = this->xAxis;
  168. QCPAxis *valueAxis = this->yAxis;
  169. SmoothCurveGraph *scGraph = new SmoothCurveGraph(keyAxis, valueAxis);
  170. scGraph->setPen(QPen(Qt::blue)); // line color blue for first graph
  171. QVector<double> x, y;
  172. QVector<QString> labels;
  173. for (int i = 0; i < m_sourceData.size(); ++i) {
  174. x << i + 1;
  175. y << m_sourceData.at(i).value;
  176. labels << m_sourceData.at(i).name;
  177. }
  178. // configure right and top axis to show ticks but no labels:
  179. // (see QCPAxisRect::setupFullAxesBox for a quicker method to do this)
  180. this->xAxis2->setVisible(true);
  181. this->xAxis2->setTickLabels(false);
  182. this->yAxis2->setVisible(true);
  183. this->yAxis2->setTickLabels(false);
  184. // make left and bottom axes always transfer their ranges to right and top axes:
  185. connect(this->xAxis, SIGNAL(rangeChanged(QCPRange)), this->xAxis2, SLOT(setRange(QCPRange)));
  186. connect(this->yAxis, SIGNAL(rangeChanged(QCPRange)), this->yAxis2, SLOT(setRange(QCPRange)));
  187. // pass data points to graphs:
  188. scGraph->setData(x, y);
  189. scGraph->setScatterStyle(QCPScatterStyle(QCPScatterStyle::ssCross, QColor(Qt::red), QColor(Qt::white), 4));
  190. QSharedPointer<QCPAxisTickerText> textTicker(new QCPAxisTickerText);
  191. textTicker->addTicks(x, labels);
  192. keyAxis->setTicker(textTicker); // 设置为文字轴
  193. keyAxis->setTickLabelRotation(75); // 轴刻度文字旋转60度
  194. keyAxis->setSubTicks(false); // 不显示子刻度
  195. keyAxis->setTickLength(0, 4); // 轴内外刻度的长度分别是0,4,也就是轴内的刻度线不显示
  196. keyAxis->setRange(0, x.size() + 2); // 设置范围
  197. keyAxis->setUpperEnding(QCPLineEnding::esSpikeArrow);
  198. if (!m_title.isEmpty()) {
  199. keyAxis->setLabel(m_title);
  200. }
  201. // let the ranges scale themselves so graph 0 fits perfectly in the visible area:
  202. scGraph->rescaleAxes();
  203. // Note: we could have also just called customPlot->rescaleAxes(); instead
  204. // Allow user to drag axis ranges with mouse, zoom with mouse wheel and select graphs by clicking:
  205. this->setInteractions(QCP::iRangeDrag | QCP::iRangeZoom | QCP::iSelectPlottables);
  206. this->replot();
  207. }
  208. void PlotView::drawArea()
  209. {
  210. // create multiple graphs:
  211. for (int gi = 0; gi < m_sourceData.size(); ++gi) {
  212. this->addGraph();
  213. QColor color(20 + 200 / m_sourceData.size() * gi, 70 * (1.6 - gi / m_sourceData.size()), 150, 150);
  214. this->graph()->setLineStyle(QCPGraph::lsLine);
  215. this->graph()->setPen(QPen(color.lighter(200)));
  216. this->graph()->setBrush(QBrush(color));
  217. this->graph()->setName(m_sourceData.at(gi).name);
  218. this->graph()->setScatterStyle(
  219. QCPScatterStyle(QCPScatterStyle::ssCross, QColor(Qt::blue), QColor(Qt::white), 4));
  220. // generate random walk data:
  221. QVector<QCPGraphData> timeData(2);
  222. timeData[0].key = 0;
  223. timeData[0].value = 0;
  224. timeData[1].key = 1;
  225. timeData[1].value = m_sourceData.at(gi).value;
  226. this->graph()->data()->set(timeData);
  227. }
  228. // set axis labels:
  229. if (!m_title.isEmpty()) {
  230. this->xAxis->setLabel(m_title);
  231. }
  232. this->xAxis->setTicks(false);
  233. this->yAxis->setTicks(true);
  234. this->xAxis->setTickLabels(false);
  235. this->yAxis->setTickLabels(true);
  236. // set axis ranges to show all data:
  237. this->xAxis->setRange(0, 1.2);
  238. this->yAxis->setRange(0, 2);
  239. // make left and bottom axes always transfer their ranges to right and top axes:
  240. connect(this->xAxis, SIGNAL(rangeChanged(QCPRange)), this->xAxis2, SLOT(setRange(QCPRange)));
  241. connect(this->yAxis, SIGNAL(rangeChanged(QCPRange)), this->yAxis2, SLOT(setRange(QCPRange)));
  242. // show legend with slightly transparent background brush:
  243. this->legend->setVisible(true);
  244. this->legend->setFont(QFont("微软雅黑", 9));
  245. this->legend->setRowSpacing(-3);
  246. this->legend->setBrush(QColor(255, 255, 255, 150));
  247. // let the ranges scale themselves so graph 0 fits perfectly in the visible area:
  248. // this->rescaleAxes();
  249. // Note: we could have also just called customPlot->rescaleAxes(); instead
  250. // Allow user to drag axis ranges with mouse, zoom with mouse wheel and select graphs by clicking:
  251. this->setInteractions(QCP::iRangeDrag | QCP::iRangeZoom | QCP::iSelectPlottables);
  252. this->replot();
  253. }