372 lines
7.3 KiB
Vue
Raw Normal View History

2022-06-08 15:48:09 +08:00
<template>
2023-06-19 19:28:30 +08:00
<canvas v-if="canvasId" :id="canvasId" :canvasId="canvasId"
:style="{'width':cWidth*pixelRatio+'px','height':cHeight*pixelRatio+'px', 'transform': 'scale('+(1/pixelRatio)+')','margin-left':-cWidth*(pixelRatio-1)/2+'px','margin-top':-cHeight*(pixelRatio-1)/2+'px'}"
@touchstart="touchStart" @touchmove="touchMove" @touchend="touchEnd" @error="error">
2022-06-08 15:48:09 +08:00
</canvas>
</template>
<script>
import uCharts from './u-charts.js';
var canvases = {};
export default {
props: {
chartType: {
required: true,
type: String,
default: 'column'
},
opts: {
required: true,
type: Object,
default () {
return null;
},
},
canvasId: {
type: String,
default: 'u-canvas',
},
cWidth: {
default: 375,
},
cHeight: {
default: 250,
},
pixelRatio: {
type: Number,
default: 1,
},
legends: {
type: Boolean,
default: true
}
},
data() {
return {
colors: [
"#5181F6",
"#00DBB6",
"#F67F51",
"#FF515A",
"#8B5CFF",
"#00CA69"
]
}
},
2023-06-19 19:28:30 +08:00
mounted() {
// this.$nextTick(() => {
// this.init();
// })
},
2022-06-08 15:48:09 +08:00
watch: {
opts: {
handler(newVal, oldVal) {
2023-06-19 19:28:30 +08:00
// // this.init();
// // console.log('canvases')
// // console.log(canvases)
// if (canvases = {}) {
// // this.$nextTick(() => {
// // this.init();
// // })
// setTimeout(()=>{
this.init();
this.changeData(this.canvasId, newVal)
// },600)
// } else {
// }
2022-06-08 15:48:09 +08:00
},
deep: true,
immediate: true,
}
},
2023-06-19 19:28:30 +08:00
2022-06-08 15:48:09 +08:00
methods: {
init() {
console.log('图表初始化')
switch (this.chartType) {
case 'column':
2023-06-19 19:28:30 +08:00
this.$nextTick(() => {
console.log('图表初始化1')
this.initColumnChart();
})
2022-06-08 15:48:09 +08:00
break;
case 'line':
2023-06-19 19:28:30 +08:00
this.$nextTick(() => {
console.log('图表初始化2')
this.initLineChart();
})
2022-06-08 15:48:09 +08:00
break;
case 'arcbar': //半圆
2023-06-19 19:28:30 +08:00
console.log('图表初始化3')
setTimeout(()=>{
this.initArcbarChart();
},1200)
2022-06-08 15:48:09 +08:00
break;
case 'ring': //圆环
2023-06-19 19:28:30 +08:00
this.$nextTick(() => {
console.log('图表初始化4')
this.initRingChart();
})
2022-06-08 15:48:09 +08:00
break;
default:
break;
}
},
initColumnChart() {
var xData = this.opts.categories
for (var i = 0; i < xData.length; i++) {
2023-06-19 19:28:30 +08:00
if (xData[i].length > 5) {
xData[i] = xData[i].substring(0, 5) + '...'
2022-06-08 15:48:09 +08:00
}
2023-06-19 19:28:30 +08:00
2022-06-08 15:48:09 +08:00
}
canvases[this.canvasId] = new uCharts({
$this: this,
canvasId: this.canvasId,
type: 'column',
legend: {
show: false
},
colors: this.colors,
fontSize: 11,
background: '#FFFFFF',
pixelRatio: this.pixelRatio,
animation: true,
categories: xData,
series: this.opts.series,
enableScroll: true,
xAxis: {
disableGrid: true,
itemCount: 6,
2023-06-19 19:28:30 +08:00
labelCount: this.opts.categories.length,
2022-06-08 15:48:09 +08:00
rotateLabel: true
},
yAxis: {
format: (val) => {
if ((val + "").indexOf(".") != -1) {
return val.toFixed(0); //保留小数后1位
} else {
return val;
}
},
// data:{
// }
// axisLine:false
//disabled:true
},
dataLabel: true,
width: this.cWidth * this.pixelRatio,
height: this.cHeight * this.pixelRatio,
extra: {
column: {
type: 'group',
width: 14
}
},
2023-06-19 19:28:30 +08:00
scales: {
2022-06-08 15:48:09 +08:00
xAxis: [{
2023-06-19 19:28:30 +08:00
stacked: true,
ticks: {
fontColor: 'red' // X轴样式
}
}],
2022-06-08 15:48:09 +08:00
}
});
},
initLineChart() {
canvases[this.canvasId] = new uCharts({
$this: this,
canvasId: this.canvasId,
type: 'line',
fontSize: 11,
legend: {
show: this.legends
},
dataLabel: false,
dataPointShape: true,
colors: this.colors,
background: '#FFFFFF',
pixelRatio: this.pixelRatio,
categories: this.opts.categories,
series: this.opts.series,
animation: true,
enableScroll: true,
xAxis: {
type: 'grid',
axisLine: false,
gridColor: '#fff',
gridType: 'dash',
dashLength: 8,
itemCount: 4,
scrollShow: true
},
yAxis: {
axisLine: false,
// gridType: 'dash',
gridColor: 'rgba(42, 43, 91, 0.1)',
// dashLength: 8,
// splitNumber: 5,
2023-06-19 19:28:30 +08:00
min: 0,
2022-06-08 15:48:09 +08:00
// max: 180,
// format: (val) => {
// return val.toFixed(0) + '元'
// }
},
width: this.cWidth * this.pixelRatio,
height: this.cHeight * this.pixelRatio,
extra: {
line: {
type: 'curve'
}
}
});
},
//半圆
initArcbarChart() {
let winWidth = 0;
uni.getSystemInfo({
success: function(res) {
winWidth = res.windowWidth / 2 - 10;
}
});
canvases[this.canvasId] = new uCharts({
$this: this,
canvasId: this.canvasId,
type: 'arcbar',
fontSize: 11,
legend: {
show: false
},
background: '#FFFFFF',
pixelRatio: 1,
series: this.opts.series,
animation: true,
width: this.cWidth,
height: this.cHeight,
dataLabel: true,
subtitle: {
name: this.opts.series[0].name,
color: '#000',
fontSize: 25,
offsetY: -12
},
extra: {
arcbar: {
type: 'default',
width: 12,
startAngle: 1,
endAngle: 0,
gap: 5,
radius: 90,
center: {
y: 100,
x: winWidth
}
}
}
});
},
//圆环
initRingChart() {
2023-06-19 19:28:30 +08:00
console.log('this.canvasId', this.canvasId)
var dataLabel = false,
legend = false;
2022-06-08 15:48:09 +08:00
var radius = 0;
2023-06-19 19:28:30 +08:00
if (this.canvasId == 'integritySafety') {
dataLabel = legend = true
2022-06-08 15:48:09 +08:00
}
2023-06-19 19:28:30 +08:00
if (this.canvasId == 'qualityPieChart') {
2022-06-08 15:48:09 +08:00
radius = 30
}
canvases[this.canvasId] = new uCharts({
2023-06-19 19:28:30 +08:00
$this: this,
canvasId: this.canvasId,
type: 'ring',
fontSize: 11,
title: this.opts.title,
legend: {
show: legend
},
extra: {
pie: {
ringWidth: 12,
offsetAngle: -90,
activeRadius: radius,
},
tooltip: {
showBox: false,
}
},
width: this.cWidth,
height: this.cHeight,
background: '#FFFFFF',
series: this.opts.series,
dataLabel: dataLabel,
});
2022-06-08 15:48:09 +08:00
},
// 这里仅作为示例传入两个参数cid为canvas-id,newdata为更新的数据需要更多参数请自行修改
changeData(cid, newdata) {
2023-06-19 19:28:30 +08:00
console.log('updateData', newdata)
2022-06-08 15:48:09 +08:00
console.log(canvases[cid])
var xData = newdata.categories
for (var i = 0; i < xData.length; i++) {
2023-06-19 19:28:30 +08:00
if (xData[i].length > 5) {
xData[i] = xData[i].substring(0, 5) + '...'
2022-06-08 15:48:09 +08:00
}
2023-06-19 19:28:30 +08:00
2022-06-08 15:48:09 +08:00
}
canvases[cid].updateData({
series: newdata.series,
categories: xData,
title: newdata.title
});
},
touchStart(e) {
canvases[this.canvasId].showToolTip(e, {
format: function(item, category) {
return category + ' ' + item.name + ':' + item.data
}
});
canvases[this.canvasId].scrollStart(e);
},
touchMove(e) {
canvases[this.canvasId].scroll(e);
},
touchEnd(e) {
canvases[this.canvasId].scrollEnd(e);
},
error(e) {
console.log(e)
}
},
};
</script>
<style scoped>
.charts {
width: 100%;
height: 100%;
flex: 1;
background-color: #FFFFFF;
}
</style>