HighCharts自定義座標軸顯示內容
阿新 • • 發佈:2019-01-08
要繪製一個這樣的圖(如下圖):Y軸為數量,X軸為時間,大約240個值,那麼此時,X軸不可能完全顯示,只選擇整點來顯示。所以這裡需要自定義X軸的顯示
直接看程式碼:
<div id="stock_img" style="width:560px;"></div>
在上面的這個div上繪製。
var chart = new Highcharts.Chart({ chart: { renderTo: 'stock_img' }, credits:{ enable:true, text:'865858.COM', href:'http://865858.COM' }, xAxis: { min:0, max:240, tickInterval:60, labels:{ formatter:function(){ var arr = []; arr[0] = '09:30'; arr[60] = '10:30'; arr[120] = '11:30/13:00'; arr[180] = '14:00'; arr[240] = '15:00'; return arr[this.value]; } }, plotLines:[{ color: '#C0C0C0', width: 1, value: 0 }, { color: '#C0C0C0', width: 1, value: 60 }, { color: '#C0C0C0', width: 1, value: 120 }, { color: '#C0C0C0', width: 1, value: 180 }, { color: '#C0C0C0', width: 1, value: 240 }] }, yAxis: { title:{text:'值'}, plotLines:[{ color: '#FF0000', width: 1, value: <?php echo $info[2]/1000;?> }] }, plotOptions: { series: { marker: { enabled: false, states: { hover: {enabled: true} } } } }, title: { text:"<?php echo $info[1],'(',$info[0],')'?>" }, series: [{ data: <?php echo $tline;?>, name: "<?php echo $info[1],'(',$info[0],')'?>" }], tooltip:{ crosshairs: [true, true], formatter:function(){ return this.point.name + "<br>" + this.y; } } });
這裡說下 series 結構裡 data 的結構,是一個二維陣列,結構如下:
[[時間,數字],[時間,數字],[時間,數字],[時間,數字],[時間,數字]....]
而240個值,最後X軸的資料就是從0-239,顯示的時候就需要自定義。上面的程式碼 xAxis: labels : formatter 就是對X軸這部分的自定義。