绘制箭头图

环境:qt5.14.2 ,mingw,qcustomplot

功能:完成绘制箭头图,且当y为负的时候让三角形朝下。对原始数据进行了拆分,分别存入不同的容器中。然后分别设置样式。

结果展示:

代码:

struct GraphInfo
{
    QVector<double> x;       
    QVector<double> y;      
    QColor penColor=Qt::red;
};
void Widget::createChart()
{

    QList<GraphInfo> listInfo;
    listInfo.append(graph1);
    listInfo.append(graph2);
    listInfo.append(graph3);

    QCPScatterStyle positiveStyle(QCPScatterStyle::ssTriangle, 7);
    QCPScatterStyle negativeStyle(QCPScatterStyle::ssTriangleInverted, 7);

    for (GraphInfo &info : listInfo)
    {
        QVector<double> x_positive, y_positive;
        QVector<double> x_negative, y_negative;

        for (int i = 0; i < info.y.size(); ++i)
        {
            if (info.y[i] >= 0)
            {
                x_positive.append(info.x[i]);
                y_positive.append(info.y[i]);
            }
            else
            {
                x_negative.append(info.x[i]);
                y_negative.append(info.y[i]);
            }
        }

        // Add positive graph
        customPlot->addGraph();
        customPlot->graph()->setPen(info.penColor);

        customPlot->graph()->setLineStyle(QCPGraph::lsImpulse);
        positiveStyle.setBrush(info.penColor);
        customPlot->graph()->setScatterStyle(positiveStyle);
        customPlot->graph()->setData(x_positive, y_positive);

        // Add negative graph
        customPlot->addGraph();
        customPlot->graph()->setPen(info.penColor);

        customPlot->graph()->setLineStyle(QCPGraph::lsImpulse);
        negativeStyle.setBrush(info.penColor);
        customPlot->graph()->setScatterStyle(negativeStyle);
        customPlot->graph()->setData(x_negative, y_negative);
    }

    customPlot->xAxis->setTicks(true);
    customPlot->yAxis->setTicks(true);
    customPlot->xAxis->setTickLabels(true);
    customPlot->yAxis->setTickLabels(true);

    customPlot->legend->setVisible(false);
    customPlot->legend->setFont(QFont("Helvetica", 9));
    customPlot->axisRect()->setupFullAxesBox();
    customPlot->rescaleAxes();

    customPlot->replot();
}

全部评论

相关推荐

09-14 17:12
已编辑
中山职业技术学院 Java
点赞 评论 收藏
分享
1 收藏 评论
分享
牛客网
牛客企业服务