Skip to content
This repository has been archived by the owner on Nov 25, 2021. It is now read-only.

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
sgratzl committed Mar 14, 2019
2 parents 93e6745 + e7ffc6f commit f432dfe
Showing 1 changed file with 24 additions and 2 deletions.
26 changes: 24 additions & 2 deletions src/plugin/hierarchical.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,19 +53,36 @@ function generateCode(labels) {
const HierarchicalPlugin = {
id: 'chartJsPluginHierarchical',

_isValidScaleType(chart, scale) {
if (!chart.config.options.scales.hasOwnProperty(scale)) {
return false;
}
if (!Array.isArray(chart.config.options.scales[scale])) {
return false;
}
return chart.config.options.scales[scale][0].hasOwnProperty('type');
},

/**
* checks whether this plugin needs ot be enabled based on wehther one is a hierarchical axis
*/
_enabled(chart) {
if (chart.config.options.scales.xAxes[0].type === 'hierarchical') {
if (!chart.config.options.hasOwnProperty('scales')) {
return null;
}
if (this._isValidScaleType(chart, 'xAxes') && chart.config.options.scales.xAxes[0].type === 'hierarchical') {
return 'x';
}
if (chart.config.options.scales.yAxes[0].type === 'hierarchical') {
if (this._isValidScaleType(chart, 'yAxes') && chart.config.options.scales.yAxes[0].type === 'hierarchical') {
return 'y';
}
return null;
},

/**
* checks whether the data has been changed by the user and all caches are invalid
* @param {*} chart
*/
_check(chart) {
if (chart.data.labels && chart.data._verify === generateCode(chart.data.labels)) {
return;
Expand All @@ -91,6 +108,10 @@ const HierarchicalPlugin = {
this._updateAttributes(chart);
},

/**
* a verify code is used to recognize when the user changes the data
* @param {*} chart
*/
_updateVerifyCode(chart) {
chart.data._verify = generateCode(chart.data.labels);
},
Expand Down Expand Up @@ -356,6 +377,7 @@ const HierarchicalPlugin = {
const flatLabels = chart.data.flatLabels;
const data = chart.data.datasets;

// use splice since Chart.js is tracking the array using this method to have a proper animation
const removed = labels.splice.apply(labels, [index, count].concat(toAdd));
removed.forEach((d) => {
d.hidden = true;
Expand Down

0 comments on commit f432dfe

Please sign in to comment.