Qt_QChart的使用记录(小白)
主要是记录柱状图的数值显示,散点图的点坐标显示(防止后续忘记,把文件都贴出来,方便复查)
资源库: 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 <QMainWindow>
#include <QtCharts/QChartView>
#include <QtCharts> 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 <QtCharts/QValueAxis>
#include <QtCharts>
#include <qlabel.h>
#include <QtGui/QMouseEvent>
#include <QtWidgets/QGridLayout>
#include <QtWidgets/QHBoxLayout> 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 <QApplication> 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);
Qt_QChart的使用记录(小白)的更多相关文章
- 我的第一次diy装机记录——小白的装机篇
接上一篇<我的第一次diy装机记录——小白的配置篇> 处理器 AMD Ryzen 5 2600X 六核主板 微星 B450M MORTAR (MS-7B89) ( AMD PCI 标准主机 ...
- 记录小白实习生的HashMap源码 put元素 的学习和一些疑问
首先看HashMap存储结构 transient Node<K,V>[] table; static class Node<K,V> implements Map.Entry& ...
- 我的第一次diy装机记录——小白的配置篇
工欲善其事,必先利其器 相对于IT人来说,电脑是个好东西,应该是第二个除了手机陪伴我们最长的东西.今年4月份来的杭州,留下了那款陪我征战4年的笔记本,没有电脑,下班后的夜晚索然无味,身心的需求也日渐强 ...
- 练手项目之image caption问题记录
小白一个,刚刚费了老大的劲完成一个练手项目--image caption,虽然跑通了,但是评估结果却惨不忍睹.于是贴上大神的作品,留待日后慢慢消化.顺便记录下自己踩坑的一些问题. 先膜拜下大神的作品. ...
- Anaconda折腾记(1)
Anaconda折腾记 谨此记录小白的我在Anaconda里面的摸爬滚打 更换更新源 可以不使用命令,直接进入C盘,进入user文件夹,进入当前的用户文件夹下,记得显示隐藏文件. 找到.condarc ...
- Ruby小白入门笔记之<个人记录档>
书写缘由 快两年的JAVA开发,因为来到一家新公司,产品需要用Ruby开发,故此才有了这从头开始,一入编程深似海啊...... 因为入门时是JAVA,所以理念跟规范早已形成,故此感觉突然采用Ruby编 ...
- openresty 的小白记录
openresty提供了一个快速访问数据库,快速响应的功能.基于lua + redis,我们可以做到快速响应,达到10k级连接的处理能力. openresty 的小白记录 好的学习资料,从lua到op ...
- elk5.4小白踩坑记录
搭建请参考:http://www.ywnds.com/?p=9776 搭建步骤:http://www.ywnds.com/?p=9776 初步解方案:先不用管这个提示,当你创建完索引后,接受数据后.一 ...
- 一个小白用 PhotoView 引起的学习记录
缘由(可跳过) 作为一个开发小白,有着各种各样想实现的功能, 最近想实现一个图片查看的功能(有放大,缩小等功能),发现了 PhotoView 这个开源框架, 用了它,腰不疼,腿不酸了 ... 添加依赖 ...
随机推荐
- 《走出软件作坊》//TODO
目录 简介 结束语 简介 作者吕建伟(@阿朱),研发管理专家,原京东技术学院院长,中国互联网技术联盟发起人,历任首席架构师.技术总监以及CTO等职位.目前已接受用友集团董事长王文京邀请,加入用友组建研 ...
- vue中v-on支持的事件总结
资源事件 事件名称 何时触发 error 资源加载失败时. abort 正在加载资源已经被中止时. load 资源及其相关资源已完成加载. beforeunload window,document 及 ...
- IPC之——信号量集(多个信号量)
如果两个进程不仅需要同步,还要保证先后执行顺序,就要用两个信号量(互斥锁)来解决 //栅栏模型:实现以下框架中的四个子进程 所有进程做完任务后 在一起执行下一次 #include <stdio ...
- mysql中in和exist的区别
mysql中in和exists的区别 -- in写法select * from A where A.id in (select bid from B ) and A.name in (select ...
- 学习C++之前要先学习C语言吗?
C++ 读作“C加加”,是“C Plus Plus”的简称.顾名思义,C++ 是在C语言的基础上增加新特性,玩出了新花样,所以叫“C Plus Plus”,就像 iPhone 7S 和 iPhone ...
- SCI|EI|ISTP|万方|istic|NSTL|CASTD|CNKI|nlc|ethesys|CALIS|CETD|proquest|NDLTD|中国科学院学位论文检索系统|学位论文
BD AC D 三大检索指的是:SCI(科学引文索引 ).EI(工程索引 ).ISTP(科技会议录索引 ) 即Science Citation Index.Engineering Index.Conf ...
- deeplearning.ai 人工智能行业大师访谈
Geoffrey Hinton 1. 怀揣着对大脑如何存储记忆的好奇,Hinton本科最开始学习生物学和物理学,然后放弃,转而学习哲学:然后觉得哲学也不靠谱,转而学习心理学:然后觉得心理学在解释大脑运 ...
- SHELL小练习
1.SHELL编程服务器IP修改脚本 脚本实现动态IP修改: 脚本实现静态IP修改: 实现IP地址输入判断正确性: IP地址修改成功判断&回滚: 2.SHELL编程Tomcat多实例管理脚本( ...
- keep pace with |sixes and sevens.|Three dozen of |setting out|in despite of|appetite for|brought up|.turn to|leave behind|As can be seen|every
Heavy but not excessive: network capacity seems to have done little more than keep pace with economi ...
- A - Divide it! CodeForces - 1176A
题目: You are given an integer nn. You can perform any of the following operations with this number an ...