DemuMesDataV/src/components/columnChart/index.vue

547 lines
15 KiB
Vue
Raw Normal View History

2018-12-20 18:25:19 +08:00
<template>
<div class="column-chart">
<loading v-if="!data" />
<div class="canvas-container">
<canvas :ref="ref" />
</div>
<label-line :label="data.labelLine" :colors="drawColors" />
</div>
</template>
<script>
import colorsMixin from '../../mixins/colorsMixin.js'
import canvasMixin from '../../mixins/canvasMixin.js'
import axisMixin from '../../mixins/axisMixin.js'
export default {
name: 'ColumnChart',
mixins: [colorsMixin, canvasMixin, axisMixin],
data () {
return {
ref: `radar-chart-${(new Date()).getTime()}`,
2018-12-23 19:06:42 +08:00
// axis base config
boundaryGap: true,
mulValueAdd: true,
horizon: false,
columnData: [],
columnItemSeriesNum: 0,
columnItemAllWidth: 0,
columnItemWidth: 0,
columnItemOffset: [],
valuePointPos: []
2018-12-20 18:25:19 +08:00
}
},
props: ['data', 'colors'],
methods: {
async init () {
2018-12-23 19:06:42 +08:00
const { initCanvas, initColors } = this
2018-12-20 18:25:19 +08:00
await initCanvas()
2018-12-23 19:06:42 +08:00
initColors()
const { data, draw } = this
2018-12-20 18:25:19 +08:00
data && draw()
},
draw () {
2018-12-23 19:06:42 +08:00
const { clearCanvas } = this
2018-12-20 18:25:19 +08:00
clearCanvas()
2018-12-23 19:06:42 +08:00
const { calcHorizon, initAxis, drawAxis } = this
calcHorizon()
2018-12-20 18:25:19 +08:00
initAxis()
drawAxis()
2018-12-20 22:36:07 +08:00
2018-12-23 19:06:42 +08:00
const { switchNormalOrCenterOriginType } = this
2018-12-20 22:36:07 +08:00
2018-12-23 19:06:42 +08:00
switchNormalOrCenterOriginType()
},
calcHorizon () {
const { data: { horizon } } = this
this.horizon = horizon
},
switchNormalOrCenterOriginType () {
const { centerOrigin, drawNormalTypeColumnChart, drawCenterOriginTypeColumnChart } = this
if (centerOrigin) drawCenterOriginTypeColumnChart()
if (!centerOrigin) drawNormalTypeColumnChart()
},
drawNormalTypeColumnChart () {
const { calcColumnConfig, calcColumnItemOffset, calcValuePointPos } = this
2018-12-20 22:36:07 +08:00
calcColumnConfig()
2018-12-23 19:06:42 +08:00
calcColumnItemOffset()
calcValuePointPos()
2018-12-20 22:36:07 +08:00
2018-12-23 19:06:42 +08:00
const { drawFigure } = this
2018-12-20 22:36:07 +08:00
2018-12-23 19:06:42 +08:00
drawFigure()
2018-12-20 22:36:07 +08:00
},
calcColumnConfig () {
2018-12-23 19:06:42 +08:00
const { data: { data }, labelAxisTagPos, axisOriginPos, horizon } = this
2018-12-20 22:36:07 +08:00
2018-12-23 19:06:42 +08:00
const columnData = this.columnData = data.filter(({ type }) =>
!(type === 'polyline' || type === 'smoothline'))
2018-12-20 22:36:07 +08:00
2018-12-23 19:06:42 +08:00
const columnItemSeriesNum = this.columnItemSeriesNum = columnData.length
2018-12-20 22:36:07 +08:00
2018-12-23 19:06:42 +08:00
const columnItemAllWidth = this.columnItemAllWidth = (horizon
? axisOriginPos[1] - labelAxisTagPos[0][1]
: labelAxisTagPos[0][0] - axisOriginPos[0]) * 2
2018-12-20 22:36:07 +08:00
2018-12-23 19:06:42 +08:00
this.columnItemWidth = columnItemAllWidth / (columnItemSeriesNum + 1)
},
calcColumnItemOffset () {
const { columnItemSeriesNum, columnItemAllWidth, columnItemWidth } = this
2018-12-20 22:36:07 +08:00
2018-12-23 19:06:42 +08:00
const { data: { spaceBetween } } = this
2018-12-20 22:36:07 +08:00
2018-12-23 19:06:42 +08:00
const halfColumnWidth = columnItemWidth / 2
const halfColumnItemAllWidth = columnItemAllWidth / 2
let columnItemOffset = new Array(columnItemSeriesNum).fill(0)
if (spaceBetween) {
const spaceGap = columnItemWidth / (columnItemSeriesNum + 1)
this.columnItemOffset = columnItemOffset.map((t, i) =>
spaceGap * (i + 1) + columnItemWidth * i + halfColumnWidth - halfColumnItemAllWidth)
}
if (!spaceBetween) {
this.columnItemOffset = columnItemOffset.map((t, i) =>
columnItemWidth * (i + 1) - halfColumnItemAllWidth)
}
2018-12-20 22:36:07 +08:00
},
2018-12-23 19:06:42 +08:00
calcValuePointPos () {
const { getAxisPointsPos, valueAxisMaxMin, agValueAxisMaxMin } = this
const { labelAxisTagPos, deepClone, filterNull, multipleSum } = this
2018-12-20 22:36:07 +08:00
2018-12-23 19:06:42 +08:00
const { data: { data }, axisOriginPos, axisWH, horizon } = this
2018-12-20 22:36:07 +08:00
2018-12-23 19:06:42 +08:00
const dealAfterData = deepClone(data).map(({ data, againstAxis }) => {
if (!(data[0] instanceof Array)) return { data, againstAxis }
2018-12-20 22:36:07 +08:00
2018-12-23 19:06:42 +08:00
const td = data.map(series => series.map((v, i) => {
if (!v && v !== 0) return false
2018-12-20 22:36:07 +08:00
2018-12-23 19:06:42 +08:00
return multipleSum(...filterNull(series.slice(0, i + 1)))
}))
2018-12-20 22:36:07 +08:00
2018-12-23 19:06:42 +08:00
return { data: td, againstAxis }
})
2018-12-20 22:36:07 +08:00
2018-12-23 19:06:42 +08:00
this.valuePointPos = dealAfterData.map(({ data, againstAxis }) =>
getAxisPointsPos(
againstAxis ? agValueAxisMaxMin : valueAxisMaxMin,
data,
axisOriginPos,
axisWH,
labelAxisTagPos,
horizon
))
console.error(this.valuePointPos)
2018-12-20 22:36:07 +08:00
},
2018-12-23 19:06:42 +08:00
drawFigure () {
const { data: { data, roundColumn }, valuePointPos } = this
const { drawColumn, drawRoundColumn, drawLeftEchelon, drawRightEchelon, drawPolyline, drawSmoothline } = this
data.forEach((series, i) => {
switch (series.type) {
case 'leftEchelon': drawLeftEchelon(series, valuePointPos[i], i)
break
case 'rightEchelon': drawRightEchelon(series, valuePointPos[i], i)
break
case 'polyline': drawPolyline(series, valuePointPos[i], i)
break
case 'smoothline': drawSmoothline(series, valuePointPos[i], i)
break
default: roundColumn ? drawRoundColumn(series, valuePointPos[i], i) : drawColumn(series, valuePointPos[i], i)
break
}
})
},
getCurrentColor (i) {
// const { drawColors } = this
2018-12-20 22:36:07 +08:00
},
2018-12-23 19:06:42 +08:00
getGradientColor (value, colors) {
const { data: { localGradient }, axisAnglePos, horizon } = this
2018-12-20 22:36:07 +08:00
2018-12-23 19:06:42 +08:00
const { ctx, canvas: { getLinearGradientColor } } = this
2018-12-20 22:36:07 +08:00
2018-12-23 19:06:42 +08:00
if (localGradient) {
return getLinearGradientColor(ctx,
...(horizon
? [value, [axisAnglePos.leftTop[0], value[1]]]
: [value, [value[0], axisAnglePos.leftBottom[1]]]),
colors)
} else {
return getLinearGradientColor(ctx,
...(horizon
? [axisAnglePos.leftTop, axisAnglePos.rightTop]
: [axisAnglePos.leftTop, axisAnglePos.leftBottom]),
colors)
}
},
drawColumn ({ fillColor }, points, i) {
const { columnItemWidth, columnItemOffset, labelAxisTagPos, getOffsetPoint } = this
2018-12-20 22:36:07 +08:00
2018-12-23 19:06:42 +08:00
const { ctx, drawColors, getGradientColor, deepClone } = this
2018-12-20 22:36:07 +08:00
2018-12-23 19:06:42 +08:00
ctx.setLineDash([10, 0])
2018-12-20 22:36:07 +08:00
2018-12-23 19:06:42 +08:00
ctx.lineWidth = columnItemWidth
2018-12-20 22:36:07 +08:00
2018-12-23 19:06:42 +08:00
const currentColor = fillColor || drawColors[i]
2018-12-20 22:36:07 +08:00
2018-12-23 19:06:42 +08:00
const currentOffset = columnItemOffset.shift()
2018-12-20 22:36:07 +08:00
2018-12-23 19:06:42 +08:00
const offsetTagPos = deepClone(labelAxisTagPos).map(p => getOffsetPoint(p, currentOffset))
2018-12-20 22:36:07 +08:00
2018-12-23 19:06:42 +08:00
points.forEach((point, i) => {
if (point[0] instanceof Array) {
let lastEnd = offsetTagPos[i]
2018-12-20 22:36:07 +08:00
2018-12-23 19:06:42 +08:00
point.forEach((item, j) => {
const beginPoint = getOffsetPoint(item, currentOffset)
2018-12-20 22:36:07 +08:00
2018-12-23 19:06:42 +08:00
// if (j === 0) return
if (j === 1) return
if (j === 2) return
2018-12-20 22:36:07 +08:00
2018-12-23 19:06:42 +08:00
ctx.beginPath()
ctx.strokeStyle = 'blue'
ctx.moveTo(...beginPoint)
ctx.lineTo(...lastEnd)
ctx.stroke()
2018-12-20 22:36:07 +08:00
2018-12-23 19:06:42 +08:00
lastEnd = deepClone(beginPoint)
})
return
}
2018-12-20 22:36:07 +08:00
ctx.beginPath()
2018-12-23 19:06:42 +08:00
ctx.strokeStyle = getGradientColor(point, currentColor)
ctx.moveTo(...getOffsetPoint(point, currentOffset))
ctx.lineTo(...offsetTagPos[i])
2018-12-20 22:36:07 +08:00
ctx.stroke()
})
},
2018-12-23 19:06:42 +08:00
getOffsetPoint ([x, y], offset) {
const { horizon } = this
2018-12-21 14:47:16 +08:00
2018-12-23 19:06:42 +08:00
return horizon
? [x, y + offset]
: [x + offset, y]
},
drawRoundColumn () {},
drawLeftEchelon () {},
drawRightEchelon () {},
drawPolyline () {},
drawSmoothline () {},
drawCenterOriginTypeColumnChart () {}
// draw () {
// const { clearCanvas, initColors, initAxis, drawAxis, calcColumnConfig, drawColumnBG } = this
2018-12-21 14:47:16 +08:00
2018-12-23 19:06:42 +08:00
// clearCanvas()
2018-12-21 14:47:16 +08:00
2018-12-23 19:06:42 +08:00
// initColors()
2018-12-21 14:47:16 +08:00
2018-12-23 19:06:42 +08:00
// initAxis()
2018-12-21 14:47:16 +08:00
2018-12-23 19:06:42 +08:00
// drawAxis()
2018-12-21 14:47:16 +08:00
2018-12-23 19:06:42 +08:00
// const { calcBGConfig, drawColumn, calcColumnData } = this
2018-12-21 14:47:16 +08:00
2018-12-23 19:06:42 +08:00
// calcBGConfig()
2018-12-21 14:47:16 +08:00
2018-12-23 19:06:42 +08:00
// calcColumnConfig()
2018-12-21 14:47:16 +08:00
2018-12-23 19:06:42 +08:00
// calcColumnData()
2018-12-21 14:47:16 +08:00
2018-12-23 19:06:42 +08:00
// drawColumnBG()
2018-12-21 14:47:16 +08:00
2018-12-23 19:06:42 +08:00
// drawColumn()
// },
// calcColumnConfig () {
// const { data, labelTagGap, defaultMulItemDrawType, defaultColumnType } = this
2018-12-21 14:47:16 +08:00
2018-12-23 19:06:42 +08:00
// const { data: td, columnType, mulItemDrawType } = data
2018-12-21 14:47:16 +08:00
2018-12-23 19:06:42 +08:00
// const halfGap = labelTagGap / 2
2018-12-21 14:47:16 +08:00
2018-12-23 19:06:42 +08:00
// const columnWidth = this.columnWidth = labelTagGap / (td.length + 1)
2018-12-21 14:47:16 +08:00
2018-12-23 19:06:42 +08:00
// this.columnItemREPos = new Array(td.length).fill(0).map((t, i) =>
// (i + 1) * columnWidth).map(pos => pos - halfGap)
2018-12-21 14:47:16 +08:00
2018-12-23 19:06:42 +08:00
// this.columnType = columnType || defaultColumnType
2018-12-21 14:47:16 +08:00
2018-12-23 19:06:42 +08:00
// this.mulItemDrawType = mulItemDrawType || defaultMulItemDrawType
// },
// calcBGConfig () {
// const { data, defaultBGColor, drawColors, defaultShowColumnBG } = this
2018-12-21 14:47:16 +08:00
2018-12-23 19:06:42 +08:00
// const { showColumnBG, bgColor } = data
2018-12-21 14:47:16 +08:00
2018-12-23 19:06:42 +08:00
// this.showColumnBG = showColumnBG || defaultShowColumnBG
2018-12-21 14:47:16 +08:00
2018-12-23 19:06:42 +08:00
// let trueBGColor = bgColor || defaultBGColor
2018-12-21 14:47:16 +08:00
2018-12-23 19:06:42 +08:00
// trueBGColor === 'colors' && (trueBGColor = drawColors)
2018-12-21 14:47:16 +08:00
2018-12-23 19:06:42 +08:00
// this.bgColor = trueBGColor
2018-12-21 14:47:16 +08:00
2018-12-23 19:06:42 +08:00
// this.bgColorMul = trueBGColor instanceof Array
// },
// calcColumnData () {
// const { labelAxisPos, horizon, defaultMulItemDrawType, data, filterNull } = this
2018-12-21 14:47:16 +08:00
2018-12-23 19:06:42 +08:00
// const { getAxisPointsPos, axisMaxMin, axisOriginPos, axisWH, deepClone, multipleSum } = this
2018-12-21 14:47:16 +08:00
2018-12-23 19:06:42 +08:00
// const { mulItemDrawType, data: td } = data
2018-12-21 14:47:16 +08:00
2018-12-23 19:06:42 +08:00
// const trueMulItemDrawType = this.mulItemDrawType = mulItemDrawType || defaultMulItemDrawType
2018-12-21 14:47:16 +08:00
2018-12-23 19:06:42 +08:00
// this.columnData = td.map(({ data: values }, i) =>
// values.map((v, j) => {
// if (!v) return false
2018-12-21 14:47:16 +08:00
2018-12-23 19:06:42 +08:00
// let beginPoint = labelAxisPos[j]
2018-12-21 14:47:16 +08:00
2018-12-23 19:06:42 +08:00
// if (v instanceof Array) {
// return v.map((ci, k) => {
// if (!ci) return false
2018-12-21 14:47:16 +08:00
2018-12-23 19:06:42 +08:00
// if (trueMulItemDrawType === 'cover') {
// return [
// beginPoint,
// getAxisPointsPos(axisMaxMin, ci, axisOriginPos, axisWH, beginPoint, horizon)
// ]
// } else {
// const beReutrn = [
// deepClone(beginPoint),
// getAxisPointsPos(axisMaxMin,
// multipleSum(...filterNull(v.slice(0, k + 1))), axisOriginPos, axisWH, beginPoint, horizon)
// ]
2018-12-21 14:47:16 +08:00
2018-12-23 19:06:42 +08:00
// beginPoint = deepClone(beReutrn[1])
2018-12-21 14:47:16 +08:00
2018-12-23 19:06:42 +08:00
// return beReutrn
// }
// })
// } else {
// if (!v) return false
2018-12-21 14:47:16 +08:00
2018-12-23 19:06:42 +08:00
// return [
// deepClone(beginPoint),
// getAxisPointsPos(axisMaxMin, v, axisOriginPos, axisWH, beginPoint, horizon)
// ]
// }
// }))
// },
// drawColumnBG () {
// const { ctx, showColumnBG, columnWidth, axisWH } = this
2018-12-21 14:47:16 +08:00
2018-12-23 19:06:42 +08:00
// const { bgColor, bgColorMul, horizon, labelAxisPos } = this
// const { columnType, data } = this
// if (!showColumnBG) return
// !bgColorMul && (ctx.strokeStyle = bgColor)
// const bgColorNum = bgColor.length
// const bgColumnWidth = columnWidth * data.data.length
// ctx.lineWidth = bgColumnWidth
// ctx.setLineDash([10, 0])
// ctx.lineCap = columnType
// const halfColumnWidth = bgColumnWidth / 2
// labelAxisPos.forEach((pos, i) => {
// const movePos = pos
// const endPos = horizon ? [pos[0] + axisWH[0], pos[1]] : [pos[0], pos[1] - axisWH[1]]
// if (columnType === 'round') {
// if (horizon) {
// movePos[0] += halfColumnWidth
// endPos[0] -= halfColumnWidth
// } else {
// movePos[1] -= halfColumnWidth
// endPos[1] += halfColumnWidth
// }
// }
// bgColorMul && (ctx.strokeStyle = bgColor[i % bgColorNum])
// ctx.beginPath()
// ctx.moveTo(...movePos)
// ctx.lineTo(...endPos)
// ctx.stroke()
// })
// },
// drawColumn () {
// const { ctx, drawColors, drawColorsMul, data: { data: td }, horizon } = this
// const { columnWidth, columnItemREPos, columnData, getREPos, canvas } = this
// const { axisOriginPos, axisWH, columnType, getRoundLinePoints } = this
// const { getLinearGradientColor } = canvas
// const halfColumnWidth = columnWidth / 2
// ctx.lineWidth = columnWidth
// !drawColorsMul && (ctx.strokeStyle = drawColors)
// ctx.setLineDash([10, 0])
// ctx.lineCap = columnType
// const drawColorsNum = drawColors.length
// const linearGradientColorPos = horizon ? [
// axisOriginPos,
// [axisOriginPos[0] + axisWH[0], axisOriginPos[1]]
// ] : [
// axisOriginPos,
// [axisOriginPos[0], axisOriginPos[1] - axisWH[1]]
// ]
// columnData.forEach((column, i) => {
// drawColorsMul && (ctx.strokeStyle = drawColors[i % drawColorsNum])
// let currentFillColor = td[i].fillColor
// currentFillColor === 'colors' && (currentFillColor = drawColors)
// const currentFillColorMul = currentFillColor instanceof Array
// const currentFillColorNum = currentFillColorMul ? currentFillColor.length : 0
// currentFillColor && (ctx.strokeStyle = getLinearGradientColor(ctx, ...linearGradientColorPos, currentFillColor))
// column[0][0][0] instanceof Array && column.forEach((ci, j) =>
// ci.forEach((cii, k) => {
// if (!cii) return
// drawColorsMul && (ctx.strokeStyle = drawColors[(i + k) % drawColorsNum])
// if (currentFillColorMul) (ctx.strokeStyle = currentFillColor[k % currentFillColorNum])
// ctx.beginPath()
// let currentREPos = cii.map(tci => getREPos(tci, columnItemREPos[i]))
// columnType === 'round' && (currentREPos = getRoundLinePoints(currentREPos, halfColumnWidth))
// ctx.moveTo(...currentREPos[0])
// ctx.lineTo(...currentREPos[1])
// ctx.stroke()
// }))
// !(column[0][0][0] instanceof Array) && column.forEach((ci, j) => {
// if (!ci) return
// ctx.beginPath()
// let currentREPos = ci.map(tci => getREPos(tci, columnItemREPos[i]))
// columnType === 'round' && (currentREPos = getRoundLinePoints(currentREPos, halfColumnWidth))
// ctx.moveTo(...currentREPos[0])
// ctx.lineTo(...currentREPos[1])
// ctx.stroke()
// })
// })
// },
// getREPos ([x, y], datum) {
// const { horizon } = this
// return [
// horizon ? x : x + datum,
// horizon ? y + datum : y
// ]
// },
// getRoundLinePoints ([pa, pb], columnWidth) {
// const { horizon } = this
// let [a, b, c, d] = [0, 0, 0, 0]
// if (horizon) {
// a = pa[0] + columnWidth
// b = pa[1]
// c = pb[0] - columnWidth
// d = pb[1]
// } else {
// a = pa[0]
// b = pa[1] - columnWidth
// c = pb[0]
// d = pb[1] + columnWidth
// }
// return horizon ? [
// [a > c ? c : a, b],
// [c, d]
// ] : [
// [a, b],
// [c, b > d ? d : b]
// ]
// }
2018-12-20 18:25:19 +08:00
},
mounted () {
const { init } = this
init()
}
}
</script>
<style lang="less">
.column-chart {
position: relative;
display: flex;
flex-direction: column;
.canvas-container {
flex: 1;
}
canvas {
width: 100%;
height: 100%;
}
}
</style>