echarts柱状图每个柱子显示不同颜色,并且能够实现点击每种颜色影藏对应柱子的功能...

本文介绍如何利用ECharts库在网页上绘制一个简单的柱状图,展示不同城市的数据统计情况。文中提供了完整的HTML页面代码及JavaScript实现,包括设置图表样式、数据源等关键步骤。

---------------------------------------------------------代码区---------------------------------------------------------------

<!DOCTYPE html>
<html>
  <head>
  <base href="<%=basePath%>">
  <title>测试</title>
  
  <meta http-equiv="pragma" content="no-cache">
  <meta http-equiv="cache-control" content="no-cache">
  <meta http-equiv="expires" content="0">  
  <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
  <meta http-equiv="description" content="This is my page">
  <link rel="stylesheet" href="admin/css/bootstrap.min.css">
  
  
</head>

<body>
    <section class="hj-second-page-section">
      <div class="container-fluid">
        <div class="rows">
          <div class="col-xs-12 col-sm-12 col-md-12 col-lg-12">
            <div class="rows">
              <div class="col-xs-12 col-sm-12 col-md-12 col-lg-12 hj-jumbotron-div">
                <div class="panel panel-primary ng-scope">
                
                 <!-- pannel start -->
                 <div class="panel-body vc-pannel-body-toggle">
                     <div class="rows ng-scope">
                       <div class="panel-body vc-msg-panel-body">
                         <div class="row">
                          
                          <div class="col-xs-12 col-sm-12 col-md-6 col-lg-6">
                             <div id="rt_chart1" style=""></div>
                          </div>
                          
                         </div>
                      </div>
                     </div>
                     
                 </div>
                
                 <!-- pannel end -->
                </div>
              </div>
            </div>
          </div>
        </div>
      </div>
    </section>
      
    
    <script src="echarts.js"></script>
    <script src="jquery-2.2.3.min.js"></script>
    
    <script>
      /**
       * @description 绘制柱状图
       * @author luohan
       * @date 2017-7-28
       * @param
       */
      function initChart1() {
        $("#rt_chart1").height(460);
        $("#rt_chart1").width(1005);
        $("#rt_chart1").css("border","1px solid #ddd");
        var myChart1 = echarts.init(document.getElementById("rt_chart1"));

        var option1 = {
          title : {
            text: '数据统计',
            subtext: ''
          },
          tooltip : {
            trigger: 'axis'
          },
          legend: {
            data:['北京','上海','深圳','广州']
          },
          toolbox: {
            show : true,
            feature : {
              mark : {show: true},
              dataView : {show: true, readOnly: false},
              magicType : {show: true, type: ['line', 'bar']},
              restore : {show: true},
              saveAsImage : {show: true}
            }
          },
          calculable : true,
          xAxis : [
            {
              type : 'category',
              show:false,
              data : ['横坐标自定义']
            }
          ],
          yAxis : [
            {
              type : 'value'
              
            }
          ],
          series : [
            {
              name:'北京',
              type:'bar',
              itemStyle: {
                normal: {
                  color: function(params) {
                    //首先定义一个数组
                    var colorList = [
                      '#C33531'
                    ];
                    return colorList[params.dataIndex]
                  },
                  //以下为是否显示
                  label: {
                    show: false
                  }
                }
              },
              data:[2.0],
            },
            {
              name:'上海',
              type:'bar',
              itemStyle: {
                normal: {
                  color: function(params) {
                    //首先定义一个数组
                    var colorList = [
                      '#EFE42A'
                    ];
                    return colorList[params.dataIndex]
                  },
                  //以下为是否显示
                  label: {
                    show: false
                  }
                }
              },
              data:[4.9],
            },
            {
              name:'深圳',
              type:'bar',
              itemStyle: {
                normal: {
                  color: function(params) {
                    //首先定义一个数组
                    var colorList = [
                      '#64BD3D'
                    ];
                    return colorList[params.dataIndex]
                  },
                  //以下为是否显示
                  label: {
                    show: false
                  }
                }
              },
              data:[7.9],
            },
            {
              name:'广州',
              type:'bar',
              itemStyle: {
                normal: {
                  color: function(params) {
                    //首先定义一个数组
                    var colorList = [
                      '#EE9201'
                    ];
                    return colorList[params.dataIndex]
                  },
                  //以下为是否显示
                  label: {
                    show: false
                  }
                }
              },
              data:[23.0],
            }
          ]
        };

        


        // 为echarts对象加载数据
        myChart1.setOption(option1);
      }

      initChart1();
    </script>
