echarts柱形图

该代码示例展示了如何使用Echarts库创建一个柱形图,包括设置x轴和y轴的数据,定义柱状颜色的线性渐变,以及调整图表的样式和布局。数据包括四个类别(A,B,C,D),每个类别有对应的数据数组。

echarts柱形图

在这里插入图片描述

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <script src="https://cdn.jsdelivr.net/npm/echarts@5.4.1/dist/echarts.min.js"></script>
  </head>
  <body>
    <div
      class="echart3"
      id="echarts3"
      style="width: 100%; height: 250px;background: rgb(0, 34, 69);"
    ></div>

    <script>
      const data = {
        data4: ["name1", "name2"],
        data3: ["1000", "2000"],
        data2: ["600", "800"],
        data1: ["500", "1200"],
        data0: ["700", "200"],
      };

      const myChart = echarts.init(document.getElementById("echarts3"));
      const option = {
        xAxis: {
          type: "category",
          data: data.data4,
          axisLabel: {
            formatter: "{value}",
            textStyle: {
              color: "#fff",
            },
          },
          axisLine: {
            lineStyle: {
              color: "#335B7E",
              type: "dashed",
            },
          },
        },
        grid: {
          top: "33%",
          left: "10%",
          right: "10%",
          bottom: "10%",
          containLabel: false,
        },

        tooltip: {
          trigger: "axis",
          axisPointer: {
            type: "cross",
            crossStyle: {
              color: "#999",
            },
          },
        },
        legend: {
          data: ["A", "B", "C", "D"],
          textStyle: {
            color: "#fff",
          },
        },
        yAxis: [
          {
            type: "value",
            name: "单位:个",
            nameTextStyle: {
              color: "#ffff",
            },
            splitLine: {
              show: true,
              lineStyle: {
                color: "#335B7E",
                type: "dashed",
              },
            },
            axisLine: {
              show: true,
              lineStyle: {
                color: "#335B7E",
                type: "solid",
              },
            },
            axisLabel: {
              formatter: "{value}",
              // y轴文字的配置
              textStyle: {
                color: "rgba(219,225,255,1)",
                margin: 15,
              },
              // formatter: '{value} %'//y轴的每一个刻度值后面加上‘%’号
            },
          },
        ],

        series: [
          {
            type: "bar",
            name: "A",
            barWidth: 12,
            data: data.data0,
            itemStyle: {
              normal: {
                color: new echarts.graphic.LinearGradient(1, 0, 1, 1, [
                  {
                    offset: 0,
                    color: "#37A2DA",
                  },
                  {
                    offset: 1,
                    color: "#264f79",
                  },
                ]),
              },
            },
          },
          {
            type: "bar",
            barWidth: 12,
            name: "B",
            data: data.data1,
            itemStyle: {
              normal: {
                color: new echarts.graphic.LinearGradient(1, 0, 1, 1, [
                  {
                    offset: 0,
                    color: "#E2C054",
                  },
                  {
                    offset: 1,
                    color: "#264f79",
                  },
                ]),
              },
            },
          },
          {
            type: "bar",
            barWidth: 12,
            name: "C",
            data: data.data2,
            itemStyle: {
              normal: {
                color: new echarts.graphic.LinearGradient(1, 0, 1, 1, [
                  {
                    offset: 0,
                    color: "#B6A2DE",
                  },
                  {
                    offset: 1,
                    color: "#264f79",
                  },
                ]),
              },
            },
          },
          {
            type: "bar",
            barWidth: 12,
            name: "D",
            data: data.data3,
            itemStyle: {
              normal: {
                color: new echarts.graphic.LinearGradient(1, 0, 1, 1, [
                  {
                    offset: 0,
                    color: "#00FFFF",
                  },
                  {
                    offset: 1,
                    color: "#264f79",
                  },
                ]),
              },
            },
          },
        ],
      };
      myChart.setOption(option);
      window.onresize = function() {
        myChart.resize();
      };
    </script>
  </body>
</html>

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

代码工人笔记

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值