score:1
In the example you linked, the original data is grouped by band type. Your data is grouped by set - that is, in the original each color band is grouped in the data array. In your data, each stack of bands is a group of objects in the data array.
If you want to reuse the original code, we need to translate the data (90 degrees) like so:
var initialData = [{x:"abc", y1:"3", y2:"4", y3:"10"},
{x:"abc2", y1:"6", y2:"-2", y3:"-3" },
{x:"abc3", y1:"-3", y2:"-9", y3:"4"}]
var data = ["y1","y2","y3"].map(
function(v){
return initialData.map(function(d){
return {y: +d[v]};
});
});
Then, most of the original code can be used as-is.
Another part which you can adapt is the domain for the x-scale
x = d3.scale.ordinal()
.domain(['abc','abc2','abc3'])
.rangeRoundBands([margin,w-margin], .1)
The group names are hard coded, but you have them in your objects as "x".
Instead, we can automatically use the x value of each object as the label for each set:
x = d3.scale.ordinal()
.domain(initialData.map(function(d){return d.x}))
.rangeRoundBands([margin,w-margin], .1)
Putting it all together, with some extra values: http://jsfiddle.net/fLMAZ/1/
The other option is to rewrite the code to bind the data as-is, but you will need to understand how the stacked bar chart is built and then adapt it to your original JSON.
score:0
You have a couple of options:
- Is to provide the data from the server side to make it look like the JSON structure that graph likes.
- You can parse through your JSON on the client side and make a new JSON
- You can modify the graph chart such that it will work with your JSON
Source: stackoverflow.com
Related Query
- Stacked bar chart with negative JSON data
- D3 Stacked Chart with array or JSON data
- d3.js stacked bar chart with positive and negative values
- D3 Grouped Bar Chart with JSON nested data
- Stacked Bar Chart with Time Series Data
- Prepare nested JSON data for D3 stacked bar chart
- Converting data structure of a JSON array for stacked bar chart
- D3 Stacked Chart with JSON data
- D3.js Grouped bar chart with JSON data
- D3 Stacked Bar Chart with 3 columns of data
- Bar chart with negative values
- How to update d3.js bar chart with new data
- d3.js - group 2 data values in a stacked bar chart
- d3 v4 nested data and stacked bar chart
- NVD3.js: Stacked and grouped bar chart with two y-axis
- d3 show labels only for ticks with data in a bar chart
- D3.js : How to read data from JSON in Grouped Bar Chart
- d3.js Stacked bar chart with Logarithmic Scaling
- Grouped bar chart with zoom and updateable data error
- Specifying Ticks on D3 Bar chart with Time Series data and scaleBand
- D3 V4 Rect bind data in stacked bar chart
- How to line up x axis on stacked bar chart with a line overlay in d3.js
- d3.js v4 bar chart with negative values on y axis
- grouped bar chart from JSON data instead of CSV
- how to create a stacked bar chart with images in d3
- Stacked Bar Chart with D3
- Transitioning a bar chart with negative values for the width
- Using JSON in d3v4 stacked bar chart
- D3 bar chart not working properly with all negative and positive values
- Create a bar chart with local storage data (drawing the bars doesn't work)
More Query from same tag
- D3 centre point of a line (not path)
- React Synthetic Event strange behavior
- First time using d3 and rawgit and it seemed to have stopped working
- Why does D3's brush start event interfere with later brush events?
- NVD3 CumulativeLineChart isn't rendering the right values
- D3.JS: How to animate lines
- Integrating d3.js Trend Chart (Area + Line) as data studio community visualisation
- Logarithmic scale returns NaN
- How to make d3js force directed graph less shaky when a node is dragged?
- make d3.js datamap rotate by dragging with cursor
- How to wrap d3.svg.line?
- Lost flow chart lines after upgrading from 3.5.5 to 4.2.8
- Regression that fits best scatter plot
- Stacked line charts or streamgraphs in Raphael?
- D3.js adding/removing data pie chart slices
- Reset link strength D3 force
- d3v4 checkout chart - making sure the color scheme is correct for the legend and circles
- Dynamically adding nodes to d3.js Force directed graph
- How to create elements depending on data in D3?
- Multiple Classes in SVG
- Scale and Center D3-Graphviz Graph
- D3.js, force-graph, cannot display text/label of nodes
- Converting from D3js 2.4.4 to 3.5.5
- Cannot use attr with an object in D3 v4
- Removing items with a transition prevents them from being added during that period
- Dis Alignment of bar chart in D3.js
- d3.timeMonth on axis is showing every date, instead of every month
- How to call a function with parameters from another function that has parameters?
- selecting deep elements in d3
- D3.js sunburst sorting by size