亲宝软件园·资讯

展开

Qt_QChart的使用记录(小白)

雪衣遮 人气:0

  

主要是记录柱状图的数值显示,散点图的点坐标显示(防止后续忘记,把文件都贴出来,方便复查)

资源库: WarehouseInputOrOutput.pro
 1 QT       += core gui
 2 QT += charts
 3 
 4 greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
 5 
 6 TARGET = WarehouseInputOrOutput
 7 TEMPLATE = app
 8 
 9 DEFINES += QT_DEPRECATED_WARNINGS
10 
11 SOURCES += main.cpp\
12         warehouseinputoroutput.cpp \
13         callout.cpp
14 
15 HEADERS  += warehouseinputoroutput.h \
16             callout.h
17 
18 FORMS    += warehouseinputoroutput.ui

 

头文件:WarehouseInputOrOutput.h

 1 #ifndef WAREHOUSEINPUTOROUTPUT_H
 2 #define WAREHOUSEINPUTOROUTPUT_H
 3 
 4 #include <QMainWindow>
 5 #include <QtCharts/QChartView>
 6 #include <QtCharts>
 7 
 8 namespace Ui {
 9 class WarehouseInputOrOutput;
10 }
11 
12 class WarehouseInputOrOutput : public QMainWindow
13 {
14     Q_OBJECT
15 
16 public:
17     explicit WarehouseInputOrOutput(QWidget *parent = 0);
18     ~WarehouseInputOrOutput();
19     void initData();
20     QChart* setBarData();
21     QChart* setSeatterData();
22     QChart* setChartNature(QChart* chart);
23 
27 private:
28     Ui::WarehouseInputOrOutput *ui;
29
33 };
34 
35 #endif // WAREHOUSEINPUTOROUTPUT_H

 

 

