score:1
Accepted answer
What you can do is use a for/forEach
for your data dataByCLIN
and create rects
inside each loop like this:
var bars = svg.selectAll('.rects');
dataByCLIN.forEach(function(d,i1) {
bars.data(d.values).enter().append('rect').attr('class', 'rects')
.attr('width', function(d) {
return d.cost;
})
.attr('height', 10)
.attr('y', function(d, i) {
return (i+i1+i1+1) * 20 - 20;
});
});
Here's a fiddle:
var json = [{
"CLIN": "1",
"cost": 257,
},
{
"CLIN": "1",
"cost": 846,
},
{
"CLIN": "2",
"cost": 162
},
{
"CLIN": "2",
"cost": 984
}
];
var margin = {
top: 20,
right: 160,
bottom: 35,
left: 30
};
var width = 960 - margin.left - margin.right,
height = 500 - margin.top - margin.bottom;
var 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 dataByCLIN = d3.nest()
.key(function(d) {
return d.CLIN;
})
.entries(json);
var bars = svg.selectAll('.rects');
dataByCLIN.forEach(function(d, i1) {
bars.data(d.values).enter().append('rect').attr('class', 'rects')
.attr('width', function(d) {
return d.cost;
})
.attr('height', 10)
.attr('y', function(d, i) {
return (i + i1 + i1 + 1) * 20 - 20;
});
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.4.11/d3.min.js"></script>
Source: stackoverflow.com
Related Query
- How to call all values in nested JSON data (d3js)
- How do you call data from nested JSON array using d3?
- How to animate transition over grouped, nested json coordinate data with D3.js?
- How do you retrieve the key values of a nested data set in D3
- How do I access values in a d3 json file with nested array objects
- How to access nested data Values in D3
- How to retrieve the key values of a nested data set in D3
- How should i use nested data in d3js
- how to convert data selected from a postgres database to json or csv to use it with d3js lib?
- how to access data from nested json array D3
- d3js - how to prevent the `line` from `null` values in json data?
- how can i group JSON data and generate a graph of each group in D3js
- d3Js - how to arrange my values as around the 360d and automate data in a circle?
- c3js line chart all data unselected how to show tick values
- How to access nested data in D3js
- How to change randomised data used for graph values with my input data? D3js donut graph
- d3.js How to create one paragraph element for sum of all json object values
- D3 - how to deal with JSON data structures?
- how to encode this data to parent / children structure in JSON
- How do I create a tree layout using JSON data in d3.v4 - without stratify()
- How to select unique values in d3.js from data
- How do I load JSON data synchronously with d3.js?
- Nvd3: How prevent to display chart between -1 and 1 if have all y values 0?
- How to load data from an internal JSON array rather than from an external resource / file for a collapsible tree in d3.js?
- Creating nested Json structure with multiple key values in Python from Json
- D3 - How to update force simulation when data values change
- Updating the data of a pack layout from JSON call and redrawing
- How do I write recursive d3.js code to deal with nested data structures?
- d3.js - max and min value from json data which has array of values
- How do I process data that is nested multiple levels in D3?
More Query from same tag
- How to transform code from a tsv import to direct data
- Hierarchical edge bundling 2 way - link colour
- D3 hierarchical bar chart - Rotate X and Y Axes
- Filtering and matching json data in D3.js hover
- Load external data (.tsv) into d3.js
- How to graph dates on X axis while skipping weekends d3.js?
- Test mouseover and mouseout events in AngularJS directive
- How to show nested Rectangle in D3
- Access nested data without appending in d3.js
- Plotly.js in relative barmode labels are overlapped when both bars have value 0
- How to zoom a d3 chart correctly?
- Make Responsive Svg inside angular-gridster item
- Use months instead of year in D3plus.js .time()
- d3 Links Not Properly Hiding
- d3js/svg - Polygon without fixed coordinates?
- D3 collapsible tree - jumpy on zoom
- Draw bar charts with rounded rectangles in dc.js
- How to draw svg coordinate space where x=0 and y=0 coordinates fall on the bottom left
- D3 streamgraph layer fade function
- where can I download this code
- Why click event on d3.js resuable chart creating problem?
- How to interpret short drag events as clicks
- Get x,y location of D3 point on path
- Existing nodes freeze upon new data entering
- Dynamically generate array of objects - Javascript
- D3 force directed layout with bounding box
- IOS app webview SVG ClipPath Issue
- Handling D3v3 Labels Overlapping
- D3 Bar Chart Values Out of Scale Range
- D3.js node links overflowing into sibling nodes when new nodes are being added