Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. how do I add an additional series or more to a LinearGraph?
Forum Updated to NodeBB v4.3 + New Features

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

Scheduled Pinned Locked Moved Solved General and Desktop
lineargraphqlineseries
5 Posts 2 Posters 1.9k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    mmikeinsantarosa
    wrote on 14 Mar 2019, 20:28 last edited by
    #1

    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.

    J 1 Reply Last reply 14 Mar 2019, 22:56
    0
    • M mmikeinsantarosa
      14 Mar 2019, 20:28

      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.

      J Offline
      J Offline
      JonB
      wrote on 14 Mar 2019, 22:56 last edited by JonB
      #2

      @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.

      1 Reply Last reply
      0
      • M Offline
        M Offline
        mmikeinsantarosa
        wrote on 15 Mar 2019, 18:08 last edited by
        #3

        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);
        
        1 Reply Last reply
        0
        • M Offline
          M Offline
          mmikeinsantarosa
          wrote on 15 Mar 2019, 18:58 last edited by mmikeinsantarosa
          #4

          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?

          1 Reply Last reply
          0
          • M Offline
            M Offline
            mmikeinsantarosa
            wrote on 15 Mar 2019, 20:54 last edited by mmikeinsantarosa
            #5

            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);
            }

            1 Reply Last reply
            0

            1/5

            14 Mar 2019, 20:28

            • Login

            • Login or register to search.
            1 out of 5
            • First post
              1/5
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved