Built with blockbuilder.org
Last active
November 7, 2018 09:54
-
-
Save renauld94/9790973f5bf38d2c0d974ac5ecdf586c to your computer and use it in GitHub Desktop.
fresh block
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
license: mit |
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> | |
<html lang="EN"> | |
<head> | |
<meta charset="utf-8"> | |
<title>StackedBar Chart</title> | |
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.1/d3.min.js"></script> | |
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/crossfilter/1.3.12/crossfilter.min.js"></script> | |
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/dc/2.1.9/dc.min.js"></script> | |
<link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/dc/2.1.9/dc.min.css" /> | |
</head> | |
<body> | |
<h1>StackedBart Chart:By Type and Quantity</h1> | |
<div id="stackedBarChart"></div> | |
<div id="stackedBarChart2"></div> | |
<script type="text/javascript"> | |
/////////////////////UNIVERSE DATA | |
var universe = [ | |
{"KMIST_TEMP_ID": "KTMPVN12119","ID POP": "","type": 2018,"pop": "No","name": "Nha Thuoc An Binh","Country": "","Province": "Ho Chi Minh Province","City": "Ho Chi Minh","district": "Thu Duc","ward": "Linh Trung","name_full": "Ho Chi Minh ,Nha Thuoc An Binh ,Thu Duc, Linh Trung","latitude": 10.8539167,"longitude": 106.7716921,"geo": "106.7716921,10.8539167","amenity": "pharmacy","date_entered": "2015-10-30T00:00:00","date_issued": "2015-10-30T00:00:00"}, | |
{"KMIST_TEMP_ID": "KTMPVN12119","ID POP": "","type": 2018,"pop": "No","name": "Nha Thuoc An Binh","Country": "","Province": "Ho Chi Minh Province","City": "Ho Chi Minh","district": "first","ward": "Linh Trung","name_full": "Ho Chi Minh ,Nha Thuoc An Binh ,Thu Duc, Linh Trung","latitude": 10.8539167,"longitude": 106.7716921,"geo": "106.7716921,10.8539167","amenity": "pharmacy","date_entered": "2015-10-30T00:00:00","date_issued": "2015-10-30T00:00:00"}, | |
{"KMIST_TEMP_ID": "KTMPVN12119","ID POP": "","type": "Never Covered" ,"pop": "No","name": "Nha Thuoc An Binh","Country": "","Province": "Ho Chi Minh Province","City": "Ho Chi Minh","district": "new","ward": "Linh Trung","name_full": "Ho Chi Minh ,Nha Thuoc An Binh ,Thu Duc, Linh Trung","latitude": 10.8539167,"longitude": 106.7716921,"geo": "106.7716921,10.8539167","amenity": "pharmacy","date_entered": "2015-10-30T00:00:00","date_issued": "2015-10-30T00:00:00"}, | |
{"KMIST_TEMP_ID": "KTMPVN12119","ID POP": "","type": "Other Periods","pop": "No","name": "Nha Thuoc An Binh","Country": "","Province": "Ho Chi Minh Province","City": "Ho Chi Minh","district": "Thu Duc","ward": "Linh Trung","name_full": "Ho Chi Minh ,Nha Thuoc An Binh ,Thu Duc, Linh Trung","latitude": 10.8539167,"longitude": 106.7716921,"geo": "106.7716921,10.8539167","amenity": "pharmacy","date_entered": "2015-10-30T00:00:00","date_issued": "2015-10-30T00:00:00"}]; | |
var xdata = crossfilter(universe); | |
var dimensionByType = xdata.dimension(function(d) { | |
return d.district; | |
}); | |
var groupByType = dimensionByType.group().reduce(reduceAdd, reduceRemove, reduceInitial); | |
function reduceAdd(p, v) { | |
p[v.type] = (p[v.type] || 0) + v.latitude; | |
return p; | |
} | |
function reduceRemove(p, v) { | |
p[v.type] = (p[v.type] || 0) - v.latitude; | |
return p; | |
} | |
function reduceInitial() { | |
return {}; | |
} | |
var barChart = dc.barChart("#stackedBarChart") | |
.width(1024) | |
.height(200) | |
.dimension(dimensionByType) | |
.group(groupByType, "Type: Never Covered", function(d) { | |
return d.value['Never Covered'] || 0; | |
}) | |
.stack(groupByType, "Type: 2018", function(d) { | |
return d.value['2018']; | |
}) | |
.stack(groupByType, "Type: Other Periods", function(d) { | |
return d.value["Other Periods"]; | |
}) | |
.elasticY(true) | |
.x(d3.scale.ordinal()) | |
.xUnits(dc.units.ordinal) | |
.legend(dc.legend().x(500).y(1).itemHeight(15).gap(5)); | |
barChart.yAxis().ticks(5); | |
barChart.xAxis().ticks(4); | |
dc.renderAll(); | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment