Charts.vue 5.5 KB
<!--
 * @Author: 前端 qianduan01@didihu.com.cn
 * @Date: 2022-10-25 10:12:26
 * @LastEditors: huyue
 * @LastEditTime: 2023-07-04 18:05:08
 * @FilePath: \dj-data-view\src\components\ViewTitle.vue
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
-->
<template>
  <div>
    <div>测试图</div>
    <div id="demo" style="width: 100vh;height: 100%;"></div>
  </div>
</template>

<script type="text/ecmascript-6">
import * as echarts from 'echarts'
export default {
  name: 'Charts',
  data() {
    return {}
  },
  mounted() {
    var chartDom = document.getElementById('demo')
    var myChart = echarts.init(chartDom)
    var option
    const xAxisData = []
    const data1 = []
    const data2 = []
    for (let i = 0; i < 10; i++) {
      xAxisData.push('Class' + i)
      data1.push(+(Math.random() * 2).toFixed(2))
      data2.push(+(Math.random() * 5).toFixed(2))
    }
    var emphasisStyle = {
      itemStyle: {
        shadowBlur: 10,
        shadowColor: 'rgba(0,0,0,0.3)'
      }
    }
    option = {
      legend: {
        data: ['bar', 'bar2', 'line'],
        left: '10%'
      },
      brush: {
        toolbox: ['rect', 'polygon', 'lineX', 'lineY', 'keep', 'clear'],
        xAxisIndex: 0
      },
      toolbox: {
        feature: {
          magicType: {
            type: ['stack']
          },
          dataView: {}
        }
      },
      tooltip: {},
      xAxis: {
        data: xAxisData,
        name: 'X Axis',
        axisLine: { onZero: true },
        splitLine: { show: false },
        splitArea: { show: false }
      },
      yAxis: {},
      grid: {
        bottom: 100
      },
      series: [
        {
          name: 'bar',
          type: 'bar',
          stack: 'one',
          emphasis: emphasisStyle,
          data: data1,
          label: {
            show: true
          },
          itemStyle: {
            barBorderRadius: [2, 2, 0, 0], // 柱体圆角
            color: function(params) {
              console.log(params)
              const colorList = [
                ['#22D8FF', '#5C56FF'],
                ['#2AD190', '#00B7DF'],
                ['#22D8FF', '#5C56FF'],
                ['#2AD190', '#00B7DF'],
                ['#22D8FF', '#5C56FF'],
                ['#2AD190', '#00B7DF'],
                ['#22D8FF', '#5C56FF'],
                ['#2AD190', '#00B7DF'],
                ['#22D8FF', '#5C56FF'],
                ['#2AD190', '#00B7DF'],
                ['#22D8FF', '#5C56FF'],
                ['#2AD190', '#00B7DF']
              ]
              var colorItem = colorList[params.dataIndex]
              return new echarts.graphic.LinearGradient(
                0, 0, 0, 1, [{ // 代表渐变色从正上方开始
                  offset: 0, // offset范围是0~1,用于表示位置,0是指0%处的颜色
                  color: colorItem[0]
                }, // 柱图渐变色
                {
                  offset: 1, // 指100%处的颜色
                  color: colorItem[1]
                }]
              )
            }
          }
        },
        {
          name: 'bar2',
          type: 'bar',
          stack: 'one',
          label: {
            show: true
          },
          emphasis: emphasisStyle,
          data: data2,
          itemStyle: {
            barBorderRadius: [2, 2, 0, 0], // 柱体圆角
            color: function(params) {
              console.log(params)
              const colorList = [
                ['#11627A', '#282F7A'],
                ['#145F4E', '#035972'],
                ['#11627A', '#282F7A'],
                ['#145F4E', '#035972'],
                ['#11627A', '#282F7A'],
                ['#145F4E', '#035972'],
                ['#11627A', '#282F7A'],
                ['#145F4E', '#035972'],
                ['#11627A', '#282F7A'],
                ['#145F4E', '#035972'],
                ['#11627A', '#282F7A'],
                ['#145F4E', '#035972']
              ]
              var colorItem = colorList[params.dataIndex]
              return new echarts.graphic.LinearGradient(
                0, 0, 0, 1, [{ // 代表渐变色从正上方开始
                  offset: 0, // offset范围是0~1,用于表示位置,0是指0%处的颜色
                  color: colorItem[0]
                }, // 柱图渐变色
                {
                  offset: 1, // 指100%处的颜色
                  color: colorItem[1]
                }]
              )
            }
          }
        },
        {
          name: 'Dynamic Line',
          type: 'line',
          data: data2
        }
      ]
    }
    myChart.on('brushSelected', function(params) {
      var brushed = []
      var brushComponent = params.batch[0]
      for (var sIdx = 0; sIdx < brushComponent.selected.length; sIdx++) {
        var rawIndices = brushComponent.selected[sIdx].dataIndex
        brushed.push('[Series ' + sIdx + '] ' + rawIndices.join(', '))
      }
      myChart.setOption({
        title: {
          backgroundColor: '#333',
          text: 'SELECTED DATA INDICES: \n' + brushed.join('\n'),
          bottom: 0,
          right: '10%',
          width: 100,
          textStyle: {
            fontSize: 12,
            color: '#fff'
          }
        }
      })
    })
    option && myChart.setOption(option)
  }
}
</script>

<style scoped lang="scss">
</style>