首页 > 解决方案 > Highcharts 响应在第一次渲染时被忽略

问题描述

我的图例有以下代码,以及删除移动图例的响应规则。

我现在看到的问题是图例最初总是与图表一起加载。响应式规则似乎只适用于以后(即,如果我重新加载页面,图例就在那里,如果我单击另一个页面的链接,然后返回,图例就消失了)。

有没有办法强制对负载进行响应检查?

legend: {
    enabled: true,
    verticalAlign: 'top',
    align: 'right',
    floating: true,
    margin: 0,
    padding: 0,
    x: showConviction ? -50 : -46,
    y: 10
},
responsive: {
    rules: [
        {
            condition: {
                maxWidth: 400
            },
            chartOptions: {
                legend: {
                    enabled: false
                }
            }
        }
    ]
},

标签: javascripthighcharts

解决方案


您可以检查chartWidth何时发生加载事件并禁用图例。

HTML:

<script src="https://code.highcharts.com/highcharts.js"></script>
<div id="container"></div>

JS:

Highcharts.chart('container', {

  chart: {
    events: {
      load: function() {
        var chart = this,
          legend = chart.legend,
          chartWidth = chart.chartWidth;

        if (chartWidth < 400) {
          legend.update({
            enabled: false
          });
        }
      }
    }
  },

  plotOptions: {
    series: {
      label: {
        connectorAllowed: false
      },
      pointStart: 2010
    }
  },

  series: [{
    name: 'Installation',
    data: [43934, 52503, 57177, 69658, 97031, 119931, 137133, 154175]
  }, {
    name: 'Manufacturing',
    data: [24916, 24064, 29742, 29851, 32490, 30282, 38121, 40434]
  }],

  responsive: {
    rules: [{
      condition: {
        maxWidth: 400
      },
      chartOptions: {
        legend: {
          enabled: false
        }
      }
    }]
  }

});

演示: https ://jsfiddle.net/BlackLabel/t1dswx4j/


推荐阅读