d3.js基于从json文件

时间:2016-11-16 08:51:08

标签: javascript json d3.js

我显示/隐藏了我的力布局图的链接/节点。 但是我想从文件中动态读取我的类别列表,我不想对它们进行硬编码。所以我已经有了这个:

function createFilter()
    {

        d3.select(".filterContainer").selectAll("div")
            .data(["category1", "category2", "category3"])
            .enter()
            .append("div")
            .attr("class", "checkbox-container")
            .append("label")
            .each(function(d) {
                // create checkbox for each data
                d3.select(this)
                    .append("input")
                    .attr("type", "checkbox")
                    .attr("id", function(d) {return "chk_" + d;})
                    .attr("checked", true)
                    .on("click", function(d, i) {
                        // register on click event
                        var lVisibility = this.checked? "visible":"hidden";
                        filterGraph(d, lVisibility);
                    })
                d3.select(this).append("span")
                    .text(function(d){return d;});
            });

    }

如何更改数据以读取包含对象数组的CategList中的类别,每个对象包含例如:

{
    "nodes": [
        {"country": "US", "name": "saint peter's"},
        {"country": "Brazil", "name": "saint joseph's"}        
        ...
    ],
    "links": [ ... ],
    "CategList": [
                {
                    "categ": "category1",
                    "id": "US"
                },
                {
                    "categ": "category2",
                    "id": "BR"
                },
                {
                    "categ": "category3",
                    "id": "DE"
                },
            ]   
}

如何循环浏览所有对象并提取"categ"并将其传递给d3的.data

更新

如何将其传递到多重选择器的下拉菜单中,而不是将其作为复选框传递到".filterContainer"?:

<div class="checkbox new-check check-primary">
    <input type="checkbox"  id="type_filter" />
    <label for="type_filter">Types</label>
    <br>
</div>
<select class="selectpicker" id="type_multiselector" multiple>
    <option>category1</option>
    <option>category2</option>
    <option>category3</option>
</select>

1 个答案:

答案 0 :(得分:4)

只需映射数据以创建仅包含类别的新变量,然后将其传递给d3。

这样的事情:

listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

    @Override
    public void onItemClick(AdapterView<?> parent, View view,
                            int position, long id) {

        Toast.makeText(Asesor.this, "Ha seleccionado el asesor -> "+  ParseJSON.nombres[position], Toast.LENGTH_LONG).show();
        Intent i = new Intent(Asesor.this, WelcomeNoLogin.class);

        // Pass listview item click position
        i.putExtra("position", ParseJSON.nombres[position]); /// update code here
        // Open SingleItemView.java Activity
        startActivity(i);
    }

});

回应你的评论:不确定是否完全脱离了我的头脑,但是:

long SumOfDivisors(long value)
{
    long result = 1; 

    for (long i = 2; i * i <= value; i++)
    {
       if (value % i == 0)
       {
           result += i;
           if (value / i != i) {
               result += value / i;
           }
       }
    }

    return result;
}