QtCharts 折线、饼图代码

#include "demo_01.h"

#include 
using namespace QtCharts;

#include 
#include 

double gX = 0;
double gY = 0;
QVector gPts;

demo_01::demo_01(QWidget *parent)
    : QWidget(parent)
{
   //创建图表
    QChartView* chartView = new QChartView(this);
    QChart* chart = new QChart();
    chart->setTitle(QStringLiteral("简单曲线"));
    chartView->setChart(chart);
    chartView->setRenderHint(QPainter::Antialiasing);

    //创建折线序列
    QLineSeries* series0 = new QLineSeries();

    series0->setName(QStringLiteral("sin曲线"));


    //设置线条颜色,粗细
    QPen pen;
    pen.setColor(Qt::red);
    pen.setWidth(2);
    series0->setPen(pen);

    chart->addSeries(series0);

    //序列添加数值
    /*  double t = 0, y1, y2, intv = 0.1;
    int cnt = 100;
    for (int i = 0; i < cnt; i++) {
        y1 = qSin(t);
        series0->append(t, y1);
        y2 = qSin(t + 20);
        series1->append(t, y2);
        t += intv;
    }*/


    //创建坐标轴
    QValueAxis* axisX = new QValueAxis;
    axisX->setRange(0, 10);
    axisX->setTitleText("time(secs)");
    axisX->setLabelFormat("%.2f s");  ///>坐标轴上显示的坐标保留2位小数
    axisX->setTickCount(5);        ///>设置坐标轴主刻度的个数,简单理解为垂直线有n条
    axisX->setMinorTickCount(4);    ///>次刻度线条数
    axisX->setLabelsAngle(30);      ///>设置标签角度,单位为度
    axisX->setGridLineColor(Qt::blue);

    QValueAxis* axisY = new QValueAxis;
    axisY->setRange(-2, 2);
    axisY->setTitleText("value");

    //为序列设置坐标轴
#if 0 // 方式1
    chart->setAxisX(axisX, series0);
    chart->setAxisY(axisY, series0);
    chart->setAxisX(axisX, series1);
    chart->setAxisY(axisY, series1);
#else // 方式2
    chart->addAxis(axisX, Qt::AlignBottom);
    chart->addAxis(axisY, Qt::AlignLeft);
    series0->attachAxis(axisX);
    series0->attachAxis(axisY);
#endif

    //图例是一个QLegend类的对象,通过QChart::legend()可以获得图表的图例。可以修改图例的一些属性,如位置、字体等
    chart->legend()->setVisible(true);
    chart->legend()->setAlignment(Qt::AlignRight);

    //设置序列或坐标轴的动画效果
    chart->setAnimationOptions(QChart::AnimationOption::GridAxisAnimations);

    //关于图表外观的设置
    QFont font;
    font.setPixelSize(16);
    chart->setTitleFont(font);
    chart->setTitleBrush(Qt::blue);

    //主题是预定义的图表配色样式,是QChart::ChartTheme枚举类型,有多种取值,使图表具有不同的配色效果
    chart->setTheme(QChart::ChartTheme::ChartThemeBlueCerulean);

    gPts.reserve(20000);

    //动态刷新曲线
    series0->append(0, 0);
    QTimer* timer = new QTimer(this);
    connect(timer, &QTimer::timeout, [=]
        {
            qreal dwidth = chart->plotArea().width() / (axisX->tickCount() * 4.0); //一次滚动多少宽度
            qreal dx = 10.0 / (axisX->tickCount() * 4.0); //横坐标偏移量

            gX += dx;

            for (double i = gX - dx; i < gX; i+=0.1) {
                gY = sin(i);
                gPts.append(QPointF(i, gY));
            }

            series0->replace(gPts);
          

            /*满屏之后滚动窗口*/
            if (gX > 10)
                chart->scroll(dwidth, 0); //dwidth 代表的窗口横坐标方向滚动的区域大小
                                          //dwidth 的单位不是横坐标的单位,而是窗口像素
        }
    );
    timer->start(500);


    //布局
    QVBoxLayout* mainLayout = new QVBoxLayout(this);
    mainLayout->addWidget(chartView);

    this->setMinimumSize(1024, 768);
}

QtCharts 折线、饼图代码_第1张图片

#include "demo_02.h"

#include 
using namespace QtCharts;

#include 

demo_02::demo_02(QWidget *parent)
    : QWidget(parent)
{
    QChartView* chartView = new QChartView(this);
    QChart* chart = new QChart;
    chartView->setChart(chart);

    //[] 设置主题放在这里,后面的外观设置就可以生效
    chart->setTheme(QChart::ChartTheme::ChartThemeDark);

    //[] chartView
    chartView->setMinimumSize(1024, 768);
    chartView->setRenderHint(QPainter::RenderHint::Antialiasing);

    //[] series
    QPieSeries* series = new QPieSeries(this);
    series->setHoleSize(0.2); ///> 设置饼图中心空间的大小

    auto slice1 = series->append("pieslice_1", 55);
    auto slice2 = series->append("pieslice_2", 25);
    auto slice3 = series->append("pieslice_3", 15);
    auto slice4 = series->append("pieslice_4", 27);
    auto slice5 = series->append("pieslice_5", 120); 

 
    slice1->setColor(Qt::red);
    slice1->setLabel("pieslice_1 sfas fdas");
    slice1->setLabelColor(Qt::red);
    slice1->setLabelFont(QFont("", 16));
    slice1->setLabelPosition(QPieSlice::LabelPosition::LabelOutside);
    
    QTimer* timer = new QTimer(this);
    connect(timer, &QTimer::timeout, [=]
        {
            slice1->setValue(slice1->value() + 1);
            slice1->setLabel(QString::number(slice1->value()));
        }
    );
    timer->start(666);
  
    for (auto slice : series->slices()) {
        connect(slice, &QPieSlice::hovered, [=](bool show)
            { 
                slice->setExploded(show); 
            }
        );
    }

    series->setLabelsVisible(true);

    //[] chart
    chart->addSeries(series);
    chart->legend()->setAlignment(Qt::AlignBottom);
    chart->legend()->setVisible(true);
    

    QVBoxLayout* mainLayout = new QVBoxLayout(this);
    mainLayout->addWidget(chartView);
}

 QtCharts 折线、饼图代码_第2张图片

 

你可能感兴趣的:(Qt,qt,开发语言)