feat: 增加各图表之间的联动

This commit is contained in:
Xu Zhimeng 2023-07-06 19:40:48 +08:00
parent 6c02e52e23
commit 0482aebb12
4 changed files with 291 additions and 63 deletions

View File

@ -3,6 +3,10 @@
</template> </template>
<script> <script>
import * as echarts from 'echarts' import * as echarts from 'echarts'
const events = ['brushEnd']
const zrEvents = ['mousemove', 'mousedown', 'mouseup']
export default { export default {
props: { props: {
option: { option: {
@ -18,43 +22,51 @@ export default {
return {} return {}
}, },
mounted() { mounted() {
this.chart = echarts.init(this.$refs.containerRef) this._chart = echarts.init(this.$refs.containerRef)
this.chart.setOption(this.option) this._chart.setOption(this.option)
this.initEventListener() this.initEventListener()
}, },
methods: { methods: {
initEventListener() { initEventListener() {
events.forEach(eventName => {
this._chart.on(eventName, (params) => {
this.$emit(eventName, params)
})
})
const zr = this.getZRender() const zr = this.getZRender()
zr.on('mousemove', (params) => { zrEvents.forEach(eventName => {
this.$emit('zr:mousemove', params) zr.on(eventName, params => {
this.$emit(`zr:${eventName}`, params)
})
}) })
}, },
resize() { resize() {
this.chart && this.chart.resize() this._chart && this._chart.resize()
}, },
// echart // echart
getChartInstance() { getChartInstance() {
return this.chart return this._chart
}, },
getZRender() { getZRender() {
return this.chart.getZr() return this._chart.getZr()
} }
}, },
watch : { watch: {
option: { option: {
handler() { handler() {
if(this.chart) { if (this._chart) {
this.chart.setOption(this.option) this._chart.setOption(this.option)
} }
}, },
deep: true deep: true
}, },
height() { height() {
this.$nextTick(() => { this.$nextTick(() => {
this.chart && this.chart.resize() this._chart && this._chart.resize()
}) })
} }
} }

View File

@ -5,7 +5,12 @@
<template slot="title"> <template slot="title">
Beta-Gamma Spectrum: Sample Beta-Gamma Spectrum: Sample
</template> </template>
<beta-gamma-spectrum-chart ref="betaGammaChartRef" :data="twoDData" @positionChange="handlePositionChange" /> <beta-gamma-spectrum-chart
ref="betaGammaChartRef"
:data="twoDData"
@positionChange="handlePositionChange"
@rangeChange="handleRangeChange"
/>
</beta-gamma-chart-container> </beta-gamma-chart-container>
</div> </div>
<div class="beta-and-gamma-spectrum"> <div class="beta-and-gamma-spectrum">
@ -16,7 +21,7 @@
<template slot="title"> <template slot="title">
Gamma Spectrum: Original Gamma Spectrum: Original
</template> </template>
<spectrum-line-chart ref="lineChart1Ref" /> <spectrum-line-chart ref="lineChart1Ref" @rangeChange="handleLineChartRangeChange($event, 'y')" />
</beta-gamma-chart-container> </beta-gamma-chart-container>
</div> </div>
<div class="gamma-spectrum-item"> <div class="gamma-spectrum-item">
@ -24,7 +29,7 @@
<template slot="title"> <template slot="title">
Gamma Spectrum: Projected Gamma Spectrum: Projected
</template> </template>
<spectrum-line-chart ref="lineChart2Ref" /> <spectrum-line-chart ref="lineChart2Ref" @rangeChange="handleLineChartRangeChange($event, 'y')" />
</beta-gamma-chart-container> </beta-gamma-chart-container>
</div> </div>
</div> </div>
@ -34,7 +39,12 @@
<template slot="title"> <template slot="title">
Beta Spectrum: Original Beta Spectrum: Original
</template> </template>
<spectrum-line-chart ref="lineChart3Ref" title="Beta" color="#00ff1e" /> <spectrum-line-chart
ref="lineChart3Ref"
title="Beta"
color="#00ff1e"
@rangeChange="handleLineChartRangeChange($event, 'x')"
/>
</beta-gamma-chart-container> </beta-gamma-chart-container>
</div> </div>
<div class="gamma-spectrum-item"> <div class="gamma-spectrum-item">
@ -42,7 +52,12 @@
<template slot="title"> <template slot="title">
Beta Spectrum: Projected Beta Spectrum: Projected
</template> </template>
<spectrum-line-chart ref="lineChart4Ref" title="Beta" color="#00ff1e" /> <spectrum-line-chart
ref="lineChart4Ref"
title="Beta"
color="#00ff1e"
@rangeChange="handleLineChartRangeChange($event, 'x')"
/>
</beta-gamma-chart-container> </beta-gamma-chart-container>
</div> </div>
</div> </div>
@ -142,6 +157,34 @@ export default {
// Beta Spectrumbata-gammabata channelx // Beta Spectrumbata-gammabata channelx
this.$refs.lineChart3Ref.setLinePosition(xAxis) this.$refs.lineChart3Ref.setLinePosition(xAxis)
this.$refs.lineChart4Ref.setLinePosition(xAxis) this.$refs.lineChart4Ref.setLinePosition(xAxis)
},
// 2d
handleRangeChange([x1, x2, y1, y2]) {
this.$refs.lineChart1Ref.setRange(y1, y2)
this.$refs.lineChart2Ref.setRange(y1, y2)
this.$refs.lineChart3Ref.setRange(x1, x2)
this.$refs.lineChart4Ref.setRange(x1, x2)
},
/**
* 右侧折线图表刷选时
* @param {number[]} range 范围
* @param {'x'|'y'} type 类型
**/
handleLineChartRangeChange([x1, x2], type) {
if (type == 'y') {
// gamma channel
this.$refs.lineChart1Ref.setRange(x1, x2)
this.$refs.lineChart2Ref.setRange(x1, x2)
} else if (type == 'x') {
this.$refs.lineChart3Ref.setRange(x1, x2)
this.$refs.lineChart4Ref.setRange(x1, x2)
}
this.$refs.betaGammaChartRef.setRange(x1, x2, type)
} }
} }
} }

