echarts图表-折柱混合

本文通过实例展示了如何使用图表展示供水量、用水量数据以及产销差情况,利用ECharts库创建了柱状图和折线图,详细解释了数据结构和配置选项。

效果图
在这里插入图片描述
数据结构

const nrwData = [
      { label: "10", proviceWater: "100.45", userWater: "55", nrwRate: 80.65 },
      { label: "11", proviceWater: "80", userWater: "80", nrwRate: 70 },
      { label: "12", proviceWater: "81.45", userWater: "67", nrwRate: 89 },
      { label: "01", proviceWater: "145.45", userWater: "140.45", nrwRate: 55 },
      { label: "02", proviceWater: "60.45", userWater: "45", nrwRate: 43.65 },
      { label: "03", proviceWater: "55", userWater: "50", nrwRate: 85.65 },
      { label: "10", proviceWater: "100.45", userWater: "55", nrwRate: 80.65 },
      { label: "11", proviceWater: "80", userWater: "80", nrwRate: 70 },
      { label: "12", proviceWater: "81.45", userWater: "67", nrwRate: 89 },
      { label: "01", proviceWater: "145.45", userWater: "140.45", nrwRate: 55 },
      { label: "02", proviceWater: "60.45", userWater: "45", nrwRate: 43.65 },
      { label: "03", proviceWater: "55", userWater: "50", nrwRate: 85.65 }
    ];

具体代码

var xData3 = nrwData?.map((item) => item.label);
const proviceWater = nrwData?.map((item) => item.proviceWater <= 0 ? 0 : item.proviceWater);
const userWater = nrwData?.map((item) => item.userWater <= 0 ? 0 : item.userWater);
const lineData = nrwData?.map((item) => item.nrwRate >= 100 ? 0 : item.nrwRate);
var option = {
      tooltip: {
        trigger: "axis",
        show: true,
        backgroundColor: "rgba(16, 34, 79, 0.9)",
        borderColor: "#274370",
        textStyle: {
          color: "#fff",
          fontSize: 8,
        }
      },
      legend: {
        show: true,
        itemWidth: 20,
        itemHeight: 10,
        itemGap: 10,
        textStyle: {
          fontSize: 10,
          color: "#ccc",
        },
      },
      grid: {
        left: 30,
        bottom: 20,
        top: 30,
        right: 30,
      },
      xAxis: {
        data: xData3,
        name: "\n\n\n(月)",
        nameTextStyle: {
          color: "#7089ba",
          fontSize: "10px"
        },
        // 坐标轴刻度相关设置
        axisTick: {
          show: false,
        },
        nameGap: -9,
        // 坐标轴线的相关设置
        axisLine: {
          show: true,
          lineStyle: {
            color: "#414965",
          },
        },
        // 坐标轴刻度标签的相关设置
        axisLabel: {
          // 可以设置成 0 强制显示所有标签
          interval: 0,
          textStyle: {
            color: "#7089ba",
            fontSize: "10px"
          },
          margin: 10, //刻度标签与轴线之间的距离。
        },
      },
      yAxis: [{
        type: "value",
        name: "单位:万m³",
        nameTextStyle: {
          color: "#fff",
          fontSize: "10px"
        },
        // 坐标轴在grid区域中的分隔线
        splitLine: {
          show: false,
          lineStyle: {
            color: "#414965",
            opacity: 0.3,
          },
        },
        axisTick: {
          show: false,
        },
        axisLine: {
          show: false,
        },
        axisLabel: {
          textStyle: {
            color: "#7089ba",
            fontSize: "10px",
          },
        },
      },
      {
        type: "value",
        name: "产销差(%)",
        min: 0,
        max: 100,
        nameTextStyle: {
          color: "#fff",
          fontFamily: "PingFangSC-Light",
          fontSize: "10px",
        },
        axisLabel: {
          textStyle: {
            color: "#7089ba",
            fontSize: "10px"
          }
        },
        axisTick: {
          show: false,
        },
        axisLine: {
          show: false,
        },
        splitLine: {
          show: true,
          lineStyle: {
            color: "#414965",
            opacity: 0.3,
          },
        },
      },
      ],
      series: [
        {
          name: "供水量",
          type: "bar",
          barWidth: 10,
          itemStyle: {
            opacity: 0.1,
            normal: {
              show: true,
              color: "#8c3ed8",
              barBorderRadius: 0,
              borderWidth: 0,
            },
          },
          label: {
            show: false,//数据不进行显示
            position: "top",
            distance: 10,
            color: "#fff",
          },
          tooltip: {
            valueFormatter: function (value) {
              return value + "万m³";
            }
          },
          data: proviceWater,
        },
        {
          name: "用水量",
          type: "bar",
          barWidth: 10,
          itemStyle: {
            opacity: 0.1,
            normal: {
              show: true,
              color: new echarts.graphic.LinearGradient(0, 1, 0, 0, [
                {
                  offset: 0,
                  color: "#1134ac",
                },
                {
                  offset: 1,
                  color: "#4aaaf8",
                },
              ]),
              barBorderRadius: 0,
              borderWidth: 0,
            },
          },
          label: {
            show: false,//数据不进行显示
            position: "top",
            distance: 10,
            color: "#fff",
          },
          tooltip: {
            valueFormatter: function (value) {
              return value + "万m³";
            }
          },
          data: userWater,
        },
        /*折线图*/
        {
          name: "产销差",
          type: "line",
          yAxisIndex: 1,
          z: 15,
          tooltip: {
            valueFormatter: function (value) {
              return value + "%";
            }
          },
          symbol: "circle",
          // symbolSize: 10,
          itemStyle: {
            normal: {
              color: "#84fbfb",
              borderColor: "#84fbfb",  //拐点边框颜色
              lineStyle: {
                color: "#84fbfb"//折线的颜色
              },
            },
          },
          data: lineData,
        }
      ],
    };
ECharts 是一个基于 JavaScript 的开源图表库,可以用来创建各种交互式的数据可视化图表。它提供了丰富的图表类型,包括线图、状图、饼图等等。 要创建一个混合图表,你可以使用 ECharts 提供的组合图表功能。以下是一个简单的示例代码,展示了如何使用 ECharts 创建一个混合图表: ```javascript // 引入 ECharts import echarts from 'echarts'; // 初始化图表容器 const chartContainer = document.getElementById('chart'); // 创建图表实例 const chart = echarts.init(chartContainer); // 定义图表配置项 const option = { // 设置图表标题 title: { text: '混合图表示例' }, // 设置图表提示框组件 tooltip: {}, // 设置图例组件 legend: { data: ['线', '状'] }, // 设置 x 轴配置项 xAxis: { data: ['A', 'B', 'C', 'D', 'E'] }, // 设置 y 轴配置项 yAxis: {}, // 设置数据系列 series: [ { name: '线', type: 'line', data: [10, 20, 30, 40, 50] }, { name: '状', type: 'bar', data: [20, 40, 60, 80, 100] } ] }; // 使用配置项生成图表 chart.setOption(option);``` 以上代码中,我们通过引入 ECharts 库,创建一个图表容器,并初始化一个图表实例。然后,定义了图表的配置项,包括标题、提示框、图例、x 轴、y 轴和数据系列等。最后,使用 `setOption` 方法将配置项应用到图表中,即可呈现一个混合图表。 你可以根据自己的需求,修改配置项中的数据和样式,来创建适合自己的混合图表。希望对你有帮助!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值