QChart柱状图、折线图和散点图图像及X轴刷新

使用QChart需要在*.pro中添加

QT       += charts
在*.h文件中添加使用Qt中预先定义好的宏:

[cpp] view plain copy

QT_CHARTS_BEGIN_NAMESPACE
class QChartView;
class QChart;
QT_CHARTS_END_NAMESPACE
QT_CHARTS_USE_NAMESPACE
或者使用

  1. using namespace QtCharts  
柱状图:
mainwindow.h

#include 
#include 
#include 
#include 
//#include 
//#include 
//#include 
//  #include 
#include 
#include 
//  #include 
//  #include 
#include 
#include 
#include 
#include 
 
  
#include 
 
  
QT_CHARTS_BEGIN_NAMESPACE
class QChartView;
class QChart;
QT_CHARTS_END_NAMESPACE
 
  
QT_CHARTS_USE_NAMESPACE
 
  
namespace Ui {
class MainWindow;
}
 
  
class MainWindow : public QMainWindow
{
    Q_OBJECT
 
  
public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();
private:
    void initChart();
 
  
private slots:
    void sltpushButton();
    void sltpushButton_2();
    void sltpushButton_3();
    void sltCategoriesChanged();
 
  
private:
    Ui::MainWindow *ui;
 
  
    QChart*                         m_typeChart;
    QChartView*                     m_typeView;
    QBarCategoryAxis*               m_typeAxis;
    QValueAxis*                     m_typeAxisY;
    QBarSeries*                     m_barSeries;
    QBarSet*                        m_barSet;
 
  
};

 mainwindow.cpp

