Qt绘制各种图表

绘制柱状图:

void MainWindow::iniBarChart()
{ //柱状图初始化
    QChart *chart = new QChart(); //创建chart
    chart->setTitle("Barchart演示");
    chart->setAnimationOptions(QChart::SeriesAnimations);
    ui->chartViewBar->setChart(chart); //为ChartView设置chart
    ui->chartViewBar->setRenderHint(QPainter::Antialiasing);
}

void MainWindow::buildBarChart()
{ //构造柱状图
    QChart *chart =ui->chartViewBar->chart(); //获取ChartView关联的chart
    chart->removeAllSeries(); //删除所有序列

    chart->removeAxis(chart->axisX()); //删除坐标轴
    chart->removeAxis(chart->axisY()); //删除坐标轴

//创建三个QBarSet数据集,从数据模型的表头获取Name
    QBarSet *setMath = new QBarSet(theModel->horizontalHeaderItem(colNoMath)->text());
    QBarSet *setChinese = new QBarSet(theModel->horizontalHeaderItem(colNoChinese)->text());
    QBarSet *setEnglish= new QBarSet(theModel->horizontalHeaderItem(colNoEnglish)->text());

    QLineSeries *Line = new QLineSeries(); //创建一个QLineSeries序列用于显示平均分
    Line->setName(theModel->horizontalHeaderItem(colNoAverage)->text());
    QPen    pen;
    pen.setColor(Qt::red);
    pen.setWidth(2);
    Line->setPen(pen);
//    lineseries->setPointLabelsVisible(true);
//    lineseries->setPointLabelsFormat("@yPoint");

    for(int i=0;i<theModel->rowCount();i++)
    {//从数据模型获取数据
        setMath->append(theModel->item(i,colNoMath)->text().toInt()); //数学
        setChinese->append(theModel->item(i,colNoChinese)->text().toInt()); //语文
        setEnglish->append(theModel->item(i,colNoEnglish)->text().toInt()); //英语
        Line->append(QPointF(i,theModel->item(i,colNoAverage)->text().toFloat()));  //平均分
    }

//创建一个柱状图序列 QBarSeries, 并添加三个数据集
    QBarSeries *series = new QBarSeries();
    series->append(setMath);
    series->append(setChinese);
    series->append(setEnglish);
//    series->setLabelsVisible(true);
//    series->setLabelsPosition(QAbstractBarSeries::LabelsOutsideEnd);// LabelsCenter
//    series->setLabelsPosition(QAbstractBarSeries::LabelsCenter);// LabelsCenter

//    lineseries->setPointLabelsVisible(true);
//    lineseries->setPointLabelsFormat("@yPoint");

    chart->addSeries(series); //添加柱状图序列
    chart->addSeries(Line); //添加折线图序列

//用于横坐标在字符串列表,即学生姓名
    QStringList categories;
    for (int i=0;i<theModel->rowCount();i++)
        categories <<theModel->item(i,colNoName)->text();

//用于柱状图的坐标轴
    QBarCategoryAxis *axisX = new QBarCategoryAxis();
    axisX->append(categories); //添加横坐标文字列表
//    chart->createDefaultAxes();
    chart->setAxisX(axisX, series); //设置横坐标
    chart->setAxisX(axisX, Line);//设置横坐标
    axisX->setRange(categories.at(0), categories.at(categories.count()-1)); //这只坐标轴范围

//数值型坐标作为纵轴
    QValueAxis *axisY = new QValueAxis;
    axisY->setRange(0, 100);
    axisY->setTitleText("分数");
    axisY->setTickCount(6);//11
    axisY->setLabelFormat("%.0f"); //标签格式
//    axisY->setGridLineVisible(false);
//    axisY->setMinorTickCount(4);
    chart->setAxisY(axisY, series); //为
    chart->setAxisY(axisY, Line);

//    lineseries->setPointLabelsVisible(true);
//    lineseries->setPointLabelsFormat("@yPoint");

    chart->legend()->setVisible(true); //显示图例
    chart->legend()->setAlignment(Qt::AlignBottom); //图例显示在下方
}

绘制堆叠柱状图:

void MainWindow::iniStackedBar()
{//初始化叠加柱状图绘制窗口
    QChart *chart = new QChart();
    chart->setTitle("StackedBar 演示");
    chart->setAnimationOptions(QChart::SeriesAnimations);
    ui->chartViewStackedBar->setChart(chart);
    ui->chartViewStackedBar->setRenderHint(QPainter::Antialiasing);
}

