序言
其实QML有的,QtWidget基本也有,在前面加个Q,比如说QBarSeries,本来觉得这么简单的基础写法不想写的,但是我一搜"QML 柱形图",寥寥无几,所以我只能写一份出来撑撑低端场面了
环境
Qt5.12
一、效果图
这是个简单的写法,更多的细节要自行去看文档去写了
二、源码
import QtQuick 2.0
import QtCharts 2.3
ChartView {
title: qsTr( "柱形图 " )
titleColor: "black" //定制标题字
titleFont.bold: true
anchors.fill: parent
legend.alignment: Qt.AlignBottom //上图:目前产量 目标产量的位置,这里指下方
antialiasing: true //反锯齿
//不同的坐标轴需要用不一样的Axis
BarCategoryAxis {
id: axisx
color: "#555555"
labelsFont.pointSize: 12
labelsFont.bold: true
categories: [
"03月", "04月", "05月", "06月", "07月"
]
}
ValueAxis {
id: axisy
max: 10
min: 0
tickCount: 7 //上图只是演示这个的功能,并非一般这么写,y轴方向7个指示点
labelsColor: "#111111"
labelsFont.pointSize: 12
labelsFont.bold: true
labelFormat: "%d"
}
BarSeries {
id: barSeries
axisX: axisx
axisY: axisy
labelsPosition: AbstractBarSeries.LabelsInsideEnd //指条形图里面的数字值的位置
labelsVisible: true //指条形图里面的数字值是否显示,默认不显示
BarSet {
label: "目前产量"
color: "green"
values: [ 4, 4, 7, 7, 5 ]
}
BarSet {
label: "目标产量"
color: "red"
values: [ 5, 5, 7, 8, 10]
}
}
}
源码挺简单的,我就不多介绍了,会QML的自然也会这个,不会的先学会QML吧