【问题标题】:How make to fill space after column was hidden in column chart using highchart?如何使用highchart在柱形图中隐藏列后填充空间?
【发布时间】:2016-09-05 19:45:47
【问题描述】:

有没有办法通过单击图例项隐藏一列后填充空间?

<div id="container" style="height: 300px"></div>
<script src="http://code.highcharts.com/highcharts.src.js"></script>

    var chart = new Highcharts.Chart({

    chart: {
        type: 'column',
        renderTo: 'container'
    },

    "title":{  
      "text":"TITLE",
      "align":"left"
   },
   "subtitle":{  
      "text":"Subtitle Text",
      "align":"left",
      "style":{  
         "paddingBottom":"20px"
      }
   },
   "xAxis":{  
      "categories":[  
         "Category1",
         "Category2",
         "Category3",
         "Category4",
         "Category5"
      ],
      "crosshair":true,
      "title":{  

      },
      "labels":{  
         "enabled":false,
         "format":"{value}"
      }
   },
   "yAxis":{  
      "min":0,
      "title":{  
         "text":"Meters"
      },
      "labels":{  
         "format":"{value}"
      }
   },
   "tooltip":{  
      "enabled":true,
      "headerFormat":"<span style=\"font-size:10px\">{point.key}</span><br/>",
      "shared":true,
      "useHTML":true
   },
   "legend":{  
      "enabled":true,
      "align":"left",
      "useHTML":true
   },
   "plotOptions":{  
      "column":{  
         "pointPadding":0.2,
         "borderWidth":0,
         "dataLabels":{  
            "enabled":true,
            "format":"{point.y:.2f} ",
            "style":{  
               "textShadow":0
            }
         }
      },
      "series":{  
         "grouping":false
      }
   },
    "series":[  
      {  
         "name":"Category1",
         "data":[  
            {  
               "name":"Category1",
               "y":45,
               "x":0
            }
         ]
      },
      {  
         "name":"Category2",
         "data":[  
            {  
               "name":"Category2",
               "y":43,
               "x":1
            }
         ]
      },
      {  
         "name":"Category3",
         "data":[  
            {  
               "name":"Category3",
               "y":43,
               "x":2
            }
         ]
      },
      {  
         "name":"Category4",
         "data":[  
            {  
               "name":"Category4",
               "y":42,
               "x":3
            }
         ]
      },
      {  
         "name":"Category5",
         "data":[  
            {  
               "name":"Category5",
               "y":42,
               "x":4
            }
         ]
      },
      {  
         "name":"",
         "data":[  
            {  

            }
         ],
         "visible":false,
         "showInLegend":false,
         "exporting":false
      },
      {  
         "name":"Bla bla bla",
         "data":[  
            45,
            43,
            43,
            42,
            42
         ],
         "showInLegend":false,
         "visible":false,
         "exporting":true
      }
   ],
   "exporting":{  
      "enabled":false,
      "chartOptions":{  
         "legend":{  
            "enabled":true,
            "maxHeight":null,
            "itemStyle":{  
               "fontFamily":"Arial"
            }
         }
      }
   },
   "loading":{  
      "style":{  
         "opacity":0.9
      }
   },
   "drilldown":{  
      "drillUpButton":{  
         "theme":{  
            "style":{  
               "display":"none"
            }
         }
      }
   }
});

Here is live example.

当我单击第一个或最后一个图例项时,图表正在重绘,但是当我单击中间的图例项时它没有发生?这是一个错误或功能?以及如何将中间图例项目的这种行为更改为它们也能够引发重绘图表的事件?

提前感谢

【问题讨论】:

  • 在您的 xAxis 上,当您隐藏或显示第一个和最后一个点时,您有一个正在发生变化的极端。当你改变中间点时,极端不会改变。而且我认为他们不应该改变。您可以使用 chart.redraw() 手动重绘图表,也可以使用 Axis.setExtremes() 手动更改极端值。
  • 所以,我尝试过这种方式,但没有成功,因为我无法填充隐藏列的空间,只需更改图表的比例即可。此外,我可以使用 chart.series[2].remove(true) 实现相同的结果,但在这种情况下,图例项也被删除。也许你知道我怎样才能让传奇项目不做任何改变,只删除一个相关的系列?

标签: javascript angularjs highcharts


【解决方案1】:

您可以设置和更新 xAxis 的中断以隐藏空白区域:

http://jsfiddle.net/a4vcye4p/1/

  events: {
    legendItemClick: function() {
      var series = this,
        seriesIndex = series._i,
        xAxis = series.xAxis,
        newBreaks = [];
      if (series.visible) {
        newBreaks = xAxis.options.breaks;
        newBreaks.push({
          from: seriesIndex,
          to: seriesIndex + 1,
          seriesIndex: seriesIndex
        });
      } else {
        Highcharts.each(xAxis.options.breaks, function(br) {
          if (br.seriesIndex !== seriesIndex) {
            newBreaks.push(br);
          }
        });
      }
      xAxis.update({
        breaks: newBreaks
      });
    }
  }

【讨论】:

  • 太棒了!你能说highcharts@4.1.7有一些替代品吗?
  • @slesh 此解决方案在使用 Highcharts 4.1.7 版和断轴模块时有效 - jsfiddle.net/a4vcye4p/2
  • 哦,对不起!我没有注意到依赖关系,我的错。
  • 嗨,@Kacper Madej。我遇到了您的代码的问题,但无法自己管理它,但似乎问题是 x 坐标(value parameter)here highcharts 的浮点数值.src.js:2924。请问你能帮帮我吗。这是问题的live example。要重现问题,请取消选中图表上的值:iphone、ipad、N/A、sm-g935f、sm-g930f、android 2.0、iphone 6、sm-g920f、sm-g9005。实际结果:sm-g9005不勾选后,控制台显示错误。也许您可以建议一些解决方法?非常感谢。
  • Update 到最新版本没有帮助。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-09-03
  • 2018-05-14
  • 1970-01-01
  • 2017-12-10
相关资源
最近更新 更多