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,
|
|
|
|
|
2018-12-23 22:33:45 +08:00
|
|
|
echelonOffset: 10,
|
|
|
|
|
2018-12-23 19:06:42 +08:00
|
|
|
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
|
|
|
|
))
|
2018-12-20 22:36:07 +08:00
|
|
|
},
|
2018-12-23 19:06:42 +08:00
|
|
|
drawFigure () {
|
2018-12-23 22:33:45 +08:00
|
|
|
const { data: { data }, valuePointPos } = this
|
2018-12-23 19:06:42 +08:00
|
|
|
|
2018-12-23 22:33:45 +08:00
|
|
|
const { drawColumn, drawEchelon, drawline } = this
|
2018-12-23 19:06:42 +08:00
|
|
|
|
|
|
|
data.forEach((series, i) => {
|
|
|
|
switch (series.type) {
|
2018-12-23 22:33:45 +08:00
|
|
|
case 'leftEchelon':
|
|
|
|
case 'rightEchelon': drawEchelon(series, valuePointPos[i], i)
|
2018-12-23 19:06:42 +08:00
|
|
|
break
|
2018-12-23 22:33:45 +08:00
|
|
|
|
|
|
|
case 'polyline':
|
|
|
|
case 'smoothline': drawline(series, valuePointPos[i], i)
|
2018-12-23 19:06:42 +08:00
|
|
|
break
|
|
|
|
|
2018-12-23 22:33:45 +08:00
|
|
|
default: drawColumn(series, valuePointPos[i], i)
|
2018-12-23 19:06:42 +08:00
|
|
|
break
|
|
|
|
}
|
|
|
|
})
|
|
|
|
},
|
|
|
|
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) {
|
2018-12-23 22:33:45 +08:00
|
|
|
const { ctx, columnItemWidth, drawColors } = this
|
2018-12-20 22:36:07 +08:00
|
|
|
|
2018-12-23 19:06:42 +08:00
|
|
|
ctx.setLineDash([10, 0])
|
|
|
|
ctx.lineWidth = columnItemWidth
|
2018-12-20 22:36:07 +08:00
|
|
|
|
2018-12-23 22:33:45 +08:00
|
|
|
const color = fillColor || drawColors[i]
|
|
|
|
const colorNum = color.length
|
|
|
|
const drawColorNum = drawColors.length
|
|
|
|
|
|
|
|
const { columnItemOffset, labelAxisTagPos, getOffsetPoint } = this
|
2018-12-20 22:36:07 +08:00
|
|
|
|
2018-12-23 19:06:42 +08:00
|
|
|
const currentOffset = columnItemOffset.shift()
|
2018-12-23 22:33:45 +08:00
|
|
|
const offsetTagPos = labelAxisTagPos.map(p => getOffsetPoint(p, currentOffset))
|
2018-12-20 22:36:07 +08:00
|
|
|
|
2018-12-23 22:33:45 +08:00
|
|
|
const { getGradientColor, getRoundColumnPoint, data: { roundColumn } } = this
|
2018-12-20 22:36:07 +08:00
|
|
|
|
2018-12-23 22:33:45 +08:00
|
|
|
ctx.lineCap = roundColumn ? 'round' : 'butt'
|
2018-12-20 22:36:07 +08:00
|
|
|
|
2018-12-23 22:33:45 +08:00
|
|
|
const seriesColumn = points[0][0] instanceof Array
|
2018-12-20 22:36:07 +08:00
|
|
|
|
2018-12-23 22:33:45 +08:00
|
|
|
seriesColumn && points.forEach((series, i) => {
|
|
|
|
let lastEnd = offsetTagPos[i]
|
2018-12-20 22:36:07 +08:00
|
|
|
|
2018-12-23 22:33:45 +08:00
|
|
|
series.forEach((item, j) => {
|
|
|
|
const currentPoint = getOffsetPoint(item, currentOffset)
|
2018-12-20 22:36:07 +08:00
|
|
|
|
2018-12-23 22:33:45 +08:00
|
|
|
let columnPoint = [lastEnd, currentPoint]
|
|
|
|
|
|
|
|
roundColumn && (columnPoint = getRoundColumnPoint(columnPoint))
|
|
|
|
|
|
|
|
if (typeof color === 'string') {
|
|
|
|
ctx.strokeStyle = drawColors[(i + j) % drawColorNum]
|
|
|
|
} else {
|
|
|
|
ctx.strokeStyle = color[(i + j) % colorNum]
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx.beginPath()
|
|
|
|
ctx.moveTo(...columnPoint[0])
|
|
|
|
ctx.lineTo(...columnPoint[1])
|
|
|
|
ctx.stroke()
|
|
|
|
|
|
|
|
lastEnd = currentPoint
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
!seriesColumn && points.forEach((point, i) => {
|
|
|
|
let columnPoint = [offsetTagPos[i], getOffsetPoint(point, currentOffset)]
|
|
|
|
|
|
|
|
roundColumn && (columnPoint = getRoundColumnPoint(columnPoint))
|
2018-12-20 22:36:07 +08:00
|
|
|
|
|
|
|
ctx.beginPath()
|
2018-12-23 22:33:45 +08:00
|
|
|
ctx.strokeStyle = getGradientColor(point, color)
|
|
|
|
ctx.moveTo(...columnPoint[0])
|
|
|
|
ctx.lineTo(...columnPoint[1])
|
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]
|
|
|
|
},
|
2018-12-23 22:33:45 +08:00
|
|
|
getRoundColumnPoint ([pa, pb]) {
|
|
|
|
const { horizon, columnItemWidth } = this
|
|
|
|
|
|
|
|
const radius = columnItemWidth / 2
|
|
|
|
|
|
|
|
let [a, b, c, d] = [0, 0, 0, 0]
|
|
|
|
|
|
|
|
if (horizon) {
|
|
|
|
a = pa[0] + radius
|
|
|
|
b = pa[1]
|
|
|
|
c = pb[0] - radius
|
|
|
|
d = pb[1]
|
|
|
|
} else {
|
|
|
|
a = pa[0]
|
|
|
|
b = pa[1] - radius
|
|
|
|
c = pb[0]
|
|
|
|
d = pb[1] + radius
|
|
|
|
}
|
|
|
|
|
|
|
|
return horizon ? [
|
|
|
|
[a > c ? c : a, b],
|
|
|
|
[c, d]
|
|
|
|
] : [
|
|
|
|
[a, b],
|
|
|
|
[c, b > d ? d : b]
|
|
|
|
]
|
|
|
|
},
|
|
|
|
drawline ({ lineColor, fillColor, pointColor, lineType, lineDash, type }, points, i) {
|
|
|
|
const { drawColors, ctx, axisOriginPos: [x, y], horizon } = this
|
|
|
|
|
|
|
|
const { color: { hexToRgb }, getGradientColor, getTopPoint } = this
|
|
|
|
|
|
|
|
const drawColorNum = drawColors.length
|
|
|
|
const currentColor = drawColors[i % drawColorNum]
|
|
|
|
|
|
|
|
let currentLineColor = hexToRgb(currentColor, 0.6)
|
|
|
|
let currentPointColor = currentColor
|
|
|
|
|
|
|
|
lineColor && (currentLineColor = lineColor)
|
|
|
|
pointColor && (currentPointColor = pointColor)
|
|
|
|
|
|
|
|
let currentLineType = lineType || 'line'
|
|
|
|
let currentLineDash = currentLineType === 'dashed' ? (lineDash || [5, 5]) : [10, 0]
|
|
|
|
|
|
|
|
ctx.strokeStyle = currentLineColor
|
|
|
|
|
|
|
|
const { canvas: { drawPolylinePath, drawPolyline, drawPoints } } = this
|
|
|
|
const { canvas: { drawSmoothlinePath, drawSmoothline } } = this
|
|
|
|
|
|
|
|
const lineFun = type === 'polyline' ? [drawPolylinePath, drawPolyline] : [drawSmoothlinePath, drawSmoothline]
|
|
|
|
|
|
|
|
if (fillColor) {
|
|
|
|
const lastPoint = points[points.length - 1]
|
|
|
|
|
|
|
|
ctx.fillStyle = getGradientColor(getTopPoint(points), fillColor)
|
|
|
|
|
|
|
|
lineFun[0](ctx, points, false, true, true)
|
|
|
|
ctx.lineTo(...(horizon ? [x, lastPoint[1]] : [lastPoint[0], y]))
|
|
|
|
ctx.lineTo(...(horizon ? [x, points[0][1]] : [points[0][0], y]))
|
|
|
|
|
|
|
|
ctx.closePath()
|
|
|
|
ctx.fill()
|
|
|
|
}
|
|
|
|
|
|
|
|
lineFun[1](ctx, points, 1, currentLineColor, false, currentLineDash, true, true)
|
|
|
|
|
|
|
|
drawPoints(ctx, points, 2, currentPointColor)
|
|
|
|
},
|
|
|
|
getTopPoint (points) {
|
|
|
|
const { horizon } = this
|
|
|
|
|
|
|
|
let topIndex = 0
|
|
|
|
|
|
|
|
const xPos = points.map(([x]) => x)
|
|
|
|
const yPos = points.map(([, y]) => y)
|
|
|
|
|
|
|
|
if (horizon) {
|
|
|
|
const top = Math.max(...xPos)
|
|
|
|
|
|
|
|
topIndex = xPos.findIndex(v => v === top)
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!horizon) {
|
|
|
|
const top = Math.min(...yPos)
|
|
|
|
|
|
|
|
topIndex = yPos.findIndex(v => v === top)
|
|
|
|
}
|
|
|
|
|
|
|
|
console.error(topIndex)
|
|
|
|
|
|
|
|
return points[topIndex]
|
|
|
|
},
|
|
|
|
drawEchelon ({ fillColor, type }, points, i) {
|
|
|
|
const { data: { roundColumn } } = this
|
|
|
|
|
|
|
|
const seriesColumn = points[0][0] instanceof Array
|
|
|
|
|
|
|
|
if (seriesColumn || roundColumn) return
|
|
|
|
|
|
|
|
const { ctx, columnItemOffset, labelAxisTagPos, getOffsetPoint } = this
|
|
|
|
|
|
|
|
const currentOffset = columnItemOffset.shift()
|
|
|
|
const offsetTagPos = labelAxisTagPos.map(p => getOffsetPoint(p, currentOffset))
|
|
|
|
|
|
|
|
const { drawColors, getGradientColor, getEchelonPoints } = this
|
|
|
|
|
|
|
|
const drawColorsNum = drawColors.length
|
|
|
|
|
|
|
|
const color = fillColor || drawColors[i % drawColorsNum]
|
|
|
|
|
|
|
|
const { canvas: { drawPolylinePath } } = this
|
|
|
|
|
|
|
|
points.forEach((point, i) => {
|
|
|
|
const topPoint = getOffsetPoint(point, currentOffset)
|
|
|
|
const bottomPoint = offsetTagPos[i]
|
|
|
|
|
|
|
|
const echelonPoints = getEchelonPoints(topPoint, bottomPoint, type)
|
|
|
|
|
|
|
|
drawPolylinePath(ctx, echelonPoints, true, true)
|
|
|
|
|
|
|
|
ctx.fillStyle = getGradientColor(point, color)
|
|
|
|
|
|
|
|
ctx.fill()
|
|
|
|
})
|
|
|
|
},
|
|
|
|
getEchelonPoints ([tx, ty], [bx, by], type) {
|
|
|
|
const { columnItemWidth, echelonOffset, horizon } = this
|
|
|
|
|
|
|
|
const halfWidth = columnItemWidth / 2
|
|
|
|
|
|
|
|
const echelonPoint = []
|
|
|
|
|
|
|
|
if (horizon) {
|
|
|
|
let enhance = tx - bx < echelonOffset
|
|
|
|
|
|
|
|
if (type === 'leftEchelon') {
|
|
|
|
echelonPoint[0] = [tx, ty + halfWidth]
|
|
|
|
echelonPoint[1] = [bx, ty + halfWidth]
|
|
|
|
echelonPoint[2] = [bx + echelonOffset, by - halfWidth]
|
|
|
|
echelonPoint[3] = [tx, ty - halfWidth]
|
|
|
|
}
|
|
|
|
|
|
|
|
if (type === 'rightEchelon') {
|
|
|
|
echelonPoint[0] = [tx, ty - halfWidth]
|
|
|
|
echelonPoint[1] = [bx, ty - halfWidth]
|
|
|
|
echelonPoint[2] = [bx + echelonOffset, by + halfWidth]
|
|
|
|
echelonPoint[3] = [tx, ty + halfWidth]
|
|
|
|
}
|
|
|
|
|
|
|
|
if (enhance) echelonPoint.splice(2, 1)
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!horizon) {
|
|
|
|
let enhance = by - ty < echelonOffset
|
|
|
|
|
|
|
|
if (type === 'leftEchelon') {
|
|
|
|
echelonPoint[0] = [tx + halfWidth, ty]
|
|
|
|
echelonPoint[1] = [tx + halfWidth, by]
|
|
|
|
echelonPoint[2] = [tx - halfWidth, by - echelonOffset]
|
|
|
|
echelonPoint[3] = [tx - halfWidth, ty]
|
|
|
|
}
|
|
|
|
|
|
|
|
if (type === 'rightEchelon') {
|
|
|
|
echelonPoint[0] = [tx - halfWidth, ty]
|
|
|
|
echelonPoint[1] = [tx - halfWidth, by]
|
|
|
|
echelonPoint[2] = [tx + halfWidth, by - echelonOffset]
|
|
|
|
echelonPoint[3] = [tx + halfWidth, ty]
|
|
|
|
}
|
|
|
|
|
|
|
|
if (enhance) echelonPoint.splice(2, 1)
|
|
|
|
}
|
|
|
|
|
|
|
|
return echelonPoint
|
|
|
|
},
|
2018-12-23 19:06:42 +08:00
|
|
|
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>
|