Last active
August 29, 2015 13:59
-
-
Save patperu/10735436 to your computer and use it in GitHub Desktop.
D3 Barchart - PM10
This file contains hidden or 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
| <script src="http://d3js.org/d3.v3.min.js" charset="utf-8"> </script> | |
| <script> | |
| var data0 = []; | |
| var data = []; | |
| var jsonurl = "https://USERNAME.cloudant.com/DB/_design/myView/_view/test?group_level=2"; | |
| /* Structur {"key":[year,month],"value":[mean,lenght]} | |
| {"rows":[ | |
| {"key":[2008,1],"value":[43.74016282225238,1474]}, | |
| {"key":[2008,2],"value":[43.73733719247467,1382]}, | |
| ... | |
| */ | |
| parseDate = d3.time.format("%Y-%m").parse; | |
| d3.json(jsonurl, function(error, jsondata) { | |
| if (error) { return error; } | |
| jsondata = jsondata.rows; | |
| jsondata.forEach(function (d) { | |
| data0.push( [parseDate(d.key.join('-')), d.value[0], d.value[1] ] ); | |
| data = data0.map(function(d) { | |
| return { | |
| date: d[0], | |
| value: d[1], | |
| xlen: d[2] | |
| }; | |
| }); | |
| }); | |
| // ################################################################## | |
| var margin = {top: 20, right: 20, bottom: 70, left: 40}, | |
| width = 800 - margin.left - margin.right, | |
| height = 400 - margin.top - margin.bottom; | |
| var x = d3.scale.ordinal().rangeRoundBands([0, width], .05); | |
| var y = d3.scale.linear().range([height, 0]); | |
| var xAxis = d3.svg.axis() | |
| .scale(x) | |
| .orient("bottom") | |
| .tickFormat(d3.time.format("%Y-%m")); | |
| var yAxis = d3.svg.axis() | |
| .scale(y) | |
| .orient("left") | |
| .ticks(10); | |
| 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 + ")"); | |
| x.domain(data.map(function(d) { return d.date; })); | |
| y.domain([0, d3.max(data, function(d) { return d.value + 5; })]); | |
| svg.append("g") | |
| .attr("class", "x axis") | |
| .attr("transform", "translate(0," + height + ")") | |
| .call(xAxis) | |
| .selectAll("text") | |
| .style("text-anchor", "end") | |
| .attr("dx", "-.8em") | |
| .attr("dy", "-.55em") | |
| .attr("transform", "rotate(-90)" ); | |
| svg.append("g") | |
| .attr("class", "y axis") | |
| .call(yAxis) | |
| .append("text") | |
| .attr("transform", "rotate(-90)") | |
| .attr("y", 6) | |
| .attr("dy", ".71em") | |
| .style("text-anchor", "end") | |
| .text("PM 10"); | |
| svg.selectAll("bar") | |
| .data(data) | |
| .enter() | |
| .append("rect") | |
| .attr("x", function(d) { return x(d.date); }) | |
| .attr("width", x.rangeBand()) | |
| .attr("y", function(d) { return y(d.value); }) | |
| .attr("height", function(d) { return height - y(d.value); }) | |
| .attr("fill", function(d) { | |
| return "rgb(0, 0, " + (Math.floor(d.value) * 10) + ")"; | |
| }) | |
| .on("mouseover", function() { | |
| d3.select(this) | |
| .attr("fill", "orange") | |
| }) | |
| .on("mouseout", function(d) { | |
| d3.select(this) | |
| .transition() | |
| .duration(250) | |
| .attr("fill", "rgb(0, 0, " + (Math.floor(d.value) * 10) + ")") | |
| }); | |
| }); | |
| </script> |
This file contains hidden or 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
| # ok | |
| curl -k -i -u USERNAME -X PUT https://USERNAME.cloudant.com/_api/v2/user/config/cors -H "Content-Type: application/json" -d '{"enable_cors":true,"allow_credentials":true,"allow_methods":["GET","PUT","POST","DELETE","OPTIONS"],"origins":["*"]}' | |
| # | |
| https://github.com/cloudant-labs/cors-demo |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment