PlotView.cpp 11 KB

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