Skip to content

Commit

Permalink
feat(colors): add utility to generate color range from config
Browse files Browse the repository at this point in the history
  • Loading branch information
Raphael Benitte committed Apr 19, 2016
1 parent d88e328 commit 657d8cc
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
31 changes: 31 additions & 0 deletions src/ColorUtils.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import d3 from 'd3';
import _ from 'lodash';


export const getColorGenerator = instruction => {
Expand Down Expand Up @@ -31,4 +32,34 @@ export const getColorStyleObject = (instruction, property) => {
}

return style;
};


const d3CategoricalColors = {
d310: d3.scale.category10(),
d320: d3.scale.category20(),
d320b: d3.scale.category20b(),
d320c: d3.scale.category20c()
};

const dataColor = d => (d.color || d.data.color);

export const getColorRange = instruction => {
if (instruction === 'data') {
return dataColor;
}

if (_.isFunction(instruction)) {
return instruction;
}

if (d3CategoricalColors[instruction]) {
return d3CategoricalColors[instruction];
}

if (_.isArray(instruction)) {
return d3.scale.ordinal().range(instruction);
}

throw new Error('Unable to determine color range');
};
4 changes: 4 additions & 0 deletions src/Nivo.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import d3 from 'd3';


const defaults = {
transitionDuration: 600,
transitionEasing: 'cubic-out',
colorRange: d3.scale.category20b(),
margin: {
top: 0,
right: 0,
Expand Down

0 comments on commit 657d8cc

Please sign in to comment.