title: D3.js -- How do I update dataset via Javascript?
body: <p>I have a dataset with 11 different variables (csv file with 12 columns). I want to be able to select a certain column for my scatterplot, but I'm having some difficulties. Please bear with me, as JavaScript is not my strong suit (obviously). Here's what I attempted:</p>
  
  <pre><code>&lt;div class="variables" id="fixedacidity" onclick="drawPlot('fixedacidity');"&gt;
      &lt;h1&gt;fixed acidity&lt;/h1&gt;
  &lt;/div&gt;
  &lt;div class="variables" id="volatileacidity" onclick="drawPlot('volatileacidity');"&gt;
      &lt;h1&gt;volatile acidity&lt;/h1&gt;
  &lt;/div&gt;
  &lt;div class="variables" id="citricacid" onclick="drawPlot('citricacid');"&gt;
      &lt;h1&gt;citric acid&lt;/h1&gt;
  &lt;/div&gt;
  &lt;div class="variables" id="residualsugar" onclick="drawPlot('residualsugar');"&gt;
      &lt;h1&gt;residual sugar&lt;/h1&gt;
  &lt;/div&gt;
  etc ... 
  </code></pre>
  
  <p>I made a simple menu that calls on the <code>drawPlot</code> function, but I'm having trouble trying to get the variable to pass on correctly. </p>
  
  <p>Relevant d3/javascript:</p>
  
  <pre><code>function drawPlot(selectedVar){
  
  $(".visarea").html("");
  
  var wineVar = selectedVar;
  
  var margin = {top: 20, right: 20, bottom: 30, left: 40},
      width = 860 - margin.left - margin.right,
      height = 350 - margin.top - margin.bottom;
  
  var x = d3.scale.linear()
      .range([0, width]);
  
  var y = d3.scale.linear()
      .range([height, 0])
      .domain([0,10]);
  
  var color = d3.scale.category10();
  
  var xAxis = d3.svg.axis()
      .scale(x)
      .orient("bottom");
  
  var yAxis = d3.svg.axis()
      .scale(y)
      .orient("left").ticks(10);
  
  
  var chart1 = d3.select(".visarea").append("svg")
      .attr("width", width + margin.left + margin.right)
      .attr("height", height + margin.top + margin.bottom)
    .append("g")
      .attr("transform", "translate(" + margin.left + "," + margin.top + ")");
  
  d3.csv("red.csv", function(error, data) {
    if (error) throw error;
  
    data.forEach(function(d) {
      d.wineVar = +d.wineVar;
      d.quality = +d.quality;
    });
  
    x.domain(d3.extent(data, function(d) { return d.wineVar; })).nice();
    y.domain([0,10]).nice();
  
    chart1.append("g")
        .attr("class", "x axis")
        .attr("transform", "translate(0," + height + ")")
        .call(xAxis)
      .append("text")
        .attr("class", "label")
        .attr("x", width)
        .attr("y", -6)
        .style("text-anchor", "end")
        .text(wineVar);
  
    chart1.append("g")
        .attr("class", "y axis")
        .call(yAxis)
      .append("text")
        .attr("class", "label")
        .attr("transform", "rotate(-90)")
        .attr("y", 6)
        .attr("dy", ".71em")
        .style("text-anchor", "end")
        .text("Rated Quality")
  
    chart1.selectAll(".red.dot")
        .data(data)
      .enter().append("circle")
        .attr("class", "red dot")
        .attr("r", 3)
        .attr("cx", function(d) { return x(d.wineVar); })
        .attr("cy", function(d) { return y(d.quality); })
        .style("fill", "red");
  
  });
  }
  </code></pre>
  
  <p>Although the variable gets passed on to the function, <code>d.wineVar</code>, as expected, does not return the desired value, and thus the chart does not draw the correct values.  Can anyone recommend a workaround for this? It seems so simple, yet I've spent hours failing trying to figure this out. </p>
  
  <p>Sample of red.csv:</p>
  
  <pre><code>fixedacidity,volatileacidity,citricacid,residualsugar,chlorides,freesulfurdioxide,totalsulfurdioxide,density,pH,sulphates,alcohol,quality
  7.4,0.7,0,1.9,0.076,11,34,0.9978,3.51,0.56,9.4,5
  7.8,0.88,0,2.6,0.098,25,67,0.9968,3.2,0.68,9.8,5
  7.8,0.76,0.04,2.3,0.092,15,54,0.997,3.26,0.65,9.8,5
  </code></pre>
  
  <p><img src="https://i.imgur.com/qrf97XV.jpg" alt="Image of what I&#39;m trying to accomplish. The first dataset, fixedacidity, gets drawn up fine. I&#39;m having difficulties trying to get the menu to correctly show its respective dataset. &quot;Rated Quality&quot; will always be the data for the Y-axis.">
  Image of what I'm trying to accomplish. The first dataset, fixedacidity, gets drawn up fine. I'm having difficulties trying to get the menu to correctly show its respective dataset. "Rated Quality" will always be the data for the Y-axis.</p>