Use a D3 pie chart to showcase JSON in each collection

I am trying to plot a pie chart to show how many collections are in the meaning of each name. I have checked d3.layout.pie (). Value () can only evaluate the specified array of values. Is there any solution I can get the JSON value? For example

[
    {
        "name": "json",
        "Lead": 
        [
            {"a": "aaa"},
            {"b": "bbb"},
            {"c": "ccc"},
            {"d": "ddd"}
        ],
        "Costs": 
        [
            {
                "actual": "222"
            },
            {
                "plan": "333"
            }
        ],
        "Budget": 
        [
            {
                "actual": "111"
            },
            {
                "plan": "333"
            }
        ]
    }
]

      

So the data I want to map to the value would be [4, 4, 2]

+3


source to share


1 answer


convert json to javascript object and then you can get the length of the arrays.

var temp = [
{
    "name": "json",
    "Lead": 
    [
        {"a": "aaa"},
        {"b": "bbb"},
        {"c": "ccc"},
        {"d": "ddd"}
    ],
    "Costs": 
    [
        {
            "actual": "222"
        },
        {
            "plan": "333"
        }
    ],
    "Budget": 
    [
        {
            "actual": "111"
        },
        {
            "plan": "333"
        }
    ]
}
];

 var data = [temp[0].Lead.length,temp[0].Costs.length,temp[0].Budget.length];

      



and then pass the data to the pie chart.

hope this helps :)

+1


source







All Articles