forked from cvitter/ikanow.mongodc2013.presentation
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathd3jsSimpleExample.html
More file actions
178 lines (157 loc) · 5.13 KB
/
Copy pathd3jsSimpleExample.html
File metadata and controls
178 lines (157 loc) · 5.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
<!--
Copyright 2012 The Infinit.e Open Source Project
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Simple D3.js Example</title>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<script type="text/javascript" src="lib/d3.v3.js"></script>
<link href="lib/bootstrap/css/bootstrap.css" rel="stylesheet">
<link href="lib/base.css" rel="stylesheet">
<link href="lib/bootstrap/css/bootstrap-responsive.css" rel="stylesheet">
<!-- Le HTML5 shim, for IE6-8 support of HTML5 elements -->
<!--[if lt IE 9]>
<script src="http://html5shim.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
<script type="text/javascript" src="lib/bootstrap/js/bootstrap.min.js"></script>
<script type="text/javascript" src="lib/bootstrap/js/jquery-latest.js"></script>
<style>
.chart rect {
stroke: white;
fill: steelblue;
}
.chart text.bar {
fill: black;
}
</style>
</head>
<body>
<div class="container">
<div class="main-div">
<table width="100%" cellspacing="0" cellpadding="8" >
<tr bgcolor="#000000">
<td>
<h4 style="color:#ffffff">Visualizing MongoDB Objects in Concept and Practice Sample Code</h4>
</td>
</tr>
<tr>
<td align="center">
<table cellspacing="2" cellpadding="3" width="100%">
<tr style="border-bottom-width: 1px; border-bottom-style:dotted; border-bottom-color:#DADADA;">
<td bgcolor="#FFFFDB" width="50%"><h4>Simple D3.js Example - 10 Day Chance of Rain</h4></td>
</tr>
<tr>
<td>
<table width="100%" cellspacing="1" cellpadding="3">
<tr valign="top">
<td width="100%" colspan="2">
<div id="d3_canvas" style="height:450px; width:870px; border:1px solid;"></div>
</td>
</tr>
<tr>
<td width="100%" colspan="2">
<strong>Note</strong>: See <a href="http://mbostock.github.com/d3/tutorial/bar-1.html">A Bar Chart, Part 1</a>
for more information and examples of basic bar charts with D3.js.
</td>
</tr>
</table>
</td>
</tr>
</table>
</td>
</tr>
</table>
</div>
</div>
</body>
</html>
<!---------- JavaScript/JQuery Code ---------->
<script>
// runQuery -
$(document).ready(function () { runQuery(); });
function runQuery()
{
var queryUrl = "http://127.0.0.1/ikanow.mongodc2013.presentation/json/10_day_forecast.json";
$.ajax({
type: 'GET',
url: queryUrl,
async: true,
contentType: "application/json",
dataType: 'json',
success: function(msg) {
if (msg != null) {
createChart(msg);
}
}
});
}
// createChart -
// Create simple bar chart using D3.js
// Starter code borrowed from http://mbostock.github.com/d3/tutorial/bar-1.html
function createChart(inputData) {
// Set height and width of chart to match our #d3_canvas div
var chartHeight = 450;
var chartWidth = 870;
// Select the d3_canvas div and append our SVG to it
var chart = d3.select("#d3_canvas").append("svg")
.attr("class", "chart")
.attr("width", chartWidth)
.attr("height", chartHeight)
.append("g")
.attr("transform", "translate(35,20)");
// Calculate the hight of each bar in our bar chart
var barHeight = (chartHeight - (6 * inputData.length) - 6) / inputData.length;
// Draw the graph's bars
chart.selectAll("rect")
.data(inputData)
.enter().append("rect")
.attr("y", function(d, i) { return i * (barHeight + 3); })
.attr("width", function(d, i) { return chartWidth * (d.chance_of_rain * 0.01); })
.attr("height", function(d) { return barHeight; });
// Calculate how to scale across the X axis
var x = d3.scale.linear()
.domain([0, 100])
.range([0, chartWidth]);
// Label each value with its date
chart.selectAll("text")
.data(inputData)
.enter().append("text")
.attr("x", 0)
.attr("y", function(d, i) { return i * (barHeight + 3) + (barHeight / 2); })
.attr("dx", -3)
.attr("dy", ".35em")
.attr("text-anchor", "end")
.text(function(d) { return d.date; });
// Draw the rules
chart.selectAll("line")
.data(x.ticks(10))
.enter().append("line")
.attr("x1", x)
.attr("x2", x)
.attr("y1", 0)
.attr("y2", chartHeight)
.style("stroke", "#ccc");
// Label the rules
chart.selectAll(".rule")
.data(x.ticks(10))
.enter().append("text")
.attr("class", "rule")
.attr("x", x)
.attr("y", 0)
.attr("dy", -3)
.attr("text-anchor", "middle")
.text(String);
}
</script>