[英]How to set color of node based on data value in D3JS
How do I return a color based on the max and minimum of the values using C3JS and D3JS? 如何使用C3JS和D3JS根据值的最大值和最小值返回颜色? How do I return a color based on the max and minimum of the values using C3JS and D3JS? 如何使用C3JS和D3JS根据值的最大值和最小值返回颜色?
Example for all values below 50% of the total of values set green and for 50% plus set red. 对于低于总值50%的所有值的示例,将绿色设置为绿色,对于50%加红色的设置为红色。
I'm trying to do a scatterplot with threshold features. 我正在尝试使用阈值功能绘制散点图。
var data = [
["ibm_x", 35],
["ibm_y", 45],
["ibm_z", 145],
["ibm_a", 45],
["microsoft_x", 12],
["tcs", 20],
["sun", 25],
["infosys", 75],
["ibm_b", 35],
["ibm_c", 335],
["Mike", 2],
["Susan", 5],
["Neo", 75],
["Suraj", 15],
["Leon", 35],
["Glen", 5],
["Harris", 5],
["Tom", 65],
["Nayan", 5],
["Jhagan", 1],
];
var chart = c3.generate({
point: {
r: 7
},
data: {
xs: {
Mike: 'ibm_x',
Susan: 'microsoft_x',
Glen: 'tcs',
Harris: 'sun',
Tom: 'infosys',
Lear : 'oracle',
Leon: 'ibm_y',
Neo: 'ibm_z',
Suraj: 'ibm_a',
Nayan: 'ibm_b',
Jhagan: 'ibm_c'
},
columns: data,
type: 'scatter'
},
axis: {
x: {
label: 'Interactions',
tick: {
fit: false
},
min: 1,
max: 500
},
y: {
label: 'Days',
min: 1,
max: 90
}
},
legend: {
show: false
}});
d3.selectAll(".c3-circle").style("fill", function(d, i) {
abc = (d.x+d.value);
function numberSum(N) {var total = 0;
for(var i = 1; i <= N; i++){
total += i;
}
return total;}return i % 2 ? "#F86A52" : "#49B5A6";});
Below is the fiddle - 以下是小提琴-
https://jsfiddle.net/npmarkunda/hh1o9yLw/ https://jsfiddle.net/npmarkunda/hh1o9yLw/
You could do something like this. 你可以做这样的事情。
1) Work out the average of your data 1)计算出您的数据平均值
var average = d3.mean(data.map(function(i){return i[1]})));
2) Use the average when setting the color 2)设置颜色时使用平均值
if (d.value > average)
return "#49B5A6"
else
return "#F86A52"
You can do it this way first calculate the average: 您可以通过这种方式先计算平均值:
var average = d3.mean(data.map(function(i) {
return i[1]
}));
Then use this to color the nodes( http://c3js.org/reference.html#data-color ): 然后使用它为节点着色( http://c3js.org/reference.html#data-color ):
data: {
xs: {
Mike: 'ibm_x',
Susan: 'microsoft_x',
Glen: 'tcs',
Harris: 'sun',
Tom: 'infosys',
Lear: 'oracle',
Leon: 'ibm_y',
Neo: 'ibm_z',
Suraj: 'ibm_a',
Nayan: 'ibm_b',
Jhagan: 'ibm_c'
},
//color the nodes
color: function(color, d) {
if (d.value > average) {
return "#F86A52"
} else {
return "#49B5A6"
};
},
You don't need the code below: 您不需要以下代码:
d3.selectAll(".c3-circle").style("fill", function(d, i) {
abc = (d.x+d.value);
function numberSum(N) {var total = 0;
for(var i = 1; i <= N; i++){
total += i;
}
return total;}return i % 2 ? "#F86A52" : "#49B5A6";});
Hope this helps! 希望这可以帮助!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.