Please nominate your Qt Champions for 2021!

how do I add an additional series or more to a LinearGraph?

  • From the examples:
    QLineSeries *series = new QLineSeries();

       series->append(0, 6);
       series->append(2, 4);
       series->append(3, 8);
       series->append(7, 4);
       series->append(10, 5);
       *series << QPointF(11, 1) << QPointF(13, 3) << QPointF(17, 6) << QPointF(18, 3) << QPointF(20, 2);
       QChart *chart = new QChart();
       chart->setTitle("Simple line chart example");
       QChartView *chartView = new QChartView(chart);

    I need to add additional series to the chart. Do I need to create a list of series then add them?
    Any advice much appreciated.

  • @mmikeinsantarosa
    You added your series via chart->addSeries(series);. If you want to add an additional series, you would create the new series and call that again to add it. You add series one at a time (no need/support for adding a list of them) via QChart::addSeries(), and you can access all added series via QList<QAbstractSeries *> QChart::series() const showing that there are a list of these, if that's what you were asking.

  • Here's a snip of what I've built. When the series is filled and added I also need to add and attach X & Y axis objects. Is the below close?

    _chart = new QChart();

    // add some chart properties

    //fill series from 1 to n
    // depending on what's available
    // here we're hardwired for 2 sets of series data

     for(int dataSet = 1; dataSet < 2; dataSet++)
        QLineSeries *_series = new QLineSeries();
        fillSeriesfromList(_series, dataSet);
        QString style = penStyles[defaultPenStyle];
        //Series pen color & thickness
        QPen SeriesPen(defaultPenColor);
    	//create an X Linear Axis
    	QValueAxis *axisX = new QValueAxis();
    	//Set some axis properties
    	// and more
    	_chart->addAxis(axisX, Qt::AlignBottom);
    	// add then attach the axis
    	_chart->addAxis(axisX, Qt::AlignBottom);
    	// now create a Y axis
    	QValueAxis *axisY = new QValueAxis();
    	// and set some properties
    	//then add and attach the Y Axis
    	_chart->addAxis(axisY, Qt::AlignLeft);
    // set some chart properties
    // and finally render it
    chartView = new QChartView(_chart);

  • I end up with 2 sets of X & Y axis scales on the chart doing it this way. If I skip adding the XY axis parts on the 2nd iteration, the chart doesn't draw the 2nd series.

    What XY Axis parts need to be include to get the 2nd line but don't add the X & Y scales?

  • I think I just figured it out by setting using a bool and setting it to false at the beginning. Then just before attaching the axis determine axis visibility. If the bool is false, set the axis visibility to true then set the bool to true. The next time thru, the bool will be true and set visibility to false. I ge both lines and only one set of scales.

    if (axisXHasBeenAdded == false)
    axisXHasBeenAdded = true;

Log in to reply