YZProjectCloud/yanzhu-ui-app/miniprogram/components/safety-bar-chart/index.js

267 lines
9.3 KiB
JavaScript
Raw Normal View History

2024-10-13 11:24:45 +08:00
// components/safety-pie-chart/index.js
import * as echarts from '../../ec-canvas/echarts'
Component({
/**
* 组件的属性列表
*/
properties: {
2024-12-29 21:35:34 +08:00
chartId: {
2024-10-13 11:24:45 +08:00
type: String
},
2024-12-29 21:35:34 +08:00
data: {
2024-10-13 11:24:45 +08:00
type: Array
},
2024-12-29 21:35:34 +08:00
height: {
2024-10-13 11:24:45 +08:00
type: Number
}
},
observers: {
data: function (val) {
2024-12-29 21:35:34 +08:00
this.initChart()
2024-10-13 11:24:45 +08:00
},
},
/**
* 组件的初始数据
*/
data: {
ec: {
lazyLoad: true
},
2024-12-29 21:35:34 +08:00
chart: undefined,
2024-10-13 11:24:45 +08:00
},
lifetimes: {
2024-12-29 21:35:34 +08:00
created: function () {
//在组件实例刚刚被创建时执行,注意此时不能调用 setData
2024-10-13 11:24:45 +08:00
},
2024-12-29 21:35:34 +08:00
2024-10-13 11:24:45 +08:00
attached: function () {
//在组件实例进入页面节点树时执行
2024-12-29 21:35:34 +08:00
2024-10-13 11:24:45 +08:00
},
2024-12-29 21:35:34 +08:00
2024-10-13 11:24:45 +08:00
ready: function () {
// 在组件在视图层布局完成后执行
this.initChart();
},
2024-12-29 21:35:34 +08:00
2024-10-13 11:24:45 +08:00
detached: function () {
// 在组件实例被从页面节点树移除时执行
},
2024-12-29 21:35:34 +08:00
2024-10-13 11:24:45 +08:00
},
/**
* 组件的方法列表
*/
methods: {
2024-12-29 21:35:34 +08:00
initChart() {
var id = '#' + this.data.chartId;
2024-10-13 11:24:45 +08:00
this.component = this.selectComponent(id);
2024-12-29 21:35:34 +08:00
if(this.component!=null){
this.component.init((canvas, width, height, dpr) => {
let chart = echarts.init(canvas, null, {
width: width,
height: height,
devicePixelRatio: dpr
})
chart.setOption(this.getData());
return chart;
2024-10-13 11:24:45 +08:00
})
2024-12-29 21:35:34 +08:00
}
2024-10-13 11:24:45 +08:00
},
getData() {
var data = this.data.data
2024-12-29 21:35:34 +08:00
2024-10-13 11:24:45 +08:00
var max = data[0].total
var nameData = [];
var totalData = []
var background = []
var yesMonitor = []
var notMonitor = []
var yesProp = []
var notProp = []
var unitData = []
2024-12-29 21:35:34 +08:00
for (let i = data.length - 1; i >= 0; i--) {
2024-10-13 11:24:45 +08:00
nameData.push(data[i].name);
totalData.push(data[i].total)
unitData.push(data[i].unit)
background.push(100);
yesMonitor.push(data[i].yesMonitor);
notMonitor.push(data[i].notMonitor);
2024-12-29 21:35:34 +08:00
yesProp.push((data[i].yesMonitor / max) * 100)
notProp.push((data[i].notMonitor / max) * 100)
2024-10-13 11:24:45 +08:00
}
var legend = ["已监控", "未监控"]
2024-12-29 21:35:34 +08:00
var option = {
2024-10-13 11:24:45 +08:00
grid: {
//图表的位置
2024-12-29 11:46:50 +08:00
top: "0%",
2024-10-13 11:24:45 +08:00
left: "3%",
right: "5%",
bottom: "-12%",
containLabel: true,
},
legend: {
top: "0",
//icon: "circle",
itemWidth: 10,
2024-12-29 21:35:34 +08:00
itemHeight: 10,
2024-10-13 11:24:45 +08:00
itemGap: 8,
textStyle: {
fontSize: 12,
2024-12-29 21:35:34 +08:00
color: '#c6d9fa'
2024-10-13 11:24:45 +08:00
},
data: legend,
},
xAxis: [{
2024-12-29 21:35:34 +08:00
show: false,
},
2024-10-13 11:24:45 +08:00
//由于下边X轴已经是百分比刻度了,所以需要在顶部加一个X轴,刻度是金额,也隐藏掉
{
show: false,
}
],
2024-12-29 21:35:34 +08:00
yAxis: [{
type: 'category',
axisLabel: {
show: false, //让Y轴数据不显示
},
itemStyle: {
2024-10-13 11:24:45 +08:00
2024-12-29 21:35:34 +08:00
},
axisTick: {
show: false, //隐藏Y轴刻度
},
axisLine: {
show: false, //隐藏Y轴线段
},
data: [],
}, {
show: false,
data: [],
axisLine: {
show: false
}
}],
2024-10-13 11:24:45 +08:00
series: [
//数据条--------------------我是分割线君------------------------------//
{
show: true,
type: 'bar',
xAxisIndex: 1, //代表使用第二个X轴刻度!!!!!!!!!!!!!!!!!!!!!!!!
barGap: '-100%',
barWidth: '6', //统计条宽度
itemStyle: {
normal: {
color: 'rgba(22,203,115,0.05)'
},
},
label: {
normal: {
show: true,
//label 的position位置可以是top bottom left,right,也可以是固定值
//在这里需要上下统一对齐,所以用固定值
position: [0, '-20'],
rich: { //富文本
prop: { //自定义颜色
color: '#c6d9fa',
2024-12-29 21:35:34 +08:00
fontSize: '14',
2024-10-13 11:24:45 +08:00
},
2024-12-29 21:35:34 +08:00
unit: {
2024-10-13 11:24:45 +08:00
color: '#6c829a',
2024-12-29 21:35:34 +08:00
fontSize: '12',
2024-10-13 11:24:45 +08:00
},
2024-12-29 21:35:34 +08:00
yes: {
2024-10-13 11:24:45 +08:00
color: '#55adf7',
2024-12-29 21:35:34 +08:00
fontSize: '14',
2024-10-13 11:24:45 +08:00
},
2024-12-29 21:35:34 +08:00
not: {
2024-10-13 11:24:45 +08:00
color: '#4677fa',
2024-12-29 21:35:34 +08:00
fontSize: '14',
2024-10-13 11:24:45 +08:00
},
2024-12-29 21:35:34 +08:00
index: {
2024-10-13 11:24:45 +08:00
color: '#fcbc02',
fontStyle: 'italic',
2024-12-29 21:35:34 +08:00
padding: [0, 0, 0, 5],
fontSize: '14',
2024-10-13 11:24:45 +08:00
},
name: {
width: 120,
color: '#c6d9fa',
2024-12-29 21:35:34 +08:00
padding: [0, 0, 0, 5],
fontSize: '14',
2024-10-13 11:24:45 +08:00
},
2024-12-29 21:35:34 +08:00
color: {
2024-10-13 11:24:45 +08:00
color: '#8ca2be',
2024-12-29 21:35:34 +08:00
fontSize: '14',
2024-10-13 11:24:45 +08:00
},
2024-12-29 21:35:34 +08:00
2024-10-13 11:24:45 +08:00
},
2024-12-29 21:35:34 +08:00
formatter: function (data) {
2024-10-13 11:24:45 +08:00
//富文本固定格式{colorName|这里填你想要写的内容}
//return '{arrow|}'
2024-12-29 21:35:34 +08:00
return '{index|No.' + (nameData.length - data.dataIndex) + '}{name|' + nameData[data.dataIndex] + '}{prop|' + totalData[data.dataIndex] + '}{unit| ' + unitData[data.dataIndex] + '}{prop|} {yes|' + yesMonitor[data.dataIndex] + '}{unit| ' + unitData[data.dataIndex] + '/}{not|' + notMonitor[data.dataIndex] + '}{unit| ' + unitData[data.dataIndex] + '}{prop|} ';
2024-10-13 11:24:45 +08:00
},
}
},
data: background
},
{
type: 'bar',
silent: true,
yAxisIndex: 1,
barWidth: 6,
itemStyle: {
normal: {
color: 'rgba(0,82,198,0.3)'
},
emphasis: {
color: 'rgba(0,82,198,0.3)'
}
},
data: background
2024-12-29 21:35:34 +08:00
},
2024-10-13 11:24:45 +08:00
{
2024-12-29 21:35:34 +08:00
type: 'bar',
//name:legend[0],
stack: '1',
legendHoverLink: false,
barWidth: 6,
itemStyle: {
normal: {
color: '#52adf4'
},
emphasis: {
color: '#52adf4'
}
2024-10-13 11:24:45 +08:00
},
2024-12-29 21:35:34 +08:00
data: yesProp
}, {
type: 'bar',
//name:legend[1],
stack: '1',
legendHoverLink: false,
barWidth: 6,
itemStyle: {
normal: {
color: '#4677ff'
},
emphasis: {
color: '#4677ff'
}
2024-10-13 11:24:45 +08:00
},
2024-12-29 21:35:34 +08:00
data: notProp
}
]
2024-10-13 11:24:45 +08:00
};
return option;
}
}
2024-12-29 21:35:34 +08:00
})