一杯清酒邀明月
天下本无事,庸人扰之而烦耳。

QChart 坐标轴设置

 1 // 添加轴
 2 QValueAxis *xAxis = new QValueAxis();
 3 QValueAxis *yAxis = new QValueAxis();
 4  
 5 // 坐标轴整体
 6 xAxis->setRange(0, 10);
 7 yAxis->setRange(0, 10);
 8  
 9 // 轴标题设置
10 xAxis->setTitleText(QStringLiteral("时间"));
11 xAxis->setTitleFont(QFont("宋体"));
12 QBrush brush;
13 brush.setStyle(Qt::SolidPattern);
14 brush.setColor(Qt::red);
15 xAxis->setTitleBrush(brush);
16  
17 // 轴标签设置
18 xAxis->setLabelFormat("%.1f");
19 xAxis->setLabelsAngle(45);
20 xAxis->setLabelsColor(Qt::blue);
21 xAxis->setLabelsEditable(true);
22  
23 // 轴线和刻度线设置
24 xAxis->setTickCount(11);
25 QPen pen;
26 pen.setStyle(Qt::SolidLine);
27 pen.setColor(Qt::red);
28 xAxis->setLinePen(pen);
29 xAxis->setLineVisible(true);
30 xAxis->setLinePenColor(Qt::blue);
31  
32 // 主网格线设置
33 xAxis->setGridLineVisible(true);
34 xAxis->setGridLineColor(Qt::black);
35  
36 // 次刻度和次网格线设置
37 xAxis->setMinorTickCount(1);
38 xAxis->setMinorGridLineColor(Qt::red);

完整代码

 1 QChart *chart = new QChart();
 2  
 3 chart->setTheme(QChart::ChartThemeQt);
 4 chart->setTitle("显示坐标点");
 5 chart->setMargins(QMargins(10, 10, 10, 10));
 6  
 7  
 8 QLineSeries *lineSeries0 = new QLineSeries();
 9  
10 lineSeries0->setName("时间 1");
11 lineSeries0->append(0, 8);
12 lineSeries0->append(2, 8);
13 lineSeries0->append(3, 6);
14 lineSeries0->append(5, 6);
15 lineSeries0->append(7, 9);
16 *lineSeries0 << QPointF(9, 9) << QPointF(10, 7);
17  
18 QScatterSeries *scatterSeries = new QScatterSeries();
19  
20 scatterSeries->setName("时间");
21 scatterSeries->setPointLabelsFormat("(@xPoint, @yPoint)");
22 scatterSeries->setMarkerSize(6);
23 scatterSeries->setPointLabelsVisible();
24  
25 scatterSeries->append(0, 8);
26 scatterSeries->append(2, 8);
27 scatterSeries->append(3, 6);
28 scatterSeries->append(5, 6);
29 scatterSeries->append(7, 9);
30 scatterSeries->append(9, 9);
31 scatterSeries->append(10, 7);
32  
33 // 添加轴
34 QValueAxis *xAxis = new QValueAxis();
35 QValueAxis *yAxis = new QValueAxis();
36  
37 // 坐标轴整体
38 xAxis->setRange(0, 10);
39 yAxis->setRange(0, 10);
40  
41 // 轴标题设置
42 xAxis->setTitleText(QStringLiteral("时间"));
43 xAxis->setTitleFont(QFont("宋体"));
44 QBrush brush;
45 brush.setStyle(Qt::SolidPattern);
46 brush.setColor(Qt::red);
47 xAxis->setTitleBrush(brush);
48  
49 // 轴标签设置
50 xAxis->setLabelFormat("%.1f");
51 xAxis->setLabelsAngle(45);
52 xAxis->setLabelsColor(Qt::blue);
53 xAxis->setLabelsEditable(true);
54  
55 // 轴线和刻度线设置
56 xAxis->setTickCount(11);
57 QPen pen;
58 pen.setStyle(Qt::SolidLine);
59 pen.setColor(Qt::red);
60 xAxis->setLinePen(pen);
61 xAxis->setLineVisible(true);
62 xAxis->setLinePenColor(Qt::blue);
63  
64 // 主网格线设置
65 xAxis->setGridLineVisible(true);
66 xAxis->setGridLineColor(Qt::black);
67  
68 // 次刻度和次网格线设置
69 xAxis->setMinorTickCount(1);
70 xAxis->setMinorGridLineColor(Qt::red);
71  
72 chart->addSeries(lineSeries0);
73 chart->addSeries(scatterSeries);
74  
75 chart->addAxis(xAxis, Qt::AlignBottom);
76 chart->addAxis(yAxis, Qt::AlignLeft);
77  
78 scatterSeries->attachAxis(xAxis);
79 scatterSeries->attachAxis(yAxis);
80  
81 lineSeries0->attachAxis(xAxis);
82 lineSeries0->attachAxis(yAxis);
83  
84 ui->chartView->setChart(chart);
85 ui->chartView->setRubberBand(QChartView::RectangleRubberBand);
86 ui->chartView->setRenderHint(QPainter::Antialiasing, true);

运行效果

 

 

 

posted on 2020-09-14 09:36  一杯清酒邀明月  阅读(18408)  评论(0编辑  收藏  举报