JavaScript D3 bubble chart toggle buttons

I am new to D3.js and trying to create a bubble chart with a toggle button to switch different candidates of the presidential campaign. I managed to create the chart for one candidate only, but am having trouble doing the toggle with D3.js. I am trying to do something like this, but less sophisticated and with lesser animation. How do I do this?

My HTML is as follows:

 <link rel = "stylesheet" target='_blank' href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
<link rel = "stylesheet" href = "style.css">
<link rel="stylesheet" target='_blank' href="//rawgithub.com/Caged/d3-tip/master/examples/example-styles.css">
<script src= "http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script src= "http://d3js.org/d3.v3.min.js"></script>
<script src= "d3-tip.js"></script>

<div class="container">
    <h1> Who's Giving Them Money? </h1>

    <div class="row">
      <div class="col-sm-6">
        <p> A look at the individual contributions to prominent candidates in the 2016 presidential campaign. The visualization below looks at the amount per 1,000 persons in each state of the US. Data has been sourced from the <strong> Federal Election Commission </strong> website, and is from April 1, 2015 to September 30, 2015. </p>
      </div>
    </div>

    <div class="btn-group-lg" role="group" aria-label="...">
        <button type="button" class="btn btn-default hillary dem">Hillary Clinton</button>
        <button type="button" class="btn btn-default rubio rep">Marco Rubio</button>
        <button type="button" class="btn btn-default sanders dem">Bernie Sanders</button>
        <button type="button" class="btn btn-default bush rep">Jeb Bush</button>
        <button type="button" class="btn btn-default cruz rep">Ted Cruz</button>
        <button type="button" class="btn btn-default trump rep">Donald Trump</button>
    </div>

    <div id ="viz">

    </div>
    <script src= "script.js"></script>


</div>

Here is my javascript:

    var tooltip = d3.tip().attr('class', 'd3-tip').html(
function(d) { return d['Name'] + ': $' + d['Amount'];
});


    var diameter = 500
    var color = d3.scale.ordinal()
                  .domain(['R','D'])
                  .range(['#B2182B','#2166AC']);


    var bubble = d3.layout.pack()
                   .sort(null)
                   .size([diameter, diameter])
                   .padding(3.5);

    var svg = d3.select("#viz")
        .append("svg")
        .attr("width", diameter)
        .attr("height", diameter)
        .attr("class", "bubble");


    /* modified d3-tip boilerplate */
    /* Invoke the tip in the context of your visualization */
    svg.call(tooltip);

    d3.csv("Candidates.csv", function(error, data) {

        var nest = d3.nest()
                     .key(function(d){return d['Candidate'] })
                     .entries(data);

var button = d3.select("")


data = data.map(function(d) {
    d.value = +d["Amount"];
    return d;
});

var nodes = bubble.nodes({children: data }).filter(function(d) {
    return !d.children;
});


var bubbles = svg.append("g")
    .attr("transform", "translate(0,0)")
    .selectAll(".bubble")
    .data(nodes)
    .enter();

bubbles.append("circle")
    .attr("r", function(d) { return d.r; })
    .attr("cx", function(d) {return d.x; })
    .attr("cy", function(d) { return d.y; })
    .style("fill", function(d) {
        return color(d["Past"]);
    })


//format the text for each bubble
bubbles.append("text")
    .attr("x", function(d) {
        return d.x;
    })
    .attr("y", function(d) {
        return d.y + 5;
    })
    .attr("text-anchor", "middle")
    //.text(function(d){ return d["State"] ; })
    .text(function(d) {
        return d["State"];
    });

bubbles.append("circle")
    .attr("r", function(d) { return d.r; })
    .attr("cx", function(d) {return d.x; })
    .attr("cy", function(d) { return d.y; })
    .style("fill", function(d) {
        return "rgba(0,0,0,0)";
    })
    .on('mouseover', tooltip.show)
    .on('mouseout', tooltip.hide);

});

And here's my CSV file sample. The full file can be seen here:

    Candidate,State,Name,Past,Amount
    Marco Rubio,AK,Alaska,R,10.5
    Hillary Clinton,AL,Alabama,R,10.47
    Jeb Bush,AR,Arkansas,R,4.55
    Donald Trump,AZ,Arizona,R,46.13
    Ted Cruz,CA,California,D,82.55

Just for clarification, the 'Past' field shows how the states have voted in the past (Republican/Democrat), Sorry if I made any mistakes in the post. This is my first ever post! I'd really appreciate if you could help me out. Thank you!

Answer:1

I am trying to upgrade my laravel project framework to 5.7 from 5.5 but unable to do it .. when i change composerjson requres as laravel upgrading documentation and hit composer update on project ...

I am trying to upgrade my laravel project framework to 5.7 from 5.5 but unable to do it .. when i change composerjson requres as laravel upgrading documentation and hit composer update on project ...

Javascript value and jQuery val() returns undefined on keyup event. Variable currency is a select input which I want the value of in the convertCash function. My code sample is below: var ...

Javascript value and jQuery val() returns undefined on keyup event. Variable currency is a select input which I want the value of in the convertCash function. My code sample is below: var ...

  1. jquery input value change
  2. jquery input value set
  3. jquery input value change event
  4. jquery input value length
  5. jquery input value by name
  6. jquery input value empty
  7. jquery input value get
  8. jquery input value undefined
  9. jquery input value change listener
  10. jquery input value not updating
  11. jquery input value append
  12. jquery input value selector
  13. jquery input value clear
  14. jquery input value empty check
  15. jquery input value array
  16. jquery input value validation
  17. jquery input value update
  18. jquery input value not showing
  19. jquery input value remove
  20. jquery input value increment

I'm very new to ReactJS. I have some problem with the return value of async function when I call const result = this.getFieldsAPI(); the result value is [object Promise] I see [object Promise] ...

I'm very new to ReactJS. I have some problem with the return value of async function when I call const result = this.getFieldsAPI(); the result value is [object Promise] I see [object Promise] ...

  1. async function returns promise
  2. async function returns undefined
  3. async function returns promise pending
  4. async function returns promise instead of value
  5. async function returns object promise
  6. async function returns immediately
  7. async function returns
  8. async function return value
  9. async function return type
  10. async function return error
  11. async function return value not promise
  12. async function return type typescript
  13. async function return boolean
  14. async function return observable
  15. async function return string
  16. async function return reject
  17. async function return value javascript
  18. async function return promise typescript
  19. async function return new promise
  20. async function return array

There are two arrays: [ {"id": "5c5030b9a1ccb11fe8c321f4", "quantity": 1}, {"id": "344430b94t4t34rwefewfdff", "quantity": 5}, {"id": "342343343t4t34rwefewfd53", "quantity": 3} ] and [ {"id":...

There are two arrays: [ {"id": "5c5030b9a1ccb11fe8c321f4", "quantity": 1}, {"id": "344430b94t4t34rwefewfdff", "quantity": 5}, {"id": "342343343t4t34rwefewfd53", "quantity": 3} ] and [ {"id":...

  1. merge arrays values
  2. merge arrays values php
  3. javascript merge arrays values
  4. merge two arrays values
  5. javascript merge arrays unique values
  6. php merge arrays unique values
  7. jquery merge arrays unique values
  8. php merge arrays add values
  9. php merge two arrays values