DemuMesDataV/components/pointChart/index.vue

176 lines
3.5 KiB
Vue
Raw Normal View History

2018-12-22 20:55:24 +08:00
<template>
<div class="point-chart">
<loading v-if="!data" />
<div class="canvas-container">
<canvas :ref="ref" />
</div>
2018-12-25 17:29:42 +08:00
2019-01-16 16:56:11 +08:00
<label-line :label="labelLine" :colors="drawColors" />
2018-12-25 17:29:42 +08:00
<for-slot><slot></slot></for-slot>
2018-12-22 20:55:24 +08:00
</div>
</template>
<script>
import canvasMixin from '../../mixins/canvasMixin.js'
import colorsMixin from '../../mixins/colorsMixin.js'
2018-12-23 19:06:42 +08:00
import axisMixin from '../../mixins/axisMixin.js'
2018-12-22 20:55:24 +08:00
export default {
name: 'PointChart',
2018-12-23 19:06:42 +08:00
mixins: [canvasMixin, colorsMixin, axisMixin],
2019-01-16 16:56:11 +08:00
props: ['data', 'labelLine', 'colors'],
2018-12-22 20:55:24 +08:00
data () {
return {
ref: `point-chart-${(new Date()).getTime()}`,
// axis base config
boundaryGap: true,
horizon: false,
2018-12-25 17:29:42 +08:00
mulValueAdd: false,
defaultPointRadius: 2,
valuePointPos: []
2018-12-22 20:55:24 +08:00
}
},
2019-01-16 16:56:11 +08:00
watch: {
data () {
const { checkData, draw } = this
checkData() && draw()
}
},
2018-12-22 20:55:24 +08:00
methods: {
async init () {
2019-01-16 16:56:11 +08:00
const { initCanvas, initColors } = this
2018-12-22 20:55:24 +08:00
await initCanvas()
initColors()
2019-01-16 16:56:11 +08:00
const { checkData, draw } = this
checkData() && draw()
},
checkData () {
const { data } = this
this.status = false
if (!data || !data.series) return false
this.status = true
return true
2018-12-25 17:29:42 +08:00
},
draw () {
const { clearCanvas } = this
clearCanvas()
2018-12-22 20:55:24 +08:00
2018-12-25 17:29:42 +08:00
const { initAxis, drawAxis, calcValuePointPos } = this
initAxis()
2018-12-22 20:55:24 +08:00
drawAxis()
2018-12-25 17:29:42 +08:00
calcValuePointPos()
const { drawPoints } = this
drawPoints()
},
calcValuePointPos () {
2019-01-16 16:56:11 +08:00
const { data: { series }, valueAxisMaxMin, getAxisPointsPos } = this
2018-12-25 17:29:42 +08:00
const { axisOriginPos, axisWH, labelAxisTagPos } = this
2019-01-16 16:56:11 +08:00
this.valuePointPos = series.map(({ value }, i) =>
2018-12-25 17:29:42 +08:00
getAxisPointsPos(
valueAxisMaxMin,
2019-01-16 16:56:11 +08:00
value,
2018-12-25 17:29:42 +08:00
axisOriginPos,
axisWH,
labelAxisTagPos,
false
))
},
drawPoints () {
2019-01-16 16:56:11 +08:00
const { data: { series }, drawSeriesPoint, ctx } = this
2018-12-25 17:29:42 +08:00
ctx.setLineDash([10, 0])
2019-01-16 16:56:11 +08:00
series.forEach((seriesItem, i) => drawSeriesPoint(seriesItem, i))
2018-12-25 17:29:42 +08:00
},
drawSeriesPoint ({ color: cr, edgeColor, fillColor, radius, opacity }, i) {
const { drawColors, defaultPointRadius, valuePointPos, drawPoint } = this
const { color: { hexToRgb }, data: { radius: outerRadius } } = this
const drawColorsNum = drawColors.length
const baseColor = drawColors[i % drawColorsNum]
const trueEdgeColor = edgeColor || cr || baseColor
let trueFillColor = fillColor || cr || baseColor
opacity && (trueFillColor = hexToRgb(trueFillColor, opacity))
const trueRadius = radius || outerRadius || defaultPointRadius
valuePointPos[i].forEach(cp => {
if (!cp && cp !== 0) return
const isSeries = cp[0] instanceof Array
isSeries && cp.forEach(p => (p || p === 0) && drawPoint(p, trueEdgeColor, trueFillColor, trueRadius))
!isSeries && drawPoint(cp, trueEdgeColor, trueFillColor, trueRadius)
})
},
drawPoint (pos, edgeColor, fillColor, radius) {
const { ctx } = this
ctx.beginPath()
ctx.arc(...pos, radius, 0, Math.PI * 2)
ctx.closePath()
ctx.strokeStyle = edgeColor
ctx.fillStyle = fillColor
ctx.fill()
ctx.stroke()
2018-12-22 20:55:24 +08:00
}
},
mounted () {
const { init } = this
init()
}
}
</script>
<style lang="less">
.point-chart {
position: relative;
display: flex;
2018-12-25 17:29:42 +08:00
flex-direction: column;
2018-12-22 20:55:24 +08:00
.canvas-container {
flex: 1;
canvas {
width: 100%;
height: 100%;
}
}
}
</style>