Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

customize bar in chartjs

Chart.defaults.groupableBar = Chart.helpers.clone(Chart.defaults.bar);

var helpers = Chart.helpers;
Chart.controllers.groupableBar = Chart.controllers.bar.extend({
  calculateBarX: function (index, datasetIndex) {
    // position the bars based on the stack index
    var stackIndex = this.getMeta().stackIndex;
    return Chart.controllers.bar.prototype.calculateBarX.apply(this, [index, stackIndex]);
  },

  hideOtherStacks: function (datasetIndex) {
    var meta = this.getMeta();
    var stackIndex = meta.stackIndex;

    this.hiddens = [];
    for (var i = 0; i < datasetIndex; i++) {
      var dsMeta = this.chart.getDatasetMeta(i);
      if (dsMeta.stackIndex !== stackIndex) {
        this.hiddens.push(dsMeta.hidden);
        dsMeta.hidden = true;
      }
    }
  },

  unhideOtherStacks: function (datasetIndex) {
    var meta = this.getMeta();
    var stackIndex = meta.stackIndex;

    for (var i = 0; i < datasetIndex; i++) {
      var dsMeta = this.chart.getDatasetMeta(i);
      if (dsMeta.stackIndex !== stackIndex) {
        dsMeta.hidden = this.hiddens.unshift();
      }
    }
  },

  calculateBarY: function (index, datasetIndex) {
    this.hideOtherStacks(datasetIndex);
    var barY = Chart.controllers.bar.prototype.calculateBarY.apply(this, [index, datasetIndex]);
    this.unhideOtherStacks(datasetIndex);
    return barY;
  },

  calculateBarBase: function (datasetIndex, index) {
    this.hideOtherStacks(datasetIndex);
    var barBase = Chart.controllers.bar.prototype.calculateBarBase.apply(this, [datasetIndex, index]);
    this.unhideOtherStacks(datasetIndex);
    return barBase;
  },

  getBarCount: function () {
    var stacks = [];

    // put the stack index in the dataset meta
    Chart.helpers.each(this.chart.data.datasets, function (dataset, datasetIndex) {
      var meta = this.chart.getDatasetMeta(datasetIndex);
      if (meta.bar && this.chart.isDatasetVisible(datasetIndex)) {
        var stackIndex = stacks.indexOf(dataset.stack);
        if (stackIndex === -1) {
          stackIndex = stacks.length;
          stacks.push(dataset.stack);
        }
        meta.stackIndex = stackIndex;
      }
    }, this);

    this.getMeta().stacks = stacks;
    return stacks.length;
  },
});

var data = {
  labels: ["January", "February", "March", "April", "May", "June", "July"],
  datasets: [
    {
      label: "My First dataset",
      backgroundColor: "rgba(99,255,132,0.2)",
      data: [59, 80, 81, 56, 55, 40, 65],
      stack: 1
    },
    {
      label: "My Second dataset",
      backgroundColor: "rgba(99,132,255,0.2)",
      data: [80, 81, 56, 55, 40, 65, 60],
      stack: 2
    },
    {
      label: "My Third dataset",
      backgroundColor: "rgba(255,99,132,0.2)",
      data: [60, 59, 80, 81, 56, 55, 40],
      stack: 2
    }
  ]
};

var ctx = document.getElementById("myChart").getContext("2d");
new Chart(ctx, {
  type: 'groupableBar',
  data: data,
  options: {
    scales: {
      yAxes: [{
        ticks: {
          max: 160,
        },
        stacked: true,
      }]
    }
  }
});
Comment

PREVIOUS NEXT
Code Example
Javascript :: return multiple native element react native 
Javascript :: `ForwardRef(ListboxComponent)`, expected a ReactNode. at ListboxComponent 
Javascript :: { "rules": { ".read": true, ".write": true } } 
Javascript :: how to angular html ts function 
Javascript :: get twitter username from string javascript 
Javascript :: axios check if call is already running 
Javascript :: how to call AWS Serverless api in Node/JS 
Javascript :: map vs subscribe angular 
Javascript :: convert js to ts 
Javascript :: update button response 
Javascript :: javascript shorthand ternary 
Javascript :: how to wait for dom in javascript 
Javascript :: regular expressiong to indentify bible references in a sentence 
Javascript :: rxjs fromevent mouseover 
Javascript :: Caret.editorconfig 
Javascript :: convert jquery hide function to pure javascript code 
Javascript :: jshint defined variable which are coming from different file 
Javascript :: how to use mixed quotes in a sentence in js 
Javascript :: object mapper pretty write as string 
Javascript :: variables are used to store data values. 
Javascript :: arrow function no argument object 
Javascript :: get related through lookup using javascript in ms crm 
Javascript :: message is not working on emit in node.js 
Javascript :: read pdf as array bytes using fetch 
Javascript :: can not find static files on multilevel routes in express js 
Javascript :: Google App Script Create Contact 
Javascript :: next/image working without setting domain 
Javascript :: javascript channel flutter inappWebview 
Javascript :: what is the opposite of lazy initialization 
Javascript :: how to plot a line only for current day pinescript 
ADD CONTENT
Topic
Content
Source link
Name
2+4 =