Echarts+VUE实现折线柱状图

一、代码实现

如果需要对折线和柱子、刻度、轴、文本标签等进行设置,请参考前面几篇文章或者阅读网站文档。

echarts网站:https://echarts.apache.org/zh/index.html

<template>
  <div id="main_bar_line" style="width: 100%; height: 450px"></div>
</template>

<script>
import * as echarts from 'echarts';

export default {
  name: "chart",
  data() {
    return {
      xy_data: [
        {name: '一', value: 6, value2: 6.0,},
        {name: '二', value: 32, value2: 10.2,},
        {name: '三', value: 70, value2: 10.3,},
        {name: '四', value: 86, value2: 11.5,},
        {name: '五', value: 100.7, value2: 10.6,},
        {name: '六', value: 125.6, value2: 13.5,},
        {name: '七', value: 112.2, value2: 17,},
      ],
    };
  },
  created() {
    this.initBarLine();
  },
  methods: {
    initBarLine() {
      this.$nextTick(() => {
        // 基于准备好的dom,初始化echarts实例
        let myChart = echarts.init(document.getElementById('main_bar_line'));
        // 指定图表的配置项和数据
        let option = {
          tooltip: {
            trigger: 'axis',
            axisPointer: { type: 'cross' }
          },
          legend: {
            data: ['温度', '降水量']
          },
          xAxis: [
            {
              type: 'category',
              axisTick: {
                alignWithLabel: true
              },
              data: this.xy_data.map(item => item.name+'月')
            }
          ],
          yAxis: [
            {
              type: 'value',
              name: '降水量',
              min: 0,
              max: 250,
              position: 'right',
              axisLabel: {
                formatter: '{value} ml'
              }
            },
            {
              type: 'value',
              name: '温度',
              min: 0,
              max: 25,
              position: 'left',
              axisLabel: {
                formatter: '{value} °C'
              }
            }
          ],
          series: [
            {
              name: '降水量',
              type: 'bar',
              yAxisIndex: 0,
              data: this.xy_data.map(item => item.value)
            },
            {
              name: '温度',
              type: 'line',
              smooth: true,
              yAxisIndex: 1,
              data: this.xy_data.map(item => item.value2)
            }
          ]
        };
        // 使用刚指定的配置项和数据显示图表。
        myChart.setOption(option);
      })
    },
  }
};
</script>

二、效果图

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
以下是一个使用echartsvue2实现折线结合使用的示例代码,你可以根据自己的需求进行修改和调整: ```html <template> <div class="chart-container"> <div ref="chart" class="chart"></div> </div> </template> <script> import echarts from 'echarts' export default { data() { return { chartData: { xAxisData: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'], seriesData1: [320, 332, 301, 334, 390, 330, 320, 330, 350, 380, 360, 390], seriesData2: [220, 182, 191, 234, 290, 330, 310, 320, 330, 300, 280, 260] } } }, mounted() { this.initChart() }, methods: { initChart() { const chart = echarts.init(this.$refs.chart) chart.setOption({ tooltip: { trigger: 'axis' }, legend: { data: ['Series1', 'Series2'] }, xAxis: { type: 'category', data: this.chartData.xAxisData }, yAxis: [{ type: 'value', name: 'Series1', position: 'left', axisLabel: { formatter: '{value} units' } }, { type: 'value', name: 'Series2', position: 'right', axisLabel: { formatter: '{value} units' } }], series: [{ name: 'Series1', type: 'bar', data: this.chartData.seriesData1 }, { name: 'Series2', type: 'line', yAxisIndex: 1, data: this.chartData.seriesData2 }] }) } } } </script> <style> .chart-container { width: 100%; height: 400px; display: flex; justify-content: center; align-items: center; } .chart { width: 80%; height: 80%; } </style> ``` 在这个示例代码中,我们首先引入了echarts依赖,然后在data中定义了需要展示的表数据。在mounted方法中,我们调用了initChart方法,创建了一个echarts实例并初始化了表。 在initChart方法中,我们使用了echarts的API来配置了表的tooltip、legend、xAxis、yAxis和series等属性,最终使用setOption方法将表配置应用到了echarts实例中。 在样式方面,我们在容器元素和表元素上设置了一些基本的样式。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值