#include "mainwindow.h"
#include "ui_mainwindow.h"
 
  
MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    initChart();
}
 
  
MainWindow::~MainWindow()
{
    delete ui;
}
 
  
void MainWindow::initChart()
{
    m_typeChart = new QChart();
    m_typeChart->setAnimationOptions(QChart::SeriesAnimations);//动画效果
 
  
    m_barSet = new QBarSet("eg");//创建条,可以创建多个
    m_barSet->setLabelColor(QColor(0,0,0));
 
  
    m_barSeries = new QBarSeries(m_typeChart);
    m_barSeries->setLabelsVisible(true);//判断是否在图柱上显示数值
    m_barSeries->setLabelsPosition(QAbstractBarSeries::LabelsOutsideEnd);//在柱上显示数值方式
    m_typeChart->addSeries(m_barSeries);
    m_typeChart->setTitle("XXXX");
 
  
    m_typeAxis = new QBarCategoryAxis();//创建X轴
    m_typeChart->createDefaultAxes();
    m_typeChart->setAcceptTouchEvents(true);
    m_typeChart->setAxisX(m_typeAxis, m_barSeries);
 
  
    m_typeAxisY = new QValueAxis;//创建Y轴
    m_typeAxisY->setRange(0, 10);//显示数值范围
    m_typeAxisY->setTickCount(10);     //设置多少格
//        m_typeAxisY->setLabelFormat("%u"); //设置刻度的格式  
//        m_typeAxisY->setTitleText("AxisY"); //设置Y轴标题  
//        m_typeAxisY->setGridLineVisible(true); //设置是否显示网格线
    m_typeAxisY->setMinorTickCount(5); //设置每格小刻度数目
 
  
    m_typeChart->setAxisY(m_typeAxisY, m_barSeries);
 
  
    m_typeChart->legend()->setVisible(true);
    m_typeChart->legend()->setAlignment(Qt::AlignBottom);
    m_typeChart->legend()->hide();
    m_typeView = new QChartView();
    m_typeView->setChart(m_typeChart);
    m_typeView->setRubberBand(QChartView::RectangleRubberBand);//拉伸效果
    m_typeView->setRenderHint(QPainter::Antialiasing);
 
  
    m_typeView->chart()->setTheme(QChart::ChartThemeBrownSand);
    QPalette pal = window()->palette();
    pal.setColor(QPalette::Window, QRgb(0x9e8965));
    pal.setColor(QPalette::WindowText, QRgb(0x404044));
    window()->setPalette(pal);
    ui->widget->layout()->addWidget(m_typeView);
 
  
    sltpushButton();
    connect(ui->pushButton_2, SIGNAL(clicked()), this, SLOT(sltpushButton_2()));
    connect(ui->pushButton_3, SIGNAL(clicked()), this, SLOT(sltpushButton_3()));
    connect(m_typeAxis, SIGNAL(categoriesChanged()), this, SLOT(sltCategoriesChanged()));//QValueAxis改变是会触发信号
}
 
  
void MainWindow::sltpushButton()
{
    *m_barSet << 1 << 2 << 3 << 4 << 5 << 6;
    m_barSeries->append(m_barSet);
    QStringList categories;
    categories  << "Feb" << "Mar" << "Apr" << "May" << "Jun" << "Jan";
    m_typeAxis->append(categories);//
}
 
  
void MainWindow::sltpushButton_2()
{
    m_typeAxis->clear();
}
/*****************
不适用触发信号barSeries不刷新 eg.sltpushButton_3
*******************/
void MainWindow::sltpushButton_3()
{
    //     m_typeAxis->clear();
    //     m_barSeries->clear();
    //    m_barSet = new QBarSet("eg");
    //    m_barSet->setLabelColor(QColor(0,0,0));
    //    *m_barSet << 3 << 4 << 5 << 6<< 1 << 2;
    //    m_barSeries->append(m_barSet);
    //    QStringList categories;
    //    categories << "A" << "B" << "C"<< "D" << "E" << "F";
    //    m_typeAxis->append(categories);
}
void MainWindow::sltCategoriesChanged()
{
    m_barSeries->clear();//因为下边会重新添加新的X轴所以会触发两次因此需要清空QBarSeries
    m_barSet = new QBarSet("eg");//需要重新创建一个柱
    m_barSet->setLabelColor(QColor(0,0,0));
    *m_barSet<< 2 << 3 << 4 << 5 << 6<<1;
    m_barSeries->append(m_barSet);
    QStringList categories;
    categories  << "1" << "2" << "3" << "4"<< "5"<< "6" ;
    m_typeAxis->append(categories);
}
代码下载http://download.csdn.net/download/u010304326/10227887

接线图结合散点图

mainwindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H
 
  
#include 
#include 
//#include 
//#include 
//#include 
#include 
  #include 
  #include 
//  #include 
//  #include 
#include 
#include 
#include 
#include 
#include 
 
  
QT_CHARTS_BEGIN_NAMESPACE
class QChartView;
class QChart;
QT_CHARTS_END_NAMESPACE
 
  
QT_CHARTS_USE_NAMESPACE
 
  
namespace Ui {
class MainWindow;
}
 
  
class MainWindow : public QMainWindow
{
    Q_OBJECT
 
  
public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();
private:
    void initline();
 
  
private slots:
    void sltpushButton();
    void sltpushButton_2();
    void sltpushButton_3();
    void sltCategoriesChanged();
 
  
private:
    Ui::MainWindow *ui;
    QChart*                     m_chart;
    QLineSeries*                m_lineSeries;
    QScatterSeries*             m_scatterSeries;
    QBarCategoryAxis*           m_axis;
    QValueAxis*                 m_axisY;
    QChartView*                 m_chartView;
    QList<int>                  m_list;
 
  
};
 
  
#endif // MAINWINDOW_H
 mainwindow.cpp 
  

