Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

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->legend()->hide();
       chart->addSeries(series);
       chart->createDefaultAxes();
       chart->setTitle("Simple line chart example");
    
       QChartView *chartView = new QChartView(chart);
       chartView->setRenderHint(QPainter::Antialiasing);
       ui->gLayout->addWidget(chartView,0,0);
    

    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
    _chart->setTitle(_chartTitleText);

    //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);
        _series->setName(_defaultPenName);
        QString style = penStyles[defaultPenStyle];
    
        //Series pen color & thickness
        QPen SeriesPen(defaultPenColor);
        SeriesPen.setWidth(defaultPenWidth);
        SeriesPen.setStyle(Qt::PenStyle(defaultPenStyle));
        _series->setPen(SeriesPen);
        _chart->addSeries(_series);
    
    
    	//create an X Linear Axis
    	QValueAxis *axisX = new QValueAxis();
    	//Set some axis properties
    	axisX->setTitleText(_chartXAxisUnitsText);
    	// and more
    	_chart->addAxis(axisX, Qt::AlignBottom);
    
    	// add then attach the axis
    	_chart->addAxis(axisX, Qt::AlignBottom);
    	_series->attachAxis(axisX);
    
    	// now create a Y axis
    	QValueAxis *axisY = new QValueAxis();
    
    	// and set some properties
    	axisY->setTitleText(_chartYAxisUnitsText);
    
    	//then add and attach the Y Axis
    	_chart->addAxis(axisY, Qt::AlignLeft);
    	_series->attachAxis(axisY);
    
    }
    // set some chart properties
    _chart->legend()->setAlignment(Qt::AlignBottom);
    
    // and finally render it
    chartView = new QChartView(_chart);
    chartView->setRenderHint(QPainter::Antialiasing);
    
    ui->chartLayout->addWidget(chartView,0,0);


  • 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)
    {
    axisX->setVisible(true);
    axisXHasBeenAdded = true;
    }
    else
    {
    axisX->setVisible(false);
    }


Log in to reply