2016-09-29 65 views
1

添加新系列時是否可以設置位置列?如何在特定索引的列圖中添加一系列

的,我想添加一個新的系列後,並在最後非功能性的代碼做一個例子:

$(function() { 
chart = new Highcharts.Chart({ 
    chart: { 
     type: 'column', 
     renderTo: 'container' 
    }, 
    title: { 
     text: 'Monthly Average Rainfall' 
    }, 
    subtitle: { 
     text: 'Source: WorldClimate.com' 
    }, 
    xAxis: { 
     categories: [ 
      'Jan', 
      'Feb' 
     ], 
     crosshair: true 
    }, 
    yAxis: { 
     min: 0, 
     title: { 
      text: 'Rainfall (mm)' 
     } 
    }, 
    tooltip: { 
     headerFormat: '<span style="font-size:10px">{point.key}</span><table>', 
     pointFormat: '<tr><td style="color:{series.color};padding:0">{series.name}: </td>' + 
      '<td style="padding:0"><b>{point.y:.1f} mm</b></td></tr>', 
     footerFormat: '</table>', 
     shared: true, 
     useHTML: true 
    }, 
    plotOptions: { 
     column: { 
      pointPadding: 0.2, 
      borderWidth: 0 
     } 
    }, 
    series: [{ 
     name: 'Tokyo', 
     data: [49.9, 71.5] 
    }, { 
     name: 'Berlin', 
     data: [32.4, 33.2] 
    }] 
}); 

chart.addSeries({ 
     name: 'New York', 
     data: [83.6, 78.8] 
    }) 

chart.series[0].index = 2; 
chart.series[1].index = 3; 
chart.series[2].index = 1; 
chart.redraw(); 

}); 

鏈接到我的一個的jsfiddle: http://jsfiddle.net/cug79br2/1/

在我的情況做在創建圖表時沒有全部系列,因此我需要在加載附加數據集後重新排列它們。在這個例子中,我想在索引1處添加最後一個系列,並將現有列向右移動一個檔位。

+0

這聽起來像你需要對你的查詢做一些預先考慮的工作,然後創建邏輯重新排序,然後再添加。那麼問題就變成了顏色,你想爲每個系列保持相同的顏色,否則人們會感到困惑。你也可以做到這一點。然後進入圖表建築(更簡化的代碼結構)。不幸的是,我的所有圖表符合這個標準的人不會這樣做(我讓芯片落在他們可能......沒有重新排序的地方)。所以這是我的兩分錢希望其他人看起來可以幫助你一個具體的例子。 –

+0

在我添加之前重新排序是這樣的邏輯避開了我:(預先解答如何提供幫助? – Joelbitar

回答

1

我能找到的最好的解決方法是刪除所有系列,對它們進行排序,再添加。在這種情況下,它有助於保持Highcharts對象之外的數據(有必要)。在我的情況下,一個名爲series_data的數組。

$(function() { 
var chart, series_data; 
series_data = [ 
    { 
     name: 'Tokyo', 
     data: [49.9, 71.5] 
    }, { 
     name: 'Berlin', 
     data: [32.4, 33.2] 
    } 
]; 

chart = new Highcharts.Chart({ 
    chart: { 
     type: 'column', 
     renderTo: 'container' 
    }, 
    title: { 
     text: 'Monthly Average Rainfall' 
    }, 
    subtitle: { 
     text: 'Source: WorldClimate.com' 
    }, 
    xAxis: { 
     categories: [ 
      'Jan', 
      'Feb' 
     ], 
     crosshair: true 
    }, 
    yAxis: { 
     min: 0, 
     title: { 
      text: 'Rainfall (mm)' 
     } 
    }, 
    tooltip: { 
     headerFormat: '<span style="font-size:10px">{point.key}</span><table>', 
     pointFormat: '<tr><td style="color:{series.color};padding:0">{series.name}: </td>' + 
      '<td style="padding:0"><b>{point.y:.1f} mm</b></td></tr>', 
     footerFormat: '</table>', 
     shared: true, 
     useHTML: true 
    }, 
    plotOptions: { 
     column: { 
      pointPadding: 0.2, 
      borderWidth: 0 
     } 
    }, 
    series: series_data 
}); 

while(chart.series.length){ 
    chart.series[0].remove(); 
} 

series_data.push({ 
     name: 'New York', 
     data: [83.6, 78.8] 
    } 
); 

_.each(_.sortBy(series_data, 'data.0').reverse(), function(series, i){ 
    series.index = i; 

    // To keep animations pretty and the last added to animate, do tno add it in the loop but later. 
    if(series.name === 'New York'){ 
    return; 
    } 

    chart.addSeries(series, true, false); 
}) 

chart.addSeries(series_data[2], true, false); 

}); 

http://jsfiddle.net/cug79br2/23/

在添加之後再次它們已被刪除我忽略了「紐約」系列和更高級別增加,但在其上設置索引,以使最新系列將動畫項目。

+0

乾淨的解決方案...看起來不錯。 –

1

試試這個

series: [{ 
      name: 'Tokyo', 
      data: [49.9, 71.5], 
      index: 4 
     }, { 
      name: 'Berlin', 
      data: [32.4, 33.2], 
      index: 3 
     }] 
    }); 

    chart.addSeries({ 
      name: 'New York', 
      data: [83.6, 78.8], 
      index: 2 
     }) 
     chart.addSeries({ 
      name: 'New York1', 
      data: [83.6, 78.8], 
      index: 1 
     }) 
+0

在我的情況下,我不能在前兩個系列上設置索引,因爲我沒有下兩個系列,系列稍後添加需要能夠根據其(未知負載)值在之間或之前的任何地方。 – Joelbitar

相關問題