vue 使用 echarts5

1.安装

npm install echarts

2.main.js 导入

// 引入 echarts 核心模块,核心模块提供了 echarts 使用必须要的接口。
import * as echarts from 'echarts/core'
// 引入图表后缀都为 Chart
import { BarChart, LineChart, PieChart, GaugeChart } from 'echarts/charts'
// 引入需要的组件,后缀都为 Component
import { GridComponent, TooltipComponent, LegendComponent, TitleComponent } from 'echarts/components'
// 引入 Canvas 渲染器,注意引入 CanvasRenderer 或者 SVGRenderer 是必须的一步
import { CanvasRenderer } from 'echarts/renderers'
// 注册必须的组件
echarts.use(
  [CanvasRenderer, BarChart, LineChart, PieChart, GaugeChart, GridComponent, TooltipComponent, LegendComponent, TitleComponent]
)

3. 使用

3.1 饼图

在这里插入图片描述

<template>
  <div class="op-data">
    <div :id="chartId" :style="{width: chartWidth, height: chartHeight}" />
  </div>
</template>

<script>
import * as echarts from 'echarts'
export default {
  name: 'OpData',
  props: {
    data: {
      type: Array,
      default: () => {
        return [
          { value: 1048, name: 'Search Engine' },
          { value: 735, name: 'Direct' },
          { value: 580, name: 'Email' },
          { value: 484, name: 'Union Ads' },
          { value: 300, name: 'Video Ads' }
        ]
      }
    }
  },
  data () {
    return {
      chartId: 'op-data',
      chartWidth: '',
      chartHeight: '300px',
      chartData: null
    }
  },
  watch: {
    data: {
      immediate: true,
      deep: true,
      handler: function (newVal, oldVal) {
        if (newVal && newVal.length > 0) {
          this.$nextTick(() => {
            this.init()
          })
        }
      }
    }
  },
  mounted () {
  },
  destroyed () {
    this.destory()
  },
  methods: {
    init () {
      if (this.chartData) {
        this.destory()
      }
      const option = {
        title: {
          text: '运维数据分布',
          textStyle: {
            fontSize: '16'
          },
          subtext: '人员处置事件图',
          subtextStyle: {
            fontSize: '12',
            align: 'center'
          },
          top: '5',
          left: 'center'
        },
        tooltip: {
          trigger: 'item'
        },
        legend: {
          orient: 'vertical',
          top: '5',
          left: 'left'
        },
        grid: {
          top: '20px',
          left: '5px',
          right: '5px',
          bottom: '0',
          containLabel: true
        },
        series: [
          {
            name: 'Access From',
            type: 'pie',
            top: '50px',
            center: ['50%', '50%'],
            radius: '50%',
            data: this.data,
            emphasis: {
              itemStyle: {
                shadowBlur: 10,
                shadowOffsetX: 0,
                shadowColor: 'rgba(0, 0, 0, 0.5)'
              }
            }
          }
        ]
      }
      this.chartData = echarts.init(document.getElementById(this.chartId))
      this.chartData.clear()
      this.chartData.setOption(option, true)
      window.addEventListener('resize', this.chartData.resize, false)
      // const xAxisData = []
      // if (!xAxisData || xAxisData.length === 0) {
      //   this.chartData.showLoading({
      //     text: '暂无数据',
      //     color: '#fff',
      //     textColor: '#8a8e91',
      //     fontSize: 16
      //   })
      // } else {
      //   this.chartData.hideLoading()
      // }
    },
    destory () {
      if (this.chartData) {
        this.chartData.clear()
        this.chartData.dispose()
        this.chartData = null
      }
    }
  }
}
</script>
<style lang="scss" scoped>
.op-data {
  height: 100%;
  max-height: 100%;

}
</style>

3.2 柱状图

在这里插入图片描述

3.2.1 调用图表组件代码

在这里插入图片描述

 // 运维效率X轴时间点
      opEfficiencyXAxisData: [
          "2023-04-14",
          "2023-04-15",
          "2023-04-16",
          "2023-04-17",
          "2023-04-18",
          "2023-04-19",
          "2023-04-20"],
      // 运维效率数据
      opEfficiencySeries: [{"name":"高效率","data":[0,0,0,0,0,1,0]},{"name":"正常效率","data":[0,0,0,0,1,1,3]},{"name":"低效率","data":[0,1,4,3,3,2,0]},{"name":"未处理","data":[0,0,0,0,0,0,0]}],

在这里插入图片描述

3.2.2 图表组件代码
<template>
  <div :class="initLoading ? 'op-efficiency' : 'op-efficiency-nodata'">
    <span v-if="initLoading" class="loading-text">加载中...</span>
    <el-empty v-else-if="!initLoading && data.length <= 0" :image-size="100" />
    <div :id="chartId" :style="{width: chartWidth, height: chartHeight}" />
  </div>
