• nuxt使用echarts


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

    在这里插入图片描述

    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注册为全局)
    
    • 1
    • 2
    • 3

    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 },
      ],
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    页面

    <div class="mtb-20" style="height: 3.6rem;">
        <div id="myChart" style="width: 12.5rem;height: 3.6rem;"></div>
    </div>
    
    • 1
    • 2
    • 3
    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>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116

    封装组件:有滚动条

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

    在这里插入图片描述

    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>
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
    • 143
    • 144
    • 145
    • 146
    • 147
    • 148
    • 149
    • 150
    • 151

    使用

     <div class="mtb-20" style="height: 3.6rem;">
        <BarChart :list="list" width="12.5rem" height="3.6rem"></BarChart>
     </div>
    
    • 1
    • 2
    • 3
    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)
      },
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
  • 相关阅读:
    laravel 自定义邮箱验证过期时间
    618 火热来袭,网络安全书单推荐
    京东零售大数据云原生平台化实践
    Redis 哈希Hash底层数据结构
    Swagger2 介绍与集成
    小程序学习day02-JSON配置文件、小程序页面、WXML、WXSS
    使用Axure RP并配置IIS服务结合内网穿透实现公网访问本地HTML原型页面
    设计模式-适配器-笔记
    【单片机】唯一设备ID UID固件加密
    Zookeeper安装(单机、伪集群、多机集群)
  • 原文地址:https://blog.csdn.net/weixin_43848576/article/details/133081141