D3.js 使用函数 spectrum.js 为节点圆圈着色
D3.js color the node circles with a function using spectrum.js
对于每个节点,我都有一个圆圈,我在函数的帮助下对其进行了着色 ColorType(d)
:
node.append("circle")
.attr("r", 20)
.attr("y", -25)
.style("fill", function(d) { return ColorType(d); })
.style("stroke-width",0.5)
.style("stroke",'black')
.attr("opacity", "1");
我的 ColorType
函数是
function ColorType(d){
for (var i = 0; i < TypesTab.length; i++) {
if (d.type == TypesTab[i].type) { return ColorAction;}
}
}
在上面的函数中,d.type
是我节点的类型(见下面的json文件结构)。而TypesTab[i].type
是我的每一个类型分别存储在types中,检查节点类型是否与types[中类型的值之一相同=59=],如果是,则应用 ColorAction
为节点圆着色。
这里是 ColorAction
代码,它嵌入在每个颜色选择器容器中,在类型中附加到每个类型,列表插入到 #filterColor
html dom。所以每种类型都有一个颜色选择器容器,它应该只为自己的类型着色。
$(document).ready(function () {
$.getJSON("databeta.json", function (obj) {
$('#filterColor').data('types', obj.types.map(function (o) {
// console.log(o.type);
return o.type;
})).append(obj.types.map(function (o) {
return '<li>' + o.type + '<input class="color-picker" type="text"/></li>';
}).join(''));
var data = $('#filterColor').data('types'); //stores all types
mynodes = obj.nodes;
console.log("mynodes : ", mynodes); //array of all my nodes
console.log("mynodes : ", mynodes[3].type); //reading the type of the fourth object in the nodes array
$("#filterColor .color-picker").each(function(){
$(this).spectrum({
color: (function (m, s, c) {
return (c ? arguments.callee(m, s, c - 1) : '#') +
s[m.floor(m.random() * s.length)]
})(Math, '0123456789ABCDEF', 5), //generate random initial color for each container
preferredFormat: "rgb",
showInput: true,
showPalette: true,
showAlpha: true,
palette: [["red", "rgba(0, 255, 0, .5)", "rgb(0, 0, 255)"]],
change: function(color) {
MyNode = d3.select("#node").selectAll(".entreprise").select("circle");
MyNode.style("fill", function(d) {
return d3.rgb(color.toHexString())
});
Coloration = d3.rgb(color.toHexString());
}
});
});
});
});
问题是当我在 ColorType(d)
函数中硬编码类型时,
if (d.type == "school") { return ColorAction;}
它仅成功地为 school 类型的节点着色。但是,如果我想让它动态化以便它使用颜色选择器分配给的类型为节点着色,它会失败,因为我无法与 o.type
的 each
建立连接.所以问题是把o.type
的each
传给ColorAction
and/orColorType(d)
这样每个容器只给自己类型的节点上色
这是一次不成功的尝试,因为它没有考虑 o.type
并从全局变量中读取 types 中的 type
( TypesTab
) 包含 类型 中的所有 type
:
function ColorType(d){
for (var i = 0; i < TypesTab.length; i++) {
if (d.type == TypesTab[i].type) { return ColorAction;}
}
}
下面是json结构:
{
"nodes": [
{
"type": "school",
"country": "US",
"name": "saint peter's",
"id": 1006
},
{
"type": "univeristy",
"country": "Brazil",
"name": "saint joseph's",
"id": 1007
}
...
],
"links": [
{
"source": 1006,
"target": 1007,
"value": 20
},
...
],
"types": [
{
"type": "school",
"image": "image01"
},
{
"type": "univeristy",
"image": "image02"
},
{
"type": "company",
"image": "image03"
},
...
]
}
这就是我阅读/导入的方式 JSON。 for in 内部是基本的,因此您需要更改该部分以满足您的需要。
d3.json("data.json", function (error, data) {
console.log(d3.values(data)); // do this as a check
for (var d in data) {
d.nodes = +d.nodes;
d.links = +d.links;
d.types = +d.types;
}
// svg can go here
})
对于每个节点,我都有一个圆圈,我在函数的帮助下对其进行了着色 ColorType(d)
:
node.append("circle")
.attr("r", 20)
.attr("y", -25)
.style("fill", function(d) { return ColorType(d); })
.style("stroke-width",0.5)
.style("stroke",'black')
.attr("opacity", "1");
我的 ColorType
函数是
function ColorType(d){
for (var i = 0; i < TypesTab.length; i++) {
if (d.type == TypesTab[i].type) { return ColorAction;}
}
}
在上面的函数中,d.type
是我节点的类型(见下面的json文件结构)。而TypesTab[i].type
是我的每一个类型分别存储在types中,检查节点类型是否与types[中类型的值之一相同=59=],如果是,则应用 ColorAction
为节点圆着色。
这里是 ColorAction
代码,它嵌入在每个颜色选择器容器中,在类型中附加到每个类型,列表插入到 #filterColor
html dom。所以每种类型都有一个颜色选择器容器,它应该只为自己的类型着色。
$(document).ready(function () {
$.getJSON("databeta.json", function (obj) {
$('#filterColor').data('types', obj.types.map(function (o) {
// console.log(o.type);
return o.type;
})).append(obj.types.map(function (o) {
return '<li>' + o.type + '<input class="color-picker" type="text"/></li>';
}).join(''));
var data = $('#filterColor').data('types'); //stores all types
mynodes = obj.nodes;
console.log("mynodes : ", mynodes); //array of all my nodes
console.log("mynodes : ", mynodes[3].type); //reading the type of the fourth object in the nodes array
$("#filterColor .color-picker").each(function(){
$(this).spectrum({
color: (function (m, s, c) {
return (c ? arguments.callee(m, s, c - 1) : '#') +
s[m.floor(m.random() * s.length)]
})(Math, '0123456789ABCDEF', 5), //generate random initial color for each container
preferredFormat: "rgb",
showInput: true,
showPalette: true,
showAlpha: true,
palette: [["red", "rgba(0, 255, 0, .5)", "rgb(0, 0, 255)"]],
change: function(color) {
MyNode = d3.select("#node").selectAll(".entreprise").select("circle");
MyNode.style("fill", function(d) {
return d3.rgb(color.toHexString())
});
Coloration = d3.rgb(color.toHexString());
}
});
});
});
});
问题是当我在 ColorType(d)
函数中硬编码类型时,
if (d.type == "school") { return ColorAction;}
它仅成功地为 school 类型的节点着色。但是,如果我想让它动态化以便它使用颜色选择器分配给的类型为节点着色,它会失败,因为我无法与 o.type
的 each
建立连接.所以问题是把o.type
的each
传给ColorAction
and/orColorType(d)
这样每个容器只给自己类型的节点上色
这是一次不成功的尝试,因为它没有考虑 o.type
并从全局变量中读取 types 中的 type
( TypesTab
) 包含 类型 中的所有 type
:
function ColorType(d){
for (var i = 0; i < TypesTab.length; i++) {
if (d.type == TypesTab[i].type) { return ColorAction;}
}
}
下面是json结构:
{
"nodes": [
{
"type": "school",
"country": "US",
"name": "saint peter's",
"id": 1006
},
{
"type": "univeristy",
"country": "Brazil",
"name": "saint joseph's",
"id": 1007
}
...
],
"links": [
{
"source": 1006,
"target": 1007,
"value": 20
},
...
],
"types": [
{
"type": "school",
"image": "image01"
},
{
"type": "univeristy",
"image": "image02"
},
{
"type": "company",
"image": "image03"
},
...
]
}
这就是我阅读/导入的方式 JSON。 for in 内部是基本的,因此您需要更改该部分以满足您的需要。
d3.json("data.json", function (error, data) {
console.log(d3.values(data)); // do this as a check
for (var d in data) {
d.nodes = +d.nodes;
d.links = +d.links;
d.types = +d.types;
}
// svg can go here
})