QCHART的几种坐标轴的详细介绍及使用代码示例
QCHART主要有如下几种坐标轴:
QAbstractAxis->QBarCategoryAxis:柱状图坐标轴文字
QAbstractAxis->QDateTimeAxis:日期坐标轴
QAbstractAxis->QLogValueAxis(对数做标轴)
QAbstractAxis->QValueAxis(设置坐标轴范围)->QCategoryAxis(文字描述坐标轴)
下面详细介绍各种坐标轴的使用代码:
QAbstractAxis->QBarCategoryAxis:柱状图坐标轴文字
QChartView *chartView = new QChartView;
QBarSeries *series = new QBarSeries;
// ...
chartView->chart()->addSeries(series);
chartView->chart()->createDefaultAxes();
QBarCategoryAxis *axisX = new QBarCategoryAxis;
QStringList categories;
categories << "Jan" << "Feb" << "Mar" << "Apr" << "May" << "Jun";
axisX->append(categories);
axisX->setRange("Feb", "May");
chartView->chart()->setAxisX(axisX, series);
QDateTimeAxis:日期坐标轴
QChartView *chartView = new QChartView;
chartView->chart()->addSeries(series);
// ...
QDateTimeAxis *axisX = new QDateTimeAxis;
axisX->setFormat("dd-MM-yyyy h:mm");
chartView->chart()->setAxisX(axisX, series);
QAbstractAxis->QLogValueAxis(对数做标轴)
QValueAxis *axisX = new QValueAxis();
axisX->setTitleText("Data point");
axisX->setLabelFormat("%i");
axisX->setTickCount(series->count());
chart->addAxis(axisX, Qt::AlignBottom);
series->attachAxis(axisX);
QLogValueAxis *axisY = new QLogValueAxis();
axisY->setTitleText("Values");
axisY->setLabelFormat("%g");
axisY->setBase(8.0);
axisY->setMinorTickCount(-1);
chart->addAxis(axisY, Qt::AlignLeft);
series->attachAxis(axisY);
QValueAxis(设置坐标轴范围)
QChartView *chartView = new QChartView;
QLineSeries *series = new QLineSeries;
// ...
chartView->chart()->addSeries(series);
QValueAxis *axisX = new QValueAxis;
axisX->setRange(10, 20.5);
axisX->setTickCount(10);
axisX->setLabelFormat("%.2f");
chartView->chart()->setAxisX(axisX, series);
QCategoryAxis(文字描述坐标轴)
QChartView *chartView = new QChartView;
QLineSeries *series = new QLineSeries;
// ...
chartView->chart()->addSeries(series);
QCategoryAxis *axisY = new QCategoryAxis;
axisY->setMin(0);
axisY->setMax(52);
axisY->setStartValue(15);
axisY->append("First", 20);
axisY->append("Second", 37);
axisY->append("Third", 52);
chartView->chart()->setAxisY(axisY, series);
备注:实战示例,解疑答惑。
--不间端地思考,实时地批判你的工作!