...
 
Commits (2)
......@@ -33,39 +33,30 @@ IDEAL SPEC:
export { scale }
function scale(data, config) {
var result, scale, chromatic
var result, config_scheme, scale, chromatic, d3_scheme
var metricFormula = typeof config.metric == 'function' ? config.metric
: function (d) { return d[config.metric] }
var discrete_scales = ['ordinal', 'band', 'point'],
discretizing_scales = ['quantize', 'quantile', 'threshold']
config_scheme = config.scheme
if (!config.range) {
if (config.scheme in d3) {
if (config_scheme && config_scheme in d3) {
// Explicit mention of schemeXXX or interpolateXXX
if (config.scheme.startsWith('scheme')) {
chromatic = config.count ? d3[config.scheme][config.count] : d3[config.scheme]
} else {
// interpolate
chromatic = d3[config.scheme]
}
chromatic = config.count ? d3[config_scheme][config.count] : d3[config_scheme]
} else {
config.scheme = config.scheme.toUpperCase()[0] + config.scheme.slice(1)
config_scheme = config_scheme.toUpperCase()[0] + config_scheme.slice(1)
// Decide whether schemeXXX or interpolateXXX
if (!config.count && 'interpolate' + config.scheme in d3) {
if (!config.count && 'interpolate' + config_scheme in d3) {
// interpolate
chromatic = d3['interpolate' + config.scheme]
chromatic = d3['interpolate' + config_scheme]
} else {
// discrete without config.count (provide default count)
config.count = config.count ? config.count : d3['scheme' + config.scheme].length
d3_scheme = d3['scheme' + config_scheme]
config.count = config.count ? config.count : d3_scheme.length
// for schemeBlues kind of discrete ranges, the k value is [3, 9].
// First 3 values are empty
if (typeof d3['scheme' + config.scheme][0] == 'string') {
chromatic = d3['scheme' + config.scheme]
} else {
chromatic = d3['scheme' + config.scheme][config.count]
}
chromatic = typeof d3_scheme[0] == 'string' ? d3_scheme : d3_scheme[config.count]
}
}
}
......@@ -84,7 +75,7 @@ function scale(data, config) {
}
scale = config.scale[0].toUpperCase() + config.scale.slice(1).toLowerCase()
} else if (config.scheme) {
} else if (config_scheme) {
scale = 'Sequential'
} else {
scale = 'Linear'
......@@ -93,19 +84,21 @@ function scale(data, config) {
if (config.reverse) {
// Quantize/Quantile/Threshold scales sorts the domain by ascending order
// So, no point in reversing domain. Instead reverse color scheme range
if (discretizing_scales.indexOf(scale.toLowerCase()) >= 0 && config.scheme) {
config.range = chromatic.slice().reverse()
if (discretizing_scales.indexOf(scale.toLowerCase()) >= 0 && config_scheme) {
config.range = (config.range && config.range.slice().reverse()) || chromatic.slice().reverse()
} else {
domain = domain.slice().reverse()
}
}
if (discretizing_scales.indexOf(scale.toLowerCase()) >= 0 && config.scheme && !config.reverse) {
config.range = chromatic
if (discretizing_scales.indexOf(scale.toLowerCase()) >= 0 && config_scheme && !config.reverse) {
config.range = config.range || chromatic
}
// TODO PERF: concat is too slow?
if (discretizing_scales.indexOf(scale.toLowerCase()) < 0 && config.scheme) {
console.log(config)
console.log(config.range)
if (discretizing_scales.indexOf(scale.toLowerCase()) < 0 && config_scheme) {
result = d3['scale' + scale](chromatic).domain(domain)
} else if (config.range) {
result = d3['scale' + scale]()
......
......@@ -194,9 +194,58 @@
// t.equal(quantize_scale({age: 55}), d3.schemePurples[3][1], 'reverse quantize scale 2 [0-33, 33-66, 66-100] ')
// t.equal(quantize_scale({age: 70}), d3.schemePurples[3][0], 'reverse quantize scale 3 [0-33, 33-66, 66-100] ')
t.end()
})
quantize_scale = g1.scale(data, {
metric: 'age',
domain: [0, 100],
scale: 'Quantize',
scheme: 'schemeBlues',
count: 4,
range: ['#ffffff', '#000000']
})
t.equal(quantize_scale({age: 20}), '#ffffff')
t.equal(quantize_scale({age: 50}), '#000000')
t.equal(quantize_scale({age: 100}), '#000000')
quantize_scale = g1.scale(data, {
metric: 'age',
domain: [0, 100],
scale: 'Quantize',
scheme: 'schemeBlues',
count: 4,
range: ['#ffffff', '#000000'],
reverse: true
})
t.equal(quantize_scale({age: 20}), '#000000')
t.equal(quantize_scale({age: 50}), '#ffffff')
t.equal(quantize_scale({age: 100}), '#ffffff')
quantize_scale = g1.scale(data, {
metric: 'age',
domain: [0, 100],
scale: 'Quantize',
scheme: 'schemeCategory10',
range: ['#ffffff', '#000000'],
reverse: true
})
linear_scale = g1.scale([{age: 1}, {age: 70}, {age: 100}], {
metric: 'age',
//scale: 'linear',
scheme: 'Blues'
})
t.equal(linear_scale({age: 1}), 'rgb(247, 251, 255)')
t.equal(linear_scale({age: 100}), 'rgb(8, 48, 107)')
ordinal_default_domain = g1.scale([{fruit: 1}, {fruit: 10}, {fruit: 30}], {
metric: 'fruit',
scale: 'ordinal',
scheme: 'Blues'
})
t.equal(ordinal_default_domain({fruit: 1}), '#ffffff')
t.end()
})
</script>
</body>
......