score:1
Use the general update pattern (carefully work through the three parts of the linked example and think about it for a bit. It's important if you want to understand d3)...
<!DOCTYPE html>
<meta charset="utf-8">
<html>
<style>
.bar {
fill: steelblue;
}
.bar:hover {
fill: brown;
}
.axis {
font: 10px sans-serif;
}
.axis path,
.axis line {
fill: none;
stroke: #000;
shape-rendering: crispEdges;
}
.x.axis path {
display: none;
}
select {
position: absolute;
top: 10px
}
</style>
<body>
<select id="flter" name="flter" onchange="updateData(this.id)">
<option value=""></option>
<option value="DataSet0.csv">DataSet0</option>
<option value="DataSet1.csv">DataSet1</option>
<option value="DataSet2.csv">DataSet2</option>
<option value="DataSet3.csv">DataSet3</option>
</select>
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.5/d3.min.js"></script>
<script>
var margin = {top: 60, right: 60, bottom: 90, left: 120},
width = 960 - margin.left - margin.right,
height = 700 - margin.top - margin.bottom,
svg = d3.select("body").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 + ")");
var x = d3.scale.ordinal()
.rangeRoundBands([0, width], .1, 0),
xAxis = d3.svg.axis()
.scale(x)
.orient("bottom"),
gX = svg.append("g")
.attr("class", "x axis")
.attr("transform", "translate(0," + height + ")");
var y = d3.scale.linear()
.range([height, 0]),
yAxis = d3.svg.axis()
.scale(y)
.orient("left")
.ticks(10, "$"),
gY = svg.append("g")
.attr("class", "y axis");
function updateData(s){
var s = document.getElementById(s);
var fileName = s.value;
d3.csv(fileName, type, function(error, data) {
if (error) throw error;
x.domain(data.map(function(d) { return d.Letter; }));
y.domain([(d3.min(data, function(d) { return d.Value1; }) - 50), d3.max(data, function(d) { return d.Value1; })]);
gX.call(xAxis)
.selectAll("text")
.style("text-anchor", "end")
.attr("dx", "-.8em")
.attr("dy", ".15em")
.attr("transform", "rotate(-15)" );
gY.call(yAxis)
// only add title once
.selectAll(".title")
.data(["Value1"])
.enter().append("text").attr("class", "title")
.attr("transform", "rotate(-90)")
.attr("y", 6)
.attr("dy", ".71em")
.style("text-anchor", "end")
.text(function(d){
return d
});
// GENERAL UPDATE PATTERN
// UPDATE
var bars = svg.selectAll(".bar")
.data(data);
// ENTER
bars.enter().insert("rect", ".axis")
.attr("class", "bar");
// UPDATE+ENTER (calling enter() merges enter into update)
bars.attr("x", function(d) { return x(d.Letter); })
.attr("width", x.rangeBand())
.attr("y", function(d) { return y(d.Value1); })
.attr("height", function(d) { return height - y(d.Value1); });
// EXIT
bars.exit().remove();
});
function type(d) {
d.Value1 = +d.Value1;
return d;
}
}
</script>
</body>
</html>
Source: stackoverflow.com
Related Query
- JavaScript D3 bar chart data will not update when new source is selected from drop down filter
- How to update d3.js bar chart with new data
- D3 How to update the chart after selection from drop down menu with new data
- D3 horizontal bar chart will not transition to new dataset
- how to update d3 chart when receiving new DATA
- Duplicate axis nodes when resetting d3 bar chart to original data source
- Update chart with new data not working
- d3 bar chart labels not getting updated on updating the chart with the new data
- old elements not removed from d3 bar chart on update
- How to insert new data into a bar chart from input text field?
- D3 updating bar chart with new data set cause update issue
- How do I make the height of a horizontal bar chart flexible in order to be resized when new data has been added in?
- Update Bar chart on new data
- d3.js chart not updated with new data
- D3.js update bar chart from json
- How to update data in stack bar chart in D3
- D3.js : How to read data from JSON in Grouped Bar Chart
- Label names are not displayed from input data in pie transition chart of d3.js
- Not understanding d3 v4 new general update pattern in nested data
- grouped bar chart from JSON data instead of CSV
- c3 JS scroll bar jumping when loading new data
- NVD3 Line Chart doesn't display when data passed using ajax - data.map is not a function
- D3 - Update bar chart data onclick
- I want to update the data on this d3.js bar chart by clicking a button. (onclick)
- update d3 chart with new data
- Not sure where margins in bars are coming from in bar chart
- Transition for grouped bar chart in d3 when updating the data values
- nvd3.js will not take data from jquery
- d3 flickering Text on setInterval when update data from csv
- D3 bar chart where axis labels are hyperlinks from data
More Query from same tag
- d3 Axis Bottom with Padding
- Rotated x-axis labels cut off in d3.js barchart
- Can I add an image to a d3js graph?
- How to implement D3 scales to have children inherit colour from parent with graduations?
- How to make D3.js path generator append two points?
- d3.js: axisBottom is appearing at the top instead
- D3 - Single and Multi Line chart tooltips
- How to Filter Data with D3.js?
- How to make a click event on D3
- Why does html Input form conflicts to d3 force layout?
- Drawing parallel coordinates for random selection of data
- D3: Finding the area of a geo polygon in d3
- D3.js v4 major & minor tick lines with y-Axis zero as the centered baseline
- D3JS - Bring back element on left if outside window
- D3 Multiple SVG Objects Following Path
- Choropleth maps: changing stroke color in `mouseover` shows overlapping boundaries
- Mouseover d3.js map with different layers
- How to specify timeout in d3 xhr send request
- how to change the appended text color added to a circle using d3
- How do I make an exponential growing chart line on D3.js
- how to create a stacked bar chart with images in d3
- How to dynamically select rows in a CSV file for conversion?
- D3 get axis values on zoom event
- Data table with vertical heading d3
- D3.js streamgraph transition plots nothing
- How to access variables declared outside d3.json request within it
- D3: How to refresh a chart with new data?
- D3.js - How to solve NAN error while dragging the line graph (Jsfiddle provided)
- using variable to parse data with D3js when reading in from csv file
- Building a flat calendar using D3.js