设置轴的刻度格式

  1. // axisX->setLabelFormat("%g");
  2. %A 浮点数、十六进制数字和p-记法(C99
  3. %c 一个字符
  4. %d 有符号十进制整数
  5. %e 浮点数、e-记数法
  6. %E 浮点数、E-记数法
  7. %f 浮点数、十进制记数法
  8. %g 根据数值不同自动选择%f或%e
  9. %G 根据数值不同自动选择%f或%e.
  10. %i 有符号十进制数(与%d相同)
  11. %o 无符号八进制整数
  12. %p 指针
  13. %s 字符串
  14. %u 无符号十进制整数
  15. %x 使用十六进制数字0f的无符号十六进制整数
  16. %X 使用十六进制数字0f的无符号十六进制整数
  17. %% 打印一个百分号

设置散点图和线性图

image.png

dialogGUI.h

  1. #ifndef DIALOG_GUI_H
  2. #define DIALOG_GUI_H
  3. #include <QDialog>
  4. #include <QCache>
  5. #include <QtCharts>
  6. #include <QtCharts/QSplineSeries>
  7. #include <QDateTime>
  8. #include <QByteArray>
  9. namespace Ui {
  10. class DialogGUI;
  11. }
  12. class DialogGUI : public QDialog
  13. {
  14. Q_OBJECT
  15. public:
  16. explicit DialogGUI(QWidget *parent = 0);
  17. ~DialogGUI();
  18. QList<QPointF > PointF;
  19. void sltCategoriesChanged(QByteArray);
  20. float publicFloatFun(QByteArray dat, quint8 basic_addres);
  21. public slots:
  22. private slots:
  23. // void sltpushButton();
  24. // void sltpushButton_2();
  25. // void sltpushButton_3();
  26. signals:
  27. private:
  28. Ui::DialogGUI *ui;
  29. void initline();
  30. QChart* m_chart;
  31. QLineSeries* m_lineSeries;
  32. QScatterSeries* m_scatterSeries;
  33. QBarCategoryAxis* m_axis;
  34. QValueAxis* m_axisY;
  35. QValueAxis* m_axisX;
  36. QChartView* m_chartView;
  37. QList<int> m_list;
  38. };
  39. #endif // DialogGUI_H

dialogGUI.cpp

  1. #include "dialogGUI.h"
  2. #include "ui_dialogGUI.h"
  3. #include <QtCharts/QAbstractAxis>
  4. #include <QtCharts/QValueAxis>
  5. #include <QtCore/QTime>
  6. #include <QtCore/QDebug>
  7. #include <QPen>
  8. #include <QPainter>
  9. #include <QtCharts\QLegendMarker>
  10. #include <QChartView>
  11. #define debug qDebug() << "[" << __FILE__ << ":" << __LINE__ << "]" // 调试
  12. DialogGUI::DialogGUI(QWidget *parent) :
  13. QDialog(parent),
  14. ui(new Ui::DialogGUI)
  15. {
  16. ui->setupUi(this);
  17. // initChart("name", 10, 50);
  18. float a = 1.32;
  19. float b = 5.56;
  20. PointF.append(QPointF(a, b));
  21. PointF.append(QPointF(3, 0));
  22. PointF.append(QPointF(7, 6));
  23. PointF.append(QPointF(9, 7));
  24. PointF.append(QPointF(10, 6));
  25. PointF.append(QPointF(8, 6));
  26. PointF.append(QPointF(5, 2));
  27. PointF.append(QPointF(7, 9));
  28. PointF.append(QPointF(8, 6));
  29. PointF.append(QPointF(15, 6));
  30. // 初始化
  31. initline();
  32. }
  33. DialogGUI::~DialogGUI()
  34. {
  35. delete ui;
  36. }
  37. /*-------------------------------------------------------------*/
  38. void DialogGUI::initline()
  39. {
  40. m_chart = new QChart();
  41. m_chart->setAnimationOptions(QChart::SeriesAnimations);
  42. // 线性图
  43. m_lineSeries = new QLineSeries(m_chart);
  44. m_lineSeries->setPen(QPen(Qt::lightGray,2,Qt::SolidLine));
  45. m_lineSeries->setColor(QColor(248, 167, 0));
  46. // 散点图
  47. m_scatterSeries = new QScatterSeries(m_chart);
  48. m_scatterSeries->setMarkerShape(QScatterSeries::MarkerShapeRectangle); // 设置标志形状
  49. m_scatterSeries->setMarkerSize(7); // 设置标记大小
  50. // 将线性图和散点图添加到图表上
  51. m_chart->addSeries(m_lineSeries);
  52. m_chart->addSeries(m_scatterSeries);
  53. m_chart->setTitle("单表标定点数"); // 设置曲线名字
  54. // 循环添加
  55. foreach (QPointF item, PointF) {
  56. m_lineSeries->append(item); // 添加折线
  57. m_scatterSeries->append(item); // 添加散点
  58. }
  59. // x轴
  60. m_axisX = new QValueAxis;
  61. m_axisX->setRange(0, 60); // 设置x轴范围
  62. m_axisX->setTickCount(10); //设置多少格
  63. m_axisX->setMinorTickCount(5); //设置每格小刻度线的数目
  64. m_chart->setAxisX(m_axisX, m_lineSeries);
  65. m_chart->setAxisX(m_axisX, m_scatterSeries);
  66. // m_chart->createDefaultAxes(); // 创建默认轴
  67. // m_chart->axisX()->setRange(0, 30); // 设置x轴范围
  68. // m_chart->setAcceptTouchEvents(true); // 设置接受触摸事件
  69. m_axisY = new QValueAxis;
  70. m_axisY->setRange(0, 30); // 设置y轴范围
  71. // m_axisY->setLabelFormat("%u"); //设置刻度的格式
  72. // m_axisY->setGridLineVisible(true); // 设置网格线可见
  73. m_axisY->setTickCount(10); //设置多少格
  74. m_axisY->setMinorTickCount(5); //设置每格小刻度线的数目
  75. m_chart->setAxisY(m_axisY, m_lineSeries);
  76. m_chart->setAxisY(m_axisY, m_scatterSeries);
  77. m_chart->legend()->setVisible(true);
  78. m_chart->legend()->setAlignment(Qt::AlignBottom);
  79. m_chart->legend()->hide(); // 隐藏图表说明
  80. // 视图
  81. m_chartView = new QChartView(m_chart);
  82. m_chartView->setRenderHint(QPainter::Antialiasing); // 抗锯齿
  83. // 设置主题
  84. m_chartView->chart()->setTheme(QChart::ChartThemeBrownSand);
  85. // 颜色
  86. QPalette pal = window()->palette();
  87. pal.setColor(QPalette::Window, QRgb(0x9e8965));
  88. pal.setColor(QPalette::WindowText, QRgb(0x404044));
  89. window()->setPalette(pal); // 设置调色板
  90. ui->verticalLayout->addWidget(m_chartView); // 将图表添加到ui上
  91. }
  92. // 槽函数,用于接收数据并显示到gui上
  93. void DialogGUI::sltCategoriesChanged(QByteArray data)
  94. {
  95. m_lineSeries->clear();
  96. m_scatterSeries->clear();
  97. m_chart->axisX()->setRange(0, publicFloatFun(data ,3 + 29 * 8)); // 设置x轴范围
  98. // m_axisX->setRange(0, publicFloatFun(data ,3 + 29 * 8)); // 设置x轴范围
  99. m_axisY->setRange(0, publicFloatFun(data ,7 + 29 * 8)); // 设置y轴范围
  100. for(uchar i = 0; i < 30; i++)
  101. {
  102. m_lineSeries->append(publicFloatFun(data ,3 + i * 8), publicFloatFun(data ,7 + i * 8)); // 添加折线
  103. m_scatterSeries->append(publicFloatFun(data ,3 + i * 8), publicFloatFun(data ,7 + i * 8)); // 添加散点
  104. }
  105. debug << "刷新GUI数据";
  106. }
  107. // 公共函数
  108. float DialogGUI::publicFloatFun(QByteArray dat, quint8 basic_addres)
  109. {
  110. union{
  111. float a;
  112. quint8 dat[4];
  113. }f_to_dat;
  114. f_to_dat.dat[0] = dat[basic_addres + 0];
  115. f_to_dat.dat[1] = dat[basic_addres + 1];
  116. f_to_dat.dat[2] = dat[basic_addres + 2];
  117. f_to_dat.dat[3] = dat[basic_addres + 3];
  118. return f_to_dat.a;
  119. }
  120. /*-------------------------------------------------------------*/