试一试QtCharts

2017-1-25 Plan C Qt

#include <QApplication>
#include <QBarSet>
#include <QBarCategoryAxis>
#include <QDialog>
#include <QGridLayout>
#include <QLineSeries>
#include <QSplineSeries>
#include <QPieSeries>
#include <QBarSeries>
#include <QPercentBarSeries>
#include <QAreaSeries>
#include <QChartView>
#include <QChart>
using namespace QtCharts;

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    QDialog dialog;
    QGridLayout layout(&dialog);
    QChartView vLine,vSpline,vPie,vBar,vPercent,vArea;
    layout.addWidget(&vLine,0,0);
    layout.addWidget(&vSpline,0,1);
    layout.addWidget(&vPie,0,2);
    layout.addWidget(&vBar,1,0);
    layout.addWidget(&vPercent,1,1);
    layout.addWidget(&vArea,1,2);
    dialog.show();

    /* 抗失真 */
    vLine.setRenderHint(QPainter::Antialiasing);
    vSpline.setRenderHint(QPainter::Antialiasing);
    vPie.setRenderHint(QPainter::Antialiasing);
    vBar.setRenderHint(QPainter::Antialiasing);
    vPercent.setRenderHint(QPainter::Antialiasing);
    vArea.setRenderHint(QPainter::Antialiasing);

    /* 实例化图表对象 */
    QChart cLine,cSpline,cPie,cBar,cPercent,cArea;
    vLine.setChart(&cLine);
    vSpline.setChart(&cSpline);
    vPie.setChart(&cPie);
    vBar.setChart(&cBar);
    vPercent.setChart(&cPercent);
    vArea.setChart(&cArea);

    /* 折线图 */
    QLineSeries line1;
    line1.append(0,0);
    line1.append(1,10);
    line1.append(2,5);
    line1.append(3,30);
    line1.append(4,40);
    line1.setName("line1");
    QLineSeries line2;
    line2.append(0,0);
    line2.append(1,15);
    line2.append(2,20);
    line2.append(3,30);
    line2.append(4,20);
    line2.setName("line2");
    cLine.addSeries(&line1);
    cLine.addSeries(&line2);
    cLine.createDefaultAxes();
    cLine.setTitle("Line");

    /* 平滑曲线图 */
    QSplineSeries spline1;
    spline1.append(0,0);
    spline1.append(1,10);
    spline1.append(2,5);
    spline1.append(3,30);
    spline1.append(4,40);
    spline1.setName("line1");
    QSplineSeries spline2;
    spline2.append(0,0);
    spline2.append(1,15);
    spline2.append(2,20);
    spline2.append(3,30);
    spline2.append(4,20);
    spline2.setName("line2");
    cSpline.addSeries(&spline1);
    cSpline.addSeries(&spline2);
    cSpline.createDefaultAxes();
    cSpline.setTitle("Spline");

    /* 饼状图 */
    QPieSeries* pie = new QPieSeries;
    pie->setHoleSize(0.5);
    pie->append("Male",20);
    pie->append("Female",50);
    cPie.addSeries(pie);
    cPie.setTitle("Pie");

    /* 条形图 */
    QBarSeries bar;
    QBarSet barset1("1");
    barset1 << 10 << 20 << 15;
    bar.append(&barset1);
    QBarSet barset2("2");
    barset2 << 5 << 25 << 15;
    bar.append(&barset2);
    cBar.addSeries(&bar);
    cBar.createDefaultAxes();
    QStringList category;
    category << "2015" << "2016" << "2017";
    QBarCategoryAxis axis;
    axis.append(category);
    cBar.setAxisX(&axis);


    /* 百分比条形图 */
    QPercentBarSeries percent;
    QBarSet barset3("1");
    barset3 << 10 << 20 << 15;
    percent.append(&barset3);
    QBarSet barset4("2");
    barset4 << 5 << 25 << 15;
    percent.append(&barset4);
    cPercent.addSeries(&percent);
    cPercent.createDefaultAxes();

    /* 面积图 */
    QLineSeries upper,lower;
    upper << QPoint(2,5) << QPoint(3,10) << QPoint(4,20) << QPoint(5,15);
    lower << QPoint(2,5) << QPoint(3,0) << QPoint(4,10) << QPoint(5,15);
    QAreaSeries area(&upper,&lower);
    cArea.addSeries(&area);
    cArea.createDefaultAxes();

    return a.exec();

}


标签: QtCharts

发表评论:

Powered by emlog
鄂ICP备16003833号