Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ability to add different colors for bars in the same dataset #179

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
32 changes: 24 additions & 8 deletions dist/frappe-charts.esm.js
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ 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 Expand Up @@ -539,6 +539,12 @@ const getColor = (color) => {
return PRESET_COLOR_MAP[color] || color;
};

const resolveBarColor = (colors, barIndex) => {
if(!Array.isArray(colors)) return colors;
if(barIndex > colors.length) barIndex = colors.length-1;
return colors[barIndex];
};

const AXIS_TICK_LENGTH = 6;
const LABEL_MARGIN = 4;
const LABEL_MAX_CHARS = 15;
Expand Down Expand Up @@ -1571,14 +1577,19 @@ 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.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 @@ -1733,6 +1744,7 @@ 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 Expand Up @@ -2287,7 +2299,7 @@ let componentConfigs = {
data.xPositions[j],
y,
data.barWidth,
c.color,
resolveBarColor(data.colors || this.constants.color, j),
data.labels[j],
j,
data.offsets[j],
Expand Down Expand Up @@ -2321,6 +2333,7 @@ let componentConfigs = {
yPositions: oldYPos,
offsets: oldOffsets,
labels: newLabels,
colors: newData.colors,

zeroLine: this.oldData.zeroLine,
barsWidth: this.oldData.barsWidth,
Expand Down Expand Up @@ -3412,6 +3425,7 @@ class AxisChart extends BaseChart {

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

cumulativeYs: cumulativeYs,
cumulativeYPos: scaleAll(cumulativeYs),
Expand Down Expand Up @@ -3534,7 +3548,7 @@ 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 @@ -3572,6 +3586,7 @@ 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 @@ -3660,11 +3675,12 @@ 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
2 changes: 1 addition & 1 deletion dist/frappe-charts.min.cjs.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/frappe-charts.min.cjs.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/frappe-charts.min.esm.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/frappe-charts.min.esm.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/frappe-charts.min.iife.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/frappe-charts.min.iife.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/assets/js/frappe-charts.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/assets/js/frappe-charts.min.js.map

Large diffs are not rendered by default.

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
14 changes: 10 additions & 4 deletions src/js/charts/BaseChart.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,14 +66,19 @@ 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.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
4 changes: 3 additions & 1 deletion src/js/objects/ChartComponents.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { equilizeNoOfElements } from '../utils/draw-utils';
import { translateHoriLine, translateVertLine, animateRegion, animateBar,
animateDot, animatePath, animatePathStr } from '../utils/animate';
import { getMonthName } from '../utils/date-utils';
import {resolveBarColor} from "../utils/colors";

class ChartComponent {
constructor({
Expand Down Expand Up @@ -302,7 +303,7 @@ let componentConfigs = {
data.xPositions[j],
y,
data.barWidth,
c.color,
resolveBarColor(data.colors || this.constants.color, j),
data.labels[j],
j,
data.offsets[j],
Expand Down Expand Up @@ -336,6 +337,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
6 changes: 6 additions & 0 deletions src/js/utils/colors.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,9 @@ export const getColor = (color) => {
}
return PRESET_COLOR_MAP[color] || color;
};

export const resolveBarColor = (colors, barIndex) => {
if(!Array.isArray(colors)) return colors;
if(barIndex > colors.length) barIndex = colors.length-1;
return colors[barIndex];
};