echarts 多柱状图: 随接口返回的条数展示柱状图:

这段代码使用Echarts库创建一个图表,展示从接口获取的功率数据。数据以每小时为单位,每个柱状图表示一个小时的输入和输出功率,最多显示四个柱子。此外,代码还包含了输入和输出功率的环形图表示,以及详细的图例和提示信息。数据根据接口返回的条目数量动态调整,最多支持四个设备的数据展示。
摘要由CSDN通过智能技术生成

记录一下自己的代码 ,写的很垃圾 ,有好多的方法麻烦指出

如果接口哪个里面是三条数据 就是三条柱状图:

接口数据格式:

 

代码:最多是四条数据  就这个图最多是四个柱子在一起: 

 

      creatchart() {
        const chartDom = document.getElementById('theChart');
        const myChart = echarts.init(chartDom);
        let maxnum = 0;
        this.$api.get('wp/ocable/selectOpticalCable').then(res => {
          this.echartsData = res.data;
          // console.log('===>', res);
          for (const item in res.data) {
            // console.log('aaaaaaaaaaaaaaaaaaaaaa', item, res.data[item], res.data);
            if (res.data[item].length > maxnum) {
              maxnum = res.data[item].length;
            }
          }

          const inalldata = [];
          const outalldata = [];
          for (let i = 0; i < maxnum; i++) {
            // 4GE
            inalldata[i] = [];
            outalldata[i] = [];
          }

          const newchartName = [];
          for (let i = 0; i < 24; i++) {
            newchartName.push(' ' + i + '时-' + (i + 1) + '时');
            for (let j = 0; j < maxnum; j++) {
              inalldata[j].push([i, '', '']); // 输入功率
              outalldata[j].push([i, '', '']); // 输出功率
            }
          }
          // console.log('aaaaaaaaaaaaaaa', inalldata, outalldata);
          for (const item in this.echartsData) {
            // console.log('aaaaaaaaaaaaaaaaaaaaaa', item, res.data[item], res.data);
            // this.echartsData[item].length
            // console.log('=================>', this.echartsData[item]);
            for (let i = 0; i < this.echartsData[item].length; i++) {
              if (Number(item)) {
                inalldata[i][Number(item)][1] = Number(this.echartsData[item][i].fieldCntentent);
                inalldata[i][Number(item)][2] = this.echartsData[item][i].deviceName;
                outalldata[i][Number(item)][1] = Number(this.echartsData[item][i].exporCntentent);
                outalldata[i][Number(item)][2] = this.echartsData[item][i].deviceName;
              }
            }
          }
          console.log('aaaaaaaaaaaaaaa', inalldata, outalldata);
          const series = [];
          for (let j = 0; j < maxnum; j++) {
            series.push({
              name: '输入功率',
              type: 'bar',
              // barGap: '25%',
              barWidth: 18,
              itemStyle: {
                // barBorderRadius:20,
                barBorderRadius: 20,
                // lenged文本
                opacity: 0.7,
                color: function (params) {
                  return new echarts.graphic.LinearGradient(
                    0,
                    0,
                    0,
                    1,
                    [
                      {
                        offset: 0,
                        color: '#00F7FD' // 0% 处的颜色
                      },
                      {
                        offset: 1,
                        color: '#2F14AB' // 100% 处的颜色
                      }
                    ],
                    false
                  );
                }
              },
              label: {
                normal: {
                  show: true,
                  position: 'inside',
                  fontSize: 15,
                  fontWeight: 'bold',
                  color: '#fff'
                }
              },
              zlevel: 2,
              data: inalldata[j] // 输入功率
            });
          }
          for (let j = 0; j < maxnum; j++) {
            series.push({
              name: '输出功率',
              type: 'bar',
              xAxisIndex: 1,
              // barGap: '40%',
              data: outalldata[j],
              zlevel: 1, //  层级
              barWidth: 18,

              label: {
                normal: {
                  show: true,
                  position: 'inside',
                  fontSize: 15,
                  fontWeight: 'bold',
                  color: '#F74BFA'
                }
              },
              itemStyle: {
                normal: {
                  barBorderRadius: 20,
                  color: new echarts.graphic.LinearGradient(0, 0, 0, 0.7, [
                    {
                      offset: 0,
                      color: '#602CAA'
                    },
                    {
                      offset: 1,
                      color: '#F74BFA'
                    }
                  ]),
                  opacity: 0.8
                }
              }
            });
          }
          if (maxnum % 2 === 0) {
            // 偶数
            let offset = Math.floor(maxnum / 2) * -1;
            // console.log('jishu', Math.floor(maxnum / 2));
            for (let i = 0; i < maxnum; i++) {
              series.push({
                // 红色的那个圈
                name: '输入功率',
                type: 'pictorialBar',
                symbolSize: [18, 10],
                symbolOffset: [`${offset > 0 ? offset * 130 - 65 : offset * 130 + 65}%`, 0],
                symbolPosition: 'end',
                z: 12,
                color: '#00F7FD',
                zlevel: 2,
                data: inalldata[i]
              });
              series.push({
                // 蓝色的那个圈
                name: '输出功率',
                type: 'pictorialBar',
                symbolSize: [18, 10],
                symbolOffset: [`${offset > 0 ? offset * 130 - 65 : offset * 130 + 65}%`, 0],
                symbolPosition: 'end',
                z: 12,
                color: '#602CAA',
                zlevel: 2,
                data: outalldata[i]
              });
              series.push({
                // 红色的那个圈
                name: '输入功率',
                type: 'pictorialBar',
                symbolSize: [18, 10],
                symbolOffset: [`${offset > 0 ? offset * 130 - 65 : offset * 130 + 65}%`, 0],
                symbolPosition: 'start',
                z: 12,
                color: '#00F7FD',
                zlevel: 2,
                data: inalldata[i]
              });
              series.push({
                // 蓝色的那个圈
                name: '输出功率',
                type: 'pictorialBar',
                symbolSize: [18, 10],
                symbolOffset: [`${offset > 0 ? offset * 130 - 65 : offset * 130 + 65}%`, 0],
                symbolPosition: 'start',
                z: 12,

                color: '#F74BFA',
                zlevel: 2,
                data: outalldata[i]
              });
              offset++;
              if (offset === 0) {
                offset++;
              }
            }
          } else {
            // 奇数
            let offset = Math.floor(maxnum / 2) * -1;
            // console.log('jishu', Math.floor(maxnum / 2));
            for (let i = 0; i < maxnum; i++) {
              series.push({
                // 红色的那个圈
                name: '输入功率',
                type: 'pictorialBar',
                symbolSize: [18, 10],
                symbolOffset: [`${offset * 130}%`, 0],
                symbolPosition: 'end',
                z: 12,
                color: '#00F7FD',
                zlevel: 2,
                data: inalldata[i]
              });
              series.push({
                // 蓝色的那个圈
                name: '输出功率',
                type: 'pictorialBar',
                symbolSize: [18, 10],
                symbolOffset: [`${offset * 130}%`, 0],
                symbolPosition: 'end',
                z: 12,

                color: '#602CAA',
                zlevel: 2,
                data: outalldata[i]
              });
              series.push({
                // 红色的那个圈
                name: '输入功率',
                type: 'pictorialBar',
                symbolSize: [18, 10],
                symbolOffset: [`${offset * 130}%`, 0],
                symbolPosition: 'start',
                z: 12,
                color: '#221287',
                zlevel: 2,
                data: inalldata[i]
              });
              series.push({
                // 蓝色的那个圈
                name: '输出功率',
                type: 'pictorialBar',
                symbolSize: [18, 10],
                symbolOffset: [`${offset * 130}%`, 0],
                symbolPosition: 'start',
                z: 12,

                color: '#F74BFA',
                zlevel: 2,
                data: outalldata[i]
              });
              offset++;
            }
          }
          const option = {
            backgroundColor: '#030230',
            legend: {
              // 类别关闭打开
              data: ['输出功率', '输入功率'],
              icon: 'rect',
              top: 5,
              // selectedMode: false, // 关闭点击
              right: 10,
              textStyle: {
                color: '#fff',
                fontWeight: 'normal',
                fontSize: 12
              }
            },
            // 提示框
            tooltip: {
              // trigger: 'item'
              trigger: 'axis',
              axisPointer: {
                type: 'shadow'
              },
              formatter: function (param) {
                let resultTooltip =
                  "<div style='background:;border:1px solid rgba(255,255,255,.2);padding:5px 10px;border-radius:4px;'>" + "<div style='text-align:center;'>" + param[0].name + '</div>';
                console.log('=====', param);
                function compare(property) {
                  return function (a, b) {
                    const value1 = a[property];
                    const value2 = b[property];
                    return value1 - value2;
                  };
                }
                console.log(param.sort(compare('componentIndex')));
                const params = param.sort(compare('componentIndex'));
                for (let i = 0; i < maxnum; i++) {
                  if (param[i].seriesName === '输入功率' && params[i].value[1]) {
                    resultTooltip +=
                      "<div style='padding-top:5px;'>" +
                      "<span style='display:inline-block;border-radius:4px;width:30px;height:10px;background-color:red;border: 2px solid #3eb6f5;'></span>" +
                      "<span style=''>  " +
                      params[i].value[2] +
                      params[i].seriesName +
                      ': </span>' +
                      "<span style=''>" +
                      params[i].value[1] +
                      '</span><span></span>' +
                      '</div>';
                  }
                  if (param[i + maxnum].seriesName === '输出功率' && params[i + maxnum].value[1]) {
                    resultTooltip +=
                      "<div style='padding-top:5px;'>" +
                      "<span style='display:inline-block;border-radius:4px;width:30px;height:10px;background-color:red;border: 2px solid #3eb6f5;'></span>" +
                      "<span style=''>  " +
                      params[i + maxnum].value[2] +
                      params[i + maxnum].seriesName +
                      ': </span>' +
                      "<span style=''>" +
                      params[i + maxnum].value[1] +
                      '</span><span></span>' +
                      '</div>';
                  }
                }
                return resultTooltip;
              }
            },
            grid: {
              top: '10%',
              left: '5%',
              bottom: '10%',
              right: '5%',
              containLabel: true
            },
            animation: true,
            xAxis: [
              {
                name: '时间',
                axisTick: {
                  show: false
                },
                axisLine: {
                  symbol: ['none', 'arrow'],
                  onZero: false,
                  show: true,
                  lineStyle: {
                    color: '#fff'
                  }
                },
                axisLabel: {
                  inside: false,
                  textStyle: {
                    color: '#fff',
                    fontWeight: 'normal',
                    fontSize: 12
                  }
                },
                data: newchartName
              },
              {
                type: 'category',
                axisLine: {
                  show: false,
                  lineStyle: {
                    color: '#fff'
                  }
                },
                axisTick: {
                  show: false
                },
                axisLabel: {
                  show: false
                },
                splitArea: {
                  show: false
                },
                splitLine: {
                  show: false
                },
                data: newchartName
              }
            ],
            yAxis: [
              // {
              //   show: true,
              //   name: '当前故障数',
              //   nameTextStyle: {
              //     fontSize: 20,
              //     color: '#fff'
              //   },
              //   type: 'value',
              //   axisLabel: {
              //     textStyle: {
              //       color: '#fff'
              //     }
              //   },
              //   splitLine: {
              //     lineStyle: {
              //       color: 'rgba(0,228,255,1)'
              //     }
              //   },
              //   axisLine: {
              //     show: false
              //   }
              // }
              {
                smooth: true,
                nameGap: 20,
                type: 'value',
                name: '当前故障数108 个',
                position: 'left',
                axisLine: {
                  symbol: ['none', 'arrow'],
                  // onZero: false,
                  show: true,
                  lineStyle: {
                    color: '#fff',
                    fontSize: 16
                  },
                  axisTick: {
                    inside: false
                  }
                },
                axisLabel: {
                  textStyle: {
                    show: false,
                    color: '#fff'
                  }
                },
                splitLine: {
                  show: true,
                  lineStyle: {
                    type: 'dashed',
                    opacity: 0.5,
                    color: '#999'
                  }
                }
                // axisLabel: {
                //   formatter: '{value} '
                // }
              }
            ],
            dataZoom: [
              {
                show: true,
                height: 12,
                xAxisIndex: [0, 1],
                bottom: '8%',
                start: 0,
                end: 30,
                handleIcon: 'path://M306.1,413c0,2.2-1.8,4-4,4h-59.8c-2.2,0-4-1.8-4-4V200.8c0-2.2,1.8-4,4-4h59.8c2.2,0,4,1.8,4,4V413z',
                handleSize: '100%',
                handleStyle: {
                  color: '#d3dee5'
                },
                textStyle: {
                  color: '#fff'
                },
                borderColor: '#90979c'
              }
              // {
              //   show: false,
              //   height: 12,
              //   // xAxisIndex: [1],
              //   bottom: '8%',
              //   start: 0,
              //   end: 50,
              //   handleIcon: 'path://M306.1,413c0,2.2-1.8,4-4,4h-59.8c-2.2,0-4-1.8-4-4V200.8c0-2.2,1.8-4,4-4h59.8c2.2,0,4,1.8,4,4V413z',
              //   handleSize: '100%',
              //   handleStyle: {
              //     color: '#d3dee5'
              //   },
              //   textStyle: {
              //     color: '#fff'
              //   },
              //   borderColor: '#90979c'
              // }
              // {
              //   type: 'inside',
              //   show: true,
              //   height: 15,
              //   start: 0,
              //   end: 35
              // }
            ],
            series: series

          };

          option && myChart.setOption(option);
          // const app = {
          //   currentIndex: -1
          // };
          // setInterval(function () {
          //   const dataLen = option.series[0].data.length;

          //   // 取消之前高亮的图形
          //   myChart.dispatchAction({
          //     type: 'downplay',
          //     seriesIndex: 0,
          //     dataIndex: app.currentIndex
          //   });
          //   app.currentIndex = (app.currentIndex + 1) % dataLen;
          //   // console.log(app.currentIndex);
          //   // 高亮当前图形
          //   myChart.dispatchAction({
          //     type: 'highlight',
          //     seriesIndex: 0,
          //     dataIndex: app.currentIndex
          //   });
          //   // 显示 tooltip
          //   myChart.dispatchAction({
          //     type: 'showTip',
          //     seriesIndex: 0,
          //     dataIndex: app.currentIndex
          //   });
          // }, 10000);
        });
      }