定义:WarehouseInputOrOutput.cpp

  1 #include "warehouseinputoroutput.h"
  2 #include "ui_warehouseinputoroutput.h"
  3 #include <QtCharts/QValueAxis>
  4 #include <QtCharts>
  5 #include <qlabel.h>
  7 #include <QtGui/QMouseEvent>
  8 #include <QtWidgets/QGridLayout>
  9 #include <QtWidgets/QHBoxLayout>
 10 
 11 WarehouseInputOrOutput::WarehouseInputOrOutput(QWidget *parent) :
 12     QMainWindow(parent),
 13     ui(new Ui::WarehouseInputOrOutput)
 14 {
 15 
 16     ui->setupUi(this);
 17 
 18     initData();
 19 }
 20 
 21 WarehouseInputOrOutput::~WarehouseInputOrOutput()
 22 {
 23     delete ui;
 24 }
 25 
 26 QChart* WarehouseInputOrOutput::setChartNature(QChart* chart)
 27 {
 28 
 29     chart->setTheme(QChart::ChartThemeLight); // 设置白色主题
 30     chart->setDropShadowEnabled(true); // 背景阴影
 31 //    chart->addSeries(series);    // 数据关联
 32     chart->setTitleBrush(QBrush(QColor(0, 0, 255)));     // 设置标题Brush
 33     chart->setTitleFont(QFont("宋体"));    // 设置标题字体
 34 
 35     chart->legend()->setVisible(true);
 36     chart->legend()->setAlignment(Qt::AlignBottom);  // 对齐方式:底部对齐
 37     chart->legend()->setBackgroundVisible(true);     //  设置背景 可见
 38     chart->legend()->setAutoFillBackground(true);    // 自动填充背景
 39     chart->legend()->setColor(QColor(222, 233, 251));
 40     chart->legend()->setLabelColor(QColor(0, 100, 255));
 41 //    chart->legend()->setMaximumHeight(50);   // 设置高度
 42 //    chart->legend()->hide();
 43 
 44     return chart;
 45 }
 46 
 47 QChart* WarehouseInputOrOutput::setBarData()
 48 {
 49     QChart* barChart = new QChart();
 50     QBarSet* set0 = new QBarSet("入库数");
 51     QBarSet* set1 = new QBarSet("出库数");
 52     QBarSet* set2 = new QBarSet("存量数");
 53     int maxXCount = 10;
 54     int maxYCount = 1000;
 55 
 56     // testData
 57     QMap<int, int> barData;
 58     for(int i = 0; i < 10; ++i)
 59     {
 62         barData[i] = i * 100 + 10;
 63     }
 64     for(int i = 0; i < 10;++i)
 65     {
 66         set0->append(barData[i]);
 67         set1->append(barData[9 - i]);
 68     }
 69     for(int i = 0; i < maxXCount; ++ i)
 70     {
 71         set2->append(i * 100);
 72     }
 73     set0->setLabelColor(Qt::black);
 74     set1->setLabelColor(Qt::red);
 75     set2->setLabelColor(Qt::cyan);
 76 
 77     QBarSeries* series = new QBarSeries();
 78     series->append(set0);
 79     series->append(set1);
 80     series->append(set2);
 81     series->setVisible(true);
 82     series->setLabelsVisible(true);
 83     // 柱状图数字显示在最上方
 84     series->setLabelsPosition(QAbstractBarSeries::LabelsInsideBase);
 85 
 91     setChartNature(barChart);
 92     barChart->addSeries(series);
 93     barChart->setTitle("出入库统计");
 94 
 95     QCategoryAxis*  axisX = new QCategoryAxis();
 98     axisX->setStartValue(-0.5);
 99     for(int i = 1; i < maxXCount + 1; ++ i)
100         axisX->append("物料" + QString::number(i), i - 0.5);
101 
102     QValueAxis *axisY = new QValueAxis;
103     axisY->setRange(0, maxYCount);   //改为setRange(0,1);则表示坐标为动态计算大小的
104     axisY->setTitleText("数量");
105     axisY->setLabelFormat("%d");
106 
107     barChart->setAxisX(axisX, series);
108     barChart->setAxisY(axisY, series);
109     series->attachAxis(axisX);
110     series->attachAxis(axisY);
120 
121     return barChart;
122 }
123 
124 QChart* WarehouseInputOrOutput::setSeatterData()
125 {
126     QChart* seatterChart = new QChart();
127     int maxXCount = 10;
128     int maxYCount = 10000;
129     setChartNature(seatterChart);
130     seatterChart->setTitle("存量数");
131 
132     QCategoryAxis* axisX = new QCategoryAxis();
133 //    axisX->setMin(0);
134     axisX->setMax(maxXCount);
135     axisX->setStartValue(0);
136     for(int i = 1; i < maxXCount + 1; ++ i)
137         axisX->append("timer" + QString::number(i), i);
138 
139     QValueAxis* axisY = new QValueAxis();
140     axisY->setRange(0, maxYCount);
141     axisY->setTitleText("存量");
142     axisY->setLabelFormat("%d");
143     QScatterSeries* seatter = new QScatterSeries();
144     for(int i = 1;i < maxXCount + 1; ++i)
145         seatter->append(i - 0.5, i * 1000);
146     seatter->setPen(QPen(Qt::NoPen));
147     seatter->setColor(QColor(250, 220, 0));
148     // 设置散点大小
149     seatter->setMarkerSize(25);
150     // 设置散点样式
151 //    seatter->setMarkerShape(QScatterSeries::MarkerShapeRectangle);
152     seatter->setName("存量");
153     // 显示设定点的坐标值
154     seatter->setPointLabelsFormat("(@xPoint, @yPoint)");
155     seatter->setPointLabelsVisible(true);
156 
159     seatterChart->addSeries(seatter);
157 seatter->attachAxis(axisX); 158 seatter->attachAxis(axisY);160 seatterChart->setAxisX(axisX, seatter); 161 seatterChart->setAxisY(axisY, seatter); 162 163 return seatterChart; 164 } 165 166 void WarehouseInputOrOutput::initData() 167 { 168 QChart* barChart = new QChart(); 169 barChart = setBarData(); 170 QChartView* showChart = new QChartView(barChart); 171 172 QChart* seatterChart = new QChart(); 173 seatterChart = setSeatterData(); 174 QChartView* showSeatter = new QChartView(seatterChart); 175 176 QGridLayout* gridLayout = new QGridLayout(); 177 gridLayout->addWidget(showSeatter, 1, 0); 178 gridLayout->addWidget(showChart, 2, 0); 179 resize(820, 600); 180 ui->centralWidget->setLayout(gridLayout); 181 182 return; 183 }

 

mian.cpp

 1 #include "warehouseinputoroutput.h"
 2 #include <QApplication>
 3 
 4 int main(int argc, char *argv[])
 5 {
 6     QApplication a(argc, argv);
 7     WarehouseInputOrOutput w;
 8     w.show();
 9 
10     return a.exec();
11 }

 


 

159     seatterChart->addSeries(seatter);

加载全部内容

相关教程
猜你喜欢
用户评论