绘制箭头图

环境: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();
}

全部评论

相关推荐

藏剑天涯:全要了 领4份工资
点赞 评论 收藏
分享
点赞 评论 收藏
分享
孤寡孤寡的牛牛很热情:为什么我2本9硕投了很多,都是简历或者挂,难道那个恶心人的测评真的得认真做吗
点赞 评论 收藏
分享
1 收藏 评论
分享
牛客网
牛客企业服务