score:1
Accepted answer
Your code was almost complete; the only things missing were that you needed to add a fake root node to your hierarchy, to specify the accessor for the children, and to fix the value for the leaf nodes, which had an extra layer of nesting that needed to be removed.
Leaf node value:
var nested_data = d3.nest()
[...]
.rollup(function(leaves) {
return {
value: leaves.length
}
})
.entries(data);
results in leaf nodes like so:
{ value: { value: 5 } }
remove the extra value:
from the rollup
call:
var nested_data = d3.nest()
[...]
.rollup(function(leaves) {
return leaves.length
})
.entries(data);
and now your leaf nodes look like so:
{ value: 5 }
Fake root node
Ensure all your nested data is collected in the same hierarchy by adding a fake root:
d3.hierarchy({ values: nested_data })
Child node accessor
Specify the accessor for the children as otherwise d3 will look for x.children
, and if it isn't there, will assume the node is a leaf:
d3.hierarchy({ value: nested_data }, function(d) { return d.values })
Result:
const margin = {
top: 40,
right: 10,
bottom: 10,
left: 10
};
var width = 960 - margin.left - margin.right;
var height = 960 - margin.top - margin.bottom;
var color = d3.scaleOrdinal().range(d3.schemeCategory20b);
var treemap = d3.treemap()
.size([width, height])
.padding(1)
.round(true);
var json_data = d3.json("https://raw.githubusercontent.com/vega/vega/master/docs/data/cars.json", function(error, data) {
if (error) throw error;
var nested_data = d3.nest()
.key(function(d) {
return d.Origin;
})
.key(function(d) {
return d.Cylinders;
})
.key(function(d) {
return d.Miles_per_Gallon;
})
.rollup(function(leaves) {
return leaves.length
})
.entries(data);
var root = d3.hierarchy({
values: nested_data
}, d => d.values)
.sum(function(d) {
return d.value;
})
treemap(root);
var nodes = d3.select('svg g')
.selectAll('g')
.data(root.descendants())
.enter()
.append('g')
.attr('transform', function(d) {
return 'translate(' + [d.x0, d.y0] + ')'
})
nodes
.append('rect')
.attr('width', function(d) {
return (d.x1 - d.x0) * 10;
})
.attr('height', function(d) {
return (d.y1 - d.y0) * 10;
})
.attr('fill', function(d) {
return color(d.data.key);
})
nodes
.append('text')
.text(function(d) {
return d.data.key;
})
});
body {
font: 10px sans-serif;
position: relative;
}
.node {
box-sizing: border-box;
line-height: 1em;
overflow: hidden;
position: absolute;
white-space: pre;
background: #ddd;
}
.node-label,
.node-value {
margin: 4px;
}
.node-value {
margin-top: -2px;
}
.node-value {
font-weight: bold;
}
<title>My treemap</title>
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/4.2.2/d3.min.js"></script>
<svg width="1000" height="1000">
<g></g>
</svg>
Source: stackoverflow.com
Related Query
- D3 treemap json implementation with d3.nest
- Cannot get treemap to render with new JSON data
- Coloring a Nest treemap with D3.JS
- TreeMap not working with JSON
- Convert a directory structure in the filesystem to JSON with Node.js
- D3 - how to deal with JSON data structures?
- D3: use nest function to turn flat data with parent key into a hierarchy
- How do I load JSON data synchronously with d3.js?
- How can I perform this d3.js magic (Grouped Bar Chart) with JSON rather than CSV?
- Creating nested Json structure with multiple key values in Python from Json
- d3.js, using d3.extent to find min/max of nest json values
- Difficulty accessing json file with d3 and flask
- How to replace a d3js nest function with group and rollup
- Using large JSON files with d3.js causes massive performance hits/crashes
- D3 Stacked Chart with array or JSON data
- Passing JSON data from server to client with Flask
- How to animate transition over grouped, nested json coordinate data with D3.js?
- How would d3.js difference chart example work with json data?
- Linechart with JSON object in dc.js
- Create an interactive 3d scatter plot with D3.js and Three.js with json data
- D3 animate marker along multiple paths with d3 nest group
- d3 treemap change color according to data passed in json
- Using D3.js with a javascript object instead of an external JSON file
- Create a JSON file from a Flask view to be used with D3
- D3 - dealing with JSON nested data structures
- Simple d3.js graph with a json feed input
- Parse JSON with containing function
- How do I access values in a d3 json file with nested array objects
- Creating a D3 Line Graph with Points using 2 Level Nested JSON
- Reactjs with D3js treemap
More Query from same tag
- IBM Cognos Analytics with D3.js Bar Chart height problem
- Javascript: calling function within D3 from another file
- graph layout using angularjs
- Obtain access to parent node from inside attrTween callback function (D3)
- R / d3heatmap - is there a way to rotate the axis label?
- Lines for labels outside arc (Pie chart) d3.js
- D3 js line chart shows dot on mouse over and mouse out
- Can I have a SVG Pattern with background color?
- How to reset force directed graph to its original position?
- Plot dimple(d3.js) chart in deck.js section
- Max length of an array after reading a CSV (JavaScript)
- Fail to parse/project coordinates onto JSON
- D3.js Combining Candlestick Chart with Line Graph
- line graph renders wrongly and problem with the X axis (time), cannot brush the graph - time series dc.js
- Embed d3.js to a Jekyll Post
- Transitioning svg text opacity
- Building a force-directed concentric circle graph with node ordering from inside out in D3.V4
- Zoomable Circle Packing with Automatic Text Sizing in D3.js
- removal of svg object after transition
- Make d3 observe a property in a dataset
- Changing object variables to abbreviations before passing it to a network graph
- Modified object not indexing properly on mouse click function, javascript
- How to get data from d3js polyline?
- d3 in Vue application - How to apply zooming on a map
- Applications does not take parameters with scala.js or type
- How to find links in d3 v4?
- Aligning text inside circular arc d3js
- how to access data of a d3 SVG Element
- React application breaks after downloading SVG through d3_save_svg
- Directed, acyclic graph in d3.js