使用echart绘制一个立体柱状图

效果:
在这里插入图片描述
最近在做大屏遇到了好多用echart绘图,话不多说上代码

      let Mycharts= echarts.init(
        document.getElementById("histogram")
      );

      let option = {
        title: {
          text: "个",
          position: "left",
          top: "-3%",
          textStyle: {
            color: "#41A6FC",
            fontSize: this.fontSizeRem(24),
          },
        },
        grid: {
          top: "15%",
          left: "1%",
          right: "1%",
          bottom: "3%",
          containLabel: true,
        },
        xAxis: {
          type: "category",
          data: ["xxx", "xxx", "xxx", "xxx"],
          axisLabel: {
            show: true,
            interval: 0,
            textStyle: {
              color: "#41A6FC",
              fontSize: this.fontSizeRem(24),
            },
          },
          splitLine: {
            show: false,
            lineStyle: {
              // 分割线颜色
              color: "#2D3C5C ",
              opacity: 0.3,
            },
          },
        },

        yAxis: {
          type: "value",
          axisLabel: {
            show: true,
            textStyle: {
              color: "#41A6FC",
              fontSize: this.fontSizeRem(24),
            },
          },
          splitLine: {
            show: true,
            lineStyle: {
              // 分割线颜色
              color: "#2D3C5C ",
              opacity: 0.3,
            },
          },
        },
        series: [
          柱子左面设置立体正方
          {
            // yAxisIndex: 0,
            data: [20, 25, 30, 35],
            type: "bar",
            barWidth: 9,
            itemStyle: {
              normal: {
                // //柱子左面重要的是color配置:线性渐变, 从上往下
                color: {
                  type: "linear",
                  x: 1,
                  y: 0,
                  x2: 1,
                  y2: 1,
                  colorStops: [
                    {
                      offset: 0,
                      color: "rgb(0, 192, 238,0.8)",
                    },
                    {
                      offset: 0.8,
                      color: "rgb(0, 194, 239,0.2)",
                    },
                    {
                      offset: 1,
                      color: "rgb(0, 194, 239,0)",
                    },
                  ],
                },
              },
            },
          },
          // //柱子右面
          {
            yAxisIndex: 0,
            tooltip: {
              show: true,
              padding: 100,
              position: [10, "50%"],
              borderWidth: 100,
            },
            type: "bar",
            barWidth: 13,
            barCategoryGap: "60%",
            itemStyle: {
              normal: {
                // 重要的是color配置:线性渐变, 从上往下
                color: {
                  type: "linear",
                  x: 1,
                  y: 0,
                  x2: 1,
                  y2: 1,
                  colorStops: [
                    {
                      offset: 0,
                      color: "#00CCF5 ",
                    },
                    {
                      offset: 0.8,
                      color: "rgb(4, 88, 115,0.8)",
                    },
                    {
                      offset: 1,
                      color: "rgb(4, 88, 115,0.6)",
                    },
                  ],
                },
                borderWidth: 0.3,
                borderColor: "rgba(76,182,251, 0.5)",
              },
            },
            data: [20, 25, 30, 35],
            // 间隔为0
            barGap: 0,
            z: 5,
          },
          // 这里是柱子的顶部
          {
            yAxisIndex: 0,
            z: 10,
            // type: "pictorialBar" 可以自定义图形或者使用内置选项
            type: "pictorialBar",
            symbolPosition: "end",
            data: [20, 25, 30, 35],
            symbol: "diamond",
            symbolOffset: [-0.5, -12.5],
            symbolRotate: 90,
            symbolSize: [8.5, 23.6],
            label: {
              show: true, //开启显示
              position: "top", //在上方显示
              formatter: function (data) {
                return data.value + "" + "站点";
              },
              textStyle: {
                //数值样式
                color: "#FFFFFF",
                fontSize: this.fontSizeRem(34),
              },
            },
            itemStyle: {
              normal: {
                borderWidth: 0,

                color: "rgba(69,216,249, 1)",
              },
            },
            tooltip: {
              show: false,
            },
          },
        ],
      };

      // 使用刚指定的配置项和数据显示图表。

      Mycharts.setOption(option);

fontSizeRem是我自己封装的可自适应屏幕字体大小的函数

      fontSizeRem(size){
            const clientWidth = window.innerWidth || document.documentElement.clientWidth ||
              document.body.clientWidth;
            if (!clientWidth) return;
            let fontSize = clientWidth / 1920;//尺寸大小
            return size* fontSize;
          
      }
使用 ECharts 绘制柱状图需要几个步骤: 1. 引入 ECharts 库和需要的主题。可以从 ECharts 官网下载或使用在线链接。 ```html <!-- 引入 ECharts 库 --> <script src="https://cdn.jsdelivr.net/npm/echarts/dist/echarts.min.js"></script> <!-- 引入 ECharts 主题 --> <script src="https://cdn.jsdelivr.net/npm/echarts/theme/macarons.js"></script> ``` 2. 创建一个 DOM 容器,并设置宽度、高度。 ```html <div id="chart" style="width: 800px; height: 400px;"></div> ``` 3. 使用 PHP 代码从数据库中获取数据,并将数据转换为 JavaScript 对象。 ```php // 连接数据库 $conn = mysqli_connect("localhost", "username", "password", "database"); // 查询数据 $result = mysqli_query($conn, "SELECT name, value FROM table"); // 将数据转换为 JavaScript 对象 $data = array(); while ($row = mysqli_fetch_assoc($result)) { $data[] = array("name" => $row["name"], "value" => $row["value"]); } $json_data = json_encode($data); ``` 4. 编写 JavaScript 代码,使用 ECharts 绘制柱状图。 ```javascript // 获取数据 var data = <?php echo $json_data; ?>; // 初始化 ECharts 实例 var chart = echarts.init(document.getElementById('chart'), 'macarons'); // 配置项 var option = { title: { text: '柱状图' }, tooltip: {}, xAxis: { data: data.map(function(item) { return item.name; }) }, yAxis: {}, series: [{ name: '销量', type: 'bar', data: data.map(function(item) { return item.value; }) }] }; // 使用配置项显示图表 chart.setOption(option); ``` 以上代码实现了从数据库中获取数据,将数据转换为 JavaScript 对象,然后使用 ECharts 绘制柱状图的功能。可以根据自己的数据库和需求进行相应的修改。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值