Chart Themes Example▲
This example shows the look and feel of the different built-in themes for some of the supported chart types.
Running the Example▲
To run the example from Qt Creator, open the Welcome mode and select the example from Examples. For more information, visit Building and Running an Example.
Creating Charts▲
The charts of different types are generated and added to the layout separately. For example, the line chart is created as follows. The creation of other chart types is similar.
First a chart is created.
QChart *
chart =
new
QChart();
chart-&
gt;setTitle("Line chart"
);
A common set of random data is generated and placed in a list. This list is used in each chart type to add data to the series of the chart. For the line series, QLineSeries instances are created and added to the chart.
QString name("Series "
);
int
nameIndex =
0
;
for
(const
DataList &
amp;list : m_dataTable) {
QLineSeries *
series =
new
QLineSeries(chart);
for
(const
Data &
amp;data : list)
series-&
gt;append(data.first);
series-&
gt;setName(name +
QString::
number(nameIndex));
nameIndex++
;
chart-&
gt;addSeries(series);
}
Default axes are created for the line series. We also specify ranges for the axes based on the range of the data used for the series.
chart-&
gt;createDefaultAxes();
chart-&
gt;axes(Qt::
Horizontal).first()-&
gt;setRange(0
, m_valueMax);
chart-&
gt;axes(Qt::
Vertical).first()-&
gt;setRange(0
, m_valueCount);
We also want to add more space between the labels and the y-axes. For this we specify a label format that adds space characters to the labels.
// Add space to label to add space between labels and axis
QValueAxis *
axisY =
qobject_cast&
lt;QValueAxis*&
gt;(chart-&
gt;axes(Qt::
Vertical).first());
Q_ASSERT(axisY);
axisY-&
gt;setLabelFormat("%.1f "
);
Finally the line chart is added to the grid layout.
chartView =
new
QChartView(createLineChart());
m_ui-&
gt;gridLayout-&
gt;addWidget(chartView, 1
, 2
);
Changing Theme▲
The user can select a built-in theme to be used in the example. This theme is then applied to all charts in the layout.
QChart::
ChartTheme theme =
static_cast
&
lt;QChart::
ChartTheme&
gt;(
m_ui-&
gt;themeComboBox-&