从Highcharts系列数组中删除起始“0”值

时间:2013-01-29 10:06:50

标签: javascript highcharts

我有以下生成的JavaScript代码来显示包含HighCharts的图表:

<script type="text/javascript">

var highchartsOptions   = Highcharts.setOptions(Highcharts.theme);
var TaskChart; // Chart-Objekt    
var TaskDuration          = new Array();    
// Save starting points to javascript variables for HighCharts
var startingUTC = 1288102643364;
TaskDuration =  [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,206,216,216,206,206];
for(var i = 0; i < TaskDuration.length; i++) {
    if(TaskDuration[i] == 0) {
        TaskDuration[i] = null;
    }
}
</script>
<script type="text/javascript">
$j(document).ready(function() { 
    TaskChart = new Highcharts.Chart({
    credits: {
            enabled: false
    },
    chart: {
      renderTo: "chart01",
      defaultSeriesType: 'area',
      zoomType: 'x',
      spacingBottom: 250
    },
    title: {
      text: "Task Duration"
    },
    xAxis: {
        type: 'datetime',
        dateTimeLabelFormats: {
            week: '%e. %b %Y'   
        },
        offset: 10,
        startOnTick: true,
        maxZoom: 7 * 24 * 3600000 // seven days
    },
    yAxis: {
      title: {
        text: "Duration"
      }
    },
    tooltip: {
        formatter: function() {
            return '<b>'+ this.series.name +'</b><br/>';            
        }
    },
    plotOptions: {
        area: { 
           stacking: 'normal',
           lineColor: '#666666',
           lineWidth: 1,
           marker: {
              lineWidth: 1,
              lineColor: '#666666'
           }
        }
     },
    series: [
    {
        name: 'Duration',
        pointStart: startingUTC,
        pointInterval: 24*60*60*1000, 
        data: TaskDuration
    }]
  });
});
</script>

现在问题是我要从TaskDuration系列数组的开头删除所有“0”值。

我尝试将数组中的值设置为“null”:

for(var i = 0; i < TaskDuration.length; i++) {
    if(TaskDuration[i] == 0) {
        TaskDuration[i] = null;
    }
}

但是Highcharts仍然会在图表中显示这些值。

如何防止这种行为?

我也尝试从数组中删除所有值,直到值为“0”。但是采用这种方法,“startingUTC”是错误的。也许我还要重新计算这个值......

此致 桑德罗

2 个答案:

答案 0 :(得分:0)

您需要删除不设置null的元素,使用此方法从数组中删除零元素:

var myArray=[];

for(var i = 0; i < TaskDuration.length; i++) {
    if(TaskDuration[i] != 0) {
       myArray.push(TaskDuration[i]);
    }
}

工作JSfiddle

答案 1 :(得分:0)

问题是您使用的是pointStartpointInterval PointInterval会忽略您的空点,并会向yAxis添加点数 所以,删除它。

series: [{
    name: 'Duration', 
    data: TaskDuration
}]