-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.html
145 lines (103 loc) · 3.89 KB
/
index.html
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
<html>
<meta charset="utf-8">
<!-- External dependencies -->
<link href='http://fonts.googleapis.com/css?family=Open+Sans:300,400&subset=cyrillic,latin' rel='stylesheet' type='text/css'>
<script src="http://code.jquery.com/jquery-1.11.2.min.js"></script>
<!-- TSG -->
<link href="tsg.css" rel="stylesheet">
<script src="bower_components/d3/d3.min.js"></script>
<script src="tsg.js"></script>
<!-- Example style -->
<style>
html {width: 100%}
body {font: normal 14pt 'Open Sans', sans-serif; font-weight: lighter; margin:4em; width: 80%; height: 100%}
h1 {font-weight: lighter}
.graphic {
position:relative;
font-size:11px;
font-weight: normal;
border: none;
width:100%;
height:200px;
overflow: hidden;
cursor: move;
}
</style>
<body>
<h1>D3js simple graphic + InfluxDB</h1>
<div class="graphic single">
<button class="zoom_in -round">+</button>
<button class="zoom_out -round">-</button>
</div>
<div class="graphic second"></div>
<div class="switchers"></div>
</body>
<script>
// Simple line chart
g1 = new TSG('.single', {margin: {top: 20, right: 50, bottom: 30, left: 140}, title: 'Порт 54'});
g1.influx('SELECT rx from 1h.port2, 1h.port1');
// Manual load data
g2 = new TSG('.second');
var g2data = [];
var all = {'port1': {type:'input'}, 'port2': {type:'input'}, 'port3': {type:'output'}, 'port4': {type:'output'}};
d3.json("http://localhost:8086/db/mon/series?u=root&p=pussy-root&q=" + encodeURIComponent("SELECT rx from /^1h.port.*/"), function(err, data) {
if (err) console.warn('ERROR REQUEST: ', query, err.response); else {
g2data = data;
console.log('--→',data);
//g2.simple('.second', data);
g2.bind(g2data);
var el = document.querySelector('.switchers');
s = '<p><input type="checkbox" onClick="cheka_sum(this)"> суммарный</p><div style="float:left; width:400px">';
s += 'Входы: ';
[1,2].forEach(function(d) {
s += '<p><input type="checkbox" onClick="cheka(this, ' + d + ')" checked /> № '+ d + '</p>';
});
s += '</div><div style="float:left; width:200px">';
s += 'Выходы: ';
[3,4].forEach(function(d) {
s += '<p><input type="checkbox" onClick="cheka(this, ' + d + ')" checked /> № '+ d + '</p>';
});
s += '</div>';
$(el).append(s);
}
});
window.onresize = function() { console.log('x'); g1.resize(); g2.resize();}
var summarize = false;
function redraw() {
var r = g2data.filter(function(d) { return !all[d.name] || all[d.name].disabled != true });
if (summarize) {
var inp = sum(r.filter(function(d) { return all[d.name] && all[d.name].type == 'input'; }));
var outp = sum(r.filter(function(d) { return all[d.name] && all[d.name].type == 'output'; }));
r = [];
if (inp) r.push({name: 'Вход', points: inp});
if (outp) r.push({name: 'Выход', points: outp});
g2.data = r;
console.log('sum --→', g2.data);
} else {
g2.data = r;
}
g2.update();
}
function cheka(e, p) {
var name = 'port' + p;
all[name].disabled = !e.checked;
redraw();
}
function cheka_sum(e) {
summarize = e.checked;
redraw();
}
// Вспомогательная функция — суммирует данные из массивов points
function sum(a) {
var l = a.length, s;
if (l) {
var b = a[0].points.map(function(d,index) {
var s = 0;
for(var i = 0;i<l;i++) if (typeof a[i].points[index][2] !== undefined ) s += a[i].points[index][2];
return [d[0],0,s];
})
return b;
}
}
</script>
</html>