简体   繁体   中英

d3js Switching between multiple charts

While learning on my own and being brand new to D3.js, I'm trying to create multiple pie charts with different categories switched by custom buttons.

I created an individual pie chart with effect: https://bl.ocks.org/lydiawawa/7c385eaaf24cb4e6047c9b56866fac6e/252dfbf9f27123e5577f6c54ca7dffe6fd75714e

I hope to achieve the following effect with tooltip and label, but instead of orange and apple I would like to switch in between gender, age, and race:

Desired effect: http://bl.ocks.org/j0hnsmith/5591116

This is what I have so far: https://blockbuilder.org/lydiawawa/38243015ab2ac96b6086d3bae56572b9

The most difficult part is converting the two category pie chart to three categories with tooltip and label added. I would like to have some help in achieving the effect. Thank you for any input!

Edit

I found the following effect with three categories recently, but I do not know how to add the label or legend to the graph that can be later used for tooltip as well:

http://bl.ocks.org/jfreels/6919598

I have tried to reshape json in the following format. Maybe in this way we can use d3.json instead of init()?

[
 {
   "genderC": "female",
   "gender": 533,
   "raceC": "A",
   "race": 20,
   "ageC": "0 < 12 years",
   "age": 8
 },
 {
   "genderC": "male",
   "gender": 260,
   "raceC": "A E",
   "race": 19,
   "ageC": "13 years",
   "age": 1
 },
 {
   "genderC": "",
   "gender": null,
   "raceC": "A D",
   "race": 2,
   "ageC": "14 years",
   "age": 102
 },
 {
   "genderC": "",
   "gender": null,
   "raceC": "A DE",
   "race": 1,
   "ageC": "15 years",
   "age": 195
 },
 {
   "genderC": "",
   "gender": null,
   "raceC": "A C",
   "race": 5,
   "ageC": "16 years",
   "age": 200
 },
 {
   "genderC": "",
   "gender": null,
   "raceC": "A C E",
   "race": 5,
   "ageC": "17 years",
   "age": 187
 },
 {
   "genderC": "",
   "gender": null,
   "raceC": "AB D",
   "race": 1,
   "ageC": "18 years",
   "age": 100
 },
 {
   "genderC": "",
   "gender": null,
   "raceC": "ABC E",
   "race": 1,
   "ageC": "",
   "age": null
 },
 {
   "genderC": "",
   "gender": null,
   "raceC": "ABCD",
   "race": 1,
   "ageC": "",
   "age": null
 },
 {
   "genderC": "",
   "gender": null,
   "raceC": "ABCDE",
   "race": 1,
   "ageC": "",
   "age": null
 },
 {
   "genderC": "",
   "gender": null,
   "raceC": "B",
   "race": 27,
   "ageC": "",
   "age": null
 },
 {
   "genderC": "",
   "gender": null,
   "raceC": "B H",
   "race": 0,
   "ageC": "",
   "age": null
 },
 {
   "genderC": "",
   "gender": null,
   "raceC": "B E",
   "race": 6,
   "ageC": "",
   "age": null
 },
 {
   "genderC": "",
   "gender": null,
   "raceC": "B D",
   "race": 6,
   "ageC": "",
   "age": null
 },
 {
   "genderC": "",
   "gender": null,
   "raceC": "B DE",
   "race": 2,
   "ageC": "",
   "age": null
 },
 {
   "genderC": "",
   "gender": null,
   "raceC": "BC",
   "race": 2,
   "ageC": "",
   "age": null
 },
 {
   "genderC": "",
   "gender": null,
   "raceC": "BCD",
   "race": 1,
   "ageC": "",
   "age": null
 },
 {
   "genderC": "",
   "gender": null,
   "raceC": "C",
   "race": 175,
   "ageC": "",
   "age": null
 },
 {
   "genderC": "",
   "gender": null,
   "raceC": "C E",
   "race": 17,
   "ageC": "",
   "age": null
 },
 {
   "genderC": "",
   "gender": null,
   "raceC": "CD",
   "race": 3,
   "ageC": "",
   "age": null
 },
 {
   "genderC": "",
   "gender": null,
   "raceC": "D",
   "race": 14,
   "ageC": "",
   "age": null
 },
 {
   "genderC": "",
   "gender": null,
   "raceC": "DE",
   "race": 3,
   "ageC": "",
   "age": null
 },
 {
   "genderC": "",
   "gender": null,
   "raceC": "E",
   "race": 481,
   "ageC": "",
   "age": null
 },
 {
   "genderC": "",
   "gender": null,
   "raceC": "",
   "race": null,
   "ageC": "",
   "age": null
 },
 {
   "genderC": "",
   "gender": null,
   "raceC": "",
   "race": null,
   "ageC": "",
   "age": null
 },
 {
   "genderC": "",
   "gender": null,
   "raceC": "",
   "race": null,
   "ageC": "",
   "age": null
 },
 {
   "genderC": "",
   "gender": null,
   "raceC": "",
   "race": null,
   "ageC": "",
   "age": null
 },
 {
   "genderC": "",
   "gender": null,
   "raceC": "",
   "race": null,
   "ageC": "",
   "age": null
 },
 {
   "genderC": "",
   "gender": null,
   "raceC": "",
   "race": null,
   "ageC": "",
   "age": null
 },
 {
   "genderC": "",
   "gender": null,
   "raceC": "",
   "race": null,
   "ageC": "",
   "age": null
 },
 {
   "genderC": "",
   "gender": null,
   "raceC": "",
   "race": null,
   "ageC": "",
   "age": null
 },
 {
   "genderC": "",
   "gender": null,
   "raceC": "",
   "race": null,
   "ageC": "",
   "age": null
 },
 {
   "genderC": "",
   "gender": null,
   "raceC": "",
   "race": null,
   "ageC": "",
   "age": null
 }
]

There were several issues with your code and I had to make several changes.

If I understand correctly the main idea for you was to redraw the pie charts depending on what data the user clicks on ie "Gender" "Age" or "Race".

  1. The data for each of these is very different, ie different object key-value pairs. I made all the count keys the same (in the age dataset they were Count ).

  2. Since the data is very different the examples you show where the data gets updated may not apply here as the data does not transform in this case. Instead the approach I took was to just clear the div and redraw a pie chart. So the first thing it does is to clear the chart area and then begins drawing. This significantly reduces the amount of code needed (your main.js = >300 lines, whereas mine 138 lines)

  3. I improved the tooltip as your data keys change so this needs to be accounted for in the tooltip.

  4. I moved your data to a separate js file to not clutter the main.js. I just make sure to call that before the main.js in the index.html file.

  5. I updated the function color(d) function to pick the colors from an array of objects based on the data keys. You can expand the array to your needs. If you want to use colors within a range you can use var color = d3.scale.category20(); and call the color using the index of the data like .attr("fill", function (d, i) { return color(i);}) as shown in this example http://bl.ocks.org/j0hnsmith/5591116

Here is a working block https://bl.ocks.org/akulmehta/923f277f8a10d0c35b77f6e3a84929bf/

Note that since a lot of data points for age and race are 0 , the animation stutters a bit. Also note that your labels are overlapping when you have arcs very close to each other. Hence I would suggest removing the labels.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM