QCustomPlot changing color with Y values
-
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.
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.
-
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.
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.
This may be of help: https://www.qcustomplot.com/index.php/support/forum/122
-
This may be of help: https://www.qcustomplot.com/index.php/support/forum/122
@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