ECharts 微信小程序上的应用--柱形图与折线图叠加使用

在微信小程序上使用ECharts添加图表–柱形图与折线图叠加使用

  1. 很好用的一个组件[传送门]
  2. 现在列举一个示例进行解析

先在json中配置

"usingComponents": {
    "ec-canvas": "../../ec-canvas/ec-canvas"
  }

在wxml中

<ec-canvas id="mychart-receiveRecord-multi-bar" 
canvas-id="mychart-receiveRecord-bar" ec="{{ ecBar }}">
</ec-canvas>

在wxss中

#mychart-receiveRecord-multi-bar{
  width: 100%;
  height: 600px;
  display: block;
}

在js中

import * as echarts from "../../ec-canvas/echarts.min";
Page({
  /**
   * 页面的初始数据
   */
  data: {
    // 柱形图
    ecBar: {
      onInit: function (canvas, width, height, dpr) {
        const barChart = echarts.init(canvas, null, {
          width: width,
          height: height,
          devicePixelRatio: dpr // new
        });
        canvas.setChart(barChart);
        barChart.setOption(getBarOption());

        return barChart;
      }
    },
  }
})

function getBarOption () {
  const data = dReceiveRecordList.sort(function (a, b) {
    return b.ReceiveCount - a.ReceiveCount;
  });
  // console.log(data)
  const dataLaboratoryNamelist = [];
  const dataReceiveCountlist = [];
  const dataSendCountlist = [];
  const datanotReceiveCount = [];
  const dataCurrentReceptionRatess = [];
  data.forEach(Element => {
    dataLaboratoryNamelist.push(Element.LaboratoryName);
    dataReceiveCountlist.push(Element.ReceiveCount);
    dataSendCountlist.push(Element.SendCount);
    datanotReceiveCount.push(Element.notReceiveCount);
    dataCurrentReceptionRatess.push(((Element.ReceiveCount / Element.TotalReceiveCount) * 100).toFixed(2));
  });
  //以上是将数据数组化便于后面调用
  return {
    color: ["#F7A35C", "#8085E9", "#F15C80"],
    tooltip: {
      trigger: "axis",
      axisPointer: { // 坐标轴指示器,坐标轴触发有效
        type: "shadow" // 默认为直线,可选为:'line' | 'shadow'
      }
    },
    legend: {// 可选值及数据的分类
      data: ["接收量", "发送量", "滞留量", "总接收率"]
    },
    grid: {
      left: "2%",
      right: "2%",
      bottom: "5%",
      top: "5%",
      containLabel: true
    },
    xAxis: [{//bottom x轴
      type: "value",//数据类型 
      axisLine: {
        lineStyle: {
          color: "#999"
        }
      },
      axisLabel: {
        color: "#666"
      }
    }, {//top x轴
      type: "value",
      axisLine: {
        lineStyle: {
          color: "#999"
        }
      },
      axisLabel: {
        color: "#666"
      }
    }],
    yAxis: [{//left y轴
      type: "category",
      axisTick: {
        show: false
      },
      data: dataLaboratoryNamelist,// y轴数据
      axisLine: {
        lineStyle: {
          color: "#999"
        }
      },
      axisLabel: {
        color: "#666"
      }
    }],
    series: [{
      name: "接收量",
      type: "bar",// 柱状形式
      data: dataReceiveCountlist// 数据  注:以数组形式
      //例子: ["50","20","30","40","60","70"]
    },
    {
      name: "发送量",
      type: "bar",
      data: dataSendCountlist
    },
    {
      name: "滞留量",
      type: "bar",
      data: datanotReceiveCount
    },
    {
      name: "总接收率",
      type: "line",//折线形式
      xAxisIndex: 1,// x轴下标,在这里是指top x轴
      data: dataCurrentReceptionRatess
    }
    ]
  };
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
ECharts 是一个基于 JavaScript 的开源可视化库,支持多种类型的图表,包括 3D 柱形图。而 Liquidfill 则是 ECharts 中的一个插件,可以用来创建水球图,即圆形的图表,同时支持动画效果。 要创建一个 3D 柱形图,需要在 ECharts 中引入 echarts-gl(ECharts 的 WebGL 渲染扩展库)和 echarts-liquidfill 插件。代码示例: ```js // 引入所需的模块 import * as echarts from 'echarts'; import 'echarts-gl'; import 'echarts-liquidfill'; // 初始化 ECharts 实例 const chart = echarts.init(document.getElementById('chart')); // 定义数据 const data = [ { name: 'A', value: [0, 0, 5] }, { name: 'B', value: [1, 0, 6] }, { name: 'C', value: [2, 0, 8] }, { name: 'D', value: [0, 1, 7] }, { name: 'E', value: [1, 1, 2] }, { name: 'F', value: [2, 1, 9] }, { name: 'G', value: [0, 2, 3] }, { name: 'H', value: [1, 2, 1] }, { name: 'I', value: [2, 2, 4] }, ]; // 定义配置项 const option = { grid3D: {}, xAxis3D: { type: 'category', data: ['X', 'Y', 'Z'], }, yAxis3D: { type: 'category', data: ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I'], }, zAxis3D: {}, series: [{ type: 'bar3D', data: data.map(item => ({ value: item.value.concat(item.value[2]), itemStyle: { color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [{ offset: 0, color: '#00aaff', }, { offset: 1, color: '#00ffaa', }]), }, })), shading: 'color', barSize: 1, opacity: 0.8, }, { type: 'liquidFill', data: data.map(item => ({ value: item.value[2] / 10, itemStyle: { color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [{ offset: 0, color: '#00aaff', }, { offset: 1, color: '#00ffaa', }]), }, })), shape: 'circle', label: { show: true, fontSize: 16, color: '#fff', formatter: '{b}', }, }], }; // 使用配置项生成图表 chart.setOption(option); ``` 上述代码中,`data` 数组定义了每个柱形图的名称和坐标位置,`option` 对象则定义了图表的配置项。其中,`grid3D`、`xAxis3D`、`yAxis3D` 和 `zAxis3D` 用于定义 3D 坐标系,`series` 则用于定义 3D 柱形图和水球图。柱形图的样式可以通过 `itemStyle` 属性进行设置,而水球图则可以通过 `liquidFill` 类型的系列来创建。 最后,使用 `setOption` 方法将配置项传入,即可在页面中生成 3D 柱形图

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值