score:17

Accepted answer

thank you guys for helping, i observed that chart.js will automatically take value for y-axis based on your data, you can change y/x-axis setting under options > scales , i also needed to change step size of y-axis and get rid of x-axis grid line,"ticks" is something i was looking for, here is my sample code and steps to achieved all these.

step 1) canvas this is place holder where your chart will display on jsp

<canvas width="1393px;" height="500px;" id="stacked"></canvas>

step 2) create sample datasets (this is json you need to create based on your requirement but make sure you provide exact the same formated json response as given below along with your data.

var barchartdata = {
labels: ["january", "february", "march", "april", "may", "june", "july"],
datasets: [{
    label: 'dataset 1',
    backgroundcolor: "red",
    data: [9, 6, 6, 8, 6, 9, 8]
},
{
    label: 'dataset 1',
        backgroundcolor: "rgba(225,226,228,0.5)",
        data: [9, 6, 6, 8, 6, 9, 8]
    }]
};

or you can call json from controller inside script as below

var jsonarry = <%=request.getattribute("jsonarry")%>;

step 3) call that json which you have created at step 2

  function barchart(){
          var context = document.getelementbyid('stacked').getcontext('2d');
          var mybar = new chart(context, {
          type: 'bar',
          data: jsonarry,

  options: {
         tooltips: {
          mode: 'label'
      },
      responsive: true,
      scales: {
          xaxes: [{
              ticks:{
                  stepsize : 10,

              },
              stacked: true,
            gridlines: {
                    linewidth: 0,
                    color: "rgba(255,255,255,0)"
                }
          }],
          yaxes: [{

              stacked: true,
               ticks: {
                  min: 0,
                  stepsize: 1,
              }

          }]
      }
      }
  });

};

hope this will help , for reference i have used following documentation chart js

thanks.

score:2

just remove the stacked option and it will stop starting from 0 (unless your data starts from 0).

related fiddle - http://jsfiddle.net/rsyk9he0/

for stacked charts, chart.js builds a list of positive and negative sums for each stack (bar) and then uses that to figure out the scale min and max values. if there are no negative values, the list of negative sums is a list of 0s. this forces the scale min to be 0.


scalestartvalue, scalestepwidth, etc. are options from v1.x of chart.js. you are using v2.x. see how to make integer scale in chartjs for the 2.x equivalents.


Related Query

More Query from same tag