要使用 Echarts 绘制柱状图并调用接口,你需要先引入 Echarts 的 JavaScript 文件,并在 HTML 中创建一个容器来显示图表。然后,你可以通过 AJAX 或其他方法从接口获取数据。下面是一个简单的示例代码: ```html <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Echarts 柱状图调用接口示例</title> <script src="https://cdn.staticfile.org/echarts/4.9.0/echarts.min.js"></script> </head> <body> <div id="chart-container" style="width: 600px; height: 400px;"></div> <script> // 创建柱状图实例 var myChart = echarts.init(document.getElementById('chart-container')); // 发起 AJAX 请求获取数据 var xhr = new XMLHttpRequest(); xhr.open('GET', '接口地址'); xhr.onreadystatechange = function () { if (xhr.readyState === 4 && xhr.status === 200) { var data = JSON.parse(xhr.responseText); // 根据接口返回数据配置图表 var option = { xAxis: { type: 'category', data: data.categories }, yAxis: { type: 'value' }, series: [{ data: data.values, type: 'bar' }] }; // 使用刚指定的配置项和数据显示图表 myChart.setOption(option); } }; xhr.send(); </script> </body> </html> ``` 在上面的代码中,你需要替换 `"接口地址"` 为你实际的接口地址。接口需要返回一个 JSON 格式的数据,其中 `data.categories` 代表柱状图的 x 轴数据,`data.values` 代表柱状图的 y 轴数据。 这是一个简单的示例,你可以根据实际情况进行调整和扩展。希望对你有帮助!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值