#include "mainwindow.h"
#include "ui_mainwindow.h"
 
  
MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    initline();
}
 
  
MainWindow::~MainWindow()
{
    delete ui;
}
 
  
void MainWindow::initline()
{
    m_chart = new QChart();
    m_chart->setAnimationOptions(QChart::SeriesAnimations);
    m_lineSeries = new QLineSeries(m_chart);//创建折线
    m_lineSeries->setPen(QPen(Qt::lightGray,2,Qt::SolidLine));
    m_lineSeries->setColor(QColor(248, 167, 0));
    //     m_lineSeries->setPointLabelsVisible(true);
    m_scatterSeries = new QScatterSeries(m_chart);//创建散点
    m_scatterSeries->setMarkerShape(QScatterSeries::MarkerShapeRectangle);//设置散点样式
    m_scatterSeries->setMarkerSize(15);//设置散点大小
    m_chart->addSeries(m_lineSeries);
    m_chart->addSeries(m_scatterSeries);
    m_chart->setTitle("xxxx");
 
  
    *m_lineSeries << QPointF(0, 5) << QPointF(3, 0) << QPointF(7, 6) << QPointF(9, 7) << QPointF(10, 6);
    *m_scatterSeries<< QPointF(0, 5) << QPointF(3, 0) << QPointF(7, 6) << QPointF(9, 7) << QPointF(10, 6);
 
  
    QStringList list;
    list<<"0"<<"1"<<"2"<<"3"<<"4"<<"5"<<"6"<<"7"<<"8"<<"9"<<"10"<<"11"<<"12"<<"13";
    m_axis = new QBarCategoryAxis();
    m_axis->append(list);//
    m_chart->createDefaultAxes();
    //    m_chart->setAcceptHoverEvents(true);
    m_chart->setAcceptTouchEvents(true);
    m_chart->setAxisX(m_axis, m_lineSeries);
    m_chart->setAxisX(m_axis, m_scatterSeries);
 
  
    m_axisY = new QValueAxis;
    m_axisY->setRange(0, 10);
    //    m_axisY->setLabelFormat("%u"); //设置刻度的格式
    //    m_axisY->setGridLineVisible(true);
    m_axisY->setTickCount(10);     //设置多少格
    m_axisY->setMinorTickCount(5); //设置每格小刻度线的数目
    m_chart->setAxisY(m_axisY, m_lineSeries);
    m_chart->setAxisY(m_axisY, m_scatterSeries);
    m_chart->legend()->setVisible(true);
    m_chart->legend()->setAlignment(Qt::AlignBottom);
    m_chart->legend()->hide();
    m_chartView = new QChartView(m_chart);
    m_chartView->setRenderHint(QPainter::Antialiasing);
 
  
    m_chartView->chart()->setTheme(QChart::ChartThemeBrownSand);
 
  
    QPalette pal = window()->palette();
    pal.setColor(QPalette::Window, QRgb(0x9e8965));
    pal.setColor(QPalette::WindowText, QRgb(0x404044));
    window()->setPalette(pal);
    ui->widget->layout()->addWidget(m_chartView);
    m_list<<2<<3<<5<<4;
    connect(ui->pushButton_2, SIGNAL(clicked()), this, SLOT(sltpushButton_2()));
    connect(ui->pushButton_3, SIGNAL(clicked()), this, SLOT(sltpushButton_3()));
    connect(m_axis, SIGNAL(categoriesChanged()), this, SLOT(sltCategoriesChanged()));
 
  
}
 
  
void MainWindow::sltpushButton()
{
}
void MainWindow::sltpushButton_2()
{
    m_axis->clear();
}
 
  
void MainWindow::sltpushButton_3()
{
 
  
}
void MainWindow::sltCategoriesChanged()
{
    m_lineSeries->clear();
    m_scatterSeries->clear();
    for(int i = 0 ; i<m_list.count(); i++)
    {
        m_lineSeries->append(i,m_list.at(i));
        m_scatterSeries->append(i,m_list.at(i));
    }
    QStringList categories;
    categories << "A" << "B" << "C"<< "D" << "E" << "F";
    m_axis->append(categories);//
}

代码下载:http://download.csdn.net/download/u010304326/10227980







你可能感兴趣的:(Qt,QBarSeries,QLineSeries,QScatterSeries)