vue结合echartx轴双类目显示且多系列层叠

vue结合echartx轴双类目显示且多系列层叠

最近因公司要求要实现一个大屏看板数据走势图,以前都是直接引用echars现成事例自己改动,但是这次的需求要实现柱形图的多系列层叠并且x轴是双类目的,这个是事例中没有的,自己改了很久,终于完成了,现在源码奉上,希望对大家有帮助!

vue部分

html:
<div id="Warehouse_separation_time"></div>
css:
#Warehouse_separation_time{
   width: 100%;
  height: 500px;
}

js部分

Warehouse_separation_time() {
      var itemStyle = {
        normal: {},
        emphasis: {
          barBorderWidth: 1,
          shadowBlur: 10,
          shadowOffsetX: 0,
          shadowOffsetY: 0,
        }
      };
      // 基于准备好的dom,初始化echarts实例
      let myChart = this.$echarts.init(document.getElementById('Warehouse_separation_time'))
      // 绘制图表
      myChart.setOption({
        tooltip: {
          trigger: 'axis'
        },
        legend: {
          top: '30px',
          data: ['a', 'b', 'c', 'd', 'e'],
          textStyle: {
            color: '#ccc'
          }
        },
        xAxis: [{
            type: 'category',
            data: ['0:00分', '0:10分', '0:20分', '0:30分', '0:40分'],
            axisLabel: {
              textStyle: {
                color: '#7edae8', //坐标的字体颜色
              },
            },
            axisPointer: {
              type: 'shadow'
            },
            position: 'bottom',
            offset: 20,
            axisTick: {
              show: true,
              length: 40,
              lineStyle: {
                type: 'dotted'
              }
            }
          },
          {
            type: 'category',
            data: ['a', 'b',
              'a', 'b',
              'a', 'b',
              'a', 'b',
              'a', 'b'
            ],
            axisLabel: {
              textStyle: {
                color: '#7edae8', //坐标的字体颜色
              },
            },
            position: 'bottom',
            axisPointer: {
              type: 'shadow'
            },
            axisTick: {
              show: true
            }
          }
        ],
        yAxis: [{
          type: 'value',
          axisLabel: {
            textStyle: {
              color: '#7edae8'
            }
          }
        }],
        series: [{
            name: 'a',
            type: 'bar',
            itemStyle: itemStyle,
            label: {
              normal: {
                show: true
              }
            },
            stack: '总量',
            stack: 'one',
            data: [320, 302, 301, 334, 390, 330, 320]
          },
          {
            name: 'a',
            type: 'bar',
            itemStyle: itemStyle,
            label: {
              normal: {
                show: true
              }
            },
            stack: '总量',
            stack: 'two',
            data: [320, 302, 301, 334, 390, 330, 320]
          },
          {
            name: 'b',
            type: 'bar',
            itemStyle: itemStyle,
            label: {
              normal: {
                show: true
              }
            },
            stack: '总量',
            stack: 'one',
            data: [320, 302, 301, 334, 390, 330, 320]
          },
          {
            name: 'b',
            type: 'bar',
            itemStyle: itemStyle,
            label: {
              normal: {
                show: true
              }
            },
            stack: '总量',
            stack: 'two',
            data: [320, 302, 301, 334, 390, 330, 320]
          },
          {
            name: 'c',
            type: 'bar',
            itemStyle: itemStyle,
            label: {
              normal: {
                show: true
              }
            },
            stack: 'one',
            data: [120, 132, 101, 134, 90, 230, 210]
          },
          {
            name: 'c',
            type: 'bar',
            itemStyle: itemStyle,
            label: {
              normal: {
                show: true
              }
            },
            stack: 'two',
            data: [120, 132, 101, 134, 90, 230, 210]
          },
          {
            name: 'd',
            type: 'bar',
            itemStyle: itemStyle,
            label: {
              normal: {
                show: true
              }
            },
            stack: 'one',
            data: [220, 182, 191, 234, 290, 330, 310]
          },
          {
            name: 'd',
            type: 'bar',
            itemStyle: itemStyle,
            label: {
              normal: {
                show: true
              }
            },
            stack: 'two',
            data: [220, 182, 191, 234, 290, 330, 310]
          },
          {
            name: 'e',
            type: 'bar',
            itemStyle: itemStyle,
            label: {
              normal: {
                show: true
              }
            },
            stack: 'one',
            data: [220, 182, 191, 234, 290, 330, 310]
          },
          {
            name: 'e',
            type: 'bar',
            itemStyle: itemStyle,
            label: {
              normal: {
                show: true
              }
            },
            stack: 'two',
            data: [330, 282, 91, 204, 200,210, 310]
          },
        ],
        dataZoom: [{
          type: 'slider',
          show: true
        }]
      });
    }

提示

定义的方法如要在加载时触发需要写在vue的mounted或者updated的钩子函数中,写在created中是无法找到你定义的id的
会报错
在这里插入图片描述

实现的样子在这里插入图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 5
    评论
