score:2
In D3, once you load the data through the .data(dataset)
command, you can now access each record of the data by inserting the anonymous function function(d, i) { }
as you have done in a few of your attributes.
Since your dataset is:
var dataset = [
{"country":"Hong Kong","score":8.98},
{"country":"Singapore","score":8.54},
{"country":"New Zealand","score":8.19},
{"country":"Switzerland","score":8.09},
{"country":"Mauritius","score":8.98},
{"country":"United Arab Emirates","score":8.05},
{"country":"Canada","score":8.00},
{"country":"Australia","score":7.87},
{"country":"Jordan","score":7.86},
{"country":"Chile","score":7.84},
];
each d
is a object record e.g. {"country":"Singapore","score":8.54}
, while i
refers to the index of the object d
returned e.g. 1
for our example of d
used above.
To access the score
of the object record d
, this becomes simple Javscript object notation i.e. d.score
.
Hence your .attr
call should look like:
.attr("height", function(d) {
return d.score * 4;
});
Similarly, you can extract the other fields e.g. country
with d.country
if you intend to use it in .attr("text", function(d) { return d.country; });
This is the real beauty and power of D3. If you ever want to expand your visualization with more features that is obtained through your data, then all you have to make sure is that your dataset
data contains more data attributes, and you can call them later as you iterate through the anonymous functions. And D3 is in the spirit of its name, truly being "data-driven"! :)
score:0
Something like
For( var i =0; i<dataset.length; i++){
// Dataset[i].country
// dataset[i].score
}
You have an array of objects
score:1
You will need to fix d
to d.score
.
If you want to show country
text, write svg.selectAll("text")
after svg.selectAll("rect")
.
Like this:
var w = 500;
var h = 100;
var barPadding = 1;
var dataset = [
{"country":"Hong Kong","score":8.98},
{"country":"Singapore","score":8.54},
{"country":"New Zealand","score":8.19},
{"country":"Switzerland","score":8.09},
{"country":"Mauritius","score":8.98},
{"country":"United Arab Emirates","score":8.05},
{"country":"Canada","score":8.00},
{"country":"Australia","score":7.87},
{"country":"Jordan","score":7.86},
{"country":"Chile","score":7.84},
];
//Create SVG element
var svg = d3.select("body")
.append("svg")
.attr("width", w)
.attr("height", h);
svg.selectAll("rect")
.data(dataset)
.enter()
.append("rect")
.attr("x", function(d, i) {
return i * (w / dataset.length);
})
.attr("y", function(d) {
return h - (d.score * 4);
})
.attr("width", w / dataset.length - barPadding)
.attr("height", function(d) {
return d.score * 4;
});
svg.selectAll("text")
.data(dataset)
.enter()
.append("text")
.text(function(d) {
return d.country;
})
.attr("transform", function(d, i) {
var barW = w / dataset.length;
return "translate(" +
( barW * i + barW / 2 + barPadding ) + "," +
( h - 5 ) +
")rotate(-90)";
})
.attr("font-size", "8pt")
.attr("fill", "white");
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.4.11/d3.min.js"></script>
Source: stackoverflow.com
Related Query
- D3 - How to loop through an object with keys for a bar chart
- How do I manage groups for a grouped bar chart with a dropdown to update the chart in D3?
- How to draw a Bar Chart with time on xAxis and one or more numeric data for yAxis in d3js?
- MultiBar chart with nvd3 / d3 only shows labels for every other tick on the x-axis. How can I get them all to show up?
- How to update d3.js bar chart with new data
- d3.js bar chart sorting: can't figure out how to sort x-axis labels along with bars
- How to use x and width in a bar chart with scaleTime?
- How do I implement d3's enter update exit pattern for an area chart with focus and context?
- How to Make a bar chart with rounded corners with extended grid in d3.js?
- How to modify axis labels in d3 for a stacked bar chart when the axis labels are mapped as part of the scale's domain
- d3 show labels only for ticks with data in a bar chart
- How do I create a multiline chart using d3js with json formatted for nvd3?
- How to line up x axis on stacked bar chart with a line overlay in d3.js
- how to create a stacked bar chart with images in d3
- d3.js bar chart with pos & neg bars (win/loss) for each record
- How to create a % difference arrow line with value in a bar chart using D3.js
- Transitioning a bar chart with negative values for the width
- How can I use D3.js "onClick" event with an nvd3 bar when zoomed in on chart data?
- How to make dynamic Bar Chart in d3js with angularjs
- How to create a multi-series line chart with series for each unique item in 1st column?
- how to create separate bar chart with JSON array set?
- How to draw multiple bars in a row in a bar chart with spaces in between with d3v5?
- How do I set the X values for a singled stacked horizontal bar chart using d3.js?
- D3 Bar Chart with Variable X and Y(variable width and height for bars)
- How to create bar chart with path property in d3js?
- how to add tooltip for donut chart in loop using d3js
- How to integrate dc.js with d3 horizon chart for data rendering?
- How do I change from JS Array to JS Object for D3 Chart
- How to create a d3 stacked bar chart that only iterates through columns 1 and 2 of data file?
- How can I create a basic bar chart with unique counts on the y axis and a category on the x axis?
More Query from same tag
- Accessing the ids from the JSON files to be used in D3
- NVD3, Clear svg before loading new chart
- dc.js with node.js server side
- How to make xscales and xaxis from this data D3 V4
- c3 graph in a dark background; how to change axis and tick value color
- Unable to see the line chart in D3.js
- SVG GaussianBlur quality
- Represent the same item twice in a D3 visualisation
- D3.js brush behaviour fires two 'end' events
- D3 code linking to php file outputs the actual code as text on the web page
- d3js v7, the circle moves in the opposite direction in y coordinate when I drag the circle by the cursor
- Given a data frame in JSON format, how can I automatically assign colors to variables in D3?
- Avoid overlapping data points in d3 v4 scatterplot
- How can I draw text on a D3 SVG iff it's bounding box falls within certain criteria?
- How can I get access to the current selection inside a D3 callback?
- Error: <path> attribute d: Expected number, "MNaN,NaNLNaN,NaN"
- How to fix d3.js axis mis match when using clip paths for linear filtered line charts
- How to plot graphs using networkx and d3py
- Brush d3.js with right click
- translation using d3 with translation using data from an array in d3.js
- D3js: how to plot multiseries line chart with json data
- d3 linechart transition with missing data
- Unable to create bar chart from d3 using URL
- Format of tick values on axis
- Remove svg onclick for d3.js wordcloud
- Scaling the arrowheads according to edge thickness and have them touch the outer edged of the nodes whose size vary
- Adding several y axes with zoom / pan in D3js
- Transform <g> attribute on map of d3.js
- d3.nest() not a function
- d3.js Histogram does not show columns