Last active
July 31, 2018 09:09
-
-
Save XavierGimenez/d6fd9a03a2c43f2f39e7ba83d946caae to your computer and use it in GitHub Desktop.
Bar chart with custom shapes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[ | |
{"area": "A ", "value": 30000}, | |
{"area": "B ", "value": 4800}, | |
{"area": "C ", "value": 40000}, | |
{"area": "D ", "value": 30040}, | |
{"area": "E ", "value": 120200}, | |
{"area": "F", "value": 70000} | |
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<meta charset="utf-8"> | |
<style> | |
body { | |
margin: 15px; | |
} | |
.bar.background{ | |
fill: #e0e0e0; | |
} | |
.bar.foreground { | |
fill: #6F257F; | |
} | |
.axis path, | |
.axis line { | |
fill: none; | |
stroke: #D4D8DA; | |
stroke-width: 1px; | |
shape-rendering: crispEdges; | |
} | |
.x path { | |
display: none; | |
} | |
.toolTip { | |
position: absolute; | |
display: none; | |
min-width: 80px; | |
height: auto; | |
background: none repeat scroll 0 0 #ffffff; | |
border: 1px solid #6F257F; | |
padding: 14px; | |
text-align: center; | |
} | |
</style> | |
<svg width="960" height="500"></svg> | |
<script src="https://d3js.org/d3.v4.min.js"></script> | |
<script> | |
var svg = d3.select("svg"), | |
margin = {top: 20, right: 20, bottom: 30, left: 80}, | |
width = +svg.attr("width") - margin.left - margin.right, | |
height = +svg.attr("height") - margin.top - margin.bottom; | |
var tooltip = d3.select("body").append("div").attr("class", "toolTip"); | |
var x = d3.scaleLinear().range([0, width]); | |
var y = d3.scaleBand().range([height, 0]); | |
var g = svg.append("g") | |
.attr("transform", "translate(" + margin.left + "," + margin.top + ")"); | |
var customPath = function(x, y, width, height, offset) { | |
} | |
function createPath(width, yPos, initialState) { | |
var points = [ | |
[y.bandwidth(), yPos], | |
[y.bandwidth() + width, yPos], | |
[y.bandwidth() + width - y.bandwidth(), yPos+y.bandwidth()], | |
[0, yPos+y.bandwidth()] | |
]; | |
return d3.line()(points); | |
} | |
d3.json("data.json", function(error, data) { | |
if (error) throw error; | |
data.sort(function(a, b) { return a.value - b.value; }); | |
x.domain([0, d3.max(data, function(d) { return d.value; })]); | |
y.domain(data.map(function(d) { return d.area; })).padding(0.75); | |
g.append("g") | |
.attr("class", "x axis") | |
.attr("transform", "translate(0," + height + ")") | |
.call(d3.axisBottom(x).ticks(5).tickFormat(function(d) { return parseInt(d / 1000); }).tickSizeInner([-height])); | |
g.append("g") | |
.attr("class", "y axis") | |
.call(d3.axisLeft(y)); | |
g.selectAll(".bar .background") | |
.data(data) | |
.enter() | |
.append("path") | |
.attr("class", "bar background") | |
.attr('d', function(d) { | |
return createPath( | |
x(x.domain()[1]), | |
y(d.area) | |
); | |
}); | |
g.selectAll('bar') | |
.data(data) | |
.enter() | |
.append('path') | |
.attr("class", "bar foreground") | |
.attr('d', function(d) { | |
return createPath( | |
1, | |
y(d.area) | |
); | |
}) | |
.attr('fill', 'black'); | |
g.selectAll('.bar.foreground') | |
.transition() | |
.delay(200) | |
.duration(1000) | |
.attr('d', function(d) { | |
return createPath( | |
x(d.value), | |
y(d.area) | |
); | |
}); | |
}); | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment