DemuMesDataV/mixins/axisMixin.js

673 lines
20 KiB
JavaScript
Raw Normal View History

2018-12-20 18:25:49 +08:00
export default {
data () {
return {
2019-01-16 16:56:11 +08:00
// config able
defaultAxisFontSize: 10,
defaultAxisFontFamily: 'Arial',
2018-12-23 19:07:01 +08:00
defaultAxisLineColor: '#666',
defaultGridLineColor: '#666',
2019-01-16 16:56:11 +08:00
defaultAxisTagColor: '#666',
defaultAxisUnitColor: '#666',
2018-12-23 19:07:01 +08:00
2019-01-16 16:56:11 +08:00
defaultGridLineDash: [2, 2],
2018-12-23 19:07:01 +08:00
2019-01-16 16:56:11 +08:00
defaultNoAxisLine: false,
defaultNoAxisTag: false,
defaultXAxisOffset: 20,
2018-12-23 19:07:01 +08:00
defaultAxisLineTagGap: 5,
2019-01-16 16:56:11 +08:00
// after merge
axisFontSize: 0,
axisFontFamily: '',
axisLineColor: '',
gridLineColor: '',
axisTagColor: '',
axisUnitColor: '',
gridLineDash: [],
noAxisLine: '',
noAxisTag: '',
// calc data
2018-12-20 18:25:49 +08:00
valueMaxMin: [],
2018-12-23 19:07:01 +08:00
agValueMaxMin: [],
valueAxisMaxMin: [],
agValueAxisMaxMin: [],
2018-12-20 18:25:49 +08:00
valueAxisTag: [],
2018-12-23 19:07:01 +08:00
agValueAxisTag: [],
2018-12-20 18:25:49 +08:00
labelAxisTag: [],
2018-12-23 19:07:01 +08:00
agLabelAxisTag: [],
xAxisTagBA: ['', ''],
agXAxisTagBA: ['', ''],
yAxisTagBA: ['', ''],
agYAxisTagBA: ['', ''],
addBAValueAxisTag: [],
addBAAGValueAxisTag: [],
addBALabelAxisTag: [],
addBAAGLabelAxisTag: [],
axisUnit: [],
axisOffset: [],
2018-12-20 18:25:49 +08:00
axisOriginPos: [],
axisWH: [],
2018-12-23 19:07:01 +08:00
axisAnglePos: {},
valueAxisTagPos: [],
agValueAxisTagPos: [],
labelAxisTagPos: [],
agLabelAxisTagPos: [],
tagAlign: {}
2018-12-20 18:25:49 +08:00
}
},
methods: {
initAxis () {
2018-12-23 19:07:01 +08:00
const { calcValuesMaxMin, calcValueAxisData, calcLabelAxisData } = this
calcValuesMaxMin()
2018-12-20 18:25:49 +08:00
2018-12-23 19:07:01 +08:00
calcValueAxisData()
2018-12-20 18:25:49 +08:00
2018-12-23 19:07:01 +08:00
calcLabelAxisData()
2018-12-20 18:25:49 +08:00
2018-12-23 19:07:01 +08:00
const { calcTagBA, calcAddBATag, calcAxisUnit } = this
2018-12-20 18:25:49 +08:00
2018-12-23 19:07:01 +08:00
calcTagBA()
2018-12-20 18:25:49 +08:00
2018-12-23 19:07:01 +08:00
calcAddBATag()
2018-12-20 18:25:49 +08:00
2018-12-23 19:07:01 +08:00
calcAxisUnit()
2018-12-20 18:25:49 +08:00
2019-01-16 16:56:11 +08:00
const { calcAxisFontData, calcAxisColor, calcGridLineDash } = this
2018-12-20 18:25:49 +08:00
2018-12-23 19:07:01 +08:00
calcAxisFontData()
2018-12-20 18:25:49 +08:00
2019-01-16 16:56:11 +08:00
calcAxisColor()
calcGridLineDash()
const { calcAxisLineTagStatus, calcAxisOffset, calcAxisAreaData } = this
calcAxisLineTagStatus()
2018-12-23 19:07:01 +08:00
calcAxisOffset()
2018-12-20 18:25:49 +08:00
2018-12-23 19:07:01 +08:00
calcAxisAreaData()
2018-12-20 18:25:49 +08:00
2018-12-23 19:07:01 +08:00
const { calcAxisAnglePos, calcValueAxisTagPos, calcLabelAxisTagPos } = this
2018-12-20 18:25:49 +08:00
2018-12-23 19:07:01 +08:00
calcAxisAnglePos()
2018-12-21 18:29:49 +08:00
2018-12-23 19:07:01 +08:00
calcValueAxisTagPos()
2018-12-20 18:25:49 +08:00
2018-12-23 19:07:01 +08:00
calcLabelAxisTagPos()
2018-12-20 18:25:49 +08:00
2018-12-23 19:07:01 +08:00
const { calcTagAlign } = this
2018-12-20 22:36:22 +08:00
2018-12-23 19:07:01 +08:00
calcTagAlign()
},
calcValuesMaxMin () {
2019-01-16 16:56:11 +08:00
const { data: { series }, calcValueMaxMin } = this
2018-12-23 19:07:01 +08:00
2019-01-16 16:56:11 +08:00
const valueSeries = series.filter(({ againstAxis }) => !againstAxis)
2018-12-23 19:07:01 +08:00
if (valueSeries.length) this.valueMaxMin = calcValueMaxMin(valueSeries)
2018-12-21 18:29:49 +08:00
2019-01-16 16:56:11 +08:00
const agValueSeries = series.filter(({ againstAxis }) => againstAxis)
2018-12-23 19:07:01 +08:00
if (agValueSeries.length) this.agValueMaxMin = calcValueMaxMin(agValueSeries)
2018-12-20 18:25:49 +08:00
},
2019-01-16 16:56:11 +08:00
calcValueMaxMin (series) {
2018-12-23 22:33:51 +08:00
const { mulValueAdd, calcMulValueAdd, getArrayMax, getArrayMin } = this
2018-12-23 19:07:01 +08:00
2019-01-16 16:56:11 +08:00
let valueSeries = series.map(({ value }) => value)
2018-12-20 18:25:49 +08:00
2018-12-23 22:33:51 +08:00
const min = getArrayMin(valueSeries)
2018-12-23 19:07:01 +08:00
mulValueAdd && (valueSeries = calcMulValueAdd(valueSeries))
2018-12-20 22:36:22 +08:00
2018-12-23 22:33:51 +08:00
const max = getArrayMax(valueSeries)
return [max, min]
2018-12-23 19:07:01 +08:00
},
calcMulValueAdd (values) {
const { multipleSum, filterNull } = this
2018-12-20 18:25:49 +08:00
2018-12-23 19:07:01 +08:00
return values.map(series =>
filterNull(series).map(n =>
n instanceof Array ? multipleSum(...filterNull(n)) : n))
2018-12-20 18:25:49 +08:00
},
2018-12-23 19:07:01 +08:00
calcValueAxisData () {
const { horizon, data: { x, ax, y, ay }, calcValueAxisTag } = this
2018-12-20 18:25:49 +08:00
2018-12-23 19:07:01 +08:00
const { valueMaxMin, agValueMaxMin, getValueAxisMaxMin } = this
2018-12-20 18:25:49 +08:00
2018-12-23 19:07:01 +08:00
const valueAxis = horizon ? [x, ax] : [y, ay]
2018-12-20 18:25:49 +08:00
2018-12-23 19:07:01 +08:00
if (valueMaxMin.length) {
const valueAxisTag = this.valueAxisTag = calcValueAxisTag(valueMaxMin, valueAxis[0])
this.valueAxisMaxMin = getValueAxisMaxMin(valueAxisTag)
}
if (agValueMaxMin.length) {
const agValueAxisTag = this.agValueAxisTag = calcValueAxisTag(agValueMaxMin, valueAxis[1])
this.agValueAxisMaxMin = getValueAxisMaxMin(agValueAxisTag)
}
},
2019-01-16 16:56:11 +08:00
calcValueAxisTag ([vmax, vmin], { max, min, num, fixed, tags } = {}) {
if (tags) return tags
2018-12-20 18:25:49 +08:00
let [trueMax, trueMin] = [max, min]
2019-01-16 16:56:11 +08:00
if (vmax === 0 && vmin === 0) vmax = 8
2018-12-23 19:07:01 +08:00
const thirdValueMinus = parseInt((vmax - vmin) / 3)
2018-12-20 18:25:49 +08:00
2018-12-23 19:07:01 +08:00
!max && (max !== 0) && (trueMax = vmax + thirdValueMinus)
!min && (min !== 0) && (trueMin = vmin - thirdValueMinus)
2018-12-20 18:25:49 +08:00
const trueMinus = trueMax - trueMin
2018-12-24 18:25:00 +08:00
!num && trueMinus < 9 && (num = Math.ceil(trueMinus) + 1)
2018-12-20 18:25:49 +08:00
!num && (num = 10)
const valueGap = trueMinus / (num - 1)
2018-12-23 19:07:01 +08:00
return Array(num).fill(0).map((t, i) =>
2018-12-20 18:25:49 +08:00
(trueMin + i * valueGap).toFixed(fixed))
},
2018-12-23 19:07:01 +08:00
getValueAxisMaxMin (valueTag) {
const lastIndex = valueTag.length - 1
2018-12-20 18:25:49 +08:00
2018-12-23 19:07:01 +08:00
return [
parseFloat(valueTag[lastIndex]),
parseFloat(valueTag[0])
]
2018-12-20 18:25:49 +08:00
},
2018-12-23 19:07:01 +08:00
calcLabelAxisData () {
const { horizon, data: { x, ax, y, ay } } = this
2018-12-20 18:25:49 +08:00
2018-12-23 19:07:01 +08:00
const labelAxis = horizon ? [y, ay] : [x, ax]
2018-12-20 18:25:49 +08:00
2019-01-16 16:56:11 +08:00
if (labelAxis[0] && labelAxis[0].tags) this.labelAxisTag = labelAxis[0].tags
2018-12-20 18:25:49 +08:00
2019-01-16 16:56:11 +08:00
if (labelAxis[1] && labelAxis[1].tags) this.agLabelAxisTag = labelAxis[1].tags
2018-12-23 19:07:01 +08:00
},
calcTagBA () {
const { data: { x, ax, y, ay } } = this
if (x && x.tagBefore) this.xAxisTagBA[0] = x.tagBefore
if (ax && ax.tagBefore) this.agXAxisTagBA[0] = ax.tagBefore
if (y && y.tagBefore) this.yAxisTagBA[0] = y.tagBefore
if (ay && ay.tagBefore) this.agYAxisTagBA[0] = ay.tagBefore
if (x && x.tagAfter) this.xAxisTagBA[1] = x.tagAfter
if (ax && ax.tagAfter) this.agXAxisTagBA[1] = ax.tagAfter
if (y && y.tagAfter) this.yAxisTagBA[1] = y.tagAfter
if (ay && ay.tagAfter) this.agYAxisTagBA[1] = ay.tagAfter
},
calcAddBATag () {
const { xAxisTagBA, agXAxisTagBA, yAxisTagBA, agYAxisTagBA } = this
2018-12-20 18:25:49 +08:00
2018-12-23 19:07:01 +08:00
const { valueAxisTag, agValueAxisTag, labelAxisTag, agLabelAxisTag } = this
2018-12-20 18:25:49 +08:00
2018-12-23 19:07:01 +08:00
const { horizon, addBATag } = this
2018-12-20 18:25:49 +08:00
2018-12-23 19:07:01 +08:00
const valueTagBA = horizon ? [xAxisTagBA, agXAxisTagBA] : [yAxisTagBA, agYAxisTagBA]
2018-12-20 18:25:49 +08:00
2018-12-23 19:07:01 +08:00
const labelTagBA = horizon ? [yAxisTagBA, agYAxisTagBA] : [xAxisTagBA, agXAxisTagBA]
2018-12-20 18:25:49 +08:00
2018-12-23 19:07:01 +08:00
if (valueAxisTag.length) this.addBAValueAxisTag = addBATag(valueAxisTag, valueTagBA[0])
if (agValueAxisTag.length) this.addBAAGValueAxisTag = addBATag(agValueAxisTag, valueTagBA[1])
if (labelAxisTag.length) this.addBALabelAxisTag = addBATag(labelAxisTag, labelTagBA[0])
if (agLabelAxisTag.length) this.addBAAGLabelAxisTag = addBATag(agLabelAxisTag, labelTagBA[1])
},
addBATag (tags, ba) {
return tags.map(tag => tag ? `${ba[0]}${tag}${ba[1]}` : tag)
},
calcAxisUnit () {
const { data: { x, ax, y, ay } } = this
2018-12-20 18:25:49 +08:00
2018-12-23 19:07:01 +08:00
if (x && x.unit) this.axisUnit[0] = x.unit
if (ax && ax.unit) this.axisUnit[1] = ax.unit
if (y && y.unit) this.axisUnit[2] = y.unit
if (ay && ay.unit) this.axisUnit[3] = ay.unit
2018-12-20 18:25:49 +08:00
},
2018-12-23 19:07:01 +08:00
calcAxisFontData () {
2019-01-16 16:56:11 +08:00
const { defaultAxisFontSize, defaultAxisFontFamily, data } = this
const { fontSize, fontFamily, axisFontSize, axisFontFamily }= data
2018-12-20 18:25:49 +08:00
2019-01-16 16:56:11 +08:00
this.axisFontSize = axisFontSize || fontSize || defaultAxisFontSize
2018-12-20 18:25:49 +08:00
2019-01-16 16:56:11 +08:00
this.axisFontFamily = axisFontFamily || fontFamily || defaultAxisFontFamily
},
calcAxisColor () {
const { data, defaultAxisTagColor, defaultAxisLineColor, defaultGridLineColor } = this
const { axisTagColor, axisLineColor, gridLineColor } = data
this.axisTagColor = axisTagColor || defaultAxisTagColor
2018-12-20 18:25:49 +08:00
2019-01-16 16:56:11 +08:00
this.axisLineColor = axisLineColor || defaultAxisLineColor
this.gridLineColor = gridLineColor || defaultGridLineColor
},
calcGridLineDash () {
const { data, defaultGridLineDash } = this
const { gridLineDash } = data
if (gridLineDash instanceof Array) {
this.gridLineDash = gridLineDash
} else if (gridLineDash) {
this.gridLineDash = defaultGridLineDash
} else {
this.gridLineDash = [10, 0]
}
},
calcAxisLineTagStatus () {
const { defaultNoAxisLine, defaultNoAxisTag, data } = this
const { noAxisLine, noAxisTag } = data
this.noAxisLine = noAxisLine || defaultNoAxisLine
this.noAxisTag = noAxisTag || defaultNoAxisTag
2018-12-20 18:25:49 +08:00
},
2018-12-23 19:07:01 +08:00
calcAxisOffset () {
const { horizon, axisUnit, defaultXAxisOffset } = this
2018-12-20 18:25:49 +08:00
2018-12-23 19:07:01 +08:00
const { addBAValueAxisTag, addBAAGValueAxisTag, addBALabelAxisTag, addBAAGLabelAxisTag } = this
2018-12-20 18:25:49 +08:00
2018-12-23 19:07:01 +08:00
const { axisFontSize, axisFontFamily, defaultAxisLineTagGap } = this
2018-12-20 18:25:49 +08:00
2018-12-23 19:07:01 +08:00
const { data: { x, ax, y, ay } } = this
2018-12-20 18:25:49 +08:00
2018-12-23 19:07:01 +08:00
const { ctx, canvas: { getTextsWidth }, boundaryGap } = this
2018-12-20 18:25:49 +08:00
2018-12-23 19:07:01 +08:00
ctx.font = `${axisFontSize}px ${axisFontFamily}`
2018-12-21 14:46:55 +08:00
2018-12-23 19:07:01 +08:00
this.axisOffset[0] = (ax && ax.offset) || defaultXAxisOffset
this.axisOffset[2] = (x && x.offset) || defaultXAxisOffset
2018-12-20 18:25:49 +08:00
2018-12-23 19:07:01 +08:00
const horizonAxisTags = horizon
? [addBALabelAxisTag, addBAAGLabelAxisTag]
: [addBAValueAxisTag, addBAAGValueAxisTag]
2018-12-20 18:25:49 +08:00
2018-12-23 19:07:01 +08:00
this.axisOffset[3] = (y && y.offset) ||
Math.max(...getTextsWidth(ctx, [axisUnit[2] || '']),
...getTextsWidth(ctx, horizonAxisTags[0].length ? horizonAxisTags[0] : 0)) + defaultAxisLineTagGap
2018-12-20 18:25:49 +08:00
2018-12-23 19:07:01 +08:00
// axis offset 1
const xAxisTags = horizon ? addBAValueAxisTag : addBALabelAxisTag
2018-12-20 18:25:49 +08:00
2018-12-23 19:07:01 +08:00
let xAxisTagsHalfWidth = 0
2018-12-20 18:25:49 +08:00
2018-12-23 19:07:01 +08:00
xAxisTags.length && (xAxisTagsHalfWidth = ctx.measureText(xAxisTags.length - 1).width / 2)
2018-12-20 18:25:49 +08:00
2018-12-23 19:07:01 +08:00
let rightOffset = Math.max(...getTextsWidth(ctx, [axisUnit[3] || '']),
...getTextsWidth(ctx, [axisUnit[0] || '']),
...getTextsWidth(ctx, horizonAxisTags[1].length ? horizonAxisTags[1] : [''])) + defaultAxisLineTagGap
2018-12-20 18:25:49 +08:00
2018-12-24 18:25:00 +08:00
;(!boundaryGap || horizon) && (rightOffset += (xAxisTagsHalfWidth + 5))
2018-12-21 14:46:55 +08:00
2018-12-23 19:07:01 +08:00
this.axisOffset[1] = (ay && ay.offset) || rightOffset
2018-12-20 18:25:49 +08:00
2018-12-23 19:07:01 +08:00
if (y && y.noTag) this.axisOffset[3] = 1
if (ay && ay.noTag) this.axisOffset[1] = 1
},
calcAxisAreaData () {
const { canvasWH, axisOffset, axisWH, axisOriginPos } = this
2018-12-20 18:25:49 +08:00
2018-12-23 19:07:01 +08:00
axisWH[0] = canvasWH[0] - axisOffset[1] - axisOffset[3]
axisWH[1] = canvasWH[1] - axisOffset[0] - axisOffset[2]
2018-12-21 14:46:55 +08:00
2018-12-23 19:07:01 +08:00
axisOriginPos[0] = axisOffset[3]
axisOriginPos[1] = axisWH[1] + axisOffset[0]
2018-12-20 18:25:49 +08:00
},
2018-12-23 19:07:01 +08:00
calcAxisAnglePos () {
const { axisWH, axisOriginPos, axisAnglePos } = this
2018-12-20 18:25:49 +08:00
2018-12-23 19:07:01 +08:00
axisAnglePos.leftTop = [axisOriginPos[0], axisOriginPos[1] - axisWH[1]]
axisAnglePos.rightTop = [axisOriginPos[0] + axisWH[0], axisOriginPos[1] - axisWH[1]]
2018-12-20 18:25:49 +08:00
2018-12-23 19:07:01 +08:00
axisAnglePos.leftBottom = axisOriginPos
axisAnglePos.rightBottom = [axisOriginPos[0] + axisWH[0], axisOriginPos[1]]
},
calcValueAxisTagPos () {
const { horizon, axisOriginPos, axisAnglePos } = this
2018-12-20 18:25:49 +08:00
2018-12-23 19:07:01 +08:00
const { valueAxisTag, agValueAxisTag, getValueAxisTagPos } = this
2018-12-20 18:25:49 +08:00
2018-12-23 19:07:01 +08:00
if (valueAxisTag) this.valueAxisTagPos = getValueAxisTagPos(valueAxisTag, axisOriginPos)
2018-12-20 18:25:49 +08:00
2018-12-23 19:07:01 +08:00
const basePoint = horizon ? axisAnglePos.leftTop : axisAnglePos.rightBottom
2018-12-20 18:25:49 +08:00
2018-12-23 19:07:01 +08:00
if (agValueAxisTag) this.agValueAxisTagPos = getValueAxisTagPos(agValueAxisTag, basePoint)
},
getValueAxisTagPos (tags, [x, y]) {
const { horizon, axisWH } = this
2018-12-20 18:25:49 +08:00
2018-12-23 19:07:01 +08:00
const tagsNum = tags.length
2018-12-20 18:25:49 +08:00
2018-12-23 19:07:01 +08:00
const areaLength = horizon ? axisWH[0] : axisWH[1]
2018-12-20 18:25:49 +08:00
2018-12-23 19:07:01 +08:00
const tagGap = areaLength / (tagsNum - 1)
2018-12-20 18:25:49 +08:00
2018-12-23 19:07:01 +08:00
return new Array(tagsNum).fill(0).map((t, i) =>
horizon ? [x + tagGap * i, y] : [x, y - tagGap * i])
},
calcLabelAxisTagPos () {
const { horizon, getLabelAxisTagPos, axisAnglePos } = this
const { labelAxisTag, agLabelAxisTag, axisOriginPos } = this
2018-12-20 18:25:49 +08:00
2018-12-23 19:07:01 +08:00
if (labelAxisTag.length) this.labelAxisTagPos = getLabelAxisTagPos(labelAxisTag, axisOriginPos)
2018-12-20 18:25:49 +08:00
2018-12-23 19:07:01 +08:00
const basePoint = horizon ? axisAnglePos.rightBottom : axisAnglePos.leftTop
2018-12-20 18:25:49 +08:00
2018-12-23 19:07:01 +08:00
if (agLabelAxisTag.length) this.agLabelAxisTagPos = getLabelAxisTagPos(agLabelAxisTag, basePoint)
2018-12-20 18:25:49 +08:00
},
2018-12-23 19:07:01 +08:00
getLabelAxisTagPos (tags, [x, y]) {
const { horizon, axisWH, boundaryGap } = this
const tagsNum = tags.length
2018-12-20 22:36:22 +08:00
2018-12-23 19:07:01 +08:00
const areaLength = horizon ? axisWH[1] : axisWH[0]
2018-12-20 22:36:22 +08:00
2019-01-11 14:43:26 +08:00
let gapColumnNum = boundaryGap ? tagsNum : tagsNum - 1
gapColumnNum === 0 && (gapColumnNum = 1)
const tagGap = areaLength / gapColumnNum
2018-12-20 22:36:22 +08:00
2018-12-23 19:07:01 +08:00
const halfGap = tagGap / 2
const tempPos = new Array(tagsNum).fill(0)
if (boundaryGap) {
return tempPos.map((t, i) =>
horizon ? [x, y - (tagGap * i) - halfGap] : [x + (tagGap * i) + halfGap, y])
}
2018-12-20 22:36:22 +08:00
2018-12-23 19:07:01 +08:00
if (!boundaryGap) {
return tempPos.map((t, i) =>
horizon ? [x, y + tagGap * i] : [x + tagGap * i, y])
}
},
calcTagAlign () {
const { tagAlign } = this
2018-12-20 22:36:22 +08:00
2018-12-23 19:07:01 +08:00
tagAlign.x = ['center', 'top']
tagAlign.y = ['right', 'middle']
tagAlign.ax = ['center', 'bottom']
2018-12-24 14:09:32 +08:00
tagAlign.ay = ['left', 'middle']
2018-12-20 22:36:22 +08:00
},
2018-12-20 18:25:49 +08:00
drawAxis () {
2018-12-23 19:07:01 +08:00
const { drawAxisLine, drawAxisTag, drawAxisUnit } = this
2018-12-20 18:25:49 +08:00
drawAxisLine()
drawAxisTag()
2018-12-20 22:36:22 +08:00
2018-12-23 19:07:01 +08:00
drawAxisUnit()
const { drawAxisGrid } = this
2018-12-21 14:46:55 +08:00
2018-12-23 19:07:01 +08:00
drawAxisGrid()
2018-12-20 18:25:49 +08:00
},
drawAxisLine () {
2019-01-16 16:56:11 +08:00
const { noAxisLine } = this
if (noAxisLine) return
2018-12-23 19:07:01 +08:00
const { ctx, horizon, axisOriginPos, axisAnglePos } = this
2018-12-20 18:25:49 +08:00
2019-01-16 16:56:11 +08:00
const { axisLineColor, agValueAxisTag, agLabelAxisTag } = this
2018-12-20 18:25:49 +08:00
2018-12-23 19:07:01 +08:00
const { data: { x, ax, y, ay } } = this
2018-12-20 18:25:49 +08:00
2018-12-21 14:46:55 +08:00
ctx.lineWidth = 1
2018-12-20 18:25:49 +08:00
2018-12-24 14:09:32 +08:00
if (!x || !x.noAxisLine) {
2019-01-16 16:56:11 +08:00
ctx.strokeStyle = (x && x.axisLineColor) || axisLineColor
2018-12-21 14:46:55 +08:00
ctx.beginPath()
ctx.moveTo(...axisOriginPos)
2018-12-23 19:07:01 +08:00
ctx.lineTo(...axisAnglePos.rightBottom)
2018-12-21 14:46:55 +08:00
ctx.stroke()
}
2018-12-20 18:25:49 +08:00
2018-12-24 14:09:32 +08:00
if (!y || !y.noAxisLine) {
2019-01-16 16:56:11 +08:00
ctx.strokeStyle = (y && y.axisLineColor) || axisLineColor
2018-12-21 14:46:55 +08:00
ctx.beginPath()
ctx.moveTo(...axisOriginPos)
2018-12-23 19:07:01 +08:00
ctx.lineTo(...axisAnglePos.leftTop)
ctx.stroke()
}
const agValueAxis = horizon ? ay : ax
if (agValueAxisTag.length && (!agValueAxis || !agValueAxis.noAxisLine)) {
2019-01-16 16:56:11 +08:00
ctx.strokeStyle = (agValueAxis && agValueAxis.axisLineColor) || axisLineColor
2018-12-23 19:07:01 +08:00
ctx.beginPath()
ctx.moveTo(...(horizon ? axisAnglePos.leftTop : axisAnglePos.rightTop))
ctx.lineTo(...(horizon ? axisAnglePos.rightTop : axisAnglePos.rightBottom))
ctx.stroke()
}
const agLebalAxis = horizon ? ax : ay
if (agLabelAxisTag.length && (!agLebalAxis || !agLebalAxis.noAxisLine)) {
2019-01-16 16:56:11 +08:00
ctx.strokeStyle = (agLebalAxis && agLebalAxis.axisLineColor) || axisLineColor
2018-12-23 19:07:01 +08:00
ctx.beginPath()
ctx.moveTo(...(horizon ? axisAnglePos.rightTop : axisAnglePos.leftTop))
ctx.lineTo(...(horizon ? axisAnglePos.rightBottom : axisAnglePos.rightTop))
2018-12-21 14:46:55 +08:00
ctx.stroke()
}
2018-12-20 18:25:49 +08:00
},
drawAxisTag () {
2019-01-16 16:56:11 +08:00
const { noAxisTag } = this
if (noAxisTag) return
2018-12-23 19:07:01 +08:00
const { horizon, tagAlign, defaultAxisLineTagGap: offset } = this
const { data: { x, ax, y, ay }, drawAxisSeriesTag } = this
2018-12-20 18:25:49 +08:00
2018-12-23 19:07:01 +08:00
const xAxis = horizon ? ['addBAValueAxisTag', 'valueAxisTagPos'] : ['addBALabelAxisTag', 'labelAxisTagPos']
const yAxis = horizon ? ['addBALabelAxisTag', 'labelAxisTagPos'] : ['addBAValueAxisTag', 'valueAxisTagPos']
const agXAxis = horizon ? ['addBAAGValueAxisTag', 'agValueAxisTagPos'] : ['addBAAGLabelAxisTag', 'agLabelAxisTagPos']
const agYAxis = horizon ? ['addBAAGLabelAxisTag', 'agLabelAxisTagPos'] : ['addBAAGValueAxisTag', 'agValueAxisTagPos']
2018-12-20 18:25:49 +08:00
2018-12-24 14:09:32 +08:00
if (!x || !x.noAxisTag) drawAxisSeriesTag(...xAxis.map(td => this[td]), x, tagAlign.x, [0, offset], x && x.rotate)
if (!y || !y.noAxisTag) drawAxisSeriesTag(...yAxis.map(td => this[td]), y, tagAlign.y, [-offset, 0])
if (!ax || !ax.noAxisTag) drawAxisSeriesTag(...agXAxis.map(td => this[td]), ax, tagAlign.ax, [0, -offset])
if (!ay || !ay.noAxisTag) drawAxisSeriesTag(...agYAxis.map(td => this[td]), ay, tagAlign.ay, [offset, 0])
2018-12-23 19:07:01 +08:00
},
drawAxisSeriesTag (tags, tagPos, { fontSize, fontFamily, tagColor } = {}, align, offset, rotate = false) {
2019-01-16 16:56:11 +08:00
const { ctx, axisFontSize, axisFontFamily, axisTagColor, drawColors } = this
2018-12-21 14:46:55 +08:00
2019-01-16 16:56:11 +08:00
let color = tagColor || axisTagColor
2018-12-21 14:46:55 +08:00
2018-12-23 19:07:01 +08:00
color === 'colors' && (color = drawColors)
2018-12-20 18:25:49 +08:00
2018-12-23 19:07:01 +08:00
const colorNum = color.length
2018-12-20 18:25:49 +08:00
2018-12-23 19:07:01 +08:00
const mulColor = color instanceof Array
2018-12-20 18:25:49 +08:00
2019-01-16 16:56:11 +08:00
ctx.font = `${fontSize || axisFontSize}px ${fontFamily || axisFontFamily}`
2018-12-20 18:25:49 +08:00
2018-12-23 19:07:01 +08:00
!mulColor && (ctx.fillStyle = color)
2018-12-20 18:25:49 +08:00
2018-12-23 19:07:01 +08:00
ctx.textAlign = align[0]
ctx.textBaseline = align[1]
2018-12-20 18:25:49 +08:00
2018-12-23 19:07:01 +08:00
tags.forEach((tag, i) => {
if (!tag && tag !== 0) return
2018-12-21 14:46:55 +08:00
2018-12-23 19:07:01 +08:00
const currentPos = [tagPos[i][0] + offset[0], tagPos[i][1] + offset[1]]
mulColor && (ctx.fillStyle = color[i % colorNum])
2018-12-21 14:46:55 +08:00
if (rotate) {
2018-12-24 14:09:32 +08:00
ctx.textAlign = 'left'
ctx.textBaseline = 'top'
2018-12-21 14:46:55 +08:00
ctx.save()
2018-12-23 19:07:01 +08:00
ctx.translate(...currentPos)
2018-12-24 18:25:00 +08:00
ctx.rotate(rotate * Math.PI / 180)
2018-12-21 14:46:55 +08:00
}
2018-12-23 19:07:01 +08:00
ctx.fillText(tag, ...(rotate ? [0, 0] : currentPos))
2018-12-21 14:46:55 +08:00
if (rotate) ctx.restore()
2018-12-20 18:25:49 +08:00
})
2018-12-23 19:07:01 +08:00
},
drawAxisUnit () {
2019-01-16 16:56:11 +08:00
const { noAxisTag } = this
if (noAxisTag) return
2018-12-23 19:07:01 +08:00
const { axisOriginPos, canvasWH, drawUnit, defaultAxisLineTagGap } = this
const { data: { x, ax, y, ay }, axisAnglePos } = this
if (x) {
const pos = [canvasWH[0], axisOriginPos[1] + defaultAxisLineTagGap]
drawUnit(x, pos, ['right', 'top'])
}
if (ax) {
const pos = [canvasWH[0], axisAnglePos.rightTop[1] - defaultAxisLineTagGap]
drawUnit(ax, pos, ['right', 'bottom'])
}
2018-12-20 18:25:49 +08:00
2018-12-23 19:07:01 +08:00
if (y) {
const pos = [axisOriginPos[0] - defaultAxisLineTagGap, 0]
drawUnit(y, pos, ['right', 'top'])
}
2018-12-20 18:25:49 +08:00
2018-12-23 19:07:01 +08:00
if (ay) {
const pos = [axisAnglePos.rightTop[0] + defaultAxisLineTagGap, 0]
drawUnit(ay, pos, ['left', 'top'])
}
},
drawUnit ({ unit, unitColor, fontSize, fontFamily }, pos, align) {
2019-01-16 16:56:11 +08:00
const { axisTagColor, axisFontSize, axisFontFamily } = this
2018-12-20 18:25:49 +08:00
2018-12-23 19:07:01 +08:00
const { ctx } = this
2018-12-20 18:25:49 +08:00
2018-12-23 19:07:01 +08:00
if (!unit) return
2018-12-20 18:25:49 +08:00
2019-01-16 16:56:11 +08:00
ctx.font = `${fontSize || axisFontSize}px ${fontFamily || axisFontFamily}`
2018-12-21 14:46:55 +08:00
2019-01-16 16:56:11 +08:00
ctx.fillStyle = unitColor || axisTagColor
2018-12-20 18:25:49 +08:00
2018-12-23 19:07:01 +08:00
ctx.textAlign = align[0]
ctx.textBaseline = align[1]
2018-12-20 18:25:49 +08:00
2018-12-23 19:07:01 +08:00
ctx.fillText(unit, ...pos)
2018-12-20 22:36:22 +08:00
},
2018-12-23 19:07:01 +08:00
drawAxisGrid () {
const { valueAxisTagPos, agValueAxisTagPos, labelAxisTagPos, agLabelAxisTagPos } = this
const { valueAxisTag, agValueAxisTag, labelAxisTag, agLabelAxisTag } = this
const { data: { x, ax, y, ay }, horizon, drawGrid, boundaryGap } = this
2018-12-21 14:46:55 +08:00
2018-12-23 19:07:01 +08:00
const xAxis = horizon ? [valueAxisTag, valueAxisTagPos] : [labelAxisTag, labelAxisTagPos]
2018-12-21 14:46:55 +08:00
2019-01-16 16:56:11 +08:00
let xBELineStatus = [false, false]
2018-12-24 14:09:32 +08:00
if (horizon) {
2019-01-16 16:56:11 +08:00
xBELineStatus = [true, false]
2018-12-24 14:09:32 +08:00
} else {
2019-01-16 16:56:11 +08:00
xBELineStatus = boundaryGap ? [false, false] : [true, true]
2018-12-24 14:09:32 +08:00
}
2019-01-16 16:56:11 +08:00
if (xAxis[0].length) drawGrid(x, ...xAxis, false, false, true, ...xBELineStatus)
2018-12-21 14:46:55 +08:00
2018-12-23 19:07:01 +08:00
const yAxis = horizon ? [labelAxisTag, labelAxisTagPos] : [valueAxisTag, valueAxisTagPos]
2018-12-21 14:46:55 +08:00
2018-12-23 19:07:01 +08:00
if (yAxis[0].length) drawGrid(y, ...yAxis, true, true, false, !horizon)
2018-12-21 14:46:55 +08:00
2018-12-23 19:07:01 +08:00
const agXAxis = horizon ? [agValueAxisTag, agValueAxisTagPos] : [agLabelAxisTag, agLabelAxisTagPos]
if (agXAxis[0].length) drawGrid(ax, ...agXAxis, false, false, false, ...(boundaryGap ? [false, false] : [true, true]))
const agYAxis = horizon ? [agLabelAxisTag, agLabelAxisTagPos] : [agValueAxisTag, agValueAxisTagPos]
if (agYAxis[0].length) drawGrid(ay, ...agYAxis, true, false, false, true)
2018-12-21 14:46:55 +08:00
},
2018-12-23 19:07:01 +08:00
drawGrid (axis = {}, gridTag, gridPos, horizon = true, right = true, top = true, noFirst = false, noLast = false) {
2019-01-16 16:56:11 +08:00
const { grid, gridLineColor: cGLC, gridLineDash: cGLD } = axis
2018-12-20 22:36:22 +08:00
2018-12-23 19:07:01 +08:00
if (!grid) return
2018-12-20 22:36:22 +08:00
2019-01-16 16:56:11 +08:00
const { gridLineColor, gridLineDash, defaultGridLineDash } = this
2018-12-20 22:36:22 +08:00
2018-12-23 19:07:01 +08:00
const { ctx, drawColors, axisWH } = this
2018-12-20 22:36:22 +08:00
2019-01-16 16:56:11 +08:00
let trueGridLineDash = gridLineDash
if (cGLD instanceof Array) {
trueGridLineDash = cGLD
} else if (cGLD === true) {
trueGridLineDash = defaultGridLineDash
}
2018-12-20 22:36:22 +08:00
2018-12-23 19:07:01 +08:00
ctx.setLineDash(trueGridLineDash)
2018-12-20 22:36:22 +08:00
2019-01-16 16:56:11 +08:00
let color = cGLC || gridLineColor
2018-12-20 22:36:22 +08:00
2018-12-23 19:07:01 +08:00
color === 'colors' && (color = drawColors)
2018-12-20 22:36:22 +08:00
2018-12-23 19:07:01 +08:00
const mulColor = color instanceof Array
2018-12-20 22:36:22 +08:00
2018-12-23 19:07:01 +08:00
const colorNum = color.length
2018-12-20 22:36:22 +08:00
2018-12-23 19:07:01 +08:00
!mulColor && (ctx.strokeStyle = color)
2018-12-20 22:36:22 +08:00
2018-12-23 19:07:01 +08:00
ctx.lineWidth = 1
2018-12-20 22:36:22 +08:00
2018-12-23 19:07:01 +08:00
const gridLastIndex = gridPos.length - 1
2018-12-20 22:36:22 +08:00
2018-12-23 19:07:01 +08:00
gridPos.forEach((pos, i) => {
if (!gridTag[i] && gridTag[i] !== 0) return
2018-12-20 22:36:22 +08:00
2018-12-23 19:07:01 +08:00
if (i === 0 && noFirst) return
2018-12-20 22:36:22 +08:00
2018-12-23 19:07:01 +08:00
if (i === gridLastIndex && noLast) return
2018-12-20 22:36:22 +08:00
ctx.beginPath()
2018-12-23 19:07:01 +08:00
mulColor && (ctx.strokeStyle = color[i % colorNum])
ctx.moveTo(...pos)
ctx.lineTo(...(horizon
? [right ? pos[0] + axisWH[0] : pos[0] - axisWH[0], pos[1]]
: [pos[0], top ? pos[1] - axisWH[1] : pos[1] + axisWH[1]]))
2018-12-20 22:36:22 +08:00
ctx.stroke()
})
2018-12-20 18:25:49 +08:00
}
}
}