View File

@ -13,8 +13,14 @@
</div> </div>
<div class="beta-gamma-spectrum-chart-main"> <div class="beta-gamma-spectrum-chart-main">
<!-- 2D 图表 --> <!-- 2D 图表 -->
<div class="_2d-chart" v-if="active == 0"> <div class="_2d-chart" v-show="active == 0">
<custom-chart ref="chartRef" :option="twoDOption" @zr:mousemove="handleMouseMove" /> <custom-chart
ref="chartRef"
:option="twoDOption"
@zr:mousemove="handleMouseMove"
@zr:mousedown="handleMouseDown"
@brushEnd="handleBrushEnd"
/>
<div class="bar"> <div class="bar">
<color-palette v-model="currCount" :maxValue="4" /> <color-palette v-model="currCount" :maxValue="4" />
<div>{{ currCount + 1 }}</div> <div>{{ currCount + 1 }}</div>
@ -123,7 +129,8 @@ const twoDOption = {
itemStyle: { itemStyle: {
color: '#fff' color: '#fff'
} }
} },
brush: {}
} }
//3D Surface //3D Surface
@ -284,6 +291,10 @@ export default {
} }
}, },
mounted() {
this.twoDOption.brush = { toolbox: [] }
},
methods: { methods: {
// Beta-Gamma Spectrum: Sample // Beta-Gamma Spectrum: Sample
handleChange(index) { handleChange(index) {
@ -292,7 +303,12 @@ export default {
// unzoom // unzoom
handleUnzoom() { handleUnzoom() {
console.log('%c [ handleUnzoom ]-309', 'font-size:13px; background:pink; color:#bf2c9f;') this.twoDOption.xAxis.min = 0
this.twoDOption.xAxis.max = 256
this.twoDOption.yAxis.min = 0
this.twoDOption.yAxis.max = 256
this.emitRangeChange([0, 256, 0, 256])
}, },
resize() { resize() {
@ -301,12 +317,81 @@ export default {
this.$refs._3dScannerRef && this.$refs._3dScannerRef.resize() this.$refs._3dScannerRef && this.$refs._3dScannerRef.resize()
}, },
//
handleMouseMove(param) { handleMouseMove(param) {
const { offsetX, offsetY } = param const { offsetX, offsetY } = param
const point = getXAxisAndYAxisByPosition(this.$refs.chartRef.getChartInstance(), offsetX, offsetY) const point = getXAxisAndYAxisByPosition(this.$refs.chartRef.getChartInstance(), offsetX, offsetY)
this.$emit('positionChange', point ? [point[0].toFixed(), point[1].toFixed()] : [null, null]) this.$emit('positionChange', point ? [point[0].toFixed(), point[1].toFixed()] : [null, null])
}, },
//
handleMouseDown() {
const chart = this.$refs.chartRef.getChartInstance()
chart.dispatchAction({
type: 'takeGlobalCursor',
//
key: 'brush',
brushOption: {
// brush brushType false
brushType: 'rect'
}
})
},
//
handleBrushEnd(param) {
const chart = this.$refs.chartRef.getChartInstance()
const areas = param.areas[0]
if (areas) {
const range = areas.range
const [[minX, maxX], [minY, maxY]] = range
const point1 = chart.convertFromPixel({ seriesIndex: 0 }, [minX, minY]).map(num => parseInt(num.toFixed()))
const point2 = chart.convertFromPixel({ seriesIndex: 0 }, [maxX, maxY]).map(num => parseInt(num.toFixed()))
const [x1, y2, x2, y1] = [...point1, ...point2] //
this.twoDOption.xAxis.min = x1
this.twoDOption.xAxis.max = x2
this.twoDOption.yAxis.min = y1
this.twoDOption.yAxis.max = y2
this.emitRangeChange([x1, x2, y1, y2])
}
//
chart.dispatchAction({
type: 'brush',
areas: []
})
//
chart.dispatchAction({
type: 'takeGlobalCursor'
})
},
//
emitRangeChange(range) {
this.$emit('rangeChange', range)
},
/**
* 设置范围
* @param {number} x1
* @param {number} x2
* @param {'x' | 'y'} type 要改变哪条轴
*/
setRange(x1, x2, type) {
if (type == 'x') {
this.twoDOption.xAxis.min = x1
this.twoDOption.xAxis.max = x2
} else if (type == 'y') {
this.twoDOption.yAxis.min = x1
this.twoDOption.yAxis.max = x2
}
},
// //
interpolateColor(color1, color2, percentage) { interpolateColor(color1, color2, percentage) {
const r = color1.r + (color2.r - color1.r) * percentage const r = color1.r + (color2.r - color1.r) * percentage

View File

@ -1,13 +1,25 @@
<template> <template>
<div class="spectrum-line-chart"> <div class="spectrum-line-chart">
<div class="title">{{ title + ' Count' }}</div> <div class="calculation">
<custom-chart <span>Channel: 136</span>
class="spectrum-line-chart-main" <span>Count: 1475</span>
ref="chartRef" <span class="error">Energy: 381.409</span>
:option="option" </div>
style="height: 100%" <div class="chart-container">
@zr:mousemove="handleMouseMove" <div class="left-title">{{ title + ' Count' }}</div>
></custom-chart> <custom-chart
class="spectrum-line-chart-main"
ref="chartRef"
:option="option"
style="height: 100%"
@zr:mousemove="handleMouseMove"
@zr:mousedown="handleMouseDown"
@brushEnd="handleBrushEnd"
></custom-chart>
</div>
<div class="bottom-title">
{{ title + ' Channel' }}
</div>
</div> </div>
</template> </template>
@ -18,26 +30,9 @@ import { getXAxisAndYAxisByPosition } from '@/utils/chartHelper.js'
const initialOption = { const initialOption = {
grid: { grid: {
top: 25, top: 10,
right: 12, right: 15,
bottom: 40 bottom: 20
},
title: {
text: '',
left: 'right',
top: 0,
textStyle: {
color: '#ade6ee',
fontSize: 12,
rich: {
a: {
padding: [0, 27, 0, 0]
},
b: {
color: '#ff5656'
}
}
}
}, },
xAxis: { xAxis: {
min: 0, min: 0,
@ -83,6 +78,9 @@ const initialOption = {
color: 'rgba(119, 181, 213, .2)' color: 'rgba(119, 181, 213, .2)'
} }
}, },
axisTick: {
show: false
},
axisLabel: { axisLabel: {
color: '#ade6ee' color: '#ade6ee'
} }
@ -112,7 +110,8 @@ const initialOption = {
silent: true, silent: true,
data: [] data: []
} }
} },
brush: {}
} }
export default { export default {
@ -131,13 +130,15 @@ export default {
}, },
data() { data() {
const option = cloneDeep(initialOption) const option = cloneDeep(initialOption)
option.title.text = `{a|Channel: 136}{a|Count: 1475}{b|Energy: 381.409}`
option.series.itemStyle.color = this.color option.series.itemStyle.color = this.color
option.xAxis.name = this.title + ' Channel' option.xAxis.name = this.title + ' Channel'
return { return {
option option
} }
}, },
mounted() {
this.option.brush = { toolbox: [] }
},
methods: { methods: {
resize() { resize() {
this.$refs.chartRef && this.$refs.chartRef.resize() this.$refs.chartRef && this.$refs.chartRef.resize()
@ -154,10 +155,66 @@ export default {
}, 0) }, 0)
}, },
//
setRange(min, max) {
this.option.xAxis.min = min
this.option.xAxis.max = max
},
//
handleMouseMove(param) { handleMouseMove(param) {
const { offsetX, offsetY } = param const { offsetX, offsetY } = param
const point = getXAxisAndYAxisByPosition(this.$refs.chartRef.getChartInstance(), offsetX, offsetY) const point = getXAxisAndYAxisByPosition(this.$refs.chartRef.getChartInstance(), offsetX, offsetY)
this.setLinePosition(point ? point[0].toFixed() : null) this.setLinePosition(point ? point[0].toFixed() : null)
},
//
handleMouseDown() {
const chart = this.$refs.chartRef.getChartInstance()
chart.dispatchAction({
type: 'takeGlobalCursor',
//
key: 'brush',
brushOption: {
// brush brushType false
brushType: 'lineX'
}
})
},
//
handleBrushEnd(param) {
const chart = this.$refs.chartRef.getChartInstance()
const areas = param.areas[0]
if (areas) {
const range = areas.range
const [minX, maxX] = range
const point1 = chart.convertFromPixel({ seriesIndex: 0 }, [minX, 0])
const point2 = chart.convertFromPixel({ seriesIndex: 0 }, [maxX, 0])
const [x1, x2] = [parseInt(point1[0].toFixed()), parseInt(point2[0].toFixed())] //
this.option.xAxis.min = x1
this.option.xAxis.max = x2
this.emitRangeChange([x1, x2])
}
//
chart.dispatchAction({
type: 'brush',
areas: []
})
//
chart.dispatchAction({
type: 'takeGlobalCursor'
})
},
emitRangeChange(range) {
this.$emit('rangeChange', range)
} }
} }
} }
@ -165,23 +222,54 @@ export default {
<style lang="less" scoped> <style lang="less" scoped>
.spectrum-line-chart { .spectrum-line-chart {
display: flex;
height: 100%; height: 100%;
.title { .calculation {
writing-mode: vertical-rl; line-height: 10px;
color: #5b9cba; text-align: right;
font-size: 16px; font-size: 14px;
transform: rotate(180deg);
text-align: center; .error {
user-select: none; color: #ff5656;
white-space: nowrap; }
overflow: hidden;
text-overflow: ellipsis; span {
color: #ade6ee;
&:not(:last-child) {
margin-right: 27px;
}
}
} }
&-main { .chart-container {
flex: 1; display: flex;
margin-top: 7px;
margin-bottom: 8px;
height: calc(100% - 35px);
.left-title {
writing-mode: vertical-rl;
color: #5b9cba;
font-size: 14px;
transform: rotate(180deg);
text-align: center;
user-select: none;
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
}
.spectrum-line-chart-main {
flex: 1;
}
}
.bottom-title {
line-height: 10px;
font-size: 14px;
color: #5b9cba;
text-align: center;
} }
} }
</style> </style>