2016-12-19 108 views
2

我让用户从下拉列表中选择四个数字系列中的一个,以驱动气泡图中圆形的着色。 (有半径为,x和y类似的下拉列表。)dc.js制作两种颜色之间的颜色范围

d3.select("select#colour").on('change',function(d) { 
    var tempIndex = this.selectedIndex; 
    yearlyBubbleChart.colorAccessor(function (p) { 
    return p.value[optionArray[0][tempIndex]]; 
    }); 
    yearlyBubbleChart.colors(colorbrewer[optionArray[7][tempIndex]][optionArray[8][tempIndex]]); 
    yearlyBubbleChart.colorDomain([optionArray[5][tempIndex][0], optionArray[5][tempIndex][1]]); 
}); 

要做到这一点,我使用colorAccessor,颜色和colorDomain,按照这个顺序。 (在某些情况下,我注意到这三个问题的顺序)。我希望用户能够选择最小和最大颜色,并从中驱动着色。为此,我需要为两种颜色的气泡着色,例如[ '白色', '蓝']。较高的数字值会比例更蓝。

这似乎是一件非常容易的事,但我无法解决它。提供两种颜色的数组通常用于交替,在气泡图中,最小气泡为白色,其余为蓝色。如何通过输入两种颜色来获得连续的颜色范围?

谢谢

+0

这里的关键是,[colorMixin.colors()](http://dc-js.github.io/dc.js/docs/html/dc.colorMixin.html #colors__anchor)可以采用任何类型的d3比例。如果你传递给它一组颜色,它将创建一个[序号级别](https://github.com/d3/d3-3.x-api-reference/blob/master/Ordinal-Scales.md#ordinal)对于你来说,但是你可以使用下面的@Paul显示的线性比例尺或者[量化比例尺](https://github.com/d3/d3-3.x-api-reference/blob/master/Quantitative- Scales.md#quantize-scales)如果您将连续域映射到一组离散的颜色。 – Gordon

+0

是的,如果你[看看混色的源代码](https://github.com/dc-js/dc.js/blob/develop/src/color-mixin.js),你会看到为什么需要在'colorDomain'之前调用'colors' - 前者替换了比例尺,后者在当前比例上设置了一个参数。 – Gordon

回答

3

d3知道如何插入颜色之间。您可以在范围(输出值)为颜色的位置创建线性比例。

var colorScale = d3.scale.linear() 
    .domain([0, 1]) 
    .range(['white', 'blue']) 

下面是一个示出了在集百分比由比例产生各种颜色的示例(其中,0%是全白的,100%是所有蓝色)。

var ele = d3.select('#holder'); 
 

 
var width = 500; 
 
var height = 25; 
 
var margin = 30; 
 

 
// these are the values in the domain 0 to 1 for which 
 
// we will draw bands (whose color is somewhere between 
 
// white and blue). 
 
var percents = [0, 0.1, 0.25, 0.5, 0.75, 0.9, 1]; 
 

 
// a continuous scale from 'white' to 'blue' 
 
var colorScale = d3.scale.linear() 
 
    .domain([0, 1]) 
 
    .range(['white', 'blue']) 
 

 
var x = d3.scale.ordinal() 
 
    .domain(percents) 
 
    .rangeBands([0, width]); 
 

 
var g = ele.append('svg') 
 
    .attr('width', width + 2*margin) 
 
    .attr('height', height + 2*margin) 
 
    .append('g') 
 
    .attr('transform', 'translate(' + margin + ',' + margin + ')'); 
 

 
// axis 
 
var axis = d3.svg.axis() 
 
    .orient('bottom') 
 
    .scale(x) 
 
    .tickFormat(d3.format('.2f')) 
 

 
g.append('g') 
 
    .classed('x axis', true) 
 
    .call(axis); 
 

 
var values = g.append('g') 
 
    .classed('percents', true) 
 
    .selectAll('g.percent') 
 
    .data(percents) 
 
    .enter().append('g') 
 
    .classed('percent', true) 
 
    .attr('transform', function(d){ 
 
    return 'translate(' + x(d) + ',-10)'; 
 
    }); 
 

 
var bandwidth = x.rangeBand(); 
 
values.append('rect') 
 
    .attr('x', 0) 
 
    .attr('width', bandwidth) 
 
    .attr('y', -25) 
 
    .attr('height', 25) 
 
    // use the colorScale to determine the color of each band 
 
    .style('fill', colorScale);
.domain, .tick line { 
 
    fill: none; 
 
    stroke: black; 
 
    stroke-width: 1px; 
 
    shape-rendering: crispEdges; 
 
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.4.11/d3.min.js"></script> 
 
<div id='holder'></div>

+1

这很酷,但它的水平相当低。我认为这里比较适合使用色标。 – Gordon

+0

你说得对。编辑它是一个规模。 –