2017-04-06 6 views
2

round the bars on bar chartをこのポストで見つけようとすると、提供されたjsFiddleに表示されているように動作します。これはバージョン1用です。Bar Chart.js v2の丸い棒を作成するには?

私が使用しているグラフでは、extendへの参照のために読み込みに失敗し、Chart.types.Bar.extendでスクリプトがクラッシュします。

デフォルトオプションを使用すると、チャートに問題はありません。私は正しくロードするデフォルトオプションのために最後にChart.types.Bar.extendを配置しなければならなかった。フルスクリーンでこれを実行し、表示します。

私のバージョンのChart.js 2.4.0でこれを実装しようとしました。

Chromeが報告されます。

Uncaught TypeError: Cannot read property 'extend' of undefined chart.js

このコードは、ここでも実行されません。これはなぜ起こるのですか?誰かが私を助けてくれますか?

このコードは、古いバージョンのChart.js 1.0で動作します。バージョンChart.js 2.0でこれがどのように機能するかを誰かがさらに表示できますか?ありがとうございました。ほぼいっぱいのチャートである(chart.js v2の

$(document).ready(function(){ 
 

 
\t var myBarChart1 = new Chart($('#appBarChart2_NoRound'), { 
 
\t \t type: 'bar', 
 
\t \t data: dataBar2, 
 
\t \t options: optionsBar 
 
\t }); 
 
    
 
    var ctx = $("#appBarChart2").getContext("2d"); 
 

 
    \t var myBarChart2 = new Chart(ctx).BarAlt(dataBarAlt2, { 
 
\t \t // 0 (flat) to 1 (more curvy) 
 
\t \t curvature: 1 
 
\t }); 
 
}); 
 

 
var dataBarAlt2 = { 
 
    labels: ["January", "February", "March", "April", "May", "June", "July"], 
 
    datasets: [ 
 
     { 
 
      fillColor: "#1A9BFC", 
 
      strokeColor: "#1A9BFC", 
 
      data: [65, 59, 80, 81, 56, 55, 40], 
 
     } 
 
    ] 
 
}; 
 

 
var dataBar2 = { 
 
    labels: ["January", "February", "March", "April", "May", "June", "July"], 
 
    datasets: [ 
 
     { 
 
      label: "My First dataset", 
 
      backgroundColor: '#1A9BFC', 
 
      borderColor:'#1A9BFC', 
 
      borderWidth: 1, 
 
      data: [65, 59, 80, 81, 56, 55, 40], 
 
     } 
 
    ] 
 
}; 
 

 
var optionsBar = 
 
\t \t { 
 
     scales: { 
 
      xAxes: [{ 
 
       stacked: true, 
 
\t \t \t \t barThickness: 20, 
 
\t \t \t \t gridLines:{ 
 
\t \t \t \t \t display:false, 
 
\t \t \t \t } 
 
// \t \t \t \t barPercentage:0.5, 
 
      }], 
 
      yAxes: [{ 
 
       stacked: true, 
 
\t \t \t \t 
 
// \t \t \t \t barPercentage:0.5, 
 
      }] 
 
     }, 
 
\t \t legend: { 
 
\t \t \t display: false, 
 
// \t \t \t position: 'left' 
 
\t \t } 
 
    }; 
 

 

 
Chart.types.Bar.extend({ 
 
    name: "BarAlt", 
 
    initialize: function (data) { 
 
     Chart.types.Bar.prototype.initialize.apply(this, arguments); 
 

 
     if (this.options.curvature !== undefined && this.options.curvature <= 1) { 
 
      var rectangleDraw = this.datasets[0].bars[0].draw; 
 
      var self = this; 
 
      var radius = this.datasets[0].bars[0].width * this.options.curvature * 0.5; 
 

 
      // override the rectangle draw with ours 
 
      this.datasets.forEach(function (dataset) { 
 
       dataset.bars.forEach(function (bar) { 
 
        bar.draw = function() { 
 
         // draw the original bar a little down (so that our curve brings it to its original position) 
 
         var y = bar.y; 
 
         // the min is required so animation does not start from below the axes 
 
         bar.y = Math.min(bar.y + radius, self.scale.endPoint - 1); 
 
         // adjust the bar radius depending on how much of a curve we can draw 
 
         var barRadius = (bar.y - y); 
 
         rectangleDraw.apply(bar, arguments); 
 

 
         // draw a rounded rectangle on top 
 
         Chart.helpers.drawRoundedRectangle(self.chart.ctx, bar.x - bar.width/2, bar.y - barRadius + 1, bar.width, bar.height, barRadius); 
 
         ctx.fill(); 
 

 
         // restore the y value 
 
         bar.y = y; 
 
        } 
 
       }) 
 
      }) 
 
     } 
 
    } 
 
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.5.0/Chart.js"></script> 
 
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<div> 
 
<p>Bar Chart - Working</p> 
 
<canvas id="appBarChart2_NoRound" height="100" > 
 
</div> 
 
<div> 
 
<p>Rounded Bar Chart - Not Working</p> 
 
<canvas id="appBarChart2" height="100" > 
 
</div>

+1

このコードが機能しない理由は、for for chart.js v1です。 Chart.js v2は主要な再因子であり、もはや 'Chart.types'名前空間さえありません。だからあなたの質問に「なぜこの作品はありませんか」と答えます。おそらく、chart.js 2.0で丸いバーを作成する方法について新しい質問をしたいのですか? – jordanwillis

+0

@jordanwillis shiftを押したままタイプをクリックすると、core.controller.jsが表示されます。レポートを実行するときに別の問題が発生すると、このスニペットがわかります。それは2.5の例に含まれているオンラインバージョンIに関連する可能性があります。0 – mcv

+1

おそらく彼らは後世や何かの名前空間を保っていたでしょう。しかし、結論は、v2はv1とは非常に異なり、v2に使用しようとしているコードを書き直す必要があるということです。 v2の場合と同様の機能の回答を提供したい場合はお知らせください。 – jordanwillis

答えて

3

あなたが使用しようとしていたコードは、あなたが発見したとして、chart.js v1のために実際にあると動作しません。 jsは書き換えます)。

chart.js v2で同じ結果を得るには、Chart.elements.Rectangleを拡張し、それを上書きしてdrawメソッドを上書きする必要があります。丸められた矩形(Chart.helpers.drawRoundedRectangle)を描画するchart.jsヘルパーメソッドが既に存在するので、少し修正し、すべての辺の代わりに丸みを帯びた上端だけを描画する新しいヘルパーメソッドを作成します。

// draws a rectangle with a rounded top 
Chart.helpers.drawRoundedTopRectangle = function(ctx, x, y, width, height, radius) { 
    ctx.beginPath(); 
    ctx.moveTo(x + radius, y); 
    // top right corner 
    ctx.lineTo(x + width - radius, y); 
    ctx.quadraticCurveTo(x + width, y, x + width, y + radius); 
    // bottom right corner 
    ctx.lineTo(x + width, y + height); 
    // bottom left corner 
    ctx.lineTo(x, y + height); 
    // top left 
    ctx.lineTo(x, y + radius); 
    ctx.quadraticCurveTo(x, y, x + radius, y); 
    ctx.closePath(); 
}; 

Chart.elements.RoundedTopRectangle = Chart.elements.Rectangle.extend({ 
    draw: function() { 
    var ctx = this._chart.ctx; 
    var vm = this._view; 
    var left, right, top, bottom, signX, signY, borderSkipped; 
    var borderWidth = vm.borderWidth; 

    if (!vm.horizontal) { 
     // bar 
     left = vm.x - vm.width/2; 
     right = vm.x + vm.width/2; 
     top = vm.y; 
     bottom = vm.base; 
     signX = 1; 
     signY = bottom > top? 1: -1; 
     borderSkipped = vm.borderSkipped || 'bottom'; 
    } else { 
     // horizontal bar 
     left = vm.base; 
     right = vm.x; 
     top = vm.y - vm.height/2; 
     bottom = vm.y + vm.height/2; 
     signX = right > left? 1: -1; 
     signY = 1; 
     borderSkipped = vm.borderSkipped || 'left'; 
    } 

    // Canvas doesn't allow us to stroke inside the width so we can 
    // adjust the sizes to fit if we're setting a stroke on the line 
    if (borderWidth) { 
     // borderWidth shold be less than bar width and bar height. 
     var barSize = Math.min(Math.abs(left - right), Math.abs(top - bottom)); 
     borderWidth = borderWidth > barSize? barSize: borderWidth; 
     var halfStroke = borderWidth/2; 
     // Adjust borderWidth when bar top position is near vm.base(zero). 
     var borderLeft = left + (borderSkipped !== 'left'? halfStroke * signX: 0); 
     var borderRight = right + (borderSkipped !== 'right'? -halfStroke * signX: 0); 
     var borderTop = top + (borderSkipped !== 'top'? halfStroke * signY: 0); 
     var borderBottom = bottom + (borderSkipped !== 'bottom'? -halfStroke * signY: 0); 
     // not become a vertical line? 
     if (borderLeft !== borderRight) { 
     top = borderTop; 
     bottom = borderBottom; 
     } 
     // not become a horizontal line? 
     if (borderTop !== borderBottom) { 
     left = borderLeft; 
     right = borderRight; 
     } 
    } 

    // calculate the bar width and roundess 
    var barWidth = Math.abs(left - right); 
    var roundness = this._chart.config.options.barRoundness || 0.5; 
    var radius = barWidth * roundness * 0.5; 

    // keep track of the original top of the bar 
    var prevTop = top; 

    // move the top down so there is room to draw the rounded top 
    top = prevTop + radius; 
    var barRadius = top - prevTop; 

    ctx.beginPath(); 
    ctx.fillStyle = vm.backgroundColor; 
    ctx.strokeStyle = vm.borderColor; 
    ctx.lineWidth = borderWidth; 

    // draw the rounded top rectangle 
    Chart.helpers.drawRoundedTopRectangle(ctx, left, (top - barRadius + 1), barWidth, bottom - prevTop, barRadius); 

    ctx.fill(); 
    if (borderWidth) { 
     ctx.stroke(); 
    } 

    // restore the original top value so tooltips and scales still work 
    top = prevTop; 
    }, 
}); 

次に、あなたはまた、棒グラフコントローラ(Chart.controllers.bar)を拡張し、代わりに、通常の長方形のチャートのための新たな「角の丸い四角形」を使用するdataElementTypeを上書きする必要があります。

Chart.defaults.roundedBar = Chart.helpers.clone(Chart.defaults.bar); 

Chart.controllers.roundedBar = Chart.controllers.bar.extend({ 
    dataElementType: Chart.elements.RoundedTopRectangle 
}); 

最後に、我々はどのようにトップのラウンドを制御するために上記で作成した新しいグラフの種類を使用し、barRoundnessと呼ばれる新しいオプションのプロパティを追加するには、チャートの設定を変更します(0 1は、半円形で、平坦です) 。

var ctx = document.getElementById("canvas").getContext("2d"); 
var myBar = new Chart(ctx, { 
    type: 'roundedBar', 
    data: { 
    labels: ["Car", "Bike", "Walking"], 
    datasets: [{ 
     label: 'Students', 
     backgroundColor: chartColors.blue, 
     data: [ 
     randomScalingFactor(), 
     randomScalingFactor(), 
     randomScalingFactor(), 
     ] 
    }, { 
     label: 'Teachers', 
     backgroundColor: chartColors.red, 
     data: [ 
     randomScalingFactor(), 
     randomScalingFactor(), 
     randomScalingFactor(), 
     ] 
    }, { 
     label: 'Visitors', 
     backgroundColor: chartColors.green, 
     data: [ 
     randomScalingFactor(), 
     randomScalingFactor(), 
     randomScalingFactor(), 
     ] 
    }] 
    }, 
    options: { 
    responsive: true, 
    barRoundness: 1, 
    title: { 
     display: true, 
     text: "Chart.js - Bar Chart with Rounded Tops (drawRoundedTopRectangle Method)" 
    }, 
    } 
}); 

あなたはこのcodepenで完全な作業例を見ることができます。

また、多少異なる「丸みを帯びた」外観が必要な場合は、別の方法を使用して上部(1つの2次曲線)を描画するもう1つのcodepenがあります。

+0

私はこれに戻りたい瞬間があると、私はこのアイテムを完全に閉じるでしょう。もちろんこの無駄なコメントを削除してください! :) お手伝いいただきありがとうございます! :) – mcv

+0

完全にChart.js 2+で動作します。優れた! –

関連する問題