-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathclient.js
210 lines (163 loc) · 4.29 KB
/
client.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
import Chart from 'chart.js/auto'
import domReady from 'domready'
import pattern from 'patternomaly'
domReady(main)
function main() {
const context = global.$$context
if (context.showValueLabels) {
labelPlugin()
}
const chartData = {
datasets: [],
labels: []
}
const chartConfig = {
data: chartData,
type: context.chartType,
spanGaps: true,
options: {
title: {
display: true,
text: context.title
},
scales: {
yLeft: {
position: 'left'
}
}
}
}
if (context.disableAnimation) {
chartConfig.options.animation = false
}
const { left, right } = context.yAxisAlignment
if (right.length > 0) {
chartConfig.options.scales.yRight = {
position: 'right',
grid: {
drawOnChartArea: false, // only want the grid lines for one axis to show up
}
}
}
for (let i = 0; i < context.fieldCount; i++) {
let borderColor = pickColor()
let patternName = pickPattern()
let backgroundColor
if (context.usePatterns) {
backgroundColor = pattern.draw(patternName, borderColor.toString(0.2))
} else {
backgroundColor = borderColor.toString(0.2)
}
let label = `dataset #${i + 1}`
const dataset = {
yAxisID: 'yLeft',
borderColor: borderColor.toString(),
backgroundColor,
borderWidth: 1,
pointRadius: 2,
fill: !context.noFill,
tension: 0.2,
data: []
}
if (right.includes(i)) {
dataset.yAxisID = 'yRight'
label = `${label} (right)`
}
dataset.label = label
chartData.datasets.push(dataset)
}
const chart = new Chart(document.getElementById('myChart'), chartConfig)
const host = global.document.location.host
const ws = new WebSocket('ws://' + host)
let count = 0
ws.onopen = () => console.log('opened')
ws.onclose = () => console.log('close')
ws.onerror = err => console.error(err)
ws.onmessage = event => {
let shouldTrim = count++ > context.windowSize
let entry = JSON.parse(event.data)
for (let i = 0; i < entry.values.length; i++) {
let data = chartData.datasets[i].data
data.push(parseFloat(entry.values[i]))
if (shouldTrim) {
data.shift()
}
}
let labels = chartData.labels
labels.push(entry.label)
if (shouldTrim) {
labels.shift()
}
chart.update()
}
}
class Color {
constructor(r, g, b, a = 1) {
this.r = r
this.g = g
this.b = b
this.a = a
}
toString(overrideAlpha) {
let alpha = overrideAlpha
if (overrideAlpha === undefined) {
alpha = this.a
}
return `rgba(${this.r},${this.g},${this.b},${alpha})`
}
}
let colorIndex = 0
const colors = [
new Color(0, 215, 0), new Color(0, 0, 215), new Color(0, 215, 215), new Color(244, 119, 66), new Color(215, 0, 0),
new Color(65, 244, 241), new Color(106, 65, 244), new Color(60, 60, 60), new Color(0, 0, 0)
]
let patternIndex = 0
const patternNames = [
'plus', 'cross', 'dash', 'cross-dash', 'dot', 'dot-dash', 'disc', 'ring', 'line', 'line-vertical',
'weave', 'zigzag', 'zigzag-vertical', 'diagonal', 'diagonal-right-left', 'square', 'box', 'triangle',
'triangle-inverted', 'diamond', 'diamond-box'
]
function pickColor() {
if (colorIndex === colors.length) {
colorIndex = 0
}
return colors[colorIndex++]
}
function pickPattern() {
if (patternIndex === patternNames.length) {
patternIndex = 0
}
return patternNames[patternIndex++]
}
function labelPlugin() {
Chart.plugins.register({
afterDatasetsDraw
})
function afterDatasetsDraw(chart, easing) {
// To only draw at the end of animation, check for easing === 1
var ctx = chart.ctx
for (let i = 0; i < chart.data.datasets.length; i++) {
let dataset = chart.data.datasets[i]
var meta = chart.getDatasetMeta(i)
if (!meta.hidden) {
for (let j = 0; j < meta.data.length; j++) {
let element = meta.data[j]
// Draw the text in black, with the specified font
ctx.fillStyle = 'rgb(0, 0, 0)'
var fontSize = 16
var fontStyle = 'normal'
var fontFamily = 'Helvetica Neue'
ctx.font = Chart.helpers.fontString(fontSize, fontStyle, fontFamily)
// Just naively convert to string for now
var dataString = dataset.data[j].toString()
// Make sure alignment settings are correct
ctx.textAlign = 'center'
ctx.textBaseline = 'middle'
var padding = 5
var position = element.tooltipPosition()
ctx.fillText(dataString, position.x, position.y - (fontSize / 2) - padding)
}
}
}
}
}