123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133 |
- <template>
- <div ref="preResultRef" :style="{ height: height, width: width }"></div>
- </template>
- <script>
- import * as echarts from 'echarts'
- export default {
- name: 'PlayBackChart',
- props: {
- // chartRef: {
- // type: String,
- // required: true
- // },
- width: {
- type: String,
- default: '100%'
- },
- height: {
- type: String,
- default: '400px'
- },
- /**
- * chartData包括:
- * title:''
- * legendData:[]
- * xAxisData:{}
- * yAxisData:{}
- * seriesData:{}
- */
- chartData: {
- type: Object,
- required: true
- }
- },
- data() {
- return {
- chart: null
- }
- },
- watch: {
- chartData: {
- deep: true,
- handler(val) {
- // this.initChart()
- this.$nextTick(() => {
- this.initChart()
- this.setOptions(val)
- })
- }
- }
- },
- mounted() {
- this.$nextTick(() => {
- this.initChart()
- })
- },
- beforeDestroy() {
- if (!this.chart) {
- return
- }
- this.chart.dispose()
- this.chart = null
- },
- computed: {},
- methods: {
- initChart() {
- if (this.chart) {
- this.chart.dispose()
- this.chart = null
- }
- // console.log('66', this.chartRef)
- this.chart = echarts.init(this.$refs.preResultRef)
- // 监听 dataZoom 事件
- this.chart.on('dataZoom', (params) => {
- const startValue = this.chartData.xAxisData[params.batch[0].startValue]
- const endValue = this.chartData.xAxisData[params.batch[0].endValue]
- this.$emit('data-zoom', [startValue, endValue])
- })
- this.setOptions(this.chartData)
- },
- setOptions({ title, legendData, xAxisData, yAxisData, seriesData } = {}) {
- this.chart.setOption({
- title: {
- text: title
- },
- tooltip: {
- trigger: 'axis'
- },
- legend: {
- // data: legendData,
- textStyle: {
- //图例文字的样式
- color: '#fff'
- }
- },
- toolbox: {
- show: true,
- feature: {
- dataZoom: {
- yAxisIndex: 'none'
- },
- restore: {},
- saveAsImage: {}
- }
- },
- xAxis: {
- type: 'category',
- data: xAxisData
- },
- yAxis: {
- type: 'value'
- // data: yAxisData
- },
- dataZoom: [
- {
- type: 'slider',
- start: 0,
- end: 100
- },
- {
- type: 'inside',
- start: 0,
- end: 100
- }
- ],
- // series: series
- series: seriesData
- })
- }
- }
- }
- </script>
- <style lang="scss" scoped></style>
|