-
Notifications
You must be signed in to change notification settings - Fork 0
/
chart_pic.js
382 lines (340 loc) · 13.1 KB
/
chart_pic.js
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
/**
* View chart and pic
*/
//--------------------------
// chart
//--------------------------
/* No used this valiable.
<?php foreach ($data_inis as $key => $value): ?>
<?php $dini = parse_ini_file($value); ?>
<?php if (!isset($dini["image_type"])): ?>
var myChart_<?=$dini["dname"]?>;
<?php endif; ?>
<?php endforeach ?>
*/
var chart_type = "time";
var forced = "no";
var timeFormat = 'MM/DD HH:mm:ss';
Chart.defaults.global.legend.display = false;
var gLastTime; // 最後に受け取ったデータの時間。ここで明に受け取るファイル以外のファイルの時刻チェッック用
/* separate from .php
<?php foreach ($data_inis as $key => $value): ?>
<?php $dini = parse_ini_file($value); ?>
<?php if (!isset($dini["image_type"])): ?>
var gLastTime_<?=$dini["dname"]?>;
<?php endif; ?>
<?php endforeach ?>
*/
var gLastTimes = [];
gIni.sensor_devices.forEach(function(device){
gLastTimes[device.dname] = "";
});
// コンテキストの取得
/* separate from .php
<?php foreach ($data_inis as $key => $value): ?>
<?php $dini = parse_ini_file($value); ?>
<?php if (!isset($dini["image_type"])): ?>
var ctx_<?=$dini["dname"]?> = document.getElementById("myChart_<?=$dini["dname"]?>").getContext("2d");;
<?php endif; ?>
<?php endforeach ?>
*/
var ctxs = [];
gIni.sensor_devices.forEach(function(device){
ctxs[device.dname] = document.getElementById("myChart_"+device.dname).getContext("2d");
});
// イニシャルデータ
var data = {
labels: [],
datasets: [
]
};
function toMyTime(dt){
mydate = new Date(Date.parse(dt));
return mydate.toLocaleTimeString();
};
function toMyDateTime(dt){
mydate = new Date(Date.parse(dt));
return mydate.toLocaleString();
};
// toLocalString が実装依存なので使わない
function toMyTime2(dt){
mydate = new Date(dt);
str = mydate.getHours() + ":" +
// mydate.getMinutes(); // + ":" + mydate.getSeconds()
mydate.getMinutes() + ":" +
mydate.getSeconds();
return str;
};
function toMyDateTime2(dt){
mydate = new Date(dt);
/* str = mydate.getFullYear() + "年" +
(mydate.getMonth() + 1) + "月" +
mydate.getDate() + "日 " +
toMyTime2(dt);*/
/* str = (mydate.getMonth() + 1) + "/" +
mydate.getDate() + " " +
toMyTime2(dt);*/
str = mydate.getFullYear() + "/" +
(mydate.getMonth() + 1) + "/" +
mydate.getDate() + " " +
toMyTime2(dt);
return str;
};
function toMyDateTime2_wo_yyyy(dt){
mydate = new Date(dt);
/* str = mydate.getFullYear() + "年" +
(mydate.getMonth() + 1) + "月" +
mydate.getDate() + "日 " +
toMyTime2(dt);*/
str = (mydate.getMonth() + 1) + "/" +
mydate.getDate() + " " +
toMyTime2(dt);
/* str = mydate.getFullYear() + "/" +
(mydate.getMonth() + 1) + "/" +
mydate.getDate() + " " +
toMyTime2(dt);*/
return str;
};
function setMyGraph(j_data, chart, tag, unit_str){
chart.config.data.labels.splice(0, chart.config.data.labels.length);
if (chart.config.data.datasets[0].data.length != 0){
chart.config.data.datasets[0].data.splice(0, chart.config.data.datasets[0].data.length);
}
for (i=0; i < j_data.length; i++){
chart.config.data.labels.push(toMyDateTime2(j_data[i].datetime));
chart.config.data.datasets[0].data.push(j_data[i].data.toFixed(1));
}
// グラフ領域のタグの描画
tag_val_string = toMyDateTime2_wo_yyyy(j_data[0].datetime) + ", " + j_data[0].data.toFixed(1) +unit_str;
document.getElementById(tag).innerHTML = tag_val_string;
chart.update();
};
/* separate from .php
<?php foreach ($data_inis as $key => $value): ?>
<?php $dini = parse_ini_file($value); ?>
<?php if (!isset($dini["image_type"])): ?>
function onReceiveStreamValues_<?=$dini["dname"]?>(j_data) {
// 最後のデータの時間を保存
gLastTime = j_data[0].datetime;
gLastTime_<?=$dini["dname"]?> = gLastTime;
// グラフの更新
setMyGraph(j_data, myLineChart_<?=$dini["dname"]?>,"<?=$dini["dname"]?>_tag", " <?=$dini["unit"]?>");
myLineChart_<?=$dini["dname"]?>.update();
};
<?php endif; ?>
<?php endforeach ?>
*/
function onReceiveStreamValues(device, j_data) {
// 最後のデータの時間を保存
gLastTime = j_data[0].datetime;
gLastTimes[device.dname] = gLastTime;
// グラフの更新
setMyGraph(j_data, myLineCharts[device.dname], device.dname + "_tag", device.unit);
myLineCharts[device.dname].update();
};
// time chart の時間軸のフォーマット。momentjs の display format を指定
var time = {
// string/callback - By default, date objects are expected. You may use a pattern string from http://momentjs.com/docs/#/parsing/string-format/ to parse a time string format, or use a callback function that is passed the label, and must return a moment() instance.
parser: false,
// string - By default, unit will automatically be detected. Override with 'week', 'month', 'year', etc. (see supported time measurements)
unit: false,
// Number - The number of steps of the above unit between ticks
unitStepSize: 1,
// string - By default, no rounding is applied. To round, set to a supported time unit eg. 'week', 'month', 'year', etc.
round: false,
// Moment js for each of the units. Replaces `displayFormat`
// To override, use a pattern string from http://momentjs.com/docs/#/displaying/format/
displayFormats: {
'millisecond': 'SSS [ms]',
'second': 'h:mm:ss a', // 11:20:01 AM
'minute': 'h:mm:ss a', // 11:20:01 AM
'hour': 'MM, D hA', // Sept 4, 5PM
// 'hour': 'MMM D, hA', // Sept 4, 5PM
'day': 'll', // Sep 4 2015
'week': 'll', // Week 46, or maybe "[W]WW - YYYY" ?
'month': 'MM YYYY', // Sept 2015
// 'month': 'MMM YYYY', // Sept 2015
'quarter': '[Q]Q - YYYY', // Q3
'year': 'YYYY', // 2015
},
// Sets the display format used in tooltip generation
tooltipFormat: '',
};
var options = {
// responsive: true,
scales: {
xAxes: [{
type: "time",
time: time,
ticks:{
autoSkip: true,
},
scaleLabel: {
display: false,
labelString: '日時'
}
}, ],
yAxes: [{
scaleLabel: {
display: false,
labelString: '℃'
},
ticks:{
// stepSize: 0.5,
},
}]
},
}
function new_config(){
config = {type: 'line',
data: $.extend(true, {}, data),
options: options};
config.data.datasets.push($.extend(true, {}, {data: [],fill: false}));
config.data.datasets[0].label = "ラベル";
return config;
}
function setColor_onGraph(o){
o.borderColor = "rgba(151,187,205,0.4)"
o.backgroundColor = "rgba(151,187,205,0.5)"
o.pointBorderColor = "rgba(151,187,205,0.7)"
o.pointBackgroundColor = "rgba(151,187,205,0.5)"
o.pointBorderWidth = 1;
o.fill = true;
// o.lineTension = 0;
}
// 描画
// data をそのまま渡すと全グラフの data ツリーが同じになり、x, y ラベルが同じになってしまうので
// data オブジェクトのコピー (JQuerry の extend) を渡す
Chart.defaults.global.animation = false;
/* separate from .php
<?php foreach ($data_inis as $key => $value): ?>
<?php $dini = parse_ini_file($value); ?>
<?php if (!isset($dini["image_type"])): ?>
config = new_config();
setColor_onGraph(config.data.datasets[0]);
myLineChart_<?=$dini["dname"]?> = new Chart(ctx_<?php echo $dini["dname"]?>, config);
myLineChart_<?=$dini["dname"]?>.datasets = config.data.datasets;
<?php endif; ?>
<?php endforeach ?>
*/
myLineCharts = [];
gIni.sensor_devices.forEach(function(device){
config = new_config();
setColor_onGraph(config.data.datasets[0]);
myLineCharts[device.dname] = new Chart(ctxs[device.dname], config);
myLineCharts[device.dname].datasets = config.data.datasets;
});
var iv = setInterval( function() {
$.ajax({
type: "GET",
url: "data.php",
data: {serial_id: gIni.serial_id,
show_data_lows: gIni['show_data_lows'],
// show_data_lows: <?=$ini['show_data_lows']; ?>,
// show_data_gnt: "<? = $ini['show_data_gnt']; ?>",
// LastTime: gLastTime,
/* separate from .php
ILTimes: {
// 以下、キーはファイル名(拡張子なし)
<?php foreach ($data_inis as $key => $value): ?>
<?php $dini = parse_ini_file($value); ?>
<?php if (!isset($dini["image_type"])): ?>
<?= $dini["fname"]?>: gLastTime_<?= $dini["dname"] ?>,
<?php endif; ?>
<?php endforeach ?>
}
*/
ILTimes: gLastTimes
},
dataType: "json",
})
.then(
function(data, dataType){
if(data){
/* separate from .php
<?php foreach ($data_inis as $key => $value): ?>
<?php $dini = parse_ini_file($value); ?>
<?php if (!isset($dini["image_type"])): ?>
if(data.<?= $dini["fname"]?>)
onReceiveStreamValues_<?= $dini["dname"]?>(data.<?= $dini["fname"]?>);
<?php endif; ?>
<?php endforeach ?>
*/
gIni.sensor_devices.forEach(function(device){
if(data[device.fname])
onReceiveStreamValues(device, data[device.fname]);
});
}
},
function(XMLHttpRequest, textStatus, errorThrown){
console.log('Error : ' + errorThrown);
});
/* separate from .php
<?php foreach ($data_inis as $key => $value): ?>
<?php $dini = parse_ini_file($value); ?>
<?php if (isset($dini["image_type"])): ?>
$.ajax({
type: "GET",
url: "pic.php",
data: {serial_id: "<?= $_GET['serial_id']; ?>", device: "<?= $dini["dname"] ?>"},
dataType: "json",
})
.then(
function(data, dataType){
document.getElementById("pic_file_name_<?= $dini["dname"] ?>").innerHTML = data.latest_pic_name.slice(0,4) + "年" + data.latest_pic_name.substring(4,6) + "月" + data.latest_pic_name.substring(6,8) + "日" + data.latest_pic_name.substring(8,10) + "時" + data.latest_pic_name.substring(10,12) + "分" + data.latest_pic_name.substring(12,14) + "秒";
if (data.device == ""){
// no device specified
if (data.ymd == ""){
$('#latest_pic_<?= $dini["dname"] ?>').attr('src', 'uploads/'+"<?= $_GET['serial_id'];?>"+"/"+data.latest_pic_name);
} else {
$('#latest_pic_<?= $dini["dname"] ?>').attr('src', 'uploads/'+"<?= $_GET['serial_id'];?>"+"/"+data.ymd+"/"+data.latest_pic_name);
}
} else {
// device specified
if (data.ymd == ""){
$('#latest_pic_<?= $dini["dname"] ?>').attr('src', 'uploads/'+"<?= $_GET['serial_id'];?>"+"/"+data.device+"/"+data.latest_pic_name);
} else {
$('#latest_pic_<?= $dini["dname"] ?>').attr('src', 'uploads/'+"<?= $_GET['serial_id'];?>"+"/"+data.device+"/"+data.ymd+"/"+data.latest_pic_name);
}
}
// $('#latest_pic').attr('src', 'uploads/'+"<?= $_GET['serial_id'];?>"+"/"+data.latest_pic_name);
},
function(XMLHttpRequest, textStatus, errorThrown){
console.log('Error : ' + errorThrown);
});
<?php endif; ?>
<?php endforeach ?>
*/
gIni.image_devices.forEach(function(device){
$.ajax({
type: "GET",
url: "pic.php",
data: {serial_id: gIni.serial_id, device: device.dname},
dataType: "json",
})
.then(
function(data, dataType){
document.getElementById("pic_file_name_" + device.dname).innerHTML = data.latest_pic_name.slice(0,4) + "年" + data.latest_pic_name.substring(4,6) + "月" + data.latest_pic_name.substring(6,8) + "日" + data.latest_pic_name.substring(8,10) + "時" + data.latest_pic_name.substring(10,12) + "分" + data.latest_pic_name.substring(12,14) + "秒";
if (data.device == ""){
// no device specified
if (data.ymd == ""){
$('#latest_pic_' + device.dname).attr('src', 'uploads/'+gIni.serial_id+"/"+data.latest_pic_name);
} else {
$('#latest_pic_' + device.dname).attr('src', 'uploads/'+gIni.serial_id+"/"+data.ymd+"/"+data.latest_pic_name);
}
} else {
// device specified
if (data.ymd == ""){
$('#latest_pic_' + device.dname).attr('src', 'uploads/'+gIni.serial_id+"/"+data.device+"/"+data.latest_pic_name);
} else {
$('#latest_pic_' + device.dname).attr('src', 'uploads/'+gIni.serial_id+"/"+data.device+"/"+data.ymd+"/"+data.latest_pic_name);
}
}
// $('#latest_pic').attr('src', 'uploads/'+"<?= $_GET['serial_id'];?>"+"/"+data.latest_pic_name);
},
function(XMLHttpRequest, textStatus, errorThrown){
console.log('Error : ' + errorThrown);
});
});
// }, 250 );
}, 1000 );