vue+echarts实现一个叠堆柱状图

关于一些常常用到的参考文档:这里都罗列一下,查找起来比较方便html

Element UI手册:https://cloud.tencent.com/developer/doc/1270
github地址:https://github.com/ElemeFE/elementvue

vue2.0官方网站:http://caibaojian.com/vue/guide/installation.html
element-ui官方网站:https://element.eleme.cn/#/zh-CN
Echarts官网:https://echarts.apache.org/zh/index.htmlreact


在项目里面,不少的时候都会遇到统计图,不管是折线图,柱状图仍是饼状图,不管是vue框架,react框架,仍是咱们的小程序,均可以直接使用咱们的百度团队开发的可视化图形框架Echarts,使用起来也比较简单粗暴,直接在项目里面安装,引入使用就是了。git

1:在项目里面安装echartsgithub

cnpm install echarts --s
watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_90,type_ZmFuZ3poZW5naGVpdGk=

2:在须要用图表的地方引入apache

import echarts from "echarts";
watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_90,type_ZmFuZ3poZW5naGVpdGk=

3:在template里面写一个div,用于盛饭图表的容器,能够设置一下长宽,也能够根据项目的是要作成自适应长宽高。npm

<div id="main" style="width: 100%; height: 400px"></div>

4:引入json格式的接口,这里能够从后端小伙伴那里拿过来,也能够本身取模拟一个数据,都是能够的。element-ui

import { getQuerycheckList } from "@/api/dashboard/healthy";

下面是json数据例子json

{
    "msg": "success",
    "code": 1,
    "data": {
        "healthStat": [{
            "id": 1,
            "statTime": "2021-01-08",
            "healthPersonCount": 2,
            "notHealthPersonCount": 3
        }, {
            "id": 2,
            "statTime": "2021-01-09",
            "healthPersonCount": 5,
            "notHealthPersonCount": 6
        }, {
            "id": 3,
            "statTime": "2021-01-18",
            "healthPersonCount": 0,
            "notHealthPersonCount": 1
        }, {
            "id": 4,
            "statTime": "2021-01-21",
            "healthPersonCount": 0,
            "notHealthPersonCount": 0
        }, {
            "id": 5,
            "statTime": "2021-01-22",
            "healthPersonCount": 0,
            "notHealthPersonCount": 0
        }]
    }
}

5:具体代码,请求接口,以及对请求到的json数据进行遍历,而且赋值到上面的echarts图表的框架里面去。小程序

<template>
  <div class="dashboard-container">
    <div class="dashboard-editor-container">
      <el-row
        style="background: #fff; padding: 16px 16px 0; margin-bottom: 32px"
      >
        <div id="main" style="width: 100%; height: 400px"></div>
      </el-row>
    </div>
  </div>
</template>
<script>
import { getQuerycheckList } from "@/api/dashboard/healthy";
import echarts from "echarts";
export default {
  name: "",
  data() {
    return {
      charts: "",
      arr1: [],
      arr2: [],
      arr3: [],
    };
  },
  created() {
    //健康看板统计接口定义
    this.getQuerycheckList();
  },
  methods: {
    //健康看板统计接口定义
    getQuerycheckList() {
      const params = {
        startTime: "2021-01-08",
        stopTime: "2021-02-01",
      };
      this.dataLoading = true;
      getQuerycheckList(params).then((res) => {
        console.log("查询健康看板统计接口定义接口", res);
        //统计总数
        this.total=res.data;
        this.arr1 = res.data.healthStat.map((a) => a.statTime);
        this.arr2 = res.data.healthStat.map((a) => a.healthPersonCount);
        this.arr3 = res.data.healthStat.map((a) => a.notHealthPersonCount);
        // this.opinionData =response.data.healthStat;
        this.drawLine("main");
        this.dataLoading = false;
      });
    },
    drawLine(id) {
      this.charts = echarts.init(document.getElementById(id));
      this.charts.setOption({
        tooltip: {
          trigger: "axis",
          axisPointer: {
            // 坐标轴指示器,坐标轴触发有效
            type: "shadow", // 默认为直线,可选为:'line' | 'shadow'
          },
        },
        legend: {
          data: ["健康", "不健康"],
        },
        grid: {
          left: "3%",
          right: "4%",
          bottom: "3%",
          containLabel: true,
        },
        xAxis: [
          {
            type: "category",
            data: this.arr1,
          },
        ],
        yAxis: [
          {
            type: "value",
          },
        ],
        series: [
          {
            name: "健康",
            type: "bar",
            itemStyle: {
                        normal: {
                            color: new echarts.graphic.LinearGradient(0, 1, 0, 0, [{
                                offset: 0,
                                color: "#49E8B4" // 0% 处的颜色
                            }, {
                                offset: 0.6,
                                color: "#49E8B4" // 60% 处的颜色
                            }, {
                                offset: 1,
                                color: "#3CB796" // 100% 处的颜色
                            }], false)
                        }
                    },
            data: this.arr2,
          },

          {
            name: "不健康",
            type: "bar",
            itemStyle: {
                        normal: {
                            color: new echarts.graphic.LinearGradient(0, 1, 0, 0, [{
                                offset: 0,
                                color: "#F9BB84" // 0% 处的颜色
                            }, {
                                offset: 0.6,
                                color: "#F9BB84" // 60% 处的颜色
                            }, {
                                offset: 1,
                                color: "#F487B9" // 100% 处的颜色
                            }], false)
                        }
                    },
            data: this.arr3,
          },
        ],
      });
    },
  },
};
</script>

效果是这样的~

watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_90,type_ZmFuZ3poZW5naGVpdGk=