</body>
</html>
    

 

转载于:https://www.cnblogs.com/herd/p/7250498.html

<think>嗯,用户问的是关于ECharts堆叠柱状图每个柱子上的标签不显示的问题,而且在series里设置label为false不生效。首先,我需要回忆一下ECharts的配置项结构。堆叠柱状图的标签设置通常在series的label属性里控制,但可能用户没有正确配置或者有其他覆盖的设置。 可能用户是在每个series里单独设置了label: { show: false },但发现不起作用。这时候我应该检查是否有其他层级的配置覆盖了这个设置。比如,是否在全局的label配置或者主题里启用了标签显示?或者是否在tooltip或者其他组件里响了显示? 另外,堆叠柱状图的series可能会有多个,用户可能只修改了其中一个series的label设置,而其他series仍然显示标签。需要确认是否所有的series都正确设置了label.show为false。 还有一种可能是,用户可能在使用formatter函数时返回了空字符串,但show属性还是true,这样虽然内容为空,但可能仍占据空间或者有其他效果。不过用户提到直接设置false不生效,所以可能不是这个问题。 另外,ECharts的版本问题也可能导致配置项不生效,旧版本可能存在一些bug,建议用户检查版本是否最新,或者查阅对应版本的文档。 还有可能是视觉映射(visualMap)或者数据项(itemStyle)的某些设置覆盖了标签的显示,需要检查是否有其他配置干扰。 总结可能的原因: 1. 多个series未全部设置label.show为false。 2. 存在全局或默认的label配置覆盖了series的设置。 3. ECharts版本问题导致配置不生效。 4. 其他组件或配置干扰了标签的显示。 解决方法应该是确保所有相关的series都正确关闭label,并且检查是否有其他地方的配置覆盖。另外,提供一个示例代码来演示正确配置,确保用户能够复制并测试。</think>在 ECharts 中,如果堆叠柱状图(Stacked Bar Chart)的柱子顶部标签(Label)无法隐,且直接设置 `series.label.show: false` 不生效,可能是因为以下原因及解决方法: --- ### **常见原因 & 解决方案** #### 1. **堆叠模式下多个系列的干扰** 堆叠柱状图通常由多个 `series` 组成,每个 `series` 需要单独关闭标签。如果只修改了其中一个 `series` 的 `label` 配置,其他系列可能依然显示标签。 **解决代码:** ```javascript series: [ { type: 'bar', stack: 'total', // 堆叠组名需一致 label: { show: false }, // 每个系列单独关闭 data: [/* ... */] }, { type: 'bar', stack: 'total', label: { show: false }, // 每个系列单独关闭 data: [/* ... */] } ] ``` #### 2. **全局默认配置覆盖** 如果通过 `setOption` 多次合并配置,可能存在全局 `label` 配置被覆盖。建议使用 `notMerge: true` 或完整覆盖配置。 **解决代码:** ```javascript chart.setOption({ series: [/* 完整配置 */] }, { notMerge: true }); // 强制不合并旧配置 ``` #### 3. **使用 `emphasis` 状态干扰** 如果通过 `emphasis` 设置了悬停标签,可能误以为静态标签未关闭。需要同时关闭静态和悬停状态。 **解决代码:** ```javascript series: [{ type: 'bar', label: { show: false, // 关闭静态标签 emphasis: { show: false } // 关闭悬停标签 }, data: [/* ... */] }] ``` #### 4. **版本兼容性问题** 某些旧版本 ECharts 可能存在配置项解析问题,建议升级到最新版。 --- ### **完整示例代码** ```javascript option = { xAxis: { type: 'category', data: ['A', 'B', 'C'] }, yAxis: { type: 'value' }, series: [ { type: 'bar', stack: 'total', data: [120, 132, 101], label: { show: false, // 关闭静态标签 emphasis: { show: false } // 关闭悬停标签 } }, { type: 'bar', stack: 'total', data: [220, 182, 191], label: { show: false, emphasis: { show: false } } } ] }; ``` --- ### **其他调试建议** - 在 ECharts 官网的[在线编辑器](https://echarts.apache.org/examples/zh/editor.html)中快速验证配置。 - 检查浏览器控制台是否有报错(如配置项拼写错误)。 - 使用 `console.log(option)` 输出最终配置,确认 `label.show` 值是否正确。 如果问题仍未解决,可以提供更多代码片段或截进一步分析!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值