void MainWindow::buildStackedBar()
{//绘制叠加柱状图
    QChart *chart =ui->chartViewStackedBar->chart(); //获取QChart对象
    chart->removeAllSeries();//删除所有序列
    chart->removeAxis(chart->axisX()); //删除坐标轴
    chart->removeAxis(chart->axisY());

//创建三门课程的数据集
    QBarSet *setMath = new QBarSet(theModel->horizontalHeaderItem(colNoMath)->text());
    QBarSet *setChinese = new QBarSet(theModel->horizontalHeaderItem(colNoChinese)->text());
    QBarSet *setEnglish= new QBarSet(theModel->horizontalHeaderItem(colNoEnglish)->text());

    for(int i=0;i<theModel->rowCount();i++)
    { //添加分数数据到数据集
        setMath->append(theModel->item(i,colNoMath)->text().toInt());
        setChinese->append(theModel->item(i,colNoChinese)->text().toInt());
        setEnglish->append(theModel->item(i,colNoEnglish)->text().toInt());
    }

//创建   QStackedBarSeries对象并添加数据集
    QStackedBarSeries *series = new QStackedBarSeries();
    series->append(setMath);
    series->append(setChinese);
    series->append(setEnglish);
    series->setLabelsVisible(true);//显示每段的标签
//    series->setLabelsPosition(QAbstractBarSeries::LabelsCenter);// LabelsCenter

    chart->addSeries(series); //添加序列到图表

    QStringList categories;
    for (int i=0;i<theModel->rowCount();i++)
        categories <<theModel->item(i,colNoName)->text();

    QBarCategoryAxis *axisX = new QBarCategoryAxis(); //类别坐标轴,作为横轴
    axisX->append(categories);
    chart->setAxisX(axisX, series);
    axisX->setRange(categories.at(0), categories.at(categories.count()-1));

    QValueAxis *axisY = new QValueAxis; //数值坐标轴,作为纵轴
    axisY->setRange(0, 300);
    axisY->setTitleText("总分");
    axisY->setTickCount(6);
    axisY->setLabelFormat("%.0f"); //标签格式
    chart->setAxisY(axisY, series);

    chart->legend()->setVisible(true);
    chart->legend()->setAlignment(Qt::AlignBottom);
}

绘制百分比柱状图:

void MainWindow::iniPercentBar()
{//百分比柱状图初始化
    QChart *chart = new QChart();
    chart->setTitle("PercentBar 演示");
    chart->setAnimationOptions(QChart::SeriesAnimations);
    ui->chartViewPercentBar->setChart(chart);
    ui->chartViewPercentBar->setRenderHint(QPainter::Antialiasing);
}

void MainWindow::buildPercentBar()
{//绘制百分比柱状图
    QChart *chart =ui->chartViewPercentBar->chart();
    chart->removeAllSeries();
    chart->removeAxis(chart->axisX());
    chart->removeAxis(chart->axisY());

//创建数据集
    QBarSet *setMath = new QBarSet(theModel->horizontalHeaderItem(colNoMath)->text());
    QBarSet *setChinese = new QBarSet(theModel->horizontalHeaderItem(colNoChinese)->text());
    QBarSet *setEnglish= new QBarSet(theModel->horizontalHeaderItem(colNoEnglish)->text());

    QTreeWidgetItem *item; //节点
    QStringList categories;
    for (int i=0;i<=4;i++)
    {//从分数段统计数据表里获取数据,添加到数据集
        item=ui->treeWidget->topLevelItem(i);
        categories<<item->text(0);//用作横坐标的标签
        setMath->append(item->text(colNoMath).toFloat());
        setChinese->append(item->text(colNoChinese).toFloat());
        setEnglish->append(item->text(colNoEnglish).toFloat());
    }

    QPercentBarSeries *series = new QPercentBarSeries(); //序列
    series->append(setMath);
    series->append(setChinese);
    series->append(setEnglish);
    series->setLabelsVisible(true);//显示百分比
//    series->setLabelsFormat("@value人");

    chart->addSeries(series);

    QBarCategoryAxis *axisX = new QBarCategoryAxis(); //横坐标
    axisX->append(categories);
    chart->setAxisX(axisX, series);
    axisX->setRange(categories.at(0), categories.at(categories.count()-1));

    QValueAxis *axisY = new QValueAxis;//纵坐标
    axisY->setRange(0, 100);
    axisY->setTitleText("百分比");
    axisY->setTickCount(6);
    axisY->setLabelFormat("%.1f"); //标签格式
    chart->setAxisY(axisY, series);

    chart->legend()->setVisible(true);
    chart->legend()->setAlignment(Qt::AlignRight); //AlignBottom
}

绘制散点图:

void MainWindow::iniScatterChart()
{//散点图初始化
    QChart *chart = new QChart();
    chart->setTitle("QScatterSeries 和 QSplineSeries 演示");
    chart->setAnimationOptions(QChart::SeriesAnimations);
    ui->chartViewScatter->setChart(chart);
    ui->chartViewScatter->setRenderHint(QPainter::Antialiasing);
}

