QAreaSeries theory summary
I. Overview
The QAreaSeries class displays data in the form of an area chart. QAreaSeries is used to display quantitative data. It is based on the QLineSeries class and the areas between border lines are emphasized with color. Since QAreaSeries is based on a sequence of lines, the QAreaSeries constructor requires two instances of QLineSeries, which define the upper boundary of the area. Area charts are drawn with the bottom of the graphics area as the lower boundary by default. The lower boundary can be specified with another line than the bottom of the graphics area. In this case, QAreaSeries should be initialized with two QLineSeries instances.
- Note: The terms upper and lower bounds can be misleading in cases where the value of the lower bound is greater than the value of the upper bound. The point is that the area between these two boundary lines will be filled.
Two, use
1. Create a QAreaSeries object
In order to create an area chart, we need two QLineSeries instances. They will define the upper and lower boundaries of the area.
QLineSeries *series0 = new QLineSeries();
QLineSeries *series1 = new QLineSeries();
2. Populate the data
We add data to both sequences and use the stream operator.
*series0 << QPointF(1, 5) << QPointF(3, 7) << QPointF(7, 6) << QPointF(9, 7) << QPointF(12, 6)
<< QPointF(16, 7) << QPointF(18, 5);
*series1 << QPointF(1, 3) << QPointF(3, 4) << QPointF(7, 3) << QPointF(8, 2) << QPointF(12, 3)
<< QPointF(16, 4) << QPointF(18, 3);
3. Set the area
Now we create a QAreaSeries instance using two line series objects. We set the width of the custom gradient fill and outline.
QAreaSeries *series = new QAreaSeries(series0, series1);
series->setName("Batman");
QPen pen(0x059605);
pen.setWidth(3);
series->setPen(pen);
QLinearGradient gradient(QPointF(0, 0), QPointF(0, 1));
gradient.setColorAt(0.0, 0x3cc63c);
gradient.setColorAt(1.0, 0x26f626);
gradient.setCoordinateMode(QGradient::ObjectBoundingMode);
series->setBrush(gradient);
4. Associating the area chart with the drawing device
Finally, we create the QChartView instance, set the title, set anti-aliasing, and add the region sequence. We also created default axes and specified ranges on them.
QChart *chart = new QChart();
chart->addSeries(series);
chart->setTitle("Simple areachart example");
chart->createDefaultAxes();
chart->axes(Qt::Horizontal).first()->setRange(0, 20);
chart->axes(Qt::Vertical).first()->setRange(0, 10);
5. Associate the drawing device with the GUI window
The chart is ready. We just need to add this chartView to the QWidget object.
QChartView *chartView = new QChartView(chart);
chartView->setRenderHint(QPainter::Antialiasing);