### 回答1: 好的,以下是一个简单的 Vue + ECharts 饼图示例: 1. 安装 ECharts ``` npm install echarts --save ``` 2. 在 Vue 组件中引入 ECharts ``` import echarts from 'echarts' ``` 3. 在 Vue 组件中定义数据和配置项 ``` data() { return { chartData: { legend: { data: ['直接访问', '邮件营销', '联盟广告', '视频广告', '搜索引擎'] }, series: [ { name: '访问来源', type: 'pie', radius: '55%', center: ['50%', '60%'], data: [ { value: 335, name: '直接访问' }, { value: 310, name: '邮件营销' }, { value: 234, name: '联盟广告' }, { value: 135, name: '视频广告' }, { value: 1548, name: '搜索引擎' } ], itemStyle: { emphasis: { shadowBlur: 10, shadowOffsetX: 0, shadowColor: 'rgba(0, 0, 0, 0.5)' } } } ] } } }, ``` 4. 在 Vue 组件中创建 ECharts 实例并绑定数据和配置项 ``` mounted() { var chart = echarts.init(document.getElementById('chart')) chart.setOption(this.chartData) }, ``` 5. 在 Vue 模板中渲染 ECharts 容器 ``` <template> <div id="chart" style="width: 100%; height: 400px;"></div> </template> ``` 完整代码如下: ``` <template> <div id="chart" style="width: 100%; height: 400px;"></div> </template> <script> import echarts from 'echarts' export default { data() { return { chartData: { legend: { data: ['直接访问', '邮件营销', '联盟广告', '视频广告', '搜索引擎'] }, series: [ { name: '访问来源', type: 'pie', radius: '55%', center: ['50%', '60%'], data: [ { value: 335, name: '直接访问' }, { value: 310, name: '邮件营销' }, { value: 234, name: '联盟广告' }, { value: 135, name: '视频广告' }, { value: 1548, name: '搜索引擎' } ], itemStyle: { emphasis: { shadowBlur: 10, shadowOffsetX: 0, shadowColor: 'rgba(0, 0, 0, 0.5)' } } } ] } } }, mounted() { var chart = echarts.init(document.getElementById('chart')) chart.setOption(this.chartData) } } </script> ``` ### 回答2: Vue是一个用于构建用户界面的渐进式框架,而ECharts是一款强大的数据可视化库。结合VueECharts,可以很容易地创建一个饼状图Demo。 首先,在Vue项目中安装ECharts库。可以通过npm或者直接引入ECharts的CDN链接进行安装。 在Vue组件中引入ECharts,并在其生命周期钩子函数中初始化图表。在`mounted`钩子函数中,创建一个包含饼图数据的数组,并使用ECharts的`init()`方法初始化图表。 然后,在数据数组中定义一个对象,包含饼图的名称和值。例如,可以定义两个数据对象:"苹果"和"橙子",并给予它们不同的值。将这两个对象添加到数据数组中。 接下来,使用ECharts的`setOption()`方法来配置饼图的选项。可以设置饼图的标题、颜色、半径等属性。 最后,使用ECharts的`dispose()`方法,在Vue组件销毁时销毁图表,以防止内存泄漏。 在模板中添加一个`div`元素作为容器,用于渲染图表。为了便于ECharts找到该元素,可以为其添加一个唯一的ID。 最后,使用Vue的数据绑定机制,将图表数据绑定到模板中,并使用`v-for`指令循环遍历数据数组,生成饼图的图例。 通过上述步骤,就可以实现一个简单的使用VueECharts创建的饼图Demo。可以自定义数据和图表样式,使其更加适应实际需求。 ### 回答3: 饼状图是一种常见的数据可视化图表,可以用来展示数据的占比关系。Vue是一个流行的JavaScript框架,可以与echarts这个数据可视化库结合使用,方便地创建饼状图的demo。 首先,我们需要安装Vueecharts的相关依赖包。可以使用npm或yarn命令来安装这些包。安装完成后,在Vue的组件中引入echarts库。 在Vue的组件中,可以通过以下步骤来创建饼状图的demo: 1. 在template标签中,添加一个div元素,用于承载echarts生成的图表。 ```html <template> <div> <div id="pieChart" style="width: 400px; height: 400px;"></div> </div> </template> ``` 2. 在script标签中,使用import语句引入echarts,并在mounted钩子函数中创建饼状图。 ```javascript <script> import echarts from 'echarts'; export default { mounted() { // 获取div元素 const chartDom = document.getElementById('pieChart'); // 初始化echarts实例 const myChart = echarts.init(chartDom); // 配置饼状图的数据 const option = { series: [ { type: 'pie', data: [ { value: 335, name: '直接访问' }, { value: 310, name: '邮件营销' }, { value: 234, name: '联盟广告' }, { value: 135, name: '视频广告' }, { value: 1548, name: '搜索引擎' }, ], }, ], }; // 使用配置项显示图表 myChart.setOption(option); }, }; </script> ``` 在上述代码中,我们使用了一个饼状图的示例数据,并将其添加到了series数组中。最后,使用setOption方法将配置选项应用到图表中。 通过上述步骤,我们就创建了一个使用Vueecharts的饼状图demo。当组件被挂载到DOM树后,饼图就会渲染在页面上。可以根据实际需要调整div的样式和饼状图的配置项,以实现不同的效果。
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值