</template>

<script>
import * as echarts from 'echarts'
export default {
  name: 'OpEfficiency',
  props: {
    initLoading: {
      type: Boolean,
      default: false
    },
    xAxisData: {
      type: Array,
      default: () => {
        return []
      }
    },
    data: {
      type: Array,
      default: () => {
      }
    }
  },
  data () {
    return {
      chartId: 'op-efficiency',
      chartWidth: '',
      chartHeight: '300px',
      chartData: null,
      seriesData: [],
      colorList: ['#5470c6', '#91cc75', '#fac858', '#ee6666', '#73c0de', '#3ba272', '#fc8452', '#9a60b4', '#ea7ccc']
    }
  },
  watch: {
    data: {
      immediate: true,
      deep: true,
      handler: function (newVal, oldVal) {
        if (newVal && newVal.length > 0) {
          this.$nextTick(() => {
            this.init()
          })
        }
      }
    }
  },
  mounted () {
  },
  destroyed () {
    this.destory()
  },
  methods: {
    init () {
      if (this.chartData) {
        this.destory()
      }
      const posList = [
        'left',
        'right',
        'top',
        'bottom',
        'inside',
        'insideTop',
        'insideLeft',
        'insideRight',
        'insideBottom',
        'insideTopLeft',
        'insideTopRight',
        'insideBottomLeft',
        'insideBottomRight'
      ]
      var app = {}
      app.configParameters = {
        rotate: {
          min: -90,
          max: 90
        },
        align: {
          options: {
            left: 'left',
            center: 'center',
            right: 'right'
          }
        },
        verticalAlign: {
          options: {
            top: 'top',
            middle: 'middle',
            bottom: 'bottom'
          }
        },
        position: {
          options: posList.reduce(function (map, pos) {
            map[pos] = pos
            return map
          }, {})
        },
        distance: {
          min: 0,
          max: 100
        }
      }
      app.config = {
        rotate: 90,
        align: 'left',
        verticalAlign: 'middle',
        position: 'insideBottom',
        distance: 15,
        onChange: function () {
          const labelOption = {
            rotate: app.config.rotate,
            align: app.config.align,
            verticalAlign: app.config.verticalAlign,
            position: app.config.position,
            distance: app.config.distance
          }
          this.chartData.setOption({
            series: [
              {
                label: labelOption
              },
              {
                label: labelOption
              },
              {
                label: labelOption
              },
              {
                label: labelOption
              }
            ]
          })
        }
      }
      let labelOption = {
        show: true,
        position: app.config.position,
        distance: app.config.distance,
        align: app.config.align,
        verticalAlign: app.config.verticalAlign,
        rotate: app.config.rotate,
        formatter: '{c}  {name|{a}}',
        fontSize: 16,
        rich: {
          name: {}
        },
        textStyle: {
          color: null
        }
      }
      /** 修改柱状图字体颜色 */
      this.data.forEach((e) => {
        e.color = this.getColorByName(e.name)
        // labelOption.textStyle.color = e.color
        e.labelOption = JSON.parse(JSON.stringify(labelOption))
      })
      this.setSeries()
      const option = {
        title: {
          text: '运维效率',
          textStyle: {
            fontSize: '16'
          },
          left: 'center'
        },
        tooltip: {
          trigger: 'axis',
          axisPointer: {
            type: 'shadow'
          }
        },
        xAxis: [
          {
            type: 'category',
            axisTick: { show: false },
            data: this.xAxisData
          }
        ],
        yAxis: [
          {
            type: 'value'
          }
        ],
        grid: {
          top: '25px',
          left: '5px',
          right: '5px',
          bottom: '0',
          containLabel: true
        },
        series: this.seriesData
      }
      this.chartData = echarts.init(document.getElementById(this.chartId))
      this.chartData.clear()
      this.chartData.setOption(option, true)
      window.addEventListener('resize', this.chartData.resize, false)
    },
    setSeries () {
      this.seriesData = []
      this.data.forEach(item => {
        const that = this
        const tempSeries = {
          type: 'bar',
          barGap: 0,
          label: item.labelOption,
          emphasis: {
            focus: 'series'
          },
          name: item.name,
          data: item.data,
          itemStyle: {
            normal: {
              // 修改柱状图柱体颜色
              color: function (params) {
                const colorList = that.colorList
                if (params.dataIndex >= colorList.length) {
                  params.dataIndex = params.dataIndex - colorList.length
                }
                const color = that.getColorByName(params.seriesName)
                if (color) {
                  return color
                }
                return colorList[params.dataIndex]
              }
            }
          }
        }
        that.seriesData.push(tempSeries)
      })
    },
    /** 通过名称对应不同的颜色 */
    getColorByName (name) {
      let color = null
      // 黃色
      if (name === '低效率') {
        color = this.colorList[2]
      } else if (name === '高效率') { // 绿色
        color = this.colorList[1]
      } else if (name === '正常效率') { // 蓝色
        color = this.colorList[0]
      } else if (name === '未处理') { // 红色
        color = this.colorList[3]
      }
      console.log('color', color)
      return color
    },
    // 清除图标
    destory () {
      if (this.chartData) {
        this.chartData.clear()
        this.chartData.dispose()
        this.chartData = null
      }
    }
  }
}
</script>
<style lang="scss" scoped>
@mixin op-efficiency {
  height: 100%;
  max-height: 100%;
  .loading-text {
    color: #409EFF;
  }
}
.op-efficiency {
  @include op-efficiency();
  line-height: 300px;
  text-align: center;
}
.op-efficiency-nodata {
  @include op-efficiency();
  .el-empty {
    padding: calc((300px - 120px) / 2);
  }
}
</style>

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 要在Vue使用Echarts,需要先安装EchartsVue-Echarts: 1. 安装Echarts:在终端中运行`npm install echarts --save` 2. 安装Vue-Echarts:在终端中运行`npm install vue-echarts --save` 安装完成后,在Vue组件中使用Echarts的步骤如下: 1. 在组件中引入Vue-Echarts ``` import ECharts from 'vue-echarts/components/ECharts.vue' ``` 2. 在组件中注册ECharts组件 ``` components: { 'echarts': ECharts } ``` 3. 在组件中使用ECharts ``` <template> <div> <echarts :options="chartOptions"></echarts> </div> </template> <script> import ECharts from 'vue-echarts/components/ECharts.vue' export default { components: { 'echarts': ECharts }, data() { return { chartOptions: { // ECharts配置项 } } } } </script> ``` 其中,`chartOptions`是ECharts的配置项,具体的配置项可以参考ECharts官方文档。 ### 回答2: Vue使用ECharts是一种常见的数据可视化方案。ECharts是一个基于JavaScript的开源可视化库,它提供了丰富多样的图表和图形,可以用于展示各种类型的数据。 Vue结合ECharts可以实现动态的数据可视化。首先,我们需要在Vue项目中引入ECharts的库文件。然后,在Vue组件中可以通过配置ECharts的选项来创建图表,并通过绑定数据来更新图表的展示。 Vue提供了生命周期钩子函数,我们可以在组件的mounted钩子函数中初始化ECharts图表。在这个钩子函数中,我们可以使用`this.$refs`访问组件中的DOM元素,然后创建ECharts图表实例,并通过调用其API方法来配置图表的样式和数据。 当数据变化时,我们可以使用Vue的响应式机制自动更新ECharts图表。通过监听数据的变化,我们可以在数据改变时重新渲染图表,保持图表和数据的同步。 除了基本的图表展示,VueECharts还可以通过事件绑定来实现更多的交互效果。通过监听图表的点击、鼠标移动等事件,我们可以在Vue组件中处理用户的交互行为,并根据这些交互行为来更新数据或者执行其他操作。 综上所述,VueECharts的结合可以实现强大的数据可视化功能。通过Vue的响应式机制和事件绑定,我们可以创建动态的、交互性强的数据可视化应用,并且可以根据数据的变化进行实时的更新。 ### 回答3: Vue使用Echarts是一种常见的数据可视化方案。Vue是一种流行的JavaScript框架,用于构建用户界面。而Echarts是一个基于JavaScript的图表库,可提供各种丰富的图表类型和交互功能。 在Vue项目中使用Echarts非常简单。首先,我们需要在Vue项目中安装Echarts。可以使用npm或yarn来安装echarts包。然后,我们可以在Vue组件中导入Echarts,并在需要绘制图表的地方使用它。 在Vue组件中,我们可以使用Echarts的实例化对象来创建不同类型的图表。我们可以设置图表的数据、样式、配置项等。可以根据需求定制各种图表,如折线图、柱状图、散点图等等。 Echarts提供了丰富的API和配置选项,使我们可以轻松地自定义和交互图表。我们可以设置图表的标题、轴标签、图例等内容。还可以通过响应式设计使图表适应不同的屏幕尺寸。 另外,VueEcharts的结合还可以实现动态更新图表。我们可以监听数据变化,并在数据发生变化时,动态更新图表,使图表能够实时反映数据的变化。 总之,VueEcharts的结合为开发人员提供了一种简单而强大的数据可视化方案。通过Echarts的丰富功能,我们可以轻松地在Vue项目中创建漂亮和交互性强的图表,提供更好的数据呈现和用户体验。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值