echarts图表静态数据 象形柱形图、折线图、日历饼图、饼状图四种实现

标题

页面全部代码 

<template>
  <div class="data-serve">
    <div class="side">
      <div class="side-inner">
        <router-link class="side-btn" to="/camer/pushInfo"><i class="el-icon-picture"></i>报警信息</router-link>
        <router-link style="background: #344886" class="side-btn" to="/camera/live">
          <i class="el-icon-picture"></i>统计分析
        </router-link>
      </div>
    </div>
    <div class="main">
      <div class="chart-side">
        <div class="chart-wrapper">
          <div class="chart-bg">
            <span>报警处理状态</span>
          </div>
          <div class="chart">
            <div id="pieChart" class="echart-line"></div>
          </div>
        </div>
        <div class="chart-wrapper">
          <div class="chart-bg">
            <span>报警类型分析</span>
          </div>
          <div class="chart">
            <div id="barChart" class="echart-bar" />
          </div>
        </div>

      </div>
      <div class="chart-side">
        <div class="chart-wrapper">
          <div class="chart-bg">
            <span>日报警分析</span>
          </div>
          <div class="chart">
            <div id="scatterChart" class="echart-scatter" />
          </div>
        </div>
        <div class="chart-wrapper">
          <div class="chart-bg">
            <span>报警趋势分析</span>
          </div>
          <div class="chart">
            <div id="lineChart" class="echart-line"></div>
          </div>
        </div>
      </div>
    </div>

  </div>
</template>

