QT之QCustomPlot的使用(三)--往X轴添加字符串的静态曲线

    m_pCustomPlot = new QCustomPlot(this);//初始化曲线图
    m_pCustomPlot->setGeometry(1, 1, 400, 300);//初始化曲线图坐标
    m_pCustomPlot->setLocale(QLocale(QLocale::Chinese, QLocale::China));
    QVector<double> ticks;
    QVector<QString> labels;
    QVector<double> counts;
    int nCount = 0;
    for each (auto var in m_mapData)
    {
        labels.push_back(var.first);
        ticks.push_back(var.second);
    }

    for (int i = 0; i < ticks.count(); i++)
    {
        nCount++;
        counts.push_back(nCount);
    }

    m_pCustomPlot->addGraph();
    m_pCustomPlot->graph(0)->setLineStyle(QCPGraph::lsLine);
    m_pCustomPlot->graph(0)->setPen(QPen(QColor(0, 0, 255, 200)));
    m_pCustomPlot->graph(0)->setScatterStyle(QCPScatterStyle(QCPScatterStyle::ssDisc, 5));
    m_pCustomPlot->graph(0)->setData(counts, ticks);

    m_pCustomPlot->addGraph();
    m_pCustomPlot->graph(1)->setPen(QPen(Qt::red));
    m_pCustomPlot->graph(1)->setLineStyle(QCPGraph::lsNone);
    m_pCustomPlot->graph(1)->setScatterStyle(QCPScatterStyle::ssDisc);
    m_pCustomPlot->graph(1)->setData(counts, ticks);

    //这一段是最重要的
    QSharedPointer<QCPAxisTickerText> textTicker(new QCPAxisTickerText);
    textTicker->addTicks(counts, labels);
    m_pCustomPlot->xAxis->setTicker(textTicker);
    m_pCustomPlot->xAxis->setTickLabelRotation(30);

    m_pCustomPlot->xAxis->setTickLabelFont(QFont(QFont().family(), 8));
    m_pCustomPlot->yAxis->setTickLabelFont(QFont(QFont().family(), 8));

    m_pCustomPlot->xAxis->setLabel(m_strXLabel);
    m_pCustomPlot->yAxis->setLabel(m_strYLabel);

    m_pCustomPlot->xAxis->setRange(m_xMin, m_xMax);
    m_pCustomPlot->yAxis->setRange(m_yMin, m_yMax);

    m_pCustomPlot->xAxis2->setVisible(false);
    m_pCustomPlot->yAxis2->setVisible(false);
    m_pCustomPlot->xAxis2->setTicks(false);
    m_pCustomPlot->yAxis2->setTicks(false);
    m_pCustomPlot->xAxis2->setTickLabels(false);
    m_pCustomPlot->yAxis2->setTickLabels(false);

    m_pCustomPlot->replot();
void setChartData(std::map<QString, double>& mdata)
{
    m_mapData.clear();
    std::map <QString, double>::iterator data_iter;
    for (data_iter = mdata.begin(); data_iter != mdata.end(); data_iter++)
    {
        QString strLabel = data_iter->first;
        double data = data_iter->second;
        m_mapData.insert(std::pair<QString, double>(strLabel, data));
    }
}

std::map<QString, double> m_mapData;

猜你喜欢

转载自blog.csdn.net/u012288722/article/details/79137116
今日推荐