Qt中创建柱状图

Qt中创建柱状图
1.必要设置
.pro文件

QT += charts

头文件

#include 

QT_CHARTS_USE_NAMESPACE

namespace Ui {
class MainWindow;
}

2.设置数据
存储数据到QTableView中

void MainWindow::iniData()
{
    QStringList headerList;
    headerList<<"姓名"<<"语文"<<"数学"<<"英语";
    theModel->setHorizontalHeaderLabels(headerList);//设置表头文字
    //qsrand(QTime::currentTime().second());
    for(int i=0;i<theModel->rowCount();i++)
    {
        QString Name = QString::asprintf("学生%2d",i+1);
        QStandardItem* aItem = new QStandardItem(Name);//创建Item
        aItem->setTextAlignment(Qt::AlignHCenter);//???
        theModel->setItem(i,colNoName,aItem);//学生列设置Item
        qreal Score;
        for(int j=colNoChinese;j<=colNoEnglish;j++)
        {
            Score = (qrand()%50)+50;
            aItem = new QStandardItem(QString::asprintf("%.0f",Score));
            aItem->setTextAlignment(Qt::AlignHCenter);
            theModel->setItem(i,j,aItem);
        }
        aItem->setFlags(aItem->flags()&(!Qt::ItemIsEditable));
    }
}

2.初始化

    QChart *chart = new QChart();//创建图表
    chart->setTitle("BarChart演示");//设置图表标题
    chart->setAnimationOptions(QChart::SeriesAnimations);//序列动态
    ui->chartView->setChart(chart);//图表添加到场景
    ui->chartView->setRenderHint(QPainter::Antialiasing);

3.创建柱状图

    QChart *chart = ui->chartView->chart();//获取与场景关联的图形
    chart->removeAllSeries();//删除序列
    chart->removeAxis(chart->axisX());//删除图表的X坐标轴
    chart->removeAxis(chart->axisY());//删除图标的Y坐标轴
//数据集(QBarSet)
    QBarSet *setChinese = new QBarSet(theModel->horizontalHeaderItem(colNoChinese)->text());
    QBarSet *setMath = new QBarSet(theModel->horizontalHeaderItem(colNoMath)->text());
    QBarSet *setEnglish = new QBarSet(theModel->horizontalHeaderItem(colNoEnglish)->text());
    for(int i=0;i<theModel->rowCount();i++)
    {
        //从数据模型获取数据
        setChinese->append(theModel->item(i,colNoChinese)->text().toInt());
        setMath->append(theModel->item(i,colNoMath)->text().toInt());
        setEnglish->append(theModel->item(i,colNoEnglish)->text().toInt());
    }

//依据数据创建柱状图
    QBarSeries *series = new QBarSeries();
    chart->addSeries(series);//序列添加到图表

    series->append(setChinese);//为序列添加数据
    series->append(setMath);//
    series->append(setEnglish);//

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

//横坐标——字符型
    QBarCategoryAxis *axisX = new QBarCategoryAxis();
    axisX->append(category);//横坐标的坐标值(字符型坐标)
    chart->setAxisX(axisX,series);//图表、序列、坐标配合
    axisX->setRange(category.at(0),category.at(category.count()-1));//X字符型坐标范围
//纵坐标——数值型
    QValueAxis *axisY = new QValueAxis;
    axisY->setRange(0,100);
    axisY->setTitleText("分数");
    axisY->setTickCount(10);
    axisY->setLabelFormat("%.1f");
    chart->setAxisY(axisY,series);

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

特别注意柱状图所需要的数据为QBarSet类
colNoChinese,colNoMath,colNoEnglish 为列的值,也可直接用1 2 3 等数值代替
Qt中创建柱状图_第1张图片

你可能感兴趣的:(Qt)