##// END OF EJS Templates
Documented customcolors example
Tero Ahola -
r499:cdecb97c9343
parent child
Show More
1 NO CONTENT: new file 100644, binary diff hidden
1 NO CONTENT: new file 100644, binary diff hidden
@@ -0,0 +1,21
1 /*!
2 \example examples/customcolors
3 \title CustomColors Example
4 \subtitle
5
6 The example shows how to customize different visual elements on a typical chart i.e. customize
7 the following chart:
8 \image custom_colors1.jpg
9
10 to look like this instead:
11 \image custom_colors2.jpg
12
13 Customize chart background with the company specific colors:
14 \snippet ../examples/customcolors/mainwindow.cpp 1
15
16 Customize chart axes:
17 \snippet ../examples/customcolors/mainwindow.cpp 2
18
19 Customize pen and brush of all the series we have:
20 \snippet ../examples/customcolors/mainwindow.cpp 3
21 */ No newline at end of file
@@ -1,31 +1,32
1 1 /*!
2 2 \page examples.html
3 3 \title Examples
4 4 \keyword Examples
5 5
6 6 \raw HTML
7 7 <table cellpadding="2" cellspacing="1" border="0" width="100%" class="indextable">
8 8 <tr>
9 9 <th class="titleheader" width="33%">
10 10 List of examples
11 11 </th>
12 12 </tr>
13 13 <tr>
14 14 <td valign="top">
15 15 <ul>
16 16 <li><a href="example-areachart.html">Area Chart example</a></li>
17 17 <li><a href="example-barchart.html">Bar Chart example</a></li>
18 18 <li><a href="example-linechart.html">Line Chart example</a></li>
19 19 <li><a href="example-percentbarchart.html">Percent Bar Chart example</a></li>
20 20 <li><a href="example-piechart.html">Pie Chart example</a></li>
21 21 <li><a href="example-presenterchart.html">Presenter Chart example</a></li>
22 22 <li><a href="example-scatterchart.html">Scatter Chart example</a></li>
23 23 <li><a href="example-splinechart.html">Spline Chart example</a></li>
24 24 <li><a href="example-stackedbarchart.html">Stacked Bar Chart example</a></li>
25 25 <li><a href="example-stackedbarchartdrilldown.html">Stacked Bar Chart Drilldown example</a></li>
26 <li><a href="examples-customcolors.html">Customizing colors example</a></li>
26 27 </ul>
27 28 </td>
28 29 </tr>
29 30 </table>
30 31 \endraw
31 32 */
@@ -1,97 +1,104
1 1 #include "mainwindow.h"
2 2 #include <qchartview.h>
3 3 #include <qpieseries.h>
4 4 #include <qpieslice.h>
5 5 #include <qlineseries.h>
6 6 #include <qscatterseries.h>
7 7 #include <qchartaxis.h>
8 8 #include <QDebug>
9 9
10 10 QTCOMMERCIALCHART_USE_NAMESPACE
11 11
12 12 MainWindow::MainWindow(QWidget *parent)
13 13 : QMainWindow(parent)
14 14 {
15 15 // Here's the set of company's colors used throughout the example
16 16 m_companyColor1 = "#b90020";
17 17 m_companyColor2 = "#6d0013";
18 m_companyColor3 = "#d5d5f5";
18 m_companyColor3 = "#f5d5d5";
19 19 m_companyColor4 = "#fcfcfc";
20 20
21 21 resize(400, 300);
22 22 setWindowFlags(Qt::FramelessWindowHint);
23 23
24 24 // Create chart view
25 25 m_chartView = new QChartView(this);
26 26 setCentralWidget(m_chartView);
27 27 m_chartView->setChartTitle("Custom colors example");
28 28 m_chartView->setRenderHint(QPainter::Antialiasing);
29 29
30 30 // Create line series
31 31 m_line = new QLineSeries();
32 32 m_line->add(0.0, 1.1);
33 33 m_line->add(1.0, 2.3);
34 34 m_line->add(2.0, 2.1);
35 35 m_line->add(3.0, 3.3);
36 36 m_chartView->addSeries(m_line);
37 37
38 38 // Create scatter series with the same data
39 39 m_scatter = new QScatterSeries();
40 40 m_scatter->add(m_line->data());
41 41 m_chartView->addSeries(m_scatter);
42 42
43 43 // Create pie series with different data
44 44 m_pie = new QPieSeries();
45 45 m_pie->add(1.1, "1");
46 46 m_pie->add(2.1, "2");
47 47 m_pie->add(3.0, "3");
48 48 m_pie->setPiePosition(0.7, 0.7);
49 49 m_pie->setPieSize(0.5);
50 50 m_chartView->addSeries(m_pie);
51 51
52 52 connect(&m_timer, SIGNAL(timeout()), this, SLOT(customize()));
53 53 m_timer.setInterval(1500);
54 54 m_timer.setSingleShot(false);
55 55 m_timer.start();
56 56 }
57 57
58 58 MainWindow::~MainWindow()
59 59 {
60 60 }
61 61
62 62 void MainWindow::customize()
63 63 {
64 // Customize chart background
65 // Use a gradient from color 3 to color 4 for chart background
64 //! [1]
65 // Use a gradient from "color 3" to "color 4" for chart background
66 66 QLinearGradient chartGradient(0, 0, 0, 300);
67 67 chartGradient.setColorAt(0.0, m_companyColor3);
68 68 chartGradient.setColorAt(0.5, m_companyColor4);
69 69 chartGradient.setColorAt(1.0, m_companyColor3);
70 70 m_chartView->setChartBackgroundBrush(chartGradient);
71 71 m_chartView->setBackgroundBrush(m_companyColor4);
72 72 m_chartView->setChartTitleBrush(m_companyColor1);
73 //! [1]
73 74
74 // Customize chart axis
75 //! [2]
75 76 QPen color1Pen(m_companyColor1, 4.0);
76 77 m_chartView->axisX()->setAxisPen(color1Pen);
77 78 m_chartView->axisY()->setAxisPen(color1Pen);
79 //! [2]
78 80
79 // Customize series
81 //! [3]
82 // Customize pen of the line series
80 83 m_line->setPen(color1Pen);
84
85 // Customize pen and brush for the scatter
81 86 m_scatter->setPen(color1Pen);
82 87 m_scatter->setBrush(m_companyColor3);
88
89 // Customize pen and brush for the pie
83 90 for (int i(0); i < m_pie->slices().count(); i++) {
84 91 Qt::BrushStyle style = static_cast<Qt::BrushStyle>(i + 1);
85 92 m_pie->slices().at(i)->setSliceBrush(QBrush(m_companyColor2, style));
86 93 m_pie->slices().at(i)->setSlicePen(color1Pen);
87 94 }
88
95 //! [3]
89 96
90 97 // Calculate new colors to be used on the next update for the series
91 98 m_companyColor1.setRed((m_companyColor1.red() + 25) % 255);
92 99 m_companyColor1.setGreen((m_companyColor1.green() + 25) % 255);
93 100 m_companyColor1.setBlue((m_companyColor1.blue() + 25) % 255);
94 101 m_companyColor2.setRed((m_companyColor2.red() + 25) % 255);
95 102 m_companyColor2.setGreen((m_companyColor2.green() + 25) % 255);
96 103 m_companyColor2.setBlue((m_companyColor2.blue() + 25) % 255);
97 104 }
General Comments 0
You need to be logged in to leave comments. Login now