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. Change Color of QCategoryAxis each Gridline in QT Chart

Change Color of QCategoryAxis each Gridline in QT Chart

Scheduled Pinned Locked Moved Unsolved General and Desktop
5 Posts 2 Posters 881 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
    mimamrafi
    wrote on 7 Oct 2022, 06:18 last edited by
    #1

    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
    d909c0ec-9a49-4509-b8c2-e05c2a310d98-screenshot chart axis.png

    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

    1 Reply Last reply
    0
    • A Offline
      A Offline
      A.A.SEZEN
      wrote on 7 Oct 2022, 08:34 last edited by
      #2
          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);
      
      M 1 Reply Last reply 7 Oct 2022, 08:46
      0
      • A A.A.SEZEN
        7 Oct 2022, 08:34
            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);
        
        M Offline
        M Offline
        mimamrafi
        wrote on 7 Oct 2022, 08:46 last edited by mimamrafi 10 Jul 2022, 08:48
        #3

        @A-A-SEZEN It works, thank you, but how to hide the line in the right section

        5569ea75-2ddb-4681-92eb-f8fef90007a5-image.png

        this is the output of your code

        A 1 Reply Last reply 7 Oct 2022, 10:27
        0
        • M mimamrafi
          7 Oct 2022, 08:46

          @A-A-SEZEN It works, thank you, but how to hide the line in the right section

          5569ea75-2ddb-4681-92eb-f8fef90007a5-image.png

          this is the output of your code

          A Offline
          A Offline
          A.A.SEZEN
          wrote on 7 Oct 2022, 10:27 last edited by
          #4

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

          1 Reply Last reply
          0
          • M Offline
            M Offline
            mimamrafi
            wrote on 7 Oct 2022, 12:03 last edited by
            #5

            Thank you for your reply, Now I switch to making a line series and making it like axes. Are there any clues on How to make the line series flat from left to right of the entire chart view?

            1 Reply Last reply
            0

            1/5

            7 Oct 2022, 06:18

            • 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