nuxt使用echarts——带滚动条

文章讲述了在Nuxt.js项目中集成ECharts图表库遇到的问题,包括安装包错误、图表显示问题(需定义宽高)以及如何在页面中动态调整图表大小以适应屏幕变化。
摘要由CSDN通过智能技术生成

直接在页面写,不带滚动条

在这里插入图片描述

bug1:安装包报错,就更换版本
bug2:图表出不来:需要定义宽高
bug3:需要resize大小

安装

npm install echarts@4.9.0

plugins文件夹下新建echarts.js

import Vue from 'vue'
import echarts from 'echarts' // 引入echarts
Vue.prototype.$echarts = echarts // 引入组件(将echarts注册为全局)

nuxt.config.js中配置

 plugins: [
    '@/plugins/element-ui',
    { src: '@/plugins/utils', ssr: false },// axios.js文件关闭服务端渲染
    { src: '@/plugins/axios', ssr: false }, // axios.js文件关闭服务端渲染
    { src: "~/plugins/swiper.js", ssr: false },
    { src: '~/plugins/store-cache', ssr: false },
    { src: '~/plugins/flexiable.js', ssr: false },
    { src: '@/plugins/bus', ssr: false },
    { src: '~plugins/echarts', ssr: false },
  ],

页面

<div class="mtb-20" style="height: 3.6rem;">
    <div id="myChart" style="width: 12.5rem;height: 3.6rem;"></div>
</div>
export default {
  data() {
    return {
      barChart: null,    
    };
  },
  mounted() {
    this.init()
    // 监听屏幕大小变化
    window.addEventListener("resize", this.screenResize);
  },
  beforeDestroy() {
    window.removeEventListener("resize", this.screenResize);
  },
  methods: {
    init() {
      // 找到容器
      this.barChart = this.$echarts.init(document.getElementById('myChart'))
      // 开始渲染
      this.barChart.setOption({
        grid: {
          top: "10%",
          right: "5%",
          left: "5%",
          bottom: "20%",
        },
        xAxis: [
          {
            type: "category",
            data: [],
            axisPointer: {
              type: "line",
            },
            axisLabel: {
              margin: 20,
              color: "#59588D",
              textStyle: {
                fontSize: 14,
              },
            },
          },
        ],
        yAxis: [
          {
            min: 0,
            axisLabel: {
              formatter: "{value}",
              color: "#59588D",
            },
            splitLine: {
              lineStyle: {
                color: "#f5f6f7",
              },
            },
          },
        ],
        series: [
          {
            type: "bar",
            data: [],
            barWidth: "20px",
            itemStyle: {
              normal: {
                color: '#00cdba',
                barBorderRadius: [30, 30, 0, 0],
              },
            },
            label: {
              normal: {
                show: true,
                borderRadius: 200,
                position: ["0", "-20"],
                formatter: [" {a|{c}}"].join(""),
                rich: {
                  a: {
                    color: "#333",
                    align: "center",
                  },
                },
              },
            },
          },
        ],
      })
      this.getData()
    },
    // 掉接口
    getData() {
      // 排序 
      // 当前X、Y轴数据
      // let barDataX = list.map((item) => item.name);
      // let barDataY = list.map((item) => item.value);

      let dataOption = {
        xAxis: {
          data: ['衬衫', '羊毛衫', '雪纺衫', '裤子', '高跟鞋', '袜子'],
        },
        series: [
          {
            data: [5, 20, 36, 10, 10, 20], // 数据
          },
        ],
      };
      // 生成图表
      this.barChart.setOption(dataOption);
    },
    // 监听屏幕变化
    screenResize() {
      this.$nextTick(() => {
        // 更新图表
        this.barChart.resize();
      });
    },
  },
};
</script>

封装组件:有滚动条

根据数据的条数动态改变展示的个数

在这里插入图片描述

components/BarChart.vue 组件

<template>
  <div id="index">
    <div id="chart" :style="`width:${width};height: ${height};`"></div>
  </div>
</template>
<script>

export default {
  data() {
    return {
      chart: null,
    };
  },
  props: ["list", "width", "height"],
  watch: {
    list: {
      handler(val) {
        // 深度监听没有旧值
        this.date = val.map((item) => {
          return item.date;
        });
        this.number = val.map((item) => {
          return item.number;
        });

        let option = {
          xAxis: [
            {
              data: this.date,
            },
          ],
          series: [
            {
              data: this.number, // 订单量
            },
          ],
           // ***** 柱状图横坐标大于8条启用横向滚动条
          dataZoom: [//滚动条
            {
              show: val.length > 8 ? true : false,
              type: 'slider',
              realtime: true,
              start: 0,
              end: val.length > 20 ? 30 : val.length > 8 ? 50 : 100, //滚动条初始显示百分比
              xAxisIndex: [0],
              bottom: '10',
              height: 10,
              borderColor: 'rgba(0,0,0,0)',
              textStyle: {
                color: '#05D5FF',
              },
            },
          ],
        };
        this.chart.setOption(option);
      },
      // 这里是关键,代表递归监听的变化
      deep: true,
    },
  },
  mounted() {
    this.init()
    // 监听屏幕大小变化
    window.addEventListener("resize", this.screenResize);
  },
  beforeDestroy() {
    window.removeEventListener("resize", this.screenResize);
  },
  methods: {
    init() {
      // 找到容器
      this.chart = this.$echarts.init(document.getElementById('chart'))
      // 开始渲染
      this.chart.setOption({
        grid: {
          top: "10%",
          right: "5%",
          left: "5%",
          bottom: "20%",
        },
        xAxis: [
          {
            type: "category",
            data: [],
            axisPointer: {
              type: "line",
            },
            axisLabel: {
              margin: 20,
              color: "#59588D",
              textStyle: {
                fontSize: 13,
              },
            },
          },
        ],
        yAxis: [
          {
            min: 0,
            axisLabel: {
              formatter: "{value}",
              color: "#59588D",
            },
            splitLine: {
              lineStyle: {
                color: "#f5f6f7",
              },
            },
          },
        ],
        series: [
          {
            type: "bar",
            data: [],
            barWidth: "20px",
            itemStyle: {
              normal: {
                color: '#00cdba',
                barBorderRadius: [30, 30, 0, 0],
              },
            },
            label: {
              normal: {
                show: true,
                borderRadius: 200,
                position: ["0", "-20"],
                formatter: [" {a|{c}}"].join(""),
                rich: {
                  a: {
                    color: "#333",
                    align: "center",
                  },
                },
              },
            },
          },
        ],
      })
    },
    // 监听屏幕变化
    screenResize() {
      this.$nextTick(() => {
        // 更新图表
        this.chart.resize();
      });
    },
  },
};
</script>
<style scoped></style>

使用

 <div class="mtb-20" style="height: 3.6rem;">
    <BarChart :list="list" width="12.5rem" height="3.6rem"></BarChart>
 </div>
list: [] //柱状图
//模拟接口
 mounted() {
    setTimeout(() => {
      this.list = [{
        date: 1.1,
        number: 10
      }, {
        date: 2.1,
        number: 12
      }, {
        date: 3.1,
        number: 21
      }, {
        date: 4.1,
        number: 11
      }, {
        date: 5.1,
        number: 31
      },]
    }, 1000)
  },
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小曲曲

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值