your programing

chartjs에서 시작 값을 "0"으로 설정하는 방법은 무엇입니까?

lovepro 2020. 10. 13. 08:21
반응형

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);
   }     

Chart.js 2. *의 경우 0에서 시작하는 스케일 옵션은 선형 스케일구성 옵션 아래에 나열됩니다 . 이것은 숫자 데이터에 사용되며 아마도 y 축의 경우 일 것입니다. 따라서 이것을 사용해야합니다.

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

y 축에 옵션이 사용되는 샘플 라인 차트도 여기 에서 사용할 수 있습니다 . 숫자 데이터가 x 축에 있으면 xAxes대신을 사용하십시오 yAxes. 축이 여러 개있을 수도 있으므로 배열 (및 복수)이 yAxes(또는 xAxes)에 사용됩니다 .


이 옵션을 추가하십시오 :

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

(참조 : Chart.js )

참고 : 내가 게시 한 원래 솔루션은 Highcharts 용이었습니다. Highcharts를 사용하지 않는 경우 혼동을 피하기 위해 태그를 제거하세요.


나처럼 기본 구성으로 사용해야 할 수도 있습니다. 다음과 같이 min: 0노드 내부에 배치 하십시오 defaults.scale.ticks.

defaults: {
  global: {...},
  scale: {
    ...
    ticks: { min: 0 },
  }
},

참조 : https://www.chartjs.org/docs/latest/axes/

참고 URL : https://stackoverflow.com/questions/37922518/how-to-set-start-value-as-0-in-chartjs

반응형