NEZ-3211 feat: 时序图表 数据正常加载 以及 legend重构完成

This commit is contained in:
zhangyu
2023-11-03 11:12:05 +08:00
parent c4d15e4826
commit d204546303
5 changed files with 200 additions and 82 deletions

View File

@@ -157,51 +157,27 @@ export default {
this.clickLegendD3(legendName, index, hasGrey, curIsGrey, currentIsTheOnlyOneHighlight) this.clickLegendD3(legendName, index, hasGrey, curIsGrey, currentIsTheOnlyOneHighlight)
return return
} }
if (echarts) { if (echarts) {
// 判断timeSeries类型图表 先取消多表联动
if (this.isTimeSeries && (this.isConnect && this.isConnect !== 'none')) {
chart.disconnect('timeSeriesGroup')
}
if (!hasGrey) { // 1.除当前legend外全置灰 if (!hasGrey) { // 1.除当前legend外全置灰
echarts.dispatchAction({ echarts.series.forEach((seriesItem, sindex) => {
type: 'legendInverseSelect' if (sindex > 0) {
}) seriesItem.show = false
echarts.dispatchAction({ }
type: 'legendSelect',
name: legendName
}) })
echarts.series[index + 1].show = !echarts.series[index + 1].show
this.isGrey = this.isGrey.map((g, i) => i !== index) this.isGrey = this.isGrey.map((g, i) => i !== index)
} else if (currentIsTheOnlyOneHighlight) { // 2.全高亮 } else if (currentIsTheOnlyOneHighlight) { // 2.全高亮
echarts.dispatchAction({ echarts.series.forEach((seriesItem, sindex) => {
type: 'legendAllSelect' if (sindex > 0) {
echarts.series[sindex].show = true
}
}) })
this.isGrey = this.isGrey.map(() => false) this.isGrey = this.isGrey.map(() => false)
} else { } else {
const type = curIsGrey ? 'legendSelect' : 'legendUnSelect' echarts.series[index + 1].show = !echarts.series[index + 1].show
echarts.dispatchAction({
type: type,
name: legendName
})
this.$set(this.isGrey, index, !this.isGrey[index]) this.$set(this.isGrey, index, !this.isGrey[index])
} }
if (this.isTimeSeries) { echarts.redraw()
this.$parent.legendChange(this.isGrey)
}
// 点击后 处理Y轴的刻度边的
const chartInfo = this.chartInfo
const series = this.$lodash.cloneDeep(this.series)
const dataArg = series.filter((seriesItem, seriesIndex) => !this.isGrey[seriesIndex])
const option = this.renderYAxis(dataArg, chartInfo, 'legend')
getChart(this.chartId) && getChart(this.chartId).setOption({
yAxis: [
...option.yAxis
]
})
// 判断timeSeries类型图表 建立多表联动
if (this.isTimeSeries && (this.isConnect && this.isConnect !== 'none')) {
chart.connect('timeSeriesGroup')
}
} }
}, },
clickLegendBar (legendName, index, hasGrey, curIsGrey, currentIsTheOnlyOneHighlight) { clickLegendBar (legendName, index, hasGrey, curIsGrey, currentIsTheOnlyOneHighlight) {
@@ -271,6 +247,7 @@ export default {
if (this.isGrey[index]) { if (this.isGrey[index]) {
return false return false
} }
return
if (this.chartInfo.type === 'pie' || this.chartInfo.type === 'doughnut' || this.chartInfo.type === 'rose') { if (this.chartInfo.type === 'pie' || this.chartInfo.type === 'doughnut' || this.chartInfo.type === 'rose') {
this.$emit('hoverLegendD3', legendName, index, type) this.$emit('hoverLegendD3', legendName, index, type)
} else if (this.isTimeSeries) { } else if (this.isTimeSeries) {

View File

@@ -203,3 +203,11 @@ export function initColor (colorNum = 20) {
} }
return colorList return colorList
} }
export function initIncrs () {
const arr = []
for (let i = 0; i < 53; i++) {
arr.push(Math.pow(2, i))
}
return arr
}
export const Incrs = initIncrs()

View File

@@ -15,6 +15,7 @@
</div> </div>
<chart-legend <chart-legend
ref="legend" ref="legend"
style="flex-shrink: 0"
v-if="hasLegend" v-if="hasLegend"
:chart-data="chartData" :chart-data="chartData"
:chart-info="chartInfo" :chart-info="chartInfo"
@@ -57,10 +58,11 @@ import legend from '../legend'
import chartMixin from '@/components/chart/chartMixin' import chartMixin from '@/components/chart/chartMixin'
import renderChart from '@/components/chart/renderChart' import renderChart from '@/components/chart/renderChart'
import { chartLegendPlacement } from '@/components/common/js/constants' import { chartLegendPlacement } from '@/components/common/js/constants'
import { chartCache, getChart } from '@/components/common/js/common' import { getChart, setChart, chartCache } from '@/components/common/js/common'
import { initColor } from '@/components/chart/chart/tools' import { initColor } from '@/components/chart/chart/tools'
import UPlot from 'uplot/dist/uPlot.esm' import UPlot from 'uplot/dist/uPlot.cjs'
import chartTimeSeriesMixin from '@/components/chart/chart/uplot/chartTimeSeriesMixin' import chartTimeSeriesMixin from '@/components/chart/chart/uplot/chartTimeSeriesMixin'
import chartDataFormat from '@/components/chart/chartDataFormat'
export default { export default {
name: 'chartTimeSeries', name: 'chartTimeSeries',
components: { components: {
@@ -160,42 +162,125 @@ export default {
methods: { methods: {
initChart (chartOptions = this.chartOption) { initChart (chartOptions = this.chartOption) {
console.log(this.chartData, this.chartInfo) console.log(this.chartData, this.chartInfo)
const leftUnit = this.chartInfo.unit
const leftUnitCompute = chartDataFormat.getUnit(leftUnit)
const asciiType = leftUnitCompute.ascii
console.log(asciiType)
const incrs = asciiType == 1024 ? this.incrs : undefined
console.log(incrs, 'incrs')
const decimals = this.chartInfo.param.decimals || 2
// .compute(minusVal, null, -1, decimals)
const opts = { const opts = {
id: `chart-canvas-${this.chartId}-uplot`, id: `chart-canvas-${this.chartId}-uplot`,
width: 400, width: 100,
height: 400, height: 100,
padding: [15, this.autoPadRight, 15, 15],
legend: { legend: {
show: false, show: false
live: false,
isolate: true
}, },
series: [ series: [
{},
{ {
name: this.chartData[0][0].elements.name,
label: this.chartData[0][0].elements.name,
scale: 'mb',
value: (u, v) => v == null ? null : v.toFixed(2) + ' MB',
stroke: 'green',
width: 1 / devicePixelRatio
}
}
], ],
axes: [ axes: [
{}, {},
{ {
scale: 'mb', scale: 'left',
values: (u, vals, space) => vals.map(v => +v.toFixed(2) + ' MB') values: (u, vals, space) => vals.map(v => leftUnitCompute.compute(v, null, -1, decimals)),
formatValue: (v, d) => {
console.log(v, d, 'vd')
return v
},
incrs: incrs,
size (self, values, axisIdx, cycleNum) {
const axis = self.axes[axisIdx]
// bail out, force convergence
if (cycleNum > 1) { return axis._size }
let axisSize = axis.ticks.size + axis.gap
// find longest value
const longestVal = (values ?? []).reduce((acc, val) => (
val.length > acc.length ? val : acc
), '')
if (longestVal != '') {
self.ctx.font = axis.font[0]
axisSize += self.ctx.measureText(longestVal).width / devicePixelRatio
}
return Math.ceil(axisSize)
}
} }
] ]
} }
const seriesData = this.initSeriesData(this.chartData) const { seriesData, seriesAll } = this.initSeriesData(this.chartData)
const data = [ console.log(seriesData, seriesAll)
this.chartData[0][0].values.map(item => item[0]), const data = seriesData
this.chartData[0][0].values.map(item => Number(item[1]) / 1000000).splice(0, 20) opts.series.push(...seriesAll)
] this.series = seriesAll
console.log(data) setTimeout(() => { // 延迟加载 保证legend的高度正常
const dom = document.getElementById(`chart-canvas-${this.chartId}`)
const width = dom.offsetWidth
const height = dom.offsetHeight
console.log(width, height)
opts.width = width
opts.height = height
this.chart = new UPlot(opts, data, document.getElementById(`chart-canvas-${this.chartId}`)) this.chart = new UPlot(opts, data, document.getElementById(`chart-canvas-${this.chartId}`))
setChart(this.chartId, this.chart)
}, 100)
},
size (self, values, axisIdx, cycleNum) {
const axis = self.axes[axisIdx]
// bail out, force convergence
if (cycleNum > 1) { return axis._size }
let axisSize = axis.ticks.size + axis.gap
// find longest value
const longestVal = (values ?? []).reduce((acc, val) => (
val.length > acc.length ? val : acc
), '')
if (longestVal != '') {
self.ctx.font = axis.font[0]
axisSize += self.ctx.measureText(longestVal).width / devicePixelRatio
}
return Math.ceil(axisSize)
},
autoPadRight (self, side, sidesWithAxes, cycleNum) {
const xAxis = self.axes[0]
const xVals = xAxis._values
if (xVals != null) {
// bail out, force convergence
if (cycleNum > 2) { return self._padding[1] }
const xSplits = xAxis._splits
const rightSplit = xSplits[xSplits.length - 1]
const rightSplitCoord = self.valToPos(rightSplit, 'x')
const leftPlotEdge = (self.bbox.left / devicePixelRatio)
const rightPlotEdge = leftPlotEdge + (self.bbox.width / devicePixelRatio)
const rightChartEdge = rightPlotEdge + self._padding[1]
const pxPerChar = 8
const rightVal = xVals[xVals.length - 1] + ''
const valHalfWidth = pxPerChar * (rightVal.length / 2)
const rightValEdge = leftPlotEdge + rightSplitCoord + valHalfWidth
if (rightValEdge >= rightChartEdge) {
return rightValEdge - rightPlotEdge
}
}
// default size
return 8
}, },
clickout () { clickout () {
if (this.toolbox.show) { if (this.toolbox.show) {

View File

@@ -1,12 +1,78 @@
import { initColor, Incrs } from '@/components/chart/chart/tools'
import { randomcolor } from '@/components/common/js/radomcolor/randomcolor'
import chartDataFormat from '@/components/chart/chartDataFormat'
export default { export default {
data () {
return {
seriesColor: initColor(),
incrs: Incrs
}
},
methods: { methods: {
initSeriesData (chartData) { initSeriesData (chartData) { // 处理数据 以及 series
const time = [] let time = []
const seriesData = [] const seriesData = [] // 返回的数据
const seriesAll = [ // 返回的 series
]
let chartIndex = 0
chartData.forEach(series => { // 首先处理时间 对应点没有值不包括null 则添加undefind chartData.forEach(series => { // 首先处理时间 对应点没有值不包括null 则添加undefind
console.log(series); series.forEach(item => {
time.push(...item.values.map(value => value[0]))
}) })
return seriesData })
const timeSet = new Set(time)
time = [...timeSet]
time = this.$lodash.sortBy(time, function (o) { return o })
const ObjTime = {}
time.forEach(item => {
ObjTime[item] = undefined
})
chartData.forEach(series => { // 首先处理时间 对应点没有值不包括null 则添加undefind
series.forEach(item => {
let seriesObjTime = {}
item.values.forEach(value => {
seriesObjTime[value[0]] = value[1]
})
// 处理统计数据 statistics
seriesObjTime = {
...ObjTime,
...seriesObjTime
}
console.log(seriesObjTime, seriesObjTime)
seriesData.push(Object.keys(seriesObjTime).map(time => seriesObjTime[time]))
seriesAll.push(this.renderSeries(item, chartIndex))
chartIndex++
})
})
seriesData.unshift(time)
return {
seriesData,
seriesAll
}
},
renderSeries (series, chartIndex) {
const leftUnit = this.chartInfo.unit
const leftUnitCompute = chartDataFormat.getUnit(leftUnit)
const decimals = this.chartInfo.param.decimals || 2
if (chartIndex > 19) {
this.seriesColor.push(randomcolor())
}
const obj = {
name: series.elements.name + JSON.stringify(series.metric),
label: series.elements.name + JSON.stringify(series.metric),
class: series.elements.name + JSON.stringify(series.metric),
scale: 'left', // right
// value: (u, v) => v == null ? null : (!v ? v : leftUnitCompute.compute(v, null, -1, decimals)),
values: (u, v) => series.elements.name + JSON.stringify(series.metric),
stroke: this.seriesColor[chartIndex],
width: 1 / devicePixelRatio
}
const name = series.elements.name + JSON.stringify(series.metric)
const alias = series.elements.name + JSON.stringify(series.metric)
const statistics = series.statistics
this.legends.push({ name, alias, statistics, color: this.seriesColor[chartIndex] })
return obj
} }
} }
} }

View File

@@ -326,15 +326,6 @@ export default {
}, },
mouseEnterChart () { mouseEnterChart () {
const myChart = getChart(this.chartId) const myChart = getChart(this.chartId)
if (myChart) {
setTimeout(() => {
myChart.setOption({
toolbox: {
show: true
}
})
}, 300)
}
}, },
tooltipPosition (point, params, dom, rect, size) { tooltipPosition (point, params, dom, rect, size) {
if (this.isConnect === 'tooltip' && this.$store.state.panel.currentMousemove != this.chartId) { if (this.isConnect === 'tooltip' && this.$store.state.panel.currentMousemove != this.chartId) {
@@ -370,15 +361,6 @@ export default {
}, },
mouseLeaveChart () { mouseLeaveChart () {
const myChart = getChart(this.chartId) const myChart = getChart(this.chartId)
if (myChart) {
setTimeout(() => {
myChart.setOption({
toolbox: {
show: false
}
})
}, 300)
}
}, },
getMaxValue (dataArg, chartInfo) { getMaxValue (dataArg, chartInfo) {
let maxValue = 0 let maxValue = 0