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. QCustomPlot changing color with Y values
Forum Updated to NodeBB v4.3 + New Features

QCustomPlot changing color with Y values

Scheduled Pinned Locked Moved Unsolved General and Desktop
3 Posts 2 Posters 773 Views 1 Watching
  • 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.
  • L Offline
    L Offline
    lukutis222
    wrote on 28 Feb 2023, 07:56 last edited by lukutis222
    #1

    Hello. I am working on an application which requires to measure load current consumption. I have my sensor device connected and all it does is sends serial data to my PC every 1 second. I have written a simple QT program to receive serial data and just plot the graph using QCustomPlot.

    I receive values from 1-500 and would like to add some style (color) to my graph. The value 1 should be green and as I receive higher values the color should get more and more red.

    Currently, I try the following:

    // SERIAL DATA RECEIVED AND SAVED TO result
              float value = result.toFloat();
              QPen pen;
              //determine pen color:
              if(value <pen_color_low){
                  pen.setWidth(1);
                  pen.setColor(QColor(0,255,0));
                  ui->customPlot->graph(0)->setPen(pen);
              }
    
              else if(value <pen_color_middle){
                  pen.setWidth(1);
                  pen.setColor(QColor(255,255,0));
                  ui->customPlot->graph(0)->setPen(pen);
              }
    
              else if(value <pen_color_high){
                  pen.setWidth(1);
                  pen.setColor(QColor(255,0,0));
                  ui->customPlot->graph(0)->setPen(pen);
              }
              addPoint(sample_counter,value);
              plot(ui->customPlot);
    

    addPoint and plot function are as following:

    void MainWindow::addPoint(double x, double y)
    {
        qv_x.append(x);
        qv_y.append(y);
    
    }
    
    void MainWindow::plot(QCustomPlot *customPlot)
    {
        customPlot->graph(0)->setData(qv_x,qv_y);
        customPlot->replot();
        customPlot->update();
    
    }
    

    The above code does not work as expected as it changes the color of the whole graph instead of the specific values.
    ca826927-c17f-4dcc-8331-e6c460249f52-image.png

    a9fa8741-7a36-49f2-bdc9-e40de0b79236-image.png

    What I want is to just color the values that are above certain threshold but keep the old ones the same color as it was before.

    K 1 Reply Last reply 28 Feb 2023, 09:20
    0
    • L lukutis222
      28 Feb 2023, 07:56

      Hello. I am working on an application which requires to measure load current consumption. I have my sensor device connected and all it does is sends serial data to my PC every 1 second. I have written a simple QT program to receive serial data and just plot the graph using QCustomPlot.

      I receive values from 1-500 and would like to add some style (color) to my graph. The value 1 should be green and as I receive higher values the color should get more and more red.

      Currently, I try the following:

      // SERIAL DATA RECEIVED AND SAVED TO result
                float value = result.toFloat();
                QPen pen;
                //determine pen color:
                if(value <pen_color_low){
                    pen.setWidth(1);
                    pen.setColor(QColor(0,255,0));
                    ui->customPlot->graph(0)->setPen(pen);
                }
      
                else if(value <pen_color_middle){
                    pen.setWidth(1);
                    pen.setColor(QColor(255,255,0));
                    ui->customPlot->graph(0)->setPen(pen);
                }
      
                else if(value <pen_color_high){
                    pen.setWidth(1);
                    pen.setColor(QColor(255,0,0));
                    ui->customPlot->graph(0)->setPen(pen);
                }
                addPoint(sample_counter,value);
                plot(ui->customPlot);
      

      addPoint and plot function are as following:

      void MainWindow::addPoint(double x, double y)
      {
          qv_x.append(x);
          qv_y.append(y);
      
      }
      
      void MainWindow::plot(QCustomPlot *customPlot)
      {
          customPlot->graph(0)->setData(qv_x,qv_y);
          customPlot->replot();
          customPlot->update();
      
      }
      

      The above code does not work as expected as it changes the color of the whole graph instead of the specific values.
      ca826927-c17f-4dcc-8331-e6c460249f52-image.png

      a9fa8741-7a36-49f2-bdc9-e40de0b79236-image.png

      What I want is to just color the values that are above certain threshold but keep the old ones the same color as it was before.

      K Offline
      K Offline
      kshegunov
      Moderators
      wrote on 28 Feb 2023, 09:20 last edited by
      #2

      This may be of help: https://www.qcustomplot.com/index.php/support/forum/122

      Read and abide by the Qt Code of Conduct

      L 1 Reply Last reply 28 Feb 2023, 10:29
      2
      • K kshegunov
        28 Feb 2023, 09:20

        This may be of help: https://www.qcustomplot.com/index.php/support/forum/122

        L Offline
        L Offline
        lukutis222
        wrote on 28 Feb 2023, 10:29 last edited by
        #3

        @kshegunov
        Hard to believe this issue been around since 2015. This is good reading but I didint manage to solve my problem. I use different methods of drawing graph than they are discussing. I am very new to QCustomPlot and C++ in general so the whole overriding virtual function thing is still complex to me. Hoping that someone who had simillar issues before with this can give me some additional help here. Thanks

        1 Reply Last reply
        0

        1/3

        28 Feb 2023, 07:56

        • Login

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