##// END OF EJS Templates
Tuned and documented custom chart example
Tero Ahola -
r942:b4a64675baff
parent child
Show More
1 NO CONTENT: new file 100644, binary diff hidden
NO CONTENT: new file 100644, binary diff hidden
@@ -1,7 +1,26
1 /*!
1 /*!
2 \example examples/customchart
2 \example examples/customchart
3 \title Custom chart example
3 \title Custom chart example
4 \subtitle
4 \subtitle
5
5
6 This example shows how to customize the appearance of the whole chart.
6 This example shows how to customize the appearance of the different elements on a chart.
7 \image custom_chart.png
8
9 We begin by creating a simple line series and a chart object.
10 \snippet ../examples/customchart/main.cpp 1
11
12 Let the customization begin. First we customize the series and the chart's title and background.
13 \snippet ../examples/customchart/main.cpp 2
14
15 Then we customize the axes.
16 \snippet ../examples/customchart/main.cpp 3
17
18 And the axis label values and ranges.
19 \snippet ../examples/customchart/main.cpp 4
20
21 Finally we create a view containing the chart.
22 \snippet ../examples/customchart/main.cpp 5
23
24 Now we are ready to show the chart on a main window.
25 \snippet ../examples/customchart/main.cpp 6
7 */
26 */
@@ -31,53 +31,64 int main(int argc, char *argv[])
31
31
32 //![1]
32 //![1]
33 QLineSeries* series = new QLineSeries();
33 QLineSeries* series = new QLineSeries();
34 QPen blue(Qt::yellow);
35 blue.setWidth(3);
36 series->setPen(blue);
37 //![1]
38
39 //![2]
40 *series << QPointF(0, 6) << QPointF(2, 4) << QPointF(3, 8) << QPointF(7, 4) << QPointF(10,5);
34 *series << QPointF(0, 6) << QPointF(2, 4) << QPointF(3, 8) << QPointF(7, 4) << QPointF(10,5);
41 //![2]
42
43 //![3]
44 QChart* chart = new QChart();
35 QChart* chart = new QChart();
45 chart->addSeries(series);
36 chart->addSeries(series);
46 chart->setTitle("Simple customchart example");
37 //![1]
47 //![3]
48
38
49 //![4]
39 //![2]
40 // Customize series
41 QPen pen(QRgb(0xfdb157));
42 pen.setWidth(5);
43 series->setPen(pen);
44
45 // Customize chart title
50 QFont font;
46 QFont font;
51 font.setPixelSize(18);
47 font.setPixelSize(18);
52 chart->setTitleFont(font);
48 chart->setTitleFont(font);
53 chart->setTitleBrush(Qt::yellow);
49 chart->setTitleBrush(QBrush(Qt::white));
50 chart->setTitle("Customchart example");
54
51
52 // Customize chart background
55 QLinearGradient backgroundGradient;
53 QLinearGradient backgroundGradient;
56 backgroundGradient.setStart(QPointF(0,0));
54 backgroundGradient.setStart(QPointF(0,0));
57 backgroundGradient.setFinalStop(QPointF(0,1));
55 backgroundGradient.setFinalStop(QPointF(0,1));
58 backgroundGradient.setColorAt(0.0, 0x3cc63c);
56 backgroundGradient.setColorAt(0.0, QRgb(0xd2d0d1));
59 backgroundGradient.setColorAt(1.0, 0x26f626);
57 backgroundGradient.setColorAt(1.0, QRgb(0x4c4547));
60 backgroundGradient.setCoordinateMode(QGradient::ObjectBoundingMode);
58 backgroundGradient.setCoordinateMode(QGradient::ObjectBoundingMode);
61 chart->setBackgroundBrush(backgroundGradient);
59 chart->setBackgroundBrush(backgroundGradient);
62 //![4]
60 //![2]
63
61
64 //![5]
62 //![3]
65 QPen black(Qt::black);
66 QChartAxis* axisX = chart->axisX();
63 QChartAxis* axisX = chart->axisX();
67 QChartAxis* axisY = chart->axisY();
64 QChartAxis* axisY = chart->axisY();
68
65
69 axisX->setAxisPen(black);
66 // Customize axis label font
70 axisY->setAxisPen(black);
67 QFont labelsFont;
68 labelsFont.setPixelSize(12);
69 axisX->setLabelsFont(labelsFont);
70 axisY->setLabelsFont(labelsFont);
71
72 // Customize axis colors
73 QPen axisPen(QRgb(0xd18952));
74 axisPen.setWidth(2);
75 axisX->setAxisPen(axisPen);
76 axisY->setAxisPen(axisPen);
77
78 // Customize axis label colors
79 QBrush axisBrush(Qt::white);
80 axisX->setLabelsBrush(axisBrush);
81 axisY->setLabelsBrush(axisBrush);
82
83 // Customize grid lines and shades
71 axisX->setGridLineVisible(false);
84 axisX->setGridLineVisible(false);
72 axisY->setGridLineVisible(false);
85 axisY->setGridLineVisible(false);
73
74 axisY->setShadesPen(Qt::NoPen);
86 axisY->setShadesPen(Qt::NoPen);
75 axisY->setShadesOpacity(0.5);
87 axisY->setShadesBrush(QBrush(QRgb(0xa5a2a3)));
76 axisY->setShadesBrush(Qt::white);
77 axisY->setShadesVisible(true);
88 axisY->setShadesVisible(true);
78 //![5]
89 //![3]
79
90
80 //![6]
91 //![4]
81 QChartAxisCategories* categoriesX = chart->axisX()->categories();
92 QChartAxisCategories* categoriesX = chart->axisX()->categories();
82 categoriesX->insert(1,"low");
93 categoriesX->insert(1,"low");
83 categoriesX->insert(5,"optimal");
94 categoriesX->insert(5,"optimal");
@@ -87,26 +98,24 int main(int argc, char *argv[])
87 categoriesY->insert(1,"slow");
98 categoriesY->insert(1,"slow");
88 categoriesY->insert(5,"med");
99 categoriesY->insert(5,"med");
89 categoriesY->insert(10,"fast");
100 categoriesY->insert(10,"fast");
90 //![6]
91
101
92 //![7]
93 axisX->setRange(0,10);
102 axisX->setRange(0,10);
94 axisX->setTicksCount(4);
103 axisX->setTicksCount(4);
95 axisY->setRange(0,10);
104 axisY->setRange(0,10);
96 axisY->setTicksCount(4);
105 axisY->setTicksCount(4);
97 //![7]
106 //![4]
98
107
99 //![8]
108 //![5]
100 QChartView* chartView = new QChartView(chart);
109 QChartView* chartView = new QChartView(chart);
101 chartView->setRenderHint(QPainter::Antialiasing);
110 chartView->setRenderHint(QPainter::Antialiasing);
102 //![8]
111 //![5]
103
112
104 //![9]
113 //![6]
105 QMainWindow window;
114 QMainWindow window;
106 window.setCentralWidget(chartView);
115 window.setCentralWidget(chartView);
107 window.resize(400, 300);
116 window.resize(400, 300);
108 window.show();
117 window.show();
109 //![9]
118 //![6]
110
119
111 return a.exec();
120 return a.exec();
112 }
121 }
1 NO CONTENT: file was removed, binary diff hidden
NO CONTENT: file was removed, binary diff hidden
1 NO CONTENT: file was removed, binary diff hidden
NO CONTENT: file was removed, binary diff hidden
General Comments 0
You need to be logged in to leave comments. Login now