[Chartjs]-Chart Js Negative Scale

12👍

Hi you can try this: you can define the min or max value by passing an object to tick property.

For more detail please visit this link: http://www.chartjs.org/docs/latest/axes/radial/linear.html#tick-options

var option = {
  scales: {
    yAxes: [
      {
        stacked: true,
        gridLines: {
          display: true,
          color: "rgba(255,99,132,0.2)"
        },
        ticks: {
          suggestedMax: 100,
          suggestedMin: -10
        }
      }
    ],
    xAxes: [
      {
        gridLines: {
          display: false
        }
      }
    ]
  }
};

1👍

You can use the code below to set the range of the y-axis in any chart js graph. In this example, the minimum y-axis value is -100 and the maximum y-axis value is 100.

option: {    
  scales: {
    yAxes: [{
    ticks: { min: -100, max: 100 },
    }], 
  },
}

But when using the above code, the gap between ticks(stepSize)/ gridline define automatically. So you have to set stepSize value like below. In this example, use stepSize value as 10 and you can see gridlines with a gap of 10.

option: {    
  scales: {
    yAxes: [{
      ticks: { min: -100, max: 100, stepSize: 10 },
    }], 
  }
}

So the final code will look like below according to the above question and given an explanation.

var canvas = document.getElementById("myChart");

var data = {
  labels: ["Test"],
  datasets: [
    {
      label: "-10 to 100",
      backgroundColor: "rgba(255,99,132,0.2)",
      borderColor: "rgba(255,99,132,1)",
      borderWidth: 2,
      hoverBackgroundColor: "rgba(255,99,132,0.4)",
      hoverBorderColor: "rgba(255,99,132,1)",
      data: [100],
    },
  ],
};

var option = {
  scales: {
    yAxes: [
      {
        ticks: { min: -10, max: 100, stepSize: 10 },
      },
    ],
    xAxes: [
      {
        gridLines: {
          display: false,
        },
      },
    ],
  },
};

var myBarChart = Chart.Bar(canvas, {
  data: data,
  options: option,
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.7.0/Chart.bundle.js"></script>
<canvas id="myChart" width="400" height="200"></canvas>

0👍

Since Chart.js v2.9.0, you can do this using floating bars. Individual bars can not be specified with syntax [min,max].

var canvas = document.getElementById('myChart');
var data = {
    labels: ["Test"],
    datasets: [{
            label: "-10 to 100",
            backgroundColor: "rgba(255,99,132,0.2)",
            borderColor: "rgba(255,99,132,1)",
            borderWidth: 2,
            hoverBackgroundColor: "rgba(255,99,132,0.4)",
            hoverBorderColor: "rgba(255,99,132,1)",
            data: [[-10, 100]],
        }]
};
var option = {
	scales: {
  	yAxes:[{
        gridLines: {
          display:true,
          color:"rgba(255,99,132,0.2)"
        }
    }],
    xAxes:[{
    	gridLines: {
          display:false
        }
    }]
  }
};

var myBarChart = Chart.Bar(canvas,{
	data:data,
    options:option
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.9.3/Chart.min.js"></script>
<canvas id="myChart" width="400" height="200"></canvas>

Leave a comment