QT5_chart_常见几种图形

柱状图显示数据(需要在.pro文件引入 “QT += core gui sql charts”)

    QBarSet *set0 = new QBarSet("对象0");
    QBarSet *set1 = new QBarSet("对象1");
    
    //输入数据
    *set0 <<1102<< 1054<< 	1013<< 	989<< 	941<< 	920<< 	893<< 	951<< 	980<< 	1016<< 	1056 <<1088;
    *set1 <<936<<	895<<	861<<	841<<	800<<	782<<	759<<	809<<	833<<	864<<	898<< 925;

    QBarSeries *series = new QBarSeries();
    series->append(set0);
    series->append(set1);

    QChart *chart = new QChart();
    chart->addSeries(series);
    chart->setTitle("xxx"); //xxx为图表名称会在图中显示,不同于界面名称
    chart->setAnimationOptions(QChart::SeriesAnimations);
    
    //设置x,y轴坐标轴名字
    QValueAxis *axisx = new QValueAxis;
    axisx->setTitleText("X轴");//名称任意设置
    chart->setAxisX(axisx, series);

    QValueAxis *axisy = new QValueAxis;
    axisy->setTitleText("Y轴");//名称任意设置
    chart->setAxisY(axisy,series);
    
    //设置横轴刻度线
    QStringList categories;
    categories << "Jan" << "Feb" << "Mar" << "Apr" << "May" << "Jun" << "July" << "Aug" << "Sept" << "Ocr" <<"Nove" << "Dec";
    QBarCategoryAxis *axis = new QBarCategoryAxis();
    axis->append(categories);
    chart->createDefaultAxes();//创建默认的左侧的坐标轴(根据 QBarSet 设置的值)
    chart->setAxisX(axis, series);//设置坐标轴

    chart->legend()->setVisible(true); //设置图例为显示状态
    chart->legend()->setAlignment(Qt::AlignBottom);//设置图例的显示位置在底部

    chart->setTheme(QChart::ChartThemeBlueCerulean);//设置背景,背景有几种默认选择

    QChartView *chartView = new QChartView(chart);
    chartView->setRenderHint(QPainter::Antialiasing);
    this->setCentralWidget(chartView);
    this->resize(800, 600); //调整界面大小

曲线显示数据(需要在.pro文件引入 “QT += core gui sql charts”)

       QChartView * _chart_view = new QChartView;

       this->setCentralWidget(_chart_view);

        //自定义背景
       //_chart_view->chart()->setBackgroundVisible(false);
       //setStyleSheet(R"(QGraphicsView{ background-image:url(:/tubiao/33.png);})");
       /* 以下代码完成曲线添加 */

       QLineSeries *seriesx = new QLineSeries();
       for(int i=1;i<=5223;i=i+1) {

          seriesx->append(i,a[i]); //数组a中即为要显示的数据值,个数与i的最大范围统一

       }
       _chart_view->chart()->addSeries(seriesx);


      //chart的背景和ui文件的背景不同
       _chart_view->chart()->setTheme(QChart::ChartThemeBlueCerulean);
       //_chart_view->chart()->setTheme(QChart::Ch)

       seriesx->setName("xxx");//xxx为图中显示的点名称

       _chart_view->chart()->setTitle("xxxx"); //xxxx为显示曲线的名称

       //设置x,y轴坐标轴名字
       QValueAxis *axisx = new QValueAxis;
       axisx->setTitleText("xxxx"); //xxxx为x轴名称
       _chart_view->chart()->setAxisX(axisx, seriesx);

       QValueAxis *axisy = new QValueAxis;
       axisy->setTitleText("xxxx"); //xxxx为y轴名称
      _chart_view->chart()->setAxisY(axisy,seriesx);

       _chart_view->chart()->legend()->setVisible(true); //使曲线标题可视

       _chart_view->chart()->legend()->setAlignment(Qt::AlignBottom);//将线名放于底部,参数可调
       this->resize(800, 600);
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值