2016-07-07 160 views
3

有很多擴展圖表的例子,包括橫向和縱向的一行。但是,我還沒有找到一種方法來繪製水平條形圖的垂直線。如何使用ChartJS在水平條形圖上繪製垂直線?

  1. Horizontal line on horizontal line chart
  2. Vertical line on horizontal line chart
  3. Horizontal line on vertical bar chart

沒有一個 「垂直線圖」 選項一樣有一個 「水平條形圖」 選項。如何將水平條形圖與垂直線結合起來?

Chart.js documentation

結果將有一個酒吧數據集,並可以在同一個圖表上使用同一軸上像下面所使用的線數據集:

enter image description here

回答

4

ChartJS支持自定義插件。創建一個插件,它將從圖表選項中讀取一個新屬性,並在指定索引處繪製該行。

See it on Plunkr

//Create the plug in 
 
    var originalLineDraw = Chart.controllers.horizontalBar.prototype.draw; 
 
    Chart.helpers.extend(Chart.controllers.horizontalBar.prototype, { 
 
    
 
     draw: function() { 
 
      originalLineDraw.apply(this, arguments); 
 
    
 
      var chart = this.chart; 
 
      var ctx = chart.chart.ctx; 
 
    
 
      var index = chart.config.options.lineAtIndex; 
 
      if (index) { 
 
    
 
       var xaxis = chart.scales['x-axis-0']; 
 
       var yaxis = chart.scales['y-axis-0']; 
 
    
 
       var x1 = xaxis.getPixelForValue(index);      
 
       var y1 = yaxis.top;             
 
    
 
       var x2 = xaxis.getPixelForValue(index);      
 
       var y2 = yaxis.bottom;           
 
    
 
       ctx.save(); 
 
       ctx.beginPath(); 
 
       ctx.moveTo(x1, y1); 
 
       ctx.strokeStyle = 'red'; 
 
       ctx.lineTo(x2, y2); 
 
       ctx.stroke(); 
 
    
 
       ctx.restore(); 
 
      } 
 
     } 
 
    }); 
 

 
//Set up the chart data 
 
    var data = { 
 
     labels: ["January", "February", "March", "April", "May", "June", "July"], 
 
     datasets: [ 
 
      { 
 
       label: "My First dataset", 
 
       backgroundColor: [ 
 
        'rgba(255, 99, 132, 0.2)', 
 
        'rgba(54, 162, 235, 0.2)', 
 
        'rgba(255, 206, 86, 0.2)', 
 
        'rgba(75, 192, 192, 0.2)', 
 
        'rgba(153, 102, 255, 0.2)', 
 
        'rgba(255, 159, 64, 0.2)' 
 
       ], 
 
       borderColor: [ 
 
        'rgba(255,99,132,1)', 
 
        'rgba(54, 162, 235, 1)', 
 
        'rgba(255, 206, 86, 1)', 
 
        'rgba(75, 192, 192, 1)', 
 
        'rgba(153, 102, 255, 1)', 
 
        'rgba(255, 159, 64, 1)' 
 
       ], 
 
       borderWidth: 1, 
 
       data: [65, 59, 80, 81, 56, 55, 40], 
 
      } 
 
     ] 
 
    }; 
 

 
    //Load Chart 
 
    var ctx = $("#myChart"); 
 
    var myBarChart = new Chart(ctx, { 
 
     type: 'horizontalBar', 
 
     data: data, 
 
     options: { 
 
      //Set the index of the value where you want to draw the line 
 
      lineAtIndex: 60, 
 
      legend: { 
 
      display: false 
 
      } 
 
     } 
 
    });
<canvas id="myChart"></canvas> 
 

 
    <script src="https://code.jquery.com/jquery-2.2.4.min.js" integrity="sha256-BbhdlvQf/xTY9gja0Dq3HiwQF8LaCRTXxZKRutelT44=" crossorigin="anonymous"></script> 
 
    <script src='https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.2.2/Chart.min.js'></script> 
 

 
    <script src="horizontalBarPlugin.js"></script> 
 
    <script src="buildChart.js"></script>

+0

這正是我一直在尋找。 –

0

你試過嗎?這是一條帶有垂直條形圖的水平線 - 與您的情況相反。但是,也許你可以從中獲得一些有用的東西出來吧:

var chart = c3.generate({ 
bindto: '#chartContainer', 
tooltip: { 
     grouped: false 
    }, 
data: { 

    columns: [ 

     ['data1', 30, 200, 100, 400, 150, 250], 
     ['data2', 130, 340, 200, 500, 250, 350], 
     ['data3', 30, 200, 100, 400, 150, 250], 
     ['data4', 130, 340, 200, 500, 250, 350], 
     ['data5', 130, 340, 200, 500, 250, 350], 
     ['data6', 130, 340, 200, 500, 250, 350], 
     ['data7', 130, 340, 200, 500, 250, 350], 
     ['diif1', null, null, 700 ], 
     ['diif2', null, null, 1200] 
    ], 
    types:{ 
    "data1" :"bar", 
     "data2" :"bar", 
     "data3" :"bar", 
     "data4" :"bar", 
     "data5" :"bar", 
     "data6" :"bar", 
     "data7" :"bar", 
     "diff1" : "line", 
     "diff2" : "line" 

    }, 
    order:null, 
    groups: [ 
     ['data1', 'data2','data3', 'data4'], 
     ['data5', 'data6','data7'] 
    ], 
    onclick: function (d, element) { 
     var name=d.name; 
     drilldown(name); 
    } 

}, 
    grid: { 
     y: { 
     lines:[{value:1400,text:1400}, 
       {value: 1450,text: 1450} 
      ] 
     } 
    } 
}); 


function drilldown(name){ 
alert('Call for ' +name); 
} 

http://jsfiddle.net/9nxcfzb9/12/

我在尋找類似的給你,但不與水平條形圖的東西,但時間跨度吧,如果你有一個想法,請讓我知道: How do I change or add the vertical line on a tooltip of a c3 graph?

相關問題