Hi Cliff,
To resize the chart automatically, just redraw the chart at the size you want automatically in the resize event.
When you resize a normal QLabel control with text inside, you may notice that the label text is not resized. It is because the label text is drawn using the font and font size you specified, and is not the size of the control. The same applies to the chart viewer. The chart size is determined by the size of the chart (the width and height parameters that are used to create the XYChart object), not the size of the control.
For resizing a chart, most people would expect that the text is not resized, but the chart is simply drawn in more details (more labels and grid lines) or less details (for size down). To do this, you just need to design the charting code to use a variable size instead of a hard coded size.
For example, the followings are based on the "Multi-Line Chart (2)" sample code, but modified to use a variable size. In the resize event handler, you just need to call this routine with the size you want the chart to be.
Hope this can help.
void MyDialog::drawChart(int width, int height)
{
//In this example, we simply use random data for the 3 data series.
RanSeries *r = new RanSeries(129);
DoubleArray data0 = r->getSeries(100, 100, -15, 15);
DoubleArray data1 = r->getSeries(100, 160, -15, 15);
DoubleArray data2 = r->getSeries(100, 220, -15, 15);
DoubleArray timeStamps = r->getDateSeries(100, Chart::chartTime(2014, 1, 1), 86400);
// Create a XYChart object
XYChart *c = new XYChart(width, height);
// Set the plotarea at (70, 30) and of variable size, with transparent background and
// border and light grey (0xcccccc) horizontal grid lines
c->setPlotArea(50, 30, width - 60, height - 60, Chart::Transparent, -1, Chart::Transparent, 0xcccccc);
// Add a legend box with horizontal layout above the plot area at (70, 35). Use 12pt Arial font,
// transparent background and border, and line style legend icon.
LegendBox *b = c->addLegend(45, 0, false, "arial.ttf", 9);
b->setBackground(Chart::Transparent, Chart::Transparent);
b->setLineStyleKey();
// Set axis label font to 12pt Arial
c->xAxis()->setLabelStyle("arial.ttf", 9);
c->yAxis()->setLabelStyle("arial.ttf", 9);
// Set the x and y axis stems to transparent, and the x-axis tick color to grey (0xaaaaaa)
c->xAxis()->setColors(Chart::Transparent, Chart::TextColor, Chart::TextColor, 0xaaaaaa);
c->yAxis()->setColors(Chart::Transparent);
// Set the major/minor tick lengths for the x-axis to 10 and 0.
c->xAxis()->setTickLength(10, 0);
// For the automatic axis labels, set the minimum spacing to 80/40 pixels for the x/y axis.
c->xAxis()->setTickDensity(80);
c->yAxis()->setTickDensity(40);
// Add a line layer to the chart with 3-pixel line width
LineLayer *layer = c->addLineLayer();
layer->setLineWidth(3);
// Add 3 data series to the line layer
layer->addDataSet(data0, 0x5588cc, "Alpha");
layer->addDataSet(data1, 0xee9944, "Beta");
layer->addDataSet(data2, 0x99bb55, "Gamma");
// The x-coordinates for the line layer
layer->setXData(timeStamps);
delete r;
delete myViewer->getChart();
myViewer->setChart(c);
}
Regards
Peter Kwan |