Qt图表类似于model/view,chart就是model。
创建图表的各个部件:
QChart *chart = new QChart();chart->setTitle(tr("简单函数曲线"));
// chart->setAcceptHoverEvents(true);ui->chartView->setChart(chart);ui->chartView->setRenderHint(QPainter::Antialiasing);QLineSeries *series0 = new QLineSeries();QLineSeries *series1 = new QLineSeries();series0->setName("Sin曲线");series1->setName("Cos曲线");curSeries=series0; //当前序列QPen pen;pen.setStyle(Qt::DotLine);//Qt::SolidLine, Qt::DashLine, Qt::DotLine, Qt::DashDotLinepen.setWidth(2);pen.setColor(Qt::red);series0->setPen(pen); //折线序列的线条设置pen.setStyle(Qt::SolidLine);//Qt::SolidLine, Qt::DashLine, Qt::DotLine, Qt::DashDotLinepen.setColor(Qt::blue);series1->setPen(pen);//折线序列的线条设置chart->addSeries(series0);chart->addSeries(series1);
创建坐标轴:
QValueAxis *axisX = new QValueAxis;curAxis=axisX; //当前坐标轴axisX->setRange(0, 10); //设置坐标轴范围axisX->setLabelFormat("%.1f"); //标签格式axisX->setTickCount(11); //主分隔个数axisX->setMinorTickCount(4);axisX->setTitleText("time(secs)"); //标题
// axisX->setGridLineVisible(false);QValueAxis *axisY = new QValueAxis;axisY->setRange(-2, 2);axisY->setTitleText("value");axisY->setTickCount(5);axisY->setLabelFormat("%.2f"); //标签格式
// axisY->setGridLineVisible(false);axisY->setMinorTickCount(4);
将坐标轴添加进图表:
chart->addAxis(axisX,Qt::AlignBottom); //坐标轴添加到图表,并指定方向chart->addAxis(axisY,Qt::AlignLeft);series0->attachAxis(axisX); //序列 series0 附加坐标轴series0->attachAxis(axisY);series1->attachAxis(axisX);//序列 series1 附加坐标轴series1->attachAxis(axisY);
从图表上获取数据更新界面显示:
void MainWindow::updateFromChart()
{ //从图表上获取数据更新界面显示QChart *aChart;aChart=ui->chartView->chart(); //获取chartui->editTitle->setText(aChart->title()); //图表标题QMargins mg=aChart->margins(); //边距ui->spinMarginLeft->setValue(mg.left());ui->spinMarginRight->setValue(mg.right());ui->spinMarginTop->setValue(mg.top());ui->spinMarginBottom->setValue(mg.bottom());
}
设置图表各种参数:
void MainWindow::on_btnSetTitle_clicked()
{ //设置图标标题文字QString str=ui->editTitle->text();ui->chartView->chart()->setTitle(str);
}void MainWindow::on_btnSetTitleFont_clicked()
{ //设置图标标题文字的字体QFont font=ui->chartView->chart()->titleFont();bool ok=false;font=QFontDialog::getFont(&ok,font);if (ok)ui->chartView->chart()->setTitleFont(font);
}void MainWindow::on_btnSetMargin_clicked()
{//设置图标的4个边距QMargins mgs;mgs.setLeft(ui->spinMarginLeft->value());mgs.setRight(ui->spinMarginRight->value());mgs.setTop(ui->spinMarginTop->value());mgs.setBottom(ui->spinMarginBottom->value());ui->chartView->chart()->setMargins(mgs);
}{//图例是否可见ui->chartView->chart()->legend()->setVisible(checked);
}void MainWindow::on_btnSetAxisRange_clicked()
{ //设置坐标轴的坐标范围curAxis->setRange(ui->spinAxisMin->value(),ui->spinAxisMax->value());
}void MainWindow::on_chkGridLineVisible_clicked(bool checked)
{ //轴的网格线是否可见curAxis->setGridLineVisible(checked);
}void MainWindow::on_chkMinorTickVisible_clicked(bool checked)
{ //次级刻度是否可见curAxis->setMinorGridLineVisible(checked);
}void MainWindow::on_chkBoxLegendBackground_clicked(bool checked)
{//图例的背景是否可见ui->chartView->chart()->legend()->setBackgroundVisible(checked);
}void MainWindow::on_radioButton_clicked()
{//图例的位置ui->chartView->chart()->legend()->setAlignment(Qt::AlignTop);
}void MainWindow::on_radioButton_2_clicked()
{//图例的位置ui->chartView->chart()->legend()->setAlignment(Qt::AlignBottom);
}void MainWindow::on_radioButton_3_clicked()
{//图例的位置ui->chartView->chart()->legend()->setAlignment(Qt::AlignLeft);
}void MainWindow::on_radioButton_4_clicked()
{//图例的位置ui->chartView->chart()->legend()->setAlignment(Qt::AlignRight);
}void MainWindow::on_btnLegendFont_clicked()
{ //图例的字体设置QFont font=ui->chartView->chart()->legend()->font();bool ok=false;font=QFontDialog::getFont(&ok,font);if (ok)ui->chartView->chart()->legend()->setFont(font);
}void MainWindow::on_btnLegendlabelColor_clicked()
{//图例的文字颜色设置QColor color=ui->chartView->chart()->legend()->labelColor();color=QColorDialog::getColor(color);if (color.isValid())ui->chartView->chart()->legend()->setLabelColor(color);
}void MainWindow::on_btnGridLineColor_clicked()
{ //网格线的颜色设置QColor color=curAxis->gridLineColor();color=QColorDialog::getColor(color);if (color.isValid())curAxis->setGridLineColor(color);
}void MainWindow::on_pushButton_10_clicked()
{ //网格线的Pen设置QPen pen;pen=curAxis->gridLinePen();bool ok=false;pen=QWDialogPen::getPen(pen,ok);if (ok)curAxis->setGridLinePen(pen);
}void MainWindow::on_chkAxisLineVisible_clicked(bool checked)
{//刻度是否可见curAxis->setLineVisible(checked);
}void MainWindow::on_btnAxisLinePen_clicked()
{QPen pen;pen=curAxis->linePen();bool ok=false;pen=QWDialogPen::getPen(pen,ok);if (ok)curAxis->setLinePen(pen);
}void MainWindow::on_btnAxisLinePenColor_clicked()
{QColor color=curAxis->linePenColor();color=QColorDialog::getColor(color);if (color.isValid())curAxis->setLinePenColor(color);
}void MainWindow::on_btnMinorColor_clicked()
{//次级刻度网格线颜色QColor color=curAxis->minorGridLineColor();color=QColorDialog::getColor(color);if (color.isValid())curAxis->setMinorGridLineColor(color);
}void MainWindow::on_btnMinorPen_clicked()
{//次级刻度线Pen设置QPen pen;pen=curAxis->minorGridLinePen();bool ok=false;pen=QWDialogPen::getPen(pen,ok);if (ok)curAxis->setMinorGridLinePen(pen);
}
设置数据序列各种属性:
void MainWindow::on_chkPointVisible_clicked(bool checked)
{//序列的数据点是否可见curSeries->setPointsVisible(checked);
}void MainWindow::on_chkPointLabelVisible_clicked(bool checked)
{ //序列的数据点标签是否可见curSeries->setPointLabelsVisible(checked);
}void MainWindow::on_btnSeriesName_clicked()
{ //设置序列名称curSeries->setName(ui->editSeriesName->text());if (ui->radioSeries0->isChecked())ui->radioSeries0->setText(ui->editSeriesName->text());elseui->radioSeries1->setText(ui->editSeriesName->text());
}void MainWindow::on_btnSeriesColor_clicked()
{ //序列的曲线颜色QColor color=curSeries->color();color=QColorDialog::getColor(color);if (color.isValid())curSeries->setColor(color);
}void MainWindow::on_radioSeries0_clicked()
{//获取当前数据序列if (ui->radioSeries0->isChecked())curSeries=(QLineSeries *)ui->chartView->chart()->series().at(0);elsecurSeries=(QLineSeries *)ui->chartView->chart()->series().at(1);
//获取序列的属性值,并显示到界面上ui->editSeriesName->setText(curSeries->name());ui->chkSeriesVisible->setChecked(curSeries->isVisible());ui->chkPointVisible->setChecked(curSeries->pointsVisible());ui->sliderSeriesOpacity->setValue(curSeries->opacity()*10);ui->chkPointLabelVisible->setChecked(curSeries->pointLabelsVisible());
}void MainWindow::on_chkSeriesVisible_clicked(bool checked)
{//序列是否可见this->curSeries->setVisible(checked);
}void MainWindow::on_btnSeriesPen_clicked()
{//序列线条的Pen设置bool ok=false;QPen pen;pen=curSeries->pen();pen=QWDialogPen::getPen(pen,ok);if (ok)curSeries->setPen(pen);
}void MainWindow::on_sliderSeriesOpacity_valueChanged(int value)
{//序列的透明度curSeries->setOpacity(value/10.0);
}void MainWindow::on_btnSeriesLabColor_clicked()
{//序列数据点标签颜色QColor color=curSeries->pointLabelsColor();color=QColorDialog::getColor(color);if (color.isValid())curSeries->setPointLabelsColor(color);}void MainWindow::on_btnSeriesLabFont_clicked()
{//序列数据点标签字体QFont font;font=curSeries->pointLabelsFont();bool ok=false;font=QFontDialog::getFont(&ok,font);if (ok)curSeries->setPointLabelsFont(font);
}void MainWindow::on_radioSeriesLabFormat0_clicked()
{ //序列数据点标签的显示格式curSeries->setPointLabelsFormat("@yPoint");
}void MainWindow::on_radioSeriesLabFormat1_clicked()
{//序列数据点标签的显示格式curSeries->setPointLabelsFormat("(@xPoint,@yPoint)");
}
设置坐标轴属性:
void MainWindow::on_chkBoxVisible_clicked(bool checked)
{ //坐标轴是否可见curAxis->setVisible(checked);
}void MainWindow::on_btnAxisSetTitle_clicked()
{ //设置坐标轴的标题curAxis->setTitleText(ui->editAxisTitle->text());
}void MainWindow::on_btnAxisSetTitleFont_clicked()
{ //设置坐标轴的标题的字体QFont font=curAxis->titleFont();bool ok=false;font=QFontDialog::getFont(&ok,font);if (ok)curAxis->setTitleFont(font);
}void MainWindow::on_chkBoxAxisTitle_clicked(bool checked)
{ //轴标题是否可见curAxis->setTitleVisible(checked);
}void MainWindow::on_pushButton_clicked()
{//设置坐标轴刻度标签的文字格式curAxis->setLabelFormat(ui->editAxisLabelFormat->text());
}void MainWindow::on_btnAxisLabelColor_clicked()
{//设置坐标轴刻度标签的文字颜色QColor color=curAxis->labelsColor();color=QColorDialog::getColor(color);if (color.isValid())curAxis->setLabelsColor(color);
}void MainWindow::on_btnAxisLabelFont_clicked()
{//设置坐标轴刻度标签的文字字体QFont font=curAxis->labelsFont();bool ok=false;font=QFontDialog::getFont(&ok,font);if (ok)curAxis->setLabelsFont(font);
}void MainWindow::on_chkBoxLabelsVisible_clicked(bool checked)
{//轴的刻度标签是否可见curAxis->setLabelsVisible(checked);
}
动画效果:
void MainWindow::on_cBoxAnimation_currentIndexChanged(int index)
{//动画效果ui->chartView->chart()->setAnimationOptions(QChart::AnimationOptions(index));
// ui->chartView->chart()->setAnimationOptions(QChart::SeriesAnimations);
// ui->chartView->chart()->setAnimationOptions(QChart::AllAnimations);
}
图标的主题:
void MainWindow::on_cBoxTheme_currentIndexChanged(int index)
{ //图标的主题ui->chartView->chart()->setTheme(QChart::ChartTheme(index));
}
尺寸相关:
void MainWindow::on_actZoomIn_triggered()
{//放大ui->chartView->chart()->zoom(1.2);//zoomIn();
}void MainWindow::on_actZoomOut_triggered()
{//缩小ui->chartView->chart()->zoom(0.8);//zoomOut();
}void MainWindow::on_actZoomReset_triggered()
{//复位ui->chartView->chart()->zoomReset();
}