2016-06-20 3 views
32

여기 내 코드입니다. x와 y 축 스케일 모두에서 초기 값을 "0"으로 설정해야합니다. 최신 버전의 비늘 옵션을 사용해 보았습니다.chartjs에서 시작 값을 "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); 
    }  

답변

82

차트 .js 2. *, 0에서 시작하는 눈금에 대한 옵션은 configuration options of the linear scale 아래에 나열됩니다. 이것은 숫자 데이터에 사용되며, 대부분 y 축의 경우입니다. 그래서, 당신은이를 사용할 필요 : 옵션을 y 축에 사용되는 경우

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

샘플 라인 차트도 here 사용할 수 있습니다. 숫자 데이터가 x 축에있는 경우 yAxes 대신 xAxes을 사용하십시오. yAxes (또는 xAxes)에 배열 (및 복수)이 사용된다는 점에 유의하십시오. 여러 축을 가질 수도 있기 때문입니다.

+0

고맙습니다. xnakos. 그것은 나를 위해 작동 –

+0

@SuganthanRaj 여러분 환영합니다! Chart.js 2.0의 문서를 참조하십시오. 1.0 버전에서 많은 변화가 있었고 온라인 버전의 대부분은 1.0 버전에 적용됩니다. ;) – xnakos

+0

@SuganthanRaj 예를 들어, 'scaleShowVerticalLines'는 2.0에서는 유효하지 않습니다. 또는 툴팁 템플릿. 옵션은 일반적으로 2.0에서 계층 적 접근 방식을 따릅니다. – xnakos

1

이 옵션을 추가하십시오 :

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

(참조 : Chart.js를)

NB : 당신이 다음 Highcharts을 사용하지 않는 경우 내가 게시 원래 솔루션은 Highcharts 위해이었다 혼동을 피하기 위해 태그를 제거하십시오.

+0

해당 기능이 작동하지 않습니다. 나는 최신 버전의 chartjs v2.1을 사용하고있다. –

+0

내 대답 업데이트 – wmash

관련 문제