• Qt QChart QValueAxis坐标轴设置


    QChart 坐标轴设置

     1 // 添加轴
     2 QValueAxis *xAxis = new QValueAxis();
     3 QValueAxis *yAxis = new QValueAxis();
     4  
     5 // 坐标轴整体
     6 xAxis->setRange(0, 10);
     7 yAxis->setRange(0, 10);
     8  
     9 // 轴标题设置
    10 xAxis->setTitleText(QStringLiteral("时间"));
    11 xAxis->setTitleFont(QFont("宋体"));
    12 QBrush brush;
    13 brush.setStyle(Qt::SolidPattern);
    14 brush.setColor(Qt::red);
    15 xAxis->setTitleBrush(brush);
    16  
    17 // 轴标签设置
    18 xAxis->setLabelFormat("%.1f");
    19 xAxis->setLabelsAngle(45);
    20 xAxis->setLabelsColor(Qt::blue);
    21 xAxis->setLabelsEditable(true);
    22  
    23 // 轴线和刻度线设置
    24 xAxis->setTickCount(11);
    25 QPen pen;
    26 pen.setStyle(Qt::SolidLine);
    27 pen.setColor(Qt::red);
    28 xAxis->setLinePen(pen);
    29 xAxis->setLineVisible(true);
    30 xAxis->setLinePenColor(Qt::blue);
    31  
    32 // 主网格线设置
    33 xAxis->setGridLineVisible(true);
    34 xAxis->setGridLineColor(Qt::black);
    35  
    36 // 次刻度和次网格线设置
    37 xAxis->setMinorTickCount(1);
    38 xAxis->setMinorGridLineColor(Qt::red);

    完整代码

     1 QChart *chart = new QChart();
     2  
     3 chart->setTheme(QChart::ChartThemeQt);
     4 chart->setTitle("显示坐标点");
     5 chart->setMargins(QMargins(10, 10, 10, 10));
     6  
     7  
     8 QLineSeries *lineSeries0 = new QLineSeries();
     9  
    10 lineSeries0->setName("时间 1");
    11 lineSeries0->append(0, 8);
    12 lineSeries0->append(2, 8);
    13 lineSeries0->append(3, 6);
    14 lineSeries0->append(5, 6);
    15 lineSeries0->append(7, 9);
    16 *lineSeries0 << QPointF(9, 9) << QPointF(10, 7);
    17  
    18 QScatterSeries *scatterSeries = new QScatterSeries();
    19  
    20 scatterSeries->setName("时间");
    21 scatterSeries->setPointLabelsFormat("(@xPoint, @yPoint)");
    22 scatterSeries->setMarkerSize(6);
    23 scatterSeries->setPointLabelsVisible();
    24  
    25 scatterSeries->append(0, 8);
    26 scatterSeries->append(2, 8);
    27 scatterSeries->append(3, 6);
    28 scatterSeries->append(5, 6);
    29 scatterSeries->append(7, 9);
    30 scatterSeries->append(9, 9);
    31 scatterSeries->append(10, 7);
    32  
    33 // 添加轴
    34 QValueAxis *xAxis = new QValueAxis();
    35 QValueAxis *yAxis = new QValueAxis();
    36  
    37 // 坐标轴整体
    38 xAxis->setRange(0, 10);
    39 yAxis->setRange(0, 10);
    40  
    41 // 轴标题设置
    42 xAxis->setTitleText(QStringLiteral("时间"));
    43 xAxis->setTitleFont(QFont("宋体"));
    44 QBrush brush;
    45 brush.setStyle(Qt::SolidPattern);
    46 brush.setColor(Qt::red);
    47 xAxis->setTitleBrush(brush);
    48  
    49 // 轴标签设置
    50 xAxis->setLabelFormat("%.1f");
    51 xAxis->setLabelsAngle(45);
    52 xAxis->setLabelsColor(Qt::blue);
    53 xAxis->setLabelsEditable(true);
    54  
    55 // 轴线和刻度线设置
    56 xAxis->setTickCount(11);
    57 QPen pen;
    58 pen.setStyle(Qt::SolidLine);
    59 pen.setColor(Qt::red);
    60 xAxis->setLinePen(pen);
    61 xAxis->setLineVisible(true);
    62 xAxis->setLinePenColor(Qt::blue);
    63  
    64 // 主网格线设置
    65 xAxis->setGridLineVisible(true);
    66 xAxis->setGridLineColor(Qt::black);
    67  
    68 // 次刻度和次网格线设置
    69 xAxis->setMinorTickCount(1);
    70 xAxis->setMinorGridLineColor(Qt::red);
    71  
    72 chart->addSeries(lineSeries0);
    73 chart->addSeries(scatterSeries);
    74  
    75 chart->addAxis(xAxis, Qt::AlignBottom);
    76 chart->addAxis(yAxis, Qt::AlignLeft);
    77  
    78 scatterSeries->attachAxis(xAxis);
    79 scatterSeries->attachAxis(yAxis);
    80  
    81 lineSeries0->attachAxis(xAxis);
    82 lineSeries0->attachAxis(yAxis);
    83  
    84 ui->chartView->setChart(chart);
    85 ui->chartView->setRubberBand(QChartView::RectangleRubberBand);
    86 ui->chartView->setRenderHint(QPainter::Antialiasing, true);

    运行效果

     

  • 相关阅读:
    LINQ篇:查询句法
    DLINQ(五):存储过程
    DLINQ(六):探究特性
    DLINQ
    LinQ 扩展函数的应用
    Linq 入门系列 select篇
    Linq 入门系列 [Take,Skip,TakeWhile,SkipWhile]篇
    javascript导航动画效果
    javascript简单的计算器实现
    javascript简单的日历实现
  • 原文地址:https://www.cnblogs.com/ybqjymy/p/13665040.html
Copyright © 2020-2023  润新知