Last active
December 17, 2015 16:39
-
-
Save And-How/5640366 to your computer and use it in GitHub Desktop.
testing update with old chart
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"> | |
<head> | |
<title>Stacked and Grouped Bar with Animation</title> | |
<!--modified from http://bl.ocks.org/3943967--> | |
<style> | |
body { | |
font-family: "Helvetica Neue", Helvetica, Arial, sans-serif; | |
margin: auto; | |
width: 960px; | |
} | |
h2 { | |
text-align: right; | |
margin: 0 30px 0 0; | |
} | |
text { | |
font: 20px sans-serif; | |
} | |
.axis path, .axis line { | |
fill: none; | |
stroke: black; | |
shape-rendering: crispEdges; | |
} | |
form { | |
right: 10px; | |
top: 10px; | |
} | |
</style> | |
</head> | |
<body> | |
<h1>Stacked to Grouped Bar Chart</h1> | |
<form> | |
<label><input type="radio" name="mode" value="grouped"> Grouped</label> | |
<label><input type="radio" name="mode" value="stacked" checked> Stacked</label> | |
</form> | |
<div> | |
<h2>Legend</h2> <br> | |
</div> | |
<input name="updateButton" | |
type="button" | |
value="Update" | |
onclick="updateData()"/> | |
<script src="http://d3js.org/d3.v3.js"></script> | |
<script> | |
d3.select(self.frameElement).style("height", "1000px"); | |
d3.select(self.frameElement).style("width", "1000px"); | |
var sampledata =[ | |
[ | |
{ "markervalue": 10,"employmenttype":"retail", "dwellingunittype": "single-family","scenarioname":"Base", "x": 0, "y": -5}, | |
{ "markervalue": 10,"employmenttype": "office","dwellingunittype": "attached single-family","scenarioname":"Scenario 1", "x": 1, "y": 29}, | |
{ "markervalue": 10,"employmenttype": "industrial","dwellingunittype": "detached single-family","scenarioname":"Scenario 2","x": 2, "y": 30}, | |
{ "markervalue": 10,"employmenttype": "public","dwellingunittype": "multifamily","scenarioname":"Scenario 3","x": 3, "y": 35}, | |
{ "markervalue": 10,"employmenttype": "civic","dwellingunittype": "yurt","scenarioname":"Scenario 4","x": 4, "y": 10} | |
], | |
[ | |
{ "markervalue": 10,"employmenttype": "retail","dwellingunittype": "single-family","scenarioname":"Base", "x": 0, "y": 25}, | |
{ "markervalue": 10,"employmenttype": "office","dwellingunittype": "attached single-family","scenarioname":"Scenario 1", "x": 1, "y": 30}, | |
{ "markervalue": 10,"employmenttype": "industrial","dwellingunittype": "detached single-family","scenarioname":"Scenario 2","x": 2, "y": 40}, | |
{ "markervalue": 10,"employmenttype": "public","dwellingunittype": "multifamily","scenarioname":"Scenario 3","x": 3, "y": 12}, | |
{ "markervalue": 10,"employmenttype": "civic","dwellingunittype": "yurt","scenarioname":"Scenario 4","x": 4, "y": 12} | |
], | |
[ | |
{ "markervalue": 10,"employmenttype": "retail", "dwellingunittype": "single-family","scenarioname":"Base", "x": 0, "y": 30}, | |
{ "markervalue": 10,"employmenttype": "office","dwellingunittype": "attached single-family","scenarioname":"Scenario 1", "x": 1, "y": 12}, | |
{ "markervalue": 10,"employmenttype": "industrial","dwellingunittype": "detached single-family","scenarioname":"Scenario 2","x": 2, "y": 28}, | |
{ "markervalue": 10,"employmenttype": "public","dwellingunittype": "multifamily","scenarioname":"Scenario 3","x": 3, "y": 16}, | |
{ "markervalue": 10,"employmenttype": "civic","dwellingunittype": "yurt","scenarioname":"Scenario 4","x": 4, "y": 16} | |
], | |
[ | |
{ "markervalue": 10,"employmenttype": "retail", "dwellingunittype": "single-family","scenarioname":"Base", "x": 0, "y": 25}, | |
{ "markervalue": 10,"employmenttype": "office","dwellingunittype": "attached single-family","scenarioname":"Scenario 1", "x": 1, "y": 39}, | |
{ "markervalue": 10,"employmenttype": "industrial","dwellingunittype": "detached single-family","scenarioname":"Scenario 2","x": 2, "y": -30}, | |
{ "markervalue": 10,"employmenttype": "public","dwellingunittype": "multifamily","scenarioname":"Scenario 3","x": 3, "y": 25}, | |
{ "markervalue": 10,"employmenttype": "civic","dwellingunittype": "yurt","scenarioname":"Scenario 4","x": 4, "y": 16} | |
], | |
[ | |
{ "markervalue": 10,"employmenttype": "retail", "dwellingunittype": "single-family","scenarioname":"Base", "x": 0, "y": 6}, | |
{ "markervalue": 10,"employmenttype": "office","dwellingunittype": "attached single-family","scenarioname":"Scenario 1", "x": 1, "y": 29}, | |
{ "markervalue": 10,"employmenttype": "industrial","dwellingunittype": "detached single-family","scenarioname":"Scenario 2","x": 2, "y": 30}, | |
{ "markervalue": 10,"employmenttype": "public","dwellingunittype": "multifamily","scenarioname":"Scenario 3","x": 3, "y": 30}, | |
{ "markervalue": 10,"employmenttype": "civic","dwellingunittype": "yurt","scenarioname":"Scenario 4","x": 4, "y": 40} | |
] | |
]; | |
var m = //number of samples per layer | |
scenarionames = d3.keys(sampledata[0]).filter(function(key) { return key !== "Scenario"; }), | |
stack = d3.layout.stack(), | |
layers = stack(sampledata), | |
yGroupMax = d3.max(layers, function(layer) { return d3.max(layer, function(d) { return d.y; }); }), | |
yStackMax = d3.max(layers, function(layer) { return d3.max(layer, function(d) { return d.y0 + d.y; }); }); | |
var margin = {top: 40, right: 10, bottom: 20, left: 10}, | |
width = 960 - margin.left - margin.right, | |
height = 500 - margin.top - margin.bottom; | |
var x = d3.scale.ordinal() | |
.domain(d3.range(layers.length)) | |
.rangeRoundBands([0, width], .08); | |
var y = d3.scale.linear() | |
.domain([0, yStackMax]) | |
.range([height, 0]); | |
var color = d3.scale.linear() | |
.domain([0, layers.length - 1]) | |
.range(["#aad", "#556"]); | |
var xAxis = d3.svg.axis() | |
.scale(x) | |
.tickSize(0) | |
.tickPadding(6) | |
.orient("bottom"); | |
var yAxis = d3.svg.axis() | |
.scale(y) | |
.orient("left") | |
.tickFormat(d3.format(".2s")); | |
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 layer = svg.selectAll(".layer") | |
.data(layers) | |
.enter().append("g") | |
.attr("class", "layer") | |
.style("fill", function(d, i) { return color(i); }); | |
var rect = layer.selectAll("rect") | |
.data(function(d) { return d; }) | |
.enter().append("rect") | |
.on("click", clickEvent) | |
.attr("x", function(d) { return x(d.x); }) | |
.attr("y", height) | |
.attr("width", x.rangeBand()) | |
.attr("height", 0); | |
function render() { | |
rect.data(function(d) { return d; }) | |
.append("rect") | |
.on("click", clickEvent) | |
.attr("x", function(d) { return x(d.x); }) | |
.attr("y", height) | |
.attr("width", x.rangeBand()) | |
.attr("height", 0); | |
} | |
rect.transition() | |
.delay(function(d, i) { return i * 10; }) | |
.attr("y", function(d) { return y(d.y0 + d.y); }) | |
.attr("height", function(d) { return y(d.y0) - y(d.y0 + d.y); }); | |
svg.append("g") | |
.attr("class", "x axis") | |
.attr("transform", "translate(0," + height + ")") | |
.call(xAxis); | |
svg.append("g") | |
.attr("class", "y axis") | |
.call(yAxis) | |
.append("text") | |
.attr("transform", "rotate(-90)") | |
.attr("y", 10) | |
.attr("dy", ".71.em") // vertical-align: | |
/* | |
function render() { | |
var bars = svg.selectAll("rect") | |
.data(layer); | |
bars.enter() | |
.append("rect"); | |
bars.on("click", clickEvent) | |
.attr("x", function (d, i) { | |
return i * (width / layer.length); | |
}) | |
.attr("y", function (d) { | |
return height - (d * 4); | |
}) | |
.attr("width", width / layer.length) | |
.attr("height", function (d) { | |
return d * 4; | |
}); | |
}; | |
render(); | |
*/ | |
function clickEvent(d, i) { | |
var op = prompt("Please enter the value", d); | |
layer[i] = parseInt(op, 10); | |
render(); | |
}; | |
d3.selectAll("input").on("change", change); | |
var timeout = setTimeout(function() { | |
d3.select("input[value=\"grouped\"]").property("checked", true).each(change); | |
}, 2000); | |
function change() { | |
clearTimeout(timeout); | |
if (this.value === "grouped") transitionGrouped(); | |
else transitionStacked(); | |
} | |
function transitionGrouped() { | |
y.domain([0, yGroupMax]); | |
rect.transition() | |
.duration(500) | |
.delay(function(d, i) { return i * 10; }) | |
.attr("x", function(d, i, j) { return x(d.x) + x.rangeBand() / layers.length * j; }) | |
.attr("width", x.rangeBand() / layers.length) | |
.transition() | |
.attr("y", function(d) { return y(d.y); }) | |
.attr("height", function(d) { return height - y(d.y); }); | |
} | |
function transitionStacked() { | |
y.domain([0, yStackMax]); | |
rect.transition() | |
.duration(500) | |
.delay(function(d, i) { return i * 10; }) | |
.attr("y", function(d) { return y(d.y0 + d.y); }) | |
.attr("height", function(d) { return y(d.y0) - y(d.y0 + d.y); }) | |
.transition() | |
.attr("x", function(d) { return x(d.x); }) | |
.attr("width", x.rangeBand()); | |
} | |
/* | |
var sampledata =[ | |
[ | |
{ "markervalue": 10,"employmenttype":"retail", "dwellingunittype": "single-family","scenarioname":"Base", "x": 0, "y": -5}, | |
{ "markervalue": 10,"employmenttype": "office","dwellingunittype": "attached single-family","scenarioname":"Scenario 1", "x": 1, "y": 29}, | |
{ "markervalue": 10,"employmenttype": "industrial","dwellingunittype": "detached single-family","scenarioname":"Scenario 2","x": 2, "y": 30}, | |
{ "markervalue": 10,"employmenttype": "public","dwellingunittype": "multifamily","scenarioname":"Scenario 3","x": 3, "y": 35}, | |
{ "markervalue": 10,"employmenttype": "civic","dwellingunittype": "yurt","scenarioname":"Scenario 4","x": 4, "y": 10} | |
], | |
[ | |
{ "markervalue": 10,"employmenttype": "retail","dwellingunittype": "single-family","scenarioname":"Base", "x": 0, "y": 25}, | |
{ "markervalue": 10,"employmenttype": "office","dwellingunittype": "attached single-family","scenarioname":"Scenario 1", "x": 1, "y": 30}, | |
{ "markervalue": 10,"employmenttype": "industrial","dwellingunittype": "detached single-family","scenarioname":"Scenario 2","x": 2, "y": 40}, | |
{ "markervalue": 10,"employmenttype": "public","dwellingunittype": "multifamily","scenarioname":"Scenario 3","x": 3, "y": 12}, | |
{ "markervalue": 10,"employmenttype": "civic","dwellingunittype": "yurt","scenarioname":"Scenario 4","x": 4, "y": 12} | |
], | |
[ | |
{ "markervalue": 10,"employmenttype": "retail", "dwellingunittype": "single-family","scenarioname":"Base", "x": 0, "y": 30}, | |
{ "markervalue": 10,"employmenttype": "office","dwellingunittype": "attached single-family","scenarioname":"Scenario 1", "x": 1, "y": 12}, | |
{ "markervalue": 10,"employmenttype": "industrial","dwellingunittype": "detached single-family","scenarioname":"Scenario 2","x": 2, "y": 28}, | |
{ "markervalue": 10,"employmenttype": "public","dwellingunittype": "multifamily","scenarioname":"Scenario 3","x": 3, "y": 16}, | |
{ "markervalue": 10,"employmenttype": "civic","dwellingunittype": "yurt","scenarioname":"Scenario 4","x": 4, "y": 16} | |
], | |
[ | |
{ "markervalue": 10,"employmenttype": "retail", "dwellingunittype": "single-family","scenarioname":"Base", "x": 0, "y": 25}, | |
{ "markervalue": 10,"employmenttype": "office","dwellingunittype": "attached single-family","scenarioname":"Scenario 1", "x": 1, "y": 39}, | |
{ "markervalue": 10,"employmenttype": "industrial","dwellingunittype": "detached single-family","scenarioname":"Scenario 2","x": 2, "y": -30}, | |
{ "markervalue": 10,"employmenttype": "public","dwellingunittype": "multifamily","scenarioname":"Scenario 3","x": 3, "y": 25}, | |
{ "markervalue": 10,"employmenttype": "civic","dwellingunittype": "yurt","scenarioname":"Scenario 4","x": 4, "y": 16} | |
], | |
[ | |
{ "markervalue": 10,"employmenttype": "retail", "dwellingunittype": "single-family","scenarioname":"Base", "x": 0, "y": 6}, | |
{ "markervalue": 10,"employmenttype": "office","dwellingunittype": "attached single-family","scenarioname":"Scenario 1", "x": 1, "y": 29}, | |
{ "markervalue": 10,"employmenttype": "industrial","dwellingunittype": "detached single-family","scenarioname":"Scenario 2","x": 2, "y": 30}, | |
{ "markervalue": 10,"employmenttype": "public","dwellingunittype": "multifamily","scenarioname":"Scenario 3","x": 3, "y": 30}, | |
{ "markervalue": 10,"employmenttype": "civic","dwellingunittype": "yurt","scenarioname":"Scenario 4","x": 4, "y": 40} | |
] | |
]; | |
var margin = {top: 40, right: 10, bottom: 20, left: 40}, | |
width = 960 - margin.left - margin.right, | |
height = 500 - margin.top - margin.bottom, | |
p = [20, 50, 30, 20], | |
x = d3.scale.ordinal().rangeRoundBands([0, width - p[1] - p[3]]), | |
y = d3.scale.linear().range([0, height - p[0] - p[2]]), | |
z = d3.scale.ordinal().range(["lightpink", "darkgray", "lightblue"]); | |
var barPadding = 20; | |
var dataset = [5, 10, 13, 19, 21, 25, 22, 18, 15, 13, | |
11, 12, 15, 20, 18, 17, 16, 18, 23, 25]; | |
//Create SVG element | |
var svg = d3.select("body") | |
.append("svg") | |
.attr("width", width) | |
.attr("height", height) | |
.attr("transform", "translate(" + p[3] + "," + (height - p[2]) + ")"); | |
*/ | |
</script> | |
</body> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment