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

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

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


1:在项目里面安装echarts

cnpm install echarts --s

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

import echarts from "echarts";

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

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

4:引入json格式的接口,这里可以从后端小伙伴那里拿过来,也可以自己取模拟一个数据,都是可以的。

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

下面是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>

效果是这样的~

相关文章
|
2月前
|
JavaScript 算法 前端开发
vue + echarts实现国省市三级下钻联动
vue + echarts实现国省市三级下钻联动
64 0
|
4月前
|
JavaScript
升级echarts v5.0以后vue项目报错“export ‘default‘ (imported as ‘echarts‘) was not found in ‘echarts‘
升级echarts v5.0以后vue项目报错“export ‘default‘ (imported as ‘echarts‘) was not found in ‘echarts‘
|
19天前
|
JavaScript
Vue引入Echarts图表的使用
Vue引入Echarts图表的使用
|
1月前
|
Web App开发 前端开发 JavaScript
Spring Boot整合 mybatisplus(后端) Vue+echarts+Element UI+axios(前端)---前后端项目实例demo
Spring Boot整合 mybatisplus(后端) Vue+echarts+Element UI+axios(前端)---前后端项目实例demo
27 1
|
3月前
|
JavaScript 数据可视化 前端开发
Vue整合HighCharts和ECharts实现数据可视化
Vue整合HighCharts和ECharts实现数据可视化
25 0
|
3月前
|
JSON JavaScript 定位技术
Vue中使用echarts@4.x中国地图及AMap相关API的使用
Vue中使用echarts@4.x中国地图及AMap相关API的使用
162 0
Vue中使用echarts@4.x中国地图及AMap相关API的使用
|
4月前
|
JavaScript 容器
vue echarts图表自适应屏幕变化
vue echarts图表自适应屏幕变化
64 0
|
4月前
|
资源调度 JavaScript 前端开发
vue使用echarts图表
vue使用echarts图表
34 0
|
4月前
设置echarts的grid、tooltip、柱状图渐变色、折线图渐变色
设置echarts的grid、tooltip、柱状图渐变色、折线图渐变色
|
4月前
Echarts柱状图x轴刻度间隔显示不全/x轴文字倾斜
Echarts柱状图x轴刻度间隔显示不全/x轴文字倾斜
61 0