简体   繁体   English

使用Highcharts用灰色区域填充数据空白,

[英]Filling the data gaps with a gray area using Highcharts,

I've a Highcharts Line Chart. 我有一个Highcharts折线图。 Sometimes, there's a gap between the data and I wonder whether it's possible to fill these gaps with some gray area so it's better visible that there are no data. 有时,数据之间存在间隙,我想知道是否有可能用一些灰色区域填充这些间隙,以便更好地看到没有数据。

Thanks 谢谢

Edit: Example of what I want to have: 编辑:我想要的示例: 在此处输入图片说明

You can use xAxis.plotBands to fill those gaps. 您可以使用xAxis.plotBands填补这些空白。 Note you need to calculate where you have null -points in a first place, before rendering a chart. 请注意,在绘制图表之前,需要先计算null点的位置。

Here is a simple example for you: http://jsfiddle.net/hbzoLxnw/ 这是为您提供的一个简单示例: http : //jsfiddle.net/hbzoLxnw/

you can set up a function to fill the gap: 您可以设置一个函数来填补空白:

  $('#button').click(function() {
    for (i = 0; i < chart.yAxis[0].series[1].yData.length; i++) {
      if (chart.yAxis[0].series[1].yData[i] == null) {
        chart.xAxis[0].addPlotBand({
          from: i - 1,
          to: i + 1,
          color: '#FCFFC5',
        });
      }
    }
  });

Basically, the function above detect the null value, and sets up a plotBands . 基本上,上面的函数检测null值,并设置plotBands The idea is here, but you can make it more versatile by checking if the value is at the beginning or the end, in this case, you don't have to write i+1 or i-1 but just i Check the example ( jsfiddle ): 这个想法是在这里,但你可以把它更加灵活通过检查该值的开头或结尾,在这种情况下,你没有写i+1i-1只是i检查的例子( jsfiddle ):

$(function() {
  $('#container').highcharts({
    chart: {
      type: 'area',
      spacingBottom: 30
    },
    title: {
      text: 'Fruit consumption *'
    },
    subtitle: {
      text: '* Jane\'s banana consumption is unknown',
      floating: true,
      align: 'right',
      verticalAlign: 'bottom',
      y: 15
    },
    legend: {
      layout: 'vertical',
      align: 'left',
      verticalAlign: 'top',
      x: 150,
      y: 100,
      floating: true,
      borderWidth: 1,
      backgroundColor: (Highcharts.theme && Highcharts.theme.legendBackgroundColor) || '#FFFFFF'
    },
    xAxis: {
      categories: ['Apples', 'Pears', 'Oranges', 'Bananas', 'Grapes', 'Plums', 'Strawberries', 'Raspberries']
    },
    yAxis: {
      title: {
        text: 'Y-Axis'
      },
      labels: {
        formatter: function() {
          return this.value;
        }
      }
    },
    tooltip: {
      formatter: function() {
        return '<b>' + this.series.name + '</b><br/>' +
          this.x + ': ' + this.y;
      }
    },
    plotOptions: {
      area: {
        fillOpacity: 0.5
      },
    },
    credits: {
      enabled: false
    },
    series: [{
      name: 'John',
      data: [0, 1, 4, 4, 5, 2, 3, 7]
    }, {
      name: 'Jane',
      data: [1, 0, 3, null, 3, 1, 2, 1]
    }]
  });


  // the button action
  var chart = $('#container').highcharts();

  $('#button').click(function() {
    for (i = 0; i < chart.yAxis[0].series[1].yData.length; i++) {
      if (chart.yAxis[0].series[1].yData[i] == null) {
        chart.xAxis[0].addPlotBand({
          from: i - 1,
          to: i + 1,
          color: '#FCFFC5',
        });
      }
    }
  });
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM