[英]Adding labels to a legend with dynamic colours
I'm currently producing a multi-line graph using d3.js.我目前正在使用 d3.js 生成多线图。 I'm currently looking to produce a simple legend that describes each lines Client name and its line colour by styling the font in its corresponding colour in the graph.
我目前正在寻找一个简单的图例,通过在图表中为其对应的颜色设置字体样式,来描述每行客户名称及其线条颜色。
My test data looks something like this:我的测试数据如下所示:
var data = [{
"Client": "ABC",
"sale": "202",
"time": "09:00"
}, {
"Client": "ABC",
"sale": "215",
"time": "11:00"
}, {
"Client": "ABC",
"sale": "179",
"time": "12:00"
}, {
"Client": "ABC",
"sale": "199",
"time": "13:00"
}, {
"Client": "ABC",
"sale": "134",
"time": "15:00"
}, {
"Client": "ABC",
"sale": "176",
"time": "16:00"
}, {
"Client": "ABC",
"sale": "197",
"time": "17:00"
}, {
"Client": "XYZ",
"sale": "100",
"time": "09:00"
...etc
}];
By using d3.nest(), I've sorted the json by its Client name which now produces the data shown below:通过使用 d3.nest(),我按照客户端名称对 json 进行了排序,现在生成的数据如下所示:
[{
"key": "ABC",
"values": [{
"Client": "ABC",
"sale": "202",
"year": "2000"
}, {
"Client": "ABC",
"sale": "215",
"year": "2002"
}, {
"Client": "ABC",
"sale": "179",
"year": "2004"
}, {
"Client": "ABC",
"sale": "199",
"year": "2006"
}, {
"Client": "ABC",
"sale": "134",
"year": "2008"
}, {
"Client": "ABC",
"sale": "176",
"year": "2010"
}]
}, {
"key": "XYZ",
"values": [{
"Client": "XYZ",
"sale": "100",
"year": "2000"
}, { ...etc
The new json I've produced above is called 'dataGroup'.我在上面生成的新 json 称为“dataGroup”。 I'm trying to use iteration to populate a div on my DOM with client names in their respective colours I've given them using d3.scale.category10();
我正在尝试使用迭代在我的 DOM 上填充一个 div,其中包含我使用 d3.scale.category10() 给它们的各自颜色的客户端名称;
Here is my current code for the div:这是我当前的 div 代码:
dataGroup.forEach(function(d, i){
d3.select("#value")
.append("text")
.data(dataGroup)
.style("color", function(d, i){
return colours(i);
})
.text(function(d){
return d.key;
});
});
So far, all this does is return the first Client (ABC) and then stops.到目前为止,所有这些都是返回第一个客户端(ABC)然后停止。 To my understanding, I thought d3 would've iterated through all the keys in the dataGroup but it hasn't.
据我了解,我认为 d3 会遍历 dataGroup 中的所有键,但事实并非如此。 I've tried putting a for loop in the text function but that hasn't helped either.
我试过在 text 函数中放置一个 for 循环,但这也没有帮助。
Any suggestions?有什么建议?
There is a problem here.这里有问题。 D3 will iterate, if you bind the data properly.
如果正确绑定数据,D3 将迭代。 This is how you can do:
你可以这样做:
First, select the div or span you want:首先,选择你想要的 div 或 span:
var clientText = d3.select("#value");
Then, create a variable for the texts, and bind the data from dataGroup
in this order:然后,为文本创建一个变量,
dataGroup
以下顺序绑定来自dataGroup
的数据:
var texts = clientText.selectAll(".clients")
.data(dataGroup)
.enter()
.append("text");
We call this a "select-data-enter-append" sequence.我们称之为“选择-数据-输入-追加”序列。
Then, you can do the rest of your code:然后,您可以执行其余代码:
texts.style("color", function(d, i){
return colors(i);
})
.text(function(d){
return d.key;
});
This will print all the names in sequence, not the most beatiful result.这将按顺序打印所有名称,而不是最漂亮的结果。
Here is the fiddle: https://jsfiddle.net/uwLcrzxk/这是小提琴: https : //jsfiddle.net/uwLcrzxk/
Try moving .data(dataGroup)
before the .append("text")
, and adding .enter()
, like so:尝试在
.append("text")
.data(dataGroup)
之前移动.data(dataGroup)
,并添加.enter()
,如下所示:
d3.select("#value")
.data(dataGroup)
.enter()
.append("text")
.style("color", function(d, i){
return colours(i);
})
.text(function(d){
return d.key;
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.