简体   繁体   English

如何使用d3.js过滤器为元素分配不同的CSS类?

[英]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.

相关问题 如何使用 d3.js 按不同变量对表进行排序? - How can I use d3.js to sort a table by different variables? D3.js v5: 如何使用 filter() 获取可刷条形图选定区域中的数据 - D3.js v5: How can I use filter() to get the data in the selected region of a brushable bar chart 如何在d3.js元素上使用intro.js - How to use intro.js on d3.js elements 如何防止新元素覆盖d3.js中的现有元素? - How can I prevent new elements overwriting existing elements in d3.js? 如何通过自定义过滤器功能扩展d3.js选择? - How can I extend the d3.js selection whit a custom filter function? 如何获得一组生成的D3.js元素中每个元素的高度? - How can I get the heights of each in a group of generated D3.js elements? d3.js - 当鼠标悬停在 SVG 容器上的这些元素上时,如何将光标设置为手形? - d3.js - How can I set the cursor to hand when mouseover these elements on SVG container? 如何将刻度分配为与 D3.js 中轴所基于的相同 object 的不同键值相等? - How do I assign ticks to be equal to a different key value from the same object from the one on which the axis is based on in D3.js? 如何使用D3.js获取Javascript对象数组在折线图中使用它们? - How can I use D3.js to take an array of Javascript objects use them in a line graph? 如何使用D3.js过滤数据? - How to Filter Data with D3.js?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM