Change Color of QCategoryAxis each Gridline in QT Chart
-
Hi everyone, I'm using the example of Multiple Axes in QT. I want to change the color each gridline. This is latest code
//main.cpp int main(int argc, char *argv[]) { QApplication a(argc, argv); //![1] QChart *chart = new QChart(); chart->legend()->hide(); chart->setTitle("Multiaxis chart example"); //![1] //![2] QValueAxis *axisX = new QValueAxis; axisX->setTickCount(10); chart->addAxis(axisX, Qt::AlignBottom); //![2] //![3] QSplineSeries *series = new QSplineSeries; *series << QPointF(1, 5) << QPointF(3.5, 18) << QPointF(4.8, 7.5) << QPointF(10, 2.5); chart->addSeries(series); QValueAxis *axisY = new QValueAxis; axisY->setLinePenColor(series->pen().color()); chart->addAxis(axisY, Qt::AlignLeft); series->attachAxis(axisX); series->attachAxis(axisY); //![3] //![4] series = new QSplineSeries; *series << QPointF(1, 0.5) << QPointF(1.5, 4.5) << QPointF(2.4, 2.5) << QPointF(4.3, 12.5) << QPointF(5.2, 3.5) << QPointF(7.4, 16.5) << QPointF(8.3, 7.5) << QPointF(10, 17); chart->addSeries(series); QCategoryAxis *axisY3 = new QCategoryAxis; axisY3->setGridLineColor(QColor(255,0,0)); axisY3->append("Low", 5); axisY3->setGridLineColor(QColor(0,255,0)); axisY3->append("Medium", 12); axisY3->setGridLineColor(QColor(0,0,255)); axisY3->append("High", 17); axisY3->setLinePenColor(series->pen().color()); chart->addAxis(axisY3, Qt::AlignRight); series->attachAxis(axisX); series->attachAxis(axisY3); //![4] //![5] QChartView *chartView = new QChartView(chart); chartView->setRenderHint(QPainter::Antialiasing); //![5] //![6] QMainWindow window; window.setCentralWidget(chartView); window.resize(800, 600); window.show(); //![6] return a.exec(); }
And this is my issue and my target
I think I can't use this code
QCategoryAxis *axisY3 = new QCategoryAxis; axisY3->setGridLineColor(QColor(255,0,0)); axisY3->append("Low", 5); axisY3->setGridLineColor(QColor(0,255,0)); axisY3->append("Medium", 12); axisY3->setGridLineColor(QColor(0,0,255)); axisY3->append("High", 17); axisY3->setLinePenColor(series->pen().color());
Is there any clue to change the color of each gridline? Or should I use another library? I think the Category Axis is not the best to make a custom axis, however, I only need to adjust the position and change the color. Thank you for your help
-
QCategoryAxis *axisY3 = new QCategoryAxis; axisY3->setGridLineColor(QColor(255,0,0)); axisY3->append("Low", 5); QCategoryAxis *axisY2 = new QCategoryAxis; axisY2->setGridLineColor(QColor(0,255,0)); axisY2->append("Medium", 12); QCategoryAxis *axisY1 = new QCategoryAxis; axisY1->setGridLineColor(QColor(0,0,255)); axisY1->append("High", 17); chart->addAxis(axisY3, Qt::AlignRight); series->attachAxis(axisY3); series->attachAxis(axisX); chart->addAxis(axisY2, Qt::AlignRight); series->attachAxis(axisY2); chart->addAxis(axisY1, Qt::AlignRight); series->attachAxis(axisY1);
-
@mimamrafi I wanted to draw attention to the fact that you set the GridLineColor property of axisY3 in your code 3 times. Naturally, it was using the last color you assigned. A different color should have been a different object. It's not the right solution. There are so many ways to use lines of different colors on the chart. As you have noticed, several different colors appear on your chart and GridLineColor is one of them, but it is not what you want. You can turn to others.