Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why are these 2.9 ChartJS bar charts different?

Using ChartJS 2.9.4, why are these 2 charts different?

<div >
    <canvas id="topcanvas"></canvas>
    <canvas id="bottomcanvas"></canvas>
</div>
        var leftdata = {
          labels: [0, 1, 2, 3, 4, 5], 
          datasets: [{
              label: "A",
              backgroundColor: 'rgba(0, 0, 255, 0.5)',
              data: [12, 19, 3, 5, 1],
              xAxisID: "A"
            },
            {
              label: "B",
              backgroundColor: 'rgba(0, 255, 0, 0.5)',
              data: [14, 19, 6, 2, 4],
              xAxisID: "B"
            }
          ]
        };
        
        var rightdata = {
          labels: [0, 1, 2, 3, "4", 5], // the #4 being a string is the only difference
          datasets: [{
              label: "A",
              backgroundColor: 'rgba(0, 0, 255, 0.5)',
              data: [12, 19, 3, 5, 1],
              xAxisID: "A"
            },
            {
              label: "B",
              backgroundColor: 'rgba(0, 255, 0, 0.5)',
              data: [14, 19, 6, 2, 4],
              xAxisID: "B"
            }
          ]
        };

        var options = {
          scales: {
            xAxes: [{
                id: "A",
                display: false,
                barPercentage: 1.25,
                ticks: {
                  max: 4 
                }
              },
              {
                id: "B",
                display: false,
                stacked: true,
                offset: true,
                barPercentage: 1.25,
                ticks: {
                  max: 4
                }
              },
              {
                display: true,
                ticks: {
                  autoSkip: false,
                  max: 5 
                }
              }
            ],

            yAxes: [{
              id: "bar-y-axis1",
              ticks: {
                beginAtZero: true
              }
            }]

          }
        };

        var leftctx = document.getElementById("topcanvas").getContext("2d");
        new Chart(leftctx, {
          type: 'bar',
          data: leftdata,
          options: options
        });
        var rightctx = document.getElementById("bottomcanvas").getContext("2d");
        new Chart(rightctx, {
          type: 'bar',
          data: rightdata,
          options: options
        });

Link to JSFiddleThe different Charts

like image 478
mortalapeman Avatar asked May 14 '21 21:05

mortalapeman


People also ask

How do you change the color of a bar in Chartjs?

click(function(){ barChart. data. datasets[0]. backgroundColor = "rgba(192,192,192,1)"; createNumbers(); console.

How do you make a bar graph horizontal in Chartjs?

Bar chart Bar charts are created by setting type to bar (to flip the direction of the bars, set type to horizontalBar ). The colors of the bars are set by passing one color to backgroundColor (all bars will have the same color), or an array of colors.

What is a stacked barChart?

What is a stacked bar chart? A stacked bar chart is a type of bar graph that represents the proportional contribution of individual data points in comparison to a total. The height or length of each bar represents how much each group contributes to the total.


1 Answers

This behaviour is fixed in v3 of the lib

var leftdata = {
  labels: [0, 1, 2, 3, 4, 5],
  datasets: [{
      label: "A",
      backgroundColor: 'rgba(0, 0, 255, 0.5)',
      data: [12, 19, 3, 5, 1],
      xAxisID: "A"
    },
    {
      label: "B",
      backgroundColor: 'rgba(0, 255, 0, 0.5)',
      data: [14, 19, 6, 2, 4],
      xAxisID: "B"
    }
  ]
};

var rightdata = {
  labels: [0, 1, 2, 3, "4", 5], // the #4 being a string is the only difference
  datasets: [{
      label: "A",
      backgroundColor: 'rgba(0, 0, 255, 0.5)',
      data: [12, 19, 3, 5, 1],
      xAxisID: "A"
    },
    {
      label: "B",
      backgroundColor: 'rgba(0, 255, 0, 0.5)',
      data: [14, 19, 6, 2, 4],
      xAxisID: "B"
    }
  ]
};

var options = {
  scales: {
    A: {
      display: false,
      barPercentage: 1.25,
      ticks: {
        max: 4
      },
      position: 'bottom'
    },
    B: {
      display: false,
      stacked: true,
      offset: true,
      barPercentage: 1.25,
      position: 'bottom',
      ticks: {
        max: 4
      }
    },
    x: {
      display: true,
      ticks: {
        autoSkip: false,
        max: 5
      }
    },

    y: {
      id: "bar-y-axis1",
      ticks: {
        beginAtZero: true
      }
    }

  }
};

var leftctx = document.getElementById("topcanvas").getContext("2d");
new Chart(leftctx, {
  type: 'bar',
  data: leftdata,
  options: options
});
var rightctx = document.getElementById("bottomcanvas").getContext("2d");
new Chart(rightctx, {
  type: 'bar',
  data: rightdata,
  options: options
});
<div class="grid">
  <canvas id="topcanvas"></canvas>
  <canvas id="bottomcanvas"></canvas>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/3.2.0/chart.js" integrity="sha512-opXrgVcTHsEVdBUZqTPlW9S8+99hNbaHmXtAdXXc61OUU6gOII5ku/PzZFqexHXc3hnK8IrJKHo+T7O4GRIJcw==" crossorigin="anonymous"></script>
</div>
like image 155
LeeLenalee Avatar answered Nov 07 '22 07:11

LeeLenalee