Skip to content

Commit

Permalink
Ability to add different colors for bars in the same dataset
Browse files Browse the repository at this point in the history
  • Loading branch information
reksc committed Jan 11, 2021
1 parent d034192 commit 4232caa
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 10 deletions.
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,7 @@ typings/
# next.js build output
.next

.DS_Store
.DS_Store

# IDE project-specific files
.idea
7 changes: 5 additions & 2 deletions src/js/charts/AxisChart.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ export default class AxisChart extends BaseChart {

values: values,
yPositions: scaleAll(values),
colors: d.colors,

cumulativeYs: cumulativeYs,
cumulativeYPos: scaleAll(cumulativeYs),
Expand Down Expand Up @@ -238,7 +239,7 @@ export default class AxisChart extends BaseChart {
'barGraph' + '-' + d.index,
{
index: index,
color: this.colors[index],
color: d.colors || this.colors[index],
stacked: this.barOptions.stacked,

// same for all datasets
Expand Down Expand Up @@ -276,6 +277,7 @@ export default class AxisChart extends BaseChart {
return {
xPositions: xPositions,
yPositions: d.yPositions,
colors: d.colors || undefined,
offsets: offsets,
// values: d.values,
labels: labels,
Expand Down Expand Up @@ -364,11 +366,12 @@ export default class AxisChart extends BaseChart {
titles.map((label, index) => {
let values = this.state.datasets.map((set, i) => {
let value = set.values[index];
let componentColor = set.hasOwnProperty('colors') ? set.colors : this.colors[i];
return {
title: set.name,
value: value,
yPos: set.yPositions[index],
color: this.colors[i],
color: Array.isArray(componentColor) ? (i < componentColor.length ? componentColor[i] : componentColor[0]) : componentColor,
formatted: formatY ? formatY(value) : value,
};
});
Expand Down
16 changes: 11 additions & 5 deletions src/js/charts/BaseChart.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,15 +65,20 @@ export default class BaseChart {

validateColors(colors, type) {
const validColors = [];
colors = (colors || []).concat(DEFAULT_COLORS[type]);
colors.forEach((string) => {
const color = getColor(string);
if(!isValidColor(color)) {
console.warn('"' + string + '" is not a valid color.');
colors = colors || [];
colors.forEach((entry) => {
if (Array.isArray(entry)) {
validColors.push(this.validateColors(entry, type));
return;
}
const color = getColor(entry);
if (!isValidColor(color)) {
console.warn('"' + entry + '" is not a valid color.');
} else {
validColors.push(color);
}
});
validColors.concat(DEFAULT_COLORS[type]);
return validColors;
}

Expand Down Expand Up @@ -228,6 +233,7 @@ export default class BaseChart {
console.error('No data to update.');
}
this.data = this.prepareData(data);
this.data.datasets.forEach((d, i) => { if(d.colors) { this.colors[i] = d.colors;}});
this.calc(); // builds state
this.render(this.components, this.config.animate);
}
Expand Down
3 changes: 2 additions & 1 deletion src/js/objects/ChartComponents.js
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,7 @@ let componentConfigs = {
data.xPositions[j],
y,
data.barWidth,
c.color,
data.colors ? ((j < data.colors.length) ? data.colors[j] : data.colors[0]) : Array.isArray(c.color) ? (j < c.color.length ? c.color[j] : c.color[0]) : c.color,
data.labels[j],
j,
data.offsets[j],
Expand Down Expand Up @@ -336,6 +336,7 @@ let componentConfigs = {
yPositions: oldYPos,
offsets: oldOffsets,
labels: newLabels,
colors: newData.colors,

zeroLine: this.oldData.zeroLine,
barsWidth: this.oldData.barsWidth,
Expand Down
2 changes: 1 addition & 1 deletion src/js/objects/SvgTip.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ export default class SvgTip {
this.dataPointList.innerHTML = '';

this.listValues.map((set, i) => {
const color = this.colors[i] || 'black';
const color = Array.isArray(this.colors[i]) ? this.colors[i][this.index] : (this.colors[i] || 'black');
let value = set.formatted === 0 || set.formatted ? set.formatted : set.value;

let li = $.create('li', {
Expand Down

0 comments on commit 4232caa

Please sign in to comment.