timedomainplot.cpp 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. #include "TimeDomainPlot.h"
  2. #include <QQuickItem>
  3. const int TimeDomainPlot::DATA_SIZE = 512;
  4. const double TimeDomainPlot::XAXIS_RANGE_START = 0;
  5. const double TimeDomainPlot::XAXIS_RANGE_END = 400;
  6. TimeDomainPlot::TimeDomainPlot(QQuickItem *parent): QQuickPaintedItem(parent)
  7. {
  8. _customPlot = new QCustomPlot();
  9. _customPlot->setBackground(QBrush(QColor("#cde8ff")));
  10. _voltageGraph = _customPlot->addGraph();
  11. _currentGraph = _customPlot->addGraph(_customPlot->xAxis, _customPlot->yAxis2);
  12. //设置颜色
  13. _voltageGraph->setPen(QPen(Qt::red));
  14. _currentGraph->setPen(QPen(Qt::blue));
  15. //设置Y轴范围
  16. _customPlot->yAxis->setNumberFormat("gbc");/* g 灵活的格式,b 漂亮的指数形式,c 乘号改成 x */
  17. _customPlot->yAxis->setNumberPrecision(1);/* 精度 1 */
  18. _customPlot->xAxis->setRange(XAXIS_RANGE_START, XAXIS_RANGE_END);
  19. _customPlot->yAxis2->setNumberFormat("gbc");/* g 灵活的格式,b 漂亮的指数形式,c 乘号改成 x */
  20. _customPlot->yAxis2->setNumberPrecision(1);/* 精度 1 */
  21. _customPlot->yAxis2->setVisible(true);
  22. //x轴名字
  23. _customPlot->xAxis->setLabel("时间 / ms");
  24. //Y轴名字
  25. _customPlot->yAxis->setLabel("电压 / V");
  26. _customPlot->yAxis->setTickLabelColor("red");
  27. _customPlot->yAxis->setLabelColor("red");
  28. _customPlot->yAxis2->setLabel("电流 / mA");
  29. _customPlot->yAxis2->setTickLabelColor("blue");
  30. _customPlot->yAxis2->setLabelColor("blue");
  31. }
  32. TimeDomainPlot::~TimeDomainPlot()
  33. {
  34. delete _customPlot;
  35. }
  36. void TimeDomainPlot::paint(QPainter *painter)
  37. {
  38. _customPlot->setGeometry(0,0,this->width(),this->height());
  39. painter->drawPixmap(0,0,this->width(),this->height(), _customPlot->toPixmap());
  40. }
  41. void TimeDomainPlot::setVoltageGraphData(const QVector<double> &keys, const QVector<double> &values)
  42. {
  43. _voltageGraph->setData(keys, values);
  44. }
  45. void TimeDomainPlot::setCurrentGraphData(const QVector<double> &keys, const QVector<double> &values)
  46. {
  47. _currentGraph->setData(keys, values);
  48. }
  49. void TimeDomainPlot::setVoltageRange(double min, double max)
  50. {
  51. _customPlot->yAxis->setRange(min, max);
  52. }
  53. void TimeDomainPlot::setCurrentRange(double min, double max)
  54. {
  55. _customPlot->yAxis2->setRange(min, max);
  56. }
  57. void TimeDomainPlot::replot()
  58. {
  59. update(QRect(x(),y(),width(), height()));
  60. }