void MainWindow::buildScatterChart()
{ //绘制 QScatterSeries和QSplineSeries图
    QChart *chart =ui->chartViewScatter->chart();
    chart->removeAllSeries();
    chart->removeAxis(chart->axisX());
    chart->removeAxis(chart->axisY());

    QSplineSeries *seriesLine = new QSplineSeries(); //光滑曲线序列
    seriesLine->setName("spline");
    QPen pen;
    pen.setColor(Qt::blue);
    pen.setWidth(2);
//    seriesLine->setColor(Qt::blue);
    seriesLine->setPen(pen);

    QScatterSeries *series0 = new QScatterSeries(); //散点序列
    series0->setName("散点");
    series0->setMarkerShape(QScatterSeries::MarkerShapeCircle);//MarkerShapeRectangle,MarkerShapeCircle
    series0->setBorderColor(Qt::black);
    series0->setBrush(QBrush(Qt::red));
    series0->setMarkerSize(12);

    qsrand(QTime::currentTime().second());//随机数种子

    for (int i=0;i<10;i++)
    {
        int x=(qrand() % 20);//0到20之间的随机数
        int y=(qrand() % 20);
        series0->append(x,y); //散点序列
        seriesLine->append(x,y); //光滑曲线序列
    }

    chart->addSeries(series0);
    chart->addSeries(seriesLine);

//    QValueAxis *axisY = new QValueAxis;
//    axisY->setRange(0, 20);
//    axisY->setTitleText("Y");
//    axisY->setTickCount(11);
//    axisY->setLabelFormat("%.0f"); //标签格式
//    axisY->setGridLineVisible(true);
//    chart->setAxisY(axisY, series0);
//    chart->setAxisY(axisY, seriesLine);

//    QValueAxis *axisX = new QValueAxis;
//    axisX->setRange(0, 20);
//    axisX->setTitleText("X");
//    axisX->setTickCount(11);
//    axisX->setLabelFormat("%.0f"); //标签格式
//    axisX->setGridLineVisible(true);
//    chart->setAxisX(axisX, series0);
//    chart->setAxisX(axisX, seriesLine);

    chart->createDefaultAxes(); //创建缺省的坐标轴
    chart->axisX()->setTitleText("X 轴");
    chart->axisX()->setRange(-2,22);

    chart->axisY()->setTitleText("Y 轴");
    chart->axisY()->setRange(-2,22);


    chart->legend()->setVisible(true);
    chart->legend()->setAlignment(Qt::AlignRight); //AlignBottom

    foreach (QLegendMarker* marker, chart->legend()->markers()) {
        QObject::disconnect(marker, SIGNAL(clicked()), this, SLOT(on_chartBarLegendMarkerClicked()));
        QObject::connect(marker, SIGNAL(clicked()), this, SLOT(on_chartBarLegendMarkerClicked()));
    }

}

Qt是一个跨平台的C++应用程序开发框架,它提供了丰富的图形用户界面组件,包括用于绘制图表的功能。要在Qt中创建一个简单的柱状图表程序,你可以使用QChart、QGraphicsScene等类库。以下是一个基本步骤: 1. **设置环境**:确保已经安装了Qt开发工具和Qt Charts模块。 2. **创建主窗口**:创建一个新的Qt Widgets应用,并添加一个QChartView组件到窗口中,这将是显示图表的地方。 3. **创建图表**: - 使用`QChart *chart = new QChart()` 创建一个新的图表实例。 - 设置图表标题,背景等样式属性,例如 `chart->setTitle("柱状图")`. 4. **添加系列**: - 使用`QBarSet *barSet = new QBarSet("数据集名称");` 创建一个条形数据集。 - 添加数据点,如 `barSet->append(0, 10);` 表示第一个数据点值为10。 5. **添加数据到图表**: - 将数据集添加到图表上,`chart->addSeries(barSet)`。 6. **配置轴和视觉效果**: - 创建轴,比如X轴和Y轴 (`QValueAxis *xAxis = new QValueAxis(); chart->addAxis(xAxis, Qt::AlignBottom);`)。 - 配置轴的范围和标签。 7. **设置视图模式**: - 设置图表是否为堆叠模式 (`QStackedBarSeries *stackedBarSeries = new QStackedBarSeries(); stackedBarSeries->setStyle(QStackedBarSeries::Fill); chart->addSeries(stackedBarSeries)`,如果需要堆叠柱状图)。 8. **显示图表**: - 更新图表数据 `chart->createDefaultAxes(); chart->update()`. - 最后将QChart绑定到QChartView上。 ```cpp // 示例代码片段 void drawHistogram() { // ... (以上步骤) // 绘制图表 scene->addChart(chart); chartView->setScene(scene); } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值