[英]How can I use d3.js filter to assign different css classes to elements?
For my first little d3.js project, I am adapting the "Filtering with Data" recipe from the Data Visualization with d3.js Cookbook (available at https://github.com/NickQiZhu/d3-cookbook/blob/master/src/chapter3/data-filter.html ). 对于我的第一个d3.js小项目,我正在使用d3.js Cookbook(可从https://github.com/NickQiZhu/d3-cookbook/blob/master/src获取数据可视化中的“使用数据过滤”配方) /chapter3/data-filter.html )。
My dataset contains offbeat words paired with types of "animal", "vegetable", or "mineral." 我的数据集包含与“动物”,“植物”或“矿物质”类型配对的非常规单词。 The words are displayed in black type when the page is loaded.
加载页面时,单词以黑色显示。 When the user clicks a "show animals" button, the animal words turn red.
当用户单击“显示动物”按钮时,动物单词变为红色。 Ditto for the other two types: clicking the button for a particular word type turns the corresponding words red.
与其他两种类型相同:单击特定单词类型的按钮会将对应的单词变为红色。 All words currently turn red.
当前所有单词变为红色。
What I'm trying to do, though, is have the words turn different colors based on their type: brown for animals, green for vegetables, gray for minerals. 不过,我要尝试的是根据不同的类型使单词变成不同的颜色:棕色代表动物,绿色代表蔬菜,灰色代表矿物质。 I know I need to build and fit in a conditional framework somewhere, but the specifics are evading me.
我知道我需要在某个地方构建并适应条件框架,但具体细节使我无所适从。 I imagine this exercise would be easy-peasy in straight Javascript, but I'm trying to learn d3!
我想这个练习在纯Java语言中会很容易,但是我正在尝试学习d3!
Can anybody help? 有人可以帮忙吗?
The relevant CSS: 相关的CSS:
.word {
color: black;
font-size: 2em;
line-height: 30%;
}
.word .animal {
color: brown;
}
.word.vegetable {
color: green;
}
.word .mineral {
color: lightslategray;
}
.selected {
color: red;
}
The entire script (it's rather short): 整个脚本(很短):
var dataset = [
{name: "aegirine", type: "mineral"},
{name: "burdock", type: "vegetable"},
...
{name: "yantok", type: "vegetable"},
{name: "zingel", type: "animal"}
];
function render(dataset, type) {
d3.select("body")
.select("div")
.attr("id", "worddiv")
.selectAll("p.word")
.data(dataset)
.enter()
.append("p")
.attr("class", "word")
.text(function(d) {
return d.name;
});
d3.select("body")
.selectAll("p")
.filter(function (d,i) {
return d.type == type;
})
.classed("selected", true);
} // end function render
render(dataset);
function select(type) {
render(dataset, type);
} // end function select
function clearAll() {
d3.select("body")
.selectAll("p")
.attr("class", "word");
}
The only relevant HTML is in the button div: 唯一相关的HTML在按钮div中:
<button onclick = "select('animal')">
show animals
</button>
<button onclick = "select('vegetable')">
show vegetables
</button>
<button onclick = "select('mineral')">
show minerals
</button>
<button onclick = "clearAll()">
clear
</button>
You can add a class to each item bassed on the type
attribute using the d3.selection.classed
method: 您可以使用
d3.selection.classed
方法将类添加到基于type
属性的每个项目上:
function render(dataset, type) {
d3.select("body")
.select("div")
// ... more code
.text(function(d) { return d.name; });
// Adds a class to each element based on its type
d3.select('body').selectAll('p.word')
.each(function(d) { d3.select(this).classed(d.type, true); });
d3.select("body")
// ... more code
.classed("selected", true);
} // end function render
Regards, 问候,
When setting the class of all entries to "word", you could also set the type class too: 将所有条目的类别设置为“ word”时,也可以设置类型类别:
.attr("class", function (d) {
return d.type + " word"
})
Your css should only colour these items when they also have the selected class: 您的CSS应该仅在这些项目也具有选定的类时为它们着色:
.word.animal.selected {
color: brown;
}
For bonus points, before you filter, if you remove the class "selected" from everything, you eliminate the need for a clear button. 对于奖励积分,在进行过滤之前,如果从所有内容中删除“选定”类别,则无需使用清除按钮。 Just add
.classed("selected", false)
before the filter. 只需在过滤器之前添加
.classed("selected", false)
。
See this JSfiddle for ref: http://jsfiddle.net/vzmt8ec0/ 有关参考,请参见此JSfiddle: http : //jsfiddle.net/vzmt8ec0/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.