I'm started to using d3js v5 to build a bar chart from a .csv file, and as my programming knowledge is not good, I picked an example from the web and start to working in it. I can get a specific value from the .csv and draw the chart, but I would like to implement a dropdown list to select a specific value from the .csv as showed in the image below.
I'm unable to load this data from .csv. It doesn't seem complicated, but I tried it in some ways and it didn't work out. Could someone give me any tips on how to do this?
Best regards!
Below I put the code that I working in.
1. CSV
country,year,hdi,gdp,unemp
Australia,2000,45,5.6,9
Austria,2000,54,6.1,8
Canada,2000,23,4.5,6
...
...
2. HTM - JS
<html>
<head>
<style>
.bar {
fill: steelblue;
}
.bar:hover {
fill: brown;
}
</style>
<script src="//d3js.org/d3.v5.min.js"></script>
</head>
<body>
<div>
<select id="S_indicators"></select>
</div>
<svg width="560" height="300"></svg>
<script>
var svg = d3.select("svg"),
margin = {
top: 20,
right: 20,
bottom: 30,
left: 50
},
width = +svg.attr("width") - margin.left - margin.right,
height = +svg.attr("height") - margin.top - margin.bottom,
g = svg.append("g").attr("transform", "translate(" + margin.left + "," + margin.top + ")");
var parseTime = d3.timeParse("%d-%b-%y");
var x = d3.scaleBand()
.rangeRound([0, width])
.padding(0.1);
var y = d3.scaleLinear()
.rangeRound([height, 0]);
d3.csv("output.csv").then(function (data) {
var allGroup = ["gdp","hdi","unemp"]
d3.select("#S_indicators")
.selectAll('myOptions')
.data(allGroup)
.enter()
.append('option')
.text(function (d) { return d; })
.attr("value", function (d) { return d; })
x.domain(data.map(function (d) {
return d.year;
}));
y.domain([0, d3.max(data, function (d) {
return Math.max(d.unemp);
})]);
g.append("g")
.attr("transform", "translate(0," + height + ")")
.call(d3.axisBottom(x))
g.append("g")
.call(d3.axisLeft(y))
.append("text")
.attr("fill", "#000")
.attr("transform", "rotate(-90)")
.attr("y", 6)
.attr("dy", "0.71em")
.attr("text-anchor", "end")
g.selectAll(".bar")
.data(data)
.enter().append("rect")
.attr("class", "bar")
.attr("x", function (d) {
return x(d.year);
})
.attr("y", function (d) {
return y(Number(d.unemp));
})
.attr("width", x.bandwidth())
.attr("height", function (d) {
return height - y(Number(d.unemp));
});
});
</script>
</body>
</html>
On Dec 4, 2019, at 1:35 PM, Fernando Ailson <fdiass...@gmail.com> wrote:
Hi everyone,
I'm started to using d3js v5 to build a bar chart from a .csv file, and as my programming knowledge is not good, I picked an example from the web and start to working in it. I can get a specific value from the .csv and draw the chart, but I would like to implement a dropdown list to select a specific value from the .csv as showed in the image below.
--
You received this message because you are subscribed to the Google Groups "d3-js" group.
To unsubscribe from this group and stop receiving emails from it, send an email to d3-js+un...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/d3-js/ff2eb11c-f5f9-430a-a2aa-da12ceac997f%40googlegroups.com.
<Sem tÃtulo.jpg>
var shown = "GDP";
var choices = ["GDP", "HDI", "Unemployment"]
var headers = {"GDP":"gdp", "HDI":"hdi", "Unemployment":"unemp"}
var createChart = function(data) {
 d3.selectAll("svg").remove() Â
Â
 var svg = d3.select("#drop").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 + ")");
Â
 x.domain(data.map(function(d) { return d.year; }));
 y.domain([0, d3.max(data, function(d) { return d[headers[shown]]; })]); Â
Â
 svg.selectAll(".bar")
   .data(data)
  .enter().append("rect")
   .attr("class", "bar")
   .attr("x", function(d) { return x(d.year); })
   .attr("width", x.bandwidth())
   .attr("y", function(d) { return y(d[headers[shown]]); })
   .attr("height", function(d) { return height - y(d[headers[shown]]); }) Â
   .attr("fill", "#2027f9")
Â
 // add the x Axis
 svg.append("g")
   .attr("transform", "translate(0," + height + ")")
   .style("font-size", "8px")
   .call(d3.axisBottom(x));
 // add the y Axis
 svg.append("g")
   .style("font-size", "8px")
   .call(d3.axisLeft(y));
} Â
d3.csv("output2.csv").then(function (data) {
Â
 var selector = d3.select("#drop")
    .append("select")
 .on("change", function(d){shown = d3.select(this).property('value'); createChart(data);})
 selector.selectAll("option")
    .data(choices)
    .enter().append("option")
    .attr("value", function(d){
     return d;
    })
    .text(function(d){
     return d;
    });
Â
createChart(data)