source: rtos_arduino/trunk/examples/IotText/sample3/chart.js@ 254

Last change on this file since 254 was 254, checked in by mmatsu, 8 years ago
File size: 4.7 KB
Line 
1(function(global){
2
3 global.createChart = createChart;
4
5 function createChart() {
6 this.datas = [];
7 }
8
9 // datastoreのデータを、描画用のデータに変換する
10 function get_graph_data(data){
11 return data.map(function(d) {
12 return {
13 date : new Date(d.timestamp),
14 value : d.value
15 };
16 });
17 }
18
19 // グラフを描画するdivのidを指定する
20 createChart.prototype.setSvg = function(wrapper_id) {
21 this.wrapper_id = wrapper_id || 'svg-chart';
22 }
23
24 // 描画するデータをセット/更新
25 createChart.prototype.setDatas = function(datas) {
26 this.datas = datas;
27 }
28
29 // 初期化
30 createChart.prototype.init = function() {
31
32 // 描画範囲に関する変数
33 var margin = {top: 20, right: 20, bottom: 30, left: 50},
34 width = 1040 - margin.left - margin.right,
35 height = 420 - margin.top - margin.bottom;
36
37 // x軸のスケール(時間)。レンジ(出力範囲)の指定
38 var xScale = d3.time.scale()
39 .range([0, width]);
40
41 // y軸のスケール(センサーデータの値)。レンジ(出力範囲)の指定
42 var yScale = d3.scale.linear()
43 .range([height, 0]);
44
45 // スケールをå…
46ƒã«x軸の設定(å…
47¥åŠ›å€¤ã®ç¯„囲はまだ指定していない。データを受け取ってから指定する)
48 var xAxis = d3.svg.axis()
49 .scale(xScale)
50 .orient("bottom");
51
52 // スケールをå…
53ƒã«y軸の設定
54 var yAxis = d3.svg.axis()
55 .scale(yScale)
56 .orient("left");
57
58 // SVG要素の作成(attrとかはテンプレ)
59 var svg = d3.select("#" + this.wrapper_id).append("svg")
60 .attr("class", "chart")
61 .attr("width", width + margin.left + margin.right)
62 .attr("height", height + margin.top + margin.bottom)
63 .append("g")
64 .attr("transform", "translate(" + margin.left + "," + margin.top + ")");
65
66 // 折れ線グラフの設定。xに時間、yにセンサーデータの値を設定。
67 var line = d3.svg.line()
68 .x(function(d) {
69 // xスケールでマップされた時間を返す
70 return xScale(d.date);
71 })
72 .y(function(d) {
73 // yスケールでマップされたセンサーデータの値を返す
74 return yScale(d.value);
75 });
76
77
78 // もろもろをメンバ変数に
79 this.height = height;
80 this.margin = margin;
81 this.width = width;
82 this.xScale = xScale;
83 this.yScale = yScale;
84 this.xAxis = xAxis;
85 this.yAxis = yAxis;
86 this.svg = svg;
87 this.line = line;
88 }
89
90
91 // 最初の描画
92 createChart.prototype.initialDraw = function() {
93
94 var dataset = get_graph_data(this.datas);
95
96 // ドメイン(å…
97¥åŠ›å€¤ã®ç¯„囲)の設定、extentメソッドでdatasetの最小と最大を返す
98 this.xScale.domain(d3.extent(dataset, function(d) { return d.date; }));
99 this.yScale.domain(d3.extent(dataset, function(d) { return d.value; }));
100
101 // x軸の描画
102 this.svg.append("g")
103 .attr("class", "x axis")
104 .attr("transform", "translate(0," + this.height + ")")
105 .call(this.xAxis);
106
107 // y軸の描画
108 this.svg.append("g")
109 .attr("class", "y axis")
110 .call(this.yAxis)
111 .append("text")
112 .attr("transform", "rotate(-90)")
113 .attr("y", 6)
114 .attr("dy", ".71em")
115 .style("text-anchor", "end")
116 .text("Value");
117
118 // 折れ線の描画
119 this.svg.append("path")
120 .datum(dataset)
121 .attr("class", "line")
122 .attr("d", this.line);
123 }
124
125
126 // 更新した際の描画
127 createChart.prototype.updateDraw = function() {
128
129 var dataset = get_graph_data(this.datas);
130
131 // ドメイン(å…
132¥åŠ›å€¤ã®ç¯„囲)の更新
133 this.xScale.domain(d3.extent(dataset, function(d) { return d.date; }));
134 this.yScale.domain(d3.extent(dataset, function(d) { return d.value; }));
135
136 // アニメーションしますよ、という宣言
137 this.svg = d3.select("#" + this.wrapper_id).transition();
138
139 this.svg.select(".line") // 折れ線を
140 .duration(750) // 750msで
141 .attr("d", this.line(dataset)); // (新しい)datasetに変化させる描画をアニメーション
142
143 this.svg.select(".x.axis") // x軸を
144 .duration(750) // 750msで
145 .call(this.xAxis); // (domainの変更によって変化した)xAxisに変化させる描画をアニメーション
146
147 this.svg.select(".y.axis") // y軸を
148 .duration(750) // 750msで
149 .call(this.yAxis); // (domainの変更によって変化した)yAxisに変化させる描画をアニメーション
150 }
151
152}(window))
Note: See TracBrowser for help on using the repository browser.