<script>
export default {
  data() {
    return {
      scatterData: null,
    }
  },
  mounted() {
    let _this = this;
    this.getPieEcharts(); //饼状图
    this.getBarEcharts();//柱状图
    this.getScatterEcharts();//日期饼状图
    this.getLineEcharts(); //折线图
  },
  methods: {
    //图一饼状图
    getPieEcharts() {
      let myChart = this.$echarts.init(document.getElementById("pieChart"));
      myChart.setOption({
        tooltip: {
          trigger: 'item'
        },
        series: [
          {
            type: 'pie',
            radius: '60%',
            center: ['50%', '50%'],
            // 文字后添加百分比值
            label: {
              show: true,
              formatter(param) {
                return param.name + ' (' + param.percent + '%)';
              }
            },
            roseType: 'radius',
            itemStyle: {
              color: function (params) {
                let colorList = [
                  {
                    type: 'linear',
                    x: 0,
                    y: 1,
                    x2: 1,
                    y2: 0,
                    colorStops: [
                      {
                        offset: 0,
                        color: '#7a26ee',
                      },
                      {
                        offset: 1,
                        color: '#be7eff',
                      },
                    ],
                    global: false,
                  },
                  {
                    type: 'linear',
                    x: 0,
                    y: 1,
                    x2: 1,
                    y2: 0,
                    colorStops: [
                      {
                        offset: 0,
                        color: '#25ebff',
                      },
                      {
                        offset: 1,
                        color: '#23c0e0',
                      },
                    ],
                    global: false,
                  },
                  {
                    type: 'linear',
                    x: 1,
                    y: 0,
                    x2: 1,
                    y2: 1,
                    colorStops: [
                      {
                        offset: 0,
                        color: '#1620cd',
                      },
                      {
                        offset: 1,
                        color: '#0d79f9',
                      },
                    ],
                    global: false,
                  },
                  {
                    type: 'linear',
                    x: 1,
                    y: 1,
                    x2: 0,
                    y2: 0,
                    colorStops: [
                      {
                        offset: 0,
                        color: '#0d79f9',
                      },
                      {
                        offset: 1,
                        color: '#1cbbff',
                      },
                    ],
                    global: false,
                  },
                ];
                return colorList[params.dataIndex];
              },
            },
            // animationType: 'scale',
            // animationEasing: 'elasticOut',
            // animationDelay: function (idx) {
            //   return Math.random() * 200;
            // },
            data: [
              { value: 335, name: '已处理', label: { color: "#1cbcff" } },
              { value: 310, name: '重复报警', label: { color: "#0b82f9" } },
              { value: 274, name: '误报', label: { color: "#28ebff" } },
              { value: 235, name: '未处理', label: { color: "#be71ff" } },
            ].sort(function (a, b) {
              return a.value - b.value;
            }),

          }
        ]
      })
      window.onresize = myChart.resize;
    },
    // 图2柱状图
    getBarEcharts() {
      let barChart = this.$echarts.init(document.getElementById("barChart"));
      barChart.setOption({
        tooltip: {
          trigger: 'axis',
          formatter: function (params) {
            let result = '';
            params.forEach(function (item) {
              console.log(item, 123);

              if (item.seriesType === 'bar') {  // 如果是柱体,则获取对应的系列名称和值
                result += item.name + ': ' + item.value + '<br/>';
              }
            });
            return result;
          },
          axisPointer: {
            type: 'shadow',
          }
        },
        grid: {
          left: '3%',
          right: '4%',
          bottom: '5%',
          containLabel: true
        },
        xAxis: [
          {
            type: 'category',
            data: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'],
            axisTick: {
              show: false,
            },

            //x轴颜色
            axisLine: {
              lineStyle: {
                color: '#253262',
              },
            },


            //  x轴文字的颜色设置为白色
            axisLabel: {
              color: '#fff',
              margin: 20,
            },
          },

        ],
        yAxis: [
          {
            type: 'value',
            // 不显示y轴的线
            axisLine: {
              show: false
            },
            // 不显示y轴刻度
            axisTick: {
              show: false
            },
            // 分割线颜色
            splitLine: {
              lineStyle: {
                color: '#253262',
                // type: 'dashed' //虚线

              }

            },
            // y轴刻度文字颜色设置为白色
            axisLabel: {
              color: "#fff",
            },
          }
        ],
        series: [
          // 底部的椭圆形(象形柱图):pictorialBar
          {
            type: "pictorialBar", // pictorialBar(象形柱图)
            label: { // 图形上的文本标签,可用于说明图像的一些数据信息,比如值,名称等
              show: true, //是否显示标签
              position: ['25', '-30'], // 标签的位置(可以是绝对的像素值或者百分比['50%','50%',也可以是top,left等])
              color: '#01E4FF',
              fontSize: 14
            },
            symbolSize: [40, 20], // 图形的大小用数组分别比表示宽和高,也乐意设置成10相当于[10,10]
            symbolOffset: [0, 10], // 图形相对于原本位置的偏移
            z: 12, // 象形柱状图组件的所有图形的 z 值.控制图形的前后顺序.z 值小的图形会被 z 值大的图形覆盖.
            itemStyle: { // 图形样式
              // echarts.graphic.LinearGradient(echarts内置的渐变色生成器)
              // 4个参数用于配置渐变色的起止位置,这4个参数依次对应右 下 左 上
              color: {
                type: 'linear',
                x: 0,
                y: 0,
                x2: 0,
                y2: 1,
                colorStops: [  // 渐变颜色
                  {
                    offset: 0,
                    color: '#00e3f3',
                  },
                  {
                    offset: 1,
                    color: '#1373bc',
                  },
                ],
                global: false,
              },


            },
            data: [10, 52, 200, 334, 390, 330, 220]
          },
          // 中间的长方形柱状图(柱状图):bar
          {
            type: 'bar', // 柱状图
            barWidth: 40, // 柱条的宽度,不设时自适应
            barGap: '0%', // 柱子与柱子之间的距离
            itemStyle: { // 图形样式
              // color支持(rgb(255,255,255)、rgba(255,255,255,1)、#fff,也支持渐变色和纹理填充)
              // 下面就是使用线性渐变
              color: {
                "x": 0,
                "y": 0,
                "x2": 0,
                "y2": 1,
                "type": "linear",
                "global": false,
                "colorStops": [{
                  "offset": 0, // 0%处的颜色
                  "color": "rgba(0,229,255,0.5)"
                }, {
                  "offset": 1, // 100%处的颜色
                  "color": "#1F9BFF"
                }]
              },

            },
            data: [10, 52, 200, 334, 390, 330, 220]
          },
          // 顶部的椭圆形(象形柱图):pictorialBar
          {
            type: "pictorialBar",
            symbolSize: [40, 20],
            symbolOffset: [0, -10],
            z: 12,
            symbolPosition: "end",
            itemStyle: {
              color: {
                type: 'linear',
                x: 0,
                y: 0,
                x2: 0,
                y2: 1,
                colorStops: [  // 渐变颜色
                  {
                    offset: 0,
                    color: '#00e3f3',
                  },
                  {
                    offset: 1,
                    color: '#1373bc',
                  },
                ],
                global: false,
              },
            },
            data: [10, 52, 200, 334, 390, 330, 220]
          }
        ]
      })

      window.addEventListener("resize", function () {
        // 让我们的图表调用 resize这个方法
        barChart.resize();
      })
    },

    // 图3图表
    getScatterEcharts() {
      this.scatterData = this.$echarts.init(document.getElementById("scatterChart"));
      let cellSize = [window.innerWidth / 19, window.innerHeight / 17];
      let pieRadius = window.innerWidth / 80;
      //饼图数据从后端获取
      let json = [
        { "data": [{ "name": "已完成", "value": 30, itemStyle: { color: '#43d0ff' } }, { "name": "未完成", "value": 10, itemStyle: { color: '#343bee' } }, { "name": "未巡查", "value": 56, itemStyle: { color: '#6a64fd' } }] },
        { "data": [{ "name": "已完成", "value": 30, itemStyle: { color: '#43d0ff' } }, { "name": "未完成", "value": 10, itemStyle: { color: '#343bee' } }, { "name": "未巡查", "value": 56, itemStyle: { color: '#6a64fd' } }] },
        { "data": [{ "name": "已完成", "value": 30, itemStyle: { color: '#43d0ff' } }, { "name": "未完成", "value": 10, itemStyle: { color: '#343bee' } }, { "name": "未巡查", "value": 56, itemStyle: { color: '#6a64fd' } }] },
        { "data": [{ "name": "已完成", "value": 30, itemStyle: { color: '#43d0ff' } }, { "name": "未完成", "value": 10, itemStyle: { color: '#343bee' } }, { "name": "未巡查", "value": 56, itemStyle: { color: '#6a64fd' } }] },
        { "data": [{ "name": "已完成", "value": 30, itemStyle: { color: '#43d0ff' } }, { "name": "未完成", "value": 10, itemStyle: { color: '#343bee' } }, { "name": "未巡查", "value": 56, itemStyle: { color: '#6a64fd' } }] },
        { "data": [{ "name": "已完成", "value": 30, itemStyle: { color: '#43d0ff' } }, { "name": "未完成", "value": 10, itemStyle: { color: '#343bee' } }, { "name": "未巡查", "value": 56, itemStyle: { color: '#6a64fd' } }] },
      ]
      let startDate = '2017-05';   //开始值从后端获取
      let endDate = '2017-06';   //结束值从后端获取
      // let data_name = ['已完成', '未完成', '未巡查'];//该出应该是从数据库中读取data_name
      let options = this.calendar(cellSize, pieRadius, startDate, endDate, json);
      options && this.scatterData.setOption(options)

    },
    calendar(cellSize, pieRadius, startDate, endDate, json, data_name) {
      let data_arr = [];
      let _this = this;

      for (let i = 0; i < json.length; i++) {
        data_arr.push(json[i].data);
      }

      function getVirtulData() {
        let date = +(_this.$echarts.number.parseDate(startDate));
        let end = +(_this.$echarts.number.parseDate(endDate));
        let dayTime = 3600 * 24 * 1000;
        let data = [];
        for (let time = date; time < end; time += dayTime) {
          data.push([
            _this.$echarts.format.formatTime('yyyy-MM-dd', time),
            Math.floor(Math.random() * 10000)
          ]);
        }
        return data;
      }

      function getPieSeries(scatterData, chart) {
        return _this.$echarts.util.map(scatterData, function (item, index) {
          let center = chart.convertToPixel('calendar', item);
          return {
            id: index + 'pie',
            type: 'pie',
            center: center,
            label: {
              normal: {
                formatter: '{c}',
                position: 'inside'
              }
            },
            radius: pieRadius,
            data: data_arr[index]
          };
        });
      }

      // 使用刚指定的配置项和数据显示图表。
      function getPieSeriesUpdate(scatterData, chart) {
        return _this.$echarts.util.map(scatterData, function (item, index) {
          let center = chart.convertToPixel('calendar', item);
          return {
            id: index + 'pie',
            center: center
          };
        });
      }
      let scatterData = getVirtulData();
      let option = {
        tooltip: {},
        // legend: {
        //   data: data_name,
        //   bottom: 20
        // },
        calendar: {
          top: '38', //图表位置
          left: 'center', //横向
          orient: 'vertical',
          cellSize: cellSize,  //尺寸
          yearLabel: {
            show: false,
            textStyle: {
              fontSize: 30
            },

          },
          itemStyle: {
            borderColor: '#3f58a3',  // 边框颜色
            borderWidth: 1,       // 边框宽度
            color: '#192143'
          },
          // //星期表头位置,颜色
          dayLabel: {
            margin: 10,
            firstDay: 1,
            nameMap: ['星期日', '星期一', '星期二', '星期三', '星期四', '星期五', '星期六'],
            color: '#fff',
          },
          //左侧月份隐藏
          monthLabel: {
            show: false,
          },
          range: [startDate], // 后端获取起始范围

        },
        series: [{
          id: 'label',
          type: 'scatter',
          coordinateSystem: 'calendar',  //指定饼图所使用的坐标系为日历坐标系。在日历坐标系中,每个日期对应图表中的一个格子
          symbolSize: 1,
          label: {
            normal: {
              show: true,
              // 使用时间格式化函数将日期显示为天数
              formatter: function (params) {
                return _this.$echarts.format.formatTime('dd', params.value[0]);
              },
              offset: [-cellSize[0] / 2 + 10, -cellSize[1] / 2 + 10],// 标签位置的偏移量
              textStyle: {
                color: '#fff',
                fontSize: 14
              },
            },
          },
          data: scatterData // 天数
        }]
      };
      // 监听窗口大小变化事件
      window.addEventListener("resize", () => {
        cellSize = [window.innerWidth / 19, window.innerHeight / 17];
        pieRadius = window.innerWidth / 80;
        this.scatterData.setOption(this.calendar(cellSize, pieRadius, startDate, endDate, json, data_name));
        this.scatterData.resize();
      });

      if (!_this.scatterData.inNode) {
        let pieInitialized;
        setTimeout(function () {
          pieInitialized = true;
          // 初始化饼图的系列
          _this.scatterData.setOption({
            series: getPieSeries(scatterData, _this.scatterData)
          });
        }, 10);
      }
      return option;
    },
    // 图4折线图
    getLineEcharts() {
      let lineChart = this.$echarts.init(document.getElementById("lineChart"));
      lineChart.setOption({
        tooltip: {
          trigger: 'axis',
          axisPointer: {
            type: 'shadow',
          }
        },
        xAxis: {
          type: 'category',
          boundaryGap: false,
          axisTick: {
            show: false,
          },
          //x轴颜色
          axisLine: {
            lineStyle: {
              color: '#253262',
            },
          },
          //  x轴文字的颜色设置为白色
          axisLabel: {
            color: '#fff'
          },
          data: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']
        },
        yAxis: {
          type: 'value',
          // 不显示y轴的线
          axisLine: {
            show: false
          },
          // 不显示y轴刻度
          axisTick: {
            show: false
          },
          // 分割线颜色
          splitLine: {
            lineStyle: {
              color: '#253262',
              // type: 'dashed' //虚线
            }
          },
          // y轴刻度文字颜色设置为白色
          axisLabel: {
            color: "#fff",
          },
        },
        series: [
          {
            data: [820, 932, 901, 934, 1290, 1330, 1320],
            type: 'line',
            color: '#3384f3',
            areaStyle: {
              color: {
                type: 'linear',
                x: 0,
                y: 0,
                x2: 0,
                y2: 1,
                colorStops: [  // 渐变颜色
                  {
                    offset: 0,
                    color: '#213f79',
                  },
                  {
                    offset: 1,
                    color: '#192143',
                  },
                ],
                global: false,
              },

            }
          }
        ]
      })
      window.addEventListener("resize", function () {
        // 让我们的图表调用 resize这个方法
        lineChart.resize();
      })
    },

  }
}
</script>
<style lang="scss" scoped>
.data-serve {
  width: 100%;
  height: 100%;
  display: flex;
  padding: 30px 10px 0 10px;

  .side {
    width: 240px;
    box-sizing: border-box;
    height: 100%;
    padding-left: 20px;

    .side-inner {
      width: 100%;
      height: 100%;
      box-shadow: 0 0 5px rgba(20, 120, 210, 0.6) inset;
      border: 1px solid #0070da;
      padding: 10px 20px 20px 20px;

      .side-btn {
        width: 100%;
        display: block;
        color: #fff;
        font-size: 12px;
        margin-top: 10px;
        padding: 8px 10px 8px 20px;
        letter-spacing: 3px;
        background-image: linear-gradient(to right, #3e4e72, transparent);

        i {
          margin-right: 10px;
        }
      }
    }
  }

  .main {
    width: calc(100% - 240px);
    height: 100%;
    padding: 0 20px;

    .chart-side {
      display: flex;
      width: 100%;
      height: calc(100% / 2 - 10px);
      justify-content: space-between;
      margin-top: 10px;


      .chart-wrapper {
        width: calc(100% / 2 - 5px);
        height: 100%;

        .chart-bg {
          width: 100%;
          height: 30px;
          line-height: 30px;
          padding-left: 20px;
          font-size: 14px;
          background-image: linear-gradient(to right, rgba(31, 42, 79, 0.9), rgba(255, 0, 0, 0));
          color: #fff;

          span {
            border-left: 4px solid #6fb3e8;
            padding-left: 10px;
          }
        }

        .chart {
          width: 100%;
          height: calc(100% - 40px);
          background-color: #192143;
          margin-top: 10px;

          .pieChart,
          .echart-line,
          .echart-scatter,
          .echart-bar {
            width: 100%;
            height: 100%;

          }
        }
      }
    }
  }
}
</style>

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
使用 echarts 将列表数据展示为柱形图饼状图需要经过以下几个步骤: 1. 引入 echarts 库 在项目中引入 echarts 库,可以使用 npm 或者直接下载 echarts.min.js 文件。 2. 准备数据 将需要展示的数据准备好,可以是数组、对象等格式。对于柱形图,需要准备好 x 轴和 y 轴的数据;对于饼状图,需要准备好每个饼块的名称和数值。 3. 创建 echarts 实例 在页面中创建一个 div 元素,并给它设置一个 id,用于创建 echarts 实例。例如: ``` <div id="myChart" style="width: 600px;height:400px;"></div> ``` 然后在 JavaScript 中创建 echarts 实例,例如: ``` const myChart = echarts.init(document.getElementById('myChart')); ``` 4. 配置图表项 对于柱形图,需要配置 x 轴和 y 轴的数据,以及柱形图的样式、颜色等。例如: ``` option = { xAxis: { type: 'category', data: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'] }, yAxis: { type: 'value' }, series: [{ data: [820, 932, 901, 934, 1290, 1330, 1320], type: 'bar' }] }; ``` 对于饼状图,需要配置每个饼块的名称和数值,以及饼状图的样式、颜色等。例如: ``` option = { series : [ { name: '访问来源', type: 'pie', radius: '55%', data:[ {value:335, name:'直接访问'}, {value:310, name:'邮件营销'}, {value:234, name:'联盟广告'}, {value:135, name:'视频广告'}, {value:1548, name:'搜索引擎'} ] } ] }; ``` 5. 渲染图表 将配置好的 option 对象传入 echarts 实例中,并调用实例的 setOption 方法来渲染图表。例如: ``` myChart.setOption(option); ``` 这样就可以将列表数据展示为柱形图饼状图了。同时,echarts 还支持很多其他类型的图表,可以根据需求选择使用。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值