DemuMesDataV/src/components/pointChart/index.vue

156 lines
3.2 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
<label-line :label="data.labelLine" :colors="drawColors" />
<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],
2018-12-22 20:55:24 +08:00
props: ['data', 'colors'],
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
}
},
methods: {
async init () {
2018-12-25 17:29:42 +08:00
const { initCanvas, initColors, data, draw } = this
2018-12-22 20:55:24 +08:00
await initCanvas()
initColors()
2018-12-25 17:29:42 +08:00
data && draw()
},
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 () {
const { data: { data }, valueAxisMaxMin, getAxisPointsPos } = this
const { axisOriginPos, axisWH, labelAxisTagPos } = this
this.valuePointPos = data.map(({ data: td }, i) =>
getAxisPointsPos(
valueAxisMaxMin,
td,
axisOriginPos,
axisWH,
labelAxisTagPos,
false
))
},
drawPoints () {
const { data: { data }, drawSeriesPoint, ctx } = this
ctx.setLineDash([10, 0])
data.forEach((series, i) => drawSeriesPoint(series, i))
},
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>