主要是记录柱状图的数值显示,散点图的点坐标显示(防止后续忘记,把文件都贴出来,方便复查)
资源库: WarehouseInputOrOutput.pro
QT += core gui
QT += charts
greaterThan(QT_MAJOR_VERSION, ): QT += widgets
TARGET = WarehouseInputOrOutput
TEMPLATE = app
DEFINES += QT_DEPRECATED_WARNINGS
SOURCES += main.cpp\
warehouseinputoroutput.cpp \
callout.cpp
HEADERS += warehouseinputoroutput.h \
callout.h
FORMS += warehouseinputoroutput.ui
头文件:WarehouseInputOrOutput.h
#ifndef WAREHOUSEINPUTOROUTPUT_H
#define WAREHOUSEINPUTOROUTPUT_H
#include
#include
#include
namespace Ui {
class WarehouseInputOrOutput;
}
class WarehouseInputOrOutput : public QMainWindow
{
Q_OBJECT
public:
explicit WarehouseInputOrOutput(QWidget *parent = );
~WarehouseInputOrOutput();
void initData();
QChart* setBarData();
QChart* setSeatterData();
QChart* setChartNature(QChart* chart);
private:
Ui::WarehouseInputOrOutput *ui;
};
#endif // WAREHOUSEINPUTOROUTPUT_H
定义:WarehouseInputOrOutput.cpp
#include "warehouseinputoroutput.h"
#include "ui_warehouseinputoroutput.h"
#include
#include
#include
#include
#include
#include
WarehouseInputOrOutput::WarehouseInputOrOutput(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::WarehouseInputOrOutput)
{
ui->setupUi(this);
initData();
}
WarehouseInputOrOutput::~WarehouseInputOrOutput()
{
delete ui;
}
QChart* WarehouseInputOrOutput::setChartNature(QChart* chart)
{
chart->setTheme(QChart::ChartThemeLight); // 设置白色主题
chart->setDropShadowEnabled(true); // 背景阴影
// chart->addSeries(series); // 数据关联
chart->setTitleBrush(QBrush(QColor(, , ))); // 设置标题Brush
chart->setTitleFont(QFont("宋体")); // 设置标题字体
chart->legend()->setVisible(true);
chart->legend()->setAlignment(Qt::AlignBottom); // 对齐方式:底部对齐
chart->legend()->setBackgroundVisible(true); // 设置背景 可见
chart->legend()->setAutoFillBackground(true); // 自动填充背景
chart->legend()->setColor(QColor(, , ));
chart->legend()->setLabelColor(QColor(, , ));
// chart->legend()->setMaximumHeight(50); // 设置高度
// chart->legend()->hide();
return chart;
}
QChart* WarehouseInputOrOutput::setBarData()
{
QChart* barChart = new QChart();
QBarSet* set0 = new QBarSet("入库数");
QBarSet* set1 = new QBarSet("出库数");
QBarSet* set2 = new QBarSet("存量数");
int maxXCount = ;
int maxYCount = ;
// testData
QMap<int, int> barData;
for(int i = ; i < ; ++i)
{
barData\[i\] = i \* + ;
}
for(int i = ; i < ;++i)
{
set0->append(barData\[i\]);
set1->append(barData\[ - i\]);
}
for(int i = ; i < maxXCount; ++ i)
{
set2->append(i \* );
}
set0->setLabelColor(Qt::black);
set1->setLabelColor(Qt::red);
set2->setLabelColor(Qt::cyan);
QBarSeries\* series = new QBarSeries();
series->append(set0);
series->append(set1);
series->append(set2);
series->setVisible(true);
series->setLabelsVisible(true);
// 柱状图数字显示在最上方
series->setLabelsPosition(QAbstractBarSeries::LabelsInsideBase);
setChartNature(barChart);
barChart->addSeries(series);
barChart->setTitle("出入库统计");
QCategoryAxis\* axisX = new QCategoryAxis();
axisX->setStartValue(-0.5);
for(int i = ; i < maxXCount + ; ++ i)
axisX->append("物料" + QString::number(i), i - 0.5);
QValueAxis \*axisY = new QValueAxis;
axisY->setRange(, maxYCount); //改为setRange(0,1);则表示坐标为动态计算大小的
axisY->setTitleText("数量");
axisY->setLabelFormat("%d");
barChart->setAxisX(axisX, series);
barChart->setAxisY(axisY, series);
series->attachAxis(axisX);
series->attachAxis(axisY);
return barChart;
}
QChart* WarehouseInputOrOutput::setSeatterData()
{
QChart* seatterChart = new QChart();
int maxXCount = ;
int maxYCount = ;
setChartNature(seatterChart);
seatterChart->setTitle("存量数");
QCategoryAxis\* axisX = new QCategoryAxis();
// axisX->setMin(0);
axisX->setMax(maxXCount);
axisX->setStartValue();
for(int i = ; i < maxXCount + ; ++ i)
axisX->append("timer" + QString::number(i), i);
QValueAxis\* axisY = new QValueAxis();
axisY->setRange(, maxYCount);
axisY->setTitleText("存量");
axisY->setLabelFormat("%d");
QScatterSeries\* seatter = new QScatterSeries();
for(int i = ;i < maxXCount + ; ++i)
seatter->append(i - 0.5, i \* );
seatter->setPen(QPen(Qt::NoPen));
seatter->setColor(QColor(, , ));
// 设置散点大小
seatter->setMarkerSize();
// 设置散点样式
// seatter->setMarkerShape(QScatterSeries::MarkerShapeRectangle);
seatter->setName("存量");
// 显示设定点的坐标值
seatter->setPointLabelsFormat("(@xPoint, @yPoint)");
seatter->setPointLabelsVisible(true);
seatterChart->addSeries(seatter);
seatter->attachAxis(axisX); seatter->attachAxis(axisY); seatterChart->setAxisX(axisX, seatter); seatterChart->setAxisY(axisY, seatter); return seatterChart; } void WarehouseInputOrOutput::initData() { QChart* barChart = new QChart(); barChart = setBarData(); QChartView* showChart = new QChartView(barChart); QChart* seatterChart = new QChart(); seatterChart = setSeatterData(); QChartView* showSeatter = new QChartView(seatterChart); QGridLayout* gridLayout = new QGridLayout(); gridLayout->addWidget(showSeatter, , ); gridLayout->addWidget(showChart, , ); resize(, ); ui->centralWidget->setLayout(gridLayout); return; }
mian.cpp
#include "warehouseinputoroutput.h"
#include
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
WarehouseInputOrOutput w;
w.show();
return a.exec();
}
补充个更新的函数,以便后续自查
// 更新坐标轴和数据
void WarehouseInputOrOutput::updateShow()
{
qDebug() << "enter the updateShow";
// 需计算的前置参数
int maxXCount = ; // x轴分割为几块
int maxYCount = ; // 出入库的浮动上限(传参时加10%方便显示)
int maxSeaterY = ; // 实际存量的上限(传参时加20%方便显示坐标)
QBarSet\* set0 = new QBarSet("入库数");
QBarSet\* set1 = new QBarSet("出库数");
set0->setLabelColor(Qt::black);
set1->setLabelColor(Qt::red);
int i = ;
QMap<QString, int>::const\_iterator inIt;
QMap<QString, int>::const\_iterator outIt;
for(inIt = m\_InputSum.begin(), outIt = m\_OutputSum.begin();i < maxXCount && inIt != m\_InputSum.end() && outIt != m\_OutputSum.end();++inIt, ++ outIt)
{
set0->append(inIt.value());
set1->append(outIt.value());
++i;
}
// 出库数、入库数、存量数、日期
for(int i = ; i < maxXCount; ++i)
{
m\_OutputSum\["物料" + QString::number(i)\] = i \* ;
m\_InputSum\["物料" + QString::number(i)\] = i \* ;
m\_StockSum\["物料" + QString::number(i)\] = i \* ;
}
m\_date = QDateTime::currentDateTime().toString("yyyy/MM/dd");
m\_barChart->removeAllSeries();
QBarSeries\* newSeries = new QBarSeries();
newSeries->append(set0);
newSeries->append(set1);
newSeries->setVisible(true);
newSeries->setLabelsVisible(true);
// 柱状图数字显示在最上方
newSeries->setLabelsPosition(QAbstractBarSeries::LabelsInsideBase);
m\_barChart->addSeries(newSeries);
// 移除原有数据,后续更新数据(update动作必须先移除原数据)
m\_barChart->removeAxis(m\_barChart->axisX());
m\_barChart->removeAxis(m\_barChart->axisY());
QCategoryAxis\* axisX = new QCategoryAxis();
axisX->setStartValue(-0.5);
i = ; // 辅助位置
for(inIt = m\_InputSum.begin(); inIt != m\_InputSum.end() && i < maxXCount;++inIt)
axisX->append(inIt.key(), i++ - 0.5);
QValueAxis \*axisY = new QValueAxis;
axisY->setRange(, maxYCount \* 1.1);
axisY->setTitleText("数量");
axisY->setLabelFormat("%d");
m\_barChart->setAxisX(axisX, newSeries);
m\_barChart->setAxisY(axisY, newSeries);
// 数据匹配坐标轴
newSeries->attachAxis(axisX);
newSeries->attachAxis(axisY);
// 更新散点图数据
QScatterSeries\* newSeatter = new QScatterSeries();
for(int i = ;i < maxXCount + ; ++i)
newSeatter->append(i - 0.5, i \* );
// 设置散点大小
newSeatter->setMarkerSize();
newSeatter->setName("存量");
// 显示设定点的值
newSeatter->setPointLabelsFormat("@yPoint");
newSeatter->setPointLabelsVisible(true);
m\_seatterChart->removeAllSeries();
m\_seatterChart->addSeries(newSeatter);
QCategoryAxis\* seatterAxisX = new QCategoryAxis();\\
seatterAxisX->setMax(1.1\*maxXCount);
seatterAxisX->setStartValue();
QMap<QString, int>::const\_iterator stockIt;
int i = ;
for(stockIt = m\_StockSum.begin();stockIt != m\_StockSum.end();++stockIt)
seatterAxisX->append(stockIt.key(), i++);
QValueAxis\* seatterAxisY = new QValueAxis();
seatterAxisY->setRange(, 1.2\*maxYCount);
seatterAxisY->setTitleText("存量");
seatterAxisY->setLabelFormat("%d");
// 移除原有数据,添加新数据(update动作必须先移除原数据)
m\_seatterChart->removeAxis(m\_seatterChart->axisX());
m\_seatterChart->removeAxis(m\_seatterChart->axisY());
m\_seatterChart->addAxis(seatterAxisX, newSeatter);
m\_seatterChart->addAxis(seatterAxisY, newSeatter);
// 数据匹配坐标轴
newSeatter->attachAxis(m\_seatterChart->axisX());
newSeatter->attachAxis(m\_seatterChart->axisY());
update();
}
seatterChart->addSeries(seatter);
手机扫一扫
移动阅读更方便
你可能感兴趣的文章