web-dev-qa-db-ja.com

chartjsで開始値を「0」に設定するにはどうすればよいですか?

これが私のコードです。 x軸とy軸の両方のスケールで初期値を「0」に設定する必要があります。最新バージョンのスケールオプションを試しました。

graphOptions = {               
            ///Boolean - Whether grid lines are shown across the chart
            scaleShowGridLines: false,    
            tooltipTitleTemplate: "<%= label%>",
            //String - Colour of the grid lines
            scaleGridLineColor: "rgba(0,0,0,.05)",
            //Number - Width of the grid lines
            scaleGridLineWidth: 1,
            //Boolean - Whether to show horizontal lines (except X axis)
            scaleShowHorizontalLines: true,
            //Boolean - Whether to show vertical lines (except Y axis)
            scaleShowVerticalLines: true,
            //Boolean - Whether the line is curved between points
            bezierCurve: true,
            //Number - Tension of the bezier curve between points
            bezierCurveTension: 0.4,
            //Boolean - Whether to show a dot for each point
            pointDot: true,
            //Number - Radius of each point dot in pixels
            pointDotRadius: 4,
            //Number - Pixel width of point dot stroke
            pointDotStrokeWidth: 1,               
            pointHitDetectionRadius: 20,               
            datasetStroke: true,
            datasetStrokeWidth: 2,
            datasetFill: true,               
            legendTemplate: "<ul class=\"<%=name.toLowerCase()%>-legend\"><% for (var i=0; i<datasets.length; i++){%><li><span style=\"background-color:<%=datasets[i].strokeColor%>\"></span><%if(datasets[i].label){%><%=datasets[i].label%><%}%></li><%}%></ul>"
        };
        var LineChart = new Chart(ctx).Line(graphData, graphOptions);
   }     
72
Suganthan Raj

Chart.js 2. *の場合、ゼロから開始するスケールのオプションは、 線形スケールの構成オプション の下にリストされています。これは数値データに使用されますが、これはほとんどの場合y軸に当てはまります。したがって、これを使用する必要があります。

options: {
    scales: {
        yAxes: [{
            ticks: {
                beginAtZero: true
            }
        }]
    }
}

サンプルの折れ線グラフも利用可能です here オプションはy軸に使用されます。数値データがx軸上にある場合は、xAxesの代わりにyAxesを使用します。 yAxes(またはxAxes)には配列(および複数形)が使用されることに注意してください。これは、複数の軸がある場合があるためです。

194
xnakos

このオプションを追加してください:

//Boolean - Whether the scale should start at zero, or an order of magnitude down from the lowest value
scaleBeginAtZero : true,

(参照: Chart.js

N.B:私が投稿した元のソリューションはHighcharts用でした。Highchartsを使用していない場合は、混乱を避けるためにタグを削除してください

2
wmash