score:1
Your domain for y is indeed what's wrong here. The range needs take all the variables into account, not just carbs. In other words, your max
function needs to consider the sum of all properties.
var y = d3.scale.linear()
.domain([0, d3.max(data, function(d,i){
return parseInt(d.sum);
})])
.range([height, 0]);
This is because, in the end, your bars will be as high as the sum of all your properties (since it's a stacked bar graph)
You will then need to use the stack
function when you actually draw your <rect>
elements. Each bar will contain 3 <rect>
elements (one for carbs, fat and protein stacked one on top of the other) You didn't post any code for that but it will most likely look like :
// z is your color scale here which will decide what your rectangles look like
var z = d3.scaleOrdinal()
.range(["#<colorForCarbs>", "#<colorForFat>", "#<colorForProtein>"])
.domain(["carbs", "fat", "protein"]);
g.selectAll(".serie")
.data(stack.keys(["carbs", "fat", "protein"])(data))
.enter().append("g")
.attr("class", "serie")
.attr("fill", function(d) { return z(d.key); })
.selectAll("rect")
.data(function(d) { return d; })
.enter().append("rect")
.attr(class, 'bar')
.attr("x", function(d) { return x(d.data.name); })
.attr("y", function(d) { return y(d[1]); })
.attr("height", function(d) { return y(d[0]) - y(d[1]); })
.attr("width", x.bandwidth());
A "serie" in the example is, for example, all the rectangles that are related to "carbs" (as opposed to a stack that would contain the 3 rectangles for carbs, fat and protein).
This example should give you everything you need if you are missing something : http://bl.ocks.org/mbostock/3886208
EDIT
With your updated question, this is the section of the code where you should be using the z scale
this.selectAll('bar')
.data(params.data)
.enter()
.append('rect')
.classed('bar', true)
.attr('x', function(d,i){
return x(d.name)
})
.attr('y',function(d,i){
return y(d.carbs);
})
.attr('width', function(d){
return x.rangeBand();
})
.attr('height', function(d,i){
return height - y(d.carbs)
})
.style('fill',function(d,i){
return ordinal_color_scale(i);
});
Instead of that, use
var stack = d3.stack();
g.selectAll(".serie")
.data(stack.keys(["carbs", "fat", "protein"])(data))
.enter().append("g")
.attr("class", "serie")
.attr("fill", function(d) { return z(d.key); })
.selectAll("rect")
.data(function(d) { return d; })
.enter()
.append("rect")
.attr("x", function(d) { return x(d.data.name); })
.attr("y", function(d) { return y(d[1]); })
.attr("height", function(d) { return y(d[0]) - y(d[1]); })
.attr("width", x.rangeBand());
Source: stackoverflow.com
Related Query
- d3.js create stacked bar chart from values in object
- Filter out certain values from stacked bar chart in d3js
- d3.js - group 2 data values in a stacked bar chart
- d3.js stacked bar chart with positive and negative values
- D3: Create a grouped bar chart from json objects
- d3/dc.js - How to create a stacked bar chart while telling crossfilter to treat elements in an array as separate records?
- D3 Chart version 4 Normalized Stacked Bar Chart from vertical to horizontal
- How to create a stacked bar chart using dc.js?
- How to show values stacked bar chart utilizing dc.js and d3.js?
- Create Grouped Stacked Bar Chart
- D3 transition from stacked bar to bar chart only works the first time
- how to create a stacked bar chart with images in d3
- Create a D3 Bar Chart Using a Object
- d3.js stacked bar chart sort / change order of individual values within a stack
- d3 v4 Stacked to Grouped Bar Chart from CSV
- How do I set the X values for a singled stacked horizontal bar chart using d3.js?
- c3js Stacked Bar chart from json array
- How to create a d3 stacked bar chart that only iterates through columns 1 and 2 of data file?
- d3 stacked bar chart values not showing up on chart
- Need help using d3.js to create Stacked Bar Chart
- D3 - Change stacked bar chart from clicking on legend
- Changing the orientation of the Stacked bar Chart from Vertical to Horizontal in D3
- Unable to create a bar chart from json data using d3.js
- how to display bar values in horizontal stacked bar chart
- d3 Stacked bar chart show x values without skipping
- Create Stacked Bar chart for Timeseries X axis using C3.js
- How do you create a stacked bar chart in d3 when each individual segment is an object?
- How to pull information from a CSV file for use in a stacked bar chart
- Unable to create bar chart from d3 using URL
- Graph Not Working: Stacked Bar Chart - Date versus Values
More Query from same tag
- How would I go about making stacked area chart with overlapping areas?
- How do i add two different shapes to D3 forced directed graph based on shape field value?
- D3 updating barchart using jquery $(this) selector
- Format tick values in D3
- D3 transition along segments of path and pause at coordinate values
- Use curve in d3 force directed graph
- D3 4.0 rangeRoundBands equivalent?
- d3js - How to plot a multi line chart chart for multiple groups of items that can be updated?
- How to add a link to a geography (created as a json path) in D3.js?
- D3 Dimple - How to show multiple dimple charts on same page?
- Making a grouped bar chart using d3.js
- Interactive Charts (by dc.js) not updating each other correctly
- Unable to include parentheses in hierarchical edge bundle text labels with D3 (v3)
- Interpolation of Colors
- Drag events not getting fired easily using d3-drag on svg elements
- d3.mouse is not a function
- Trying to analyse why this transition is not happening
- Setting a custom projection using d3-geo-projection command line tool
- Transform a single object into an array of objects
- gridlines in the chart, how to obtain in this example
- D3.js version 4 make nodes sticky
- D3 text wrap with text-anchor: middle
- Why does the NVD3.js line plus bar chart example render two charts instead of one?
- Using dc.js (d3.js) and trying to highlight the x axis at 0
- Uncaught TypeError in D3 Sankey Drag Function
- How can i get the new X and Y position after rotating?
- How to set background color for svg text tspan without foreign object?
- Is there a select query to grab all but my current selection?
- d3.js time format %b %e return null
- Tooltip overwritten for moving images in D3