/
ChartLinkMaker.js
457 lines (420 loc) · 12.3 KB
/
ChartLinkMaker.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
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
"use strict";
const debug = require("debug")("chart-link-maker");
// const config = require('config')
const randojs = require("@nastyox/rando.js"),
rando = randojs.rando; //, randoSequence = randojs.randoSequence
const { convertSecondsToDays, removeSpaces } = require("./jiraUtils")
const BAR_CHART_TYPE = "bar";
const LINE_CHART_TYPE = "line";
const DEFAULT_CHART_TITLE = "";
const DEFAULT_CHART_TYPE = BAR_CHART_TYPE;
const DEFAULT_WIDTH = 800;
const DEFAULT_HEIGHT = 600;
const DEFAULT_FILL = false;
/** Structure
* data = [
* { label: "label1",
* data: []
* }
* ]
* labels = []
*
* Size of labels must match # of data elements in data.data
*/
/**
* Set any negative values to 0
*
* @param {array} arr
* @returns {array} Updated array
*/
function removeNegatives(arr) {
return arr.map((x) => (x >= 0 ? x : 0));
}
/**
* Create an HTML string for a Billboard.js chart object
*
* @class ChartLinkMaker
*/
class ChartLinkMaker {
constructor(
newDataSeries,
newDataCategories,
chartType = DEFAULT_CHART_TYPE,
w = DEFAULT_WIDTH,
h = DEFAULT_HEIGHT,
fill = DEFAULT_FILL
) {
debug(
`newDataSeries: ${newDataSeries}, newDataCateogries: ${newDataCategories}, w: ${w}, h: ${h}`
);
if (newDataSeries) {
this.dataSeries = newDataSeries;
} else {
this.dataSeries = [];
}
if (newDataCategories) {
this.dataCategories = newDataCategories;
} else {
this.dataCategories = [];
}
this.chartType = chartType;
this.width = w;
this.height = h;
this.fill = fill;
debug(
`dataSeries: ${this.dataSeries}, dataCategories: ${this.dataCategories}, width: ${this.width}, height: ${this.height}, fill: ${this.fill}`
);
return this;
}
/**
* Zero out any existing settings and data: i.e. all data series (reset to empty array), all categories (reset to empty array), chart type (reset to DEFAULT_CHART_TYPE), width (reset to DEFAULT_WIDTH) and height (reset to DEFAULT_HEIGHT), and fill (reset to DEFAULT_FILL).
*
* @returns ChartLinkMaker
* @memberof ChartLinkMaker
*/
reset() {
debug("reset() called");
this.dataSeries = [];
this.dataCategories = [];
this.chartType = DEFAULT_CHART_TYPE;
this.width = DEFAULT_WIDTH;
this.height = DEFAULT_HEIGHT;
this.fill = DEFAULT_FILL;
return this;
}
/**
* Set the chart type to bar chart
*
* @returns ChartLinkMaker
* @memberof ChartLinkMaker
*/
setBarChart() {
debug("setBarChart() called");
this.chartType = BAR_CHART_TYPE;
return this;
}
/**
* Set the chart type to line chart
*
* @returns ChartLinkMaker
* @memberof ChartLinkMaker
*/
setLineChart() {
debug("setLineChart() called");
this.chartType = LINE_CHART_TYPE;
return this;
}
/**
* Set the chart type to specified type.
* Must be a chart type supported by Billboard.js
*
* @param {string} newType
* @returns ChartLinkMaker
* @memberof ChartLinkMaker
*/
setChartType(newType) {
debug(`setChartType(${newType}) called`);
this.chartType = newType;
return this;
}
/**
* Set the chart height and width.
*
* @param {object} hw { h: 400, w: 600 }
* @returns ChartLinkMaker
* @memberof ChartLinkMaker
*/
setSize(hw) {
this.h = hw.h;
this.w = hw.w;
return this;
}
/**
* Sset the chart height (pixels)
*
* @param {integer} h
* @returns ChartLinkMaker
* @memberof ChartLinkMaker
*/
setHeight(h) {
this.h = h;
return this;
}
/**
* Set the chart width (pixels)
*
* @param {integer} w
* @returns ChartLinkMaker
* @memberof ChartLinkMaker
*/
setWidth(w) {
this.w = w;
return this;
}
/**
* Set chart fill (for area charts)
*
* @param {*} newFill
* @returns ChartLinkMaker
* @memberof ChartLinkMaker
*/
setFill(newFill) {
debug(`setFill(${newFill}) called...`);
this.fill = newFill;
return this;
}
/**
* Specify the data categories, replacing all existing categories.
*
* @param {array} cats
* @returns ChartLinkMaker
* @memberof ChartLinkMaker
*/
setCategories(cats) {
debug(`setCategories(...${cats.length} items) called...`);
this.dataCategories = cats;
return this;
}
addCategory(cat) {
debug(`addCategory(${cat}) called... Adding after ${this.dataCategories[this.dataCategories.length-1]}`)
this.dataCategories.push(cat)
return this
}
/**
* Make sure the categories are valid and their sizes match the series length/size.
*
* @param {integer} [seriesSize=-1]
* @returns {boolean}
* @memberof ChartLinkMaker
*/
_validateCategories(seriesSize = -1) {
if (this.dataCategories) {
if (this.dataCategories.length == seriesSize) {
return true;
} else {
debug(`_validateCategories() failing...`);
debug(this.dataCategories);
throw new Error(
`invalid data series provided: length mismatch. Received ${seriesSize} elements, but expected ${this.dataCategories.length}. Suggest wiping and rebuilding the cache`
);
}
} else {
throw new Error(
"Must set categories using setCategories() call before adding series"
);
}
}
/**
* Update the cache with a new data series
*
* @param {string} newLabel
* @param {object} newData
* @param {boolean} [ignoreValidation=false]
* @returns ChartLinkMaker
* @memberof ChartLinkMaker
*/
addSeries(newLabel, newData, ignoreValidation = false) {
debug(`addSeries(${newLabel}, ${newData}) called...`);
try {
if (ignoreValidation || this._validateCategories(newData.length)) {
this.dataSeries.push({ label: newLabel, data: newData });
}
// debug(`... this.dataSeries now == ${JSON.stringify(this.dataSeries)}`)
} catch (ex) {
debug(`Exception caught: ${ex}`);
}
return this;
}
/**
* Create the datasets object using the existing dataSeries data
*
* @returns {string} datasets
* @memberof ChartLinkMaker
*/
_buildDatasets() {
debug(`_buildDatasets() called...`);
let datasets = [];
this.dataSeries.forEach((cat, ndx) => {
debug(`...in forEach(${JSON.stringify(cat)}, ${ndx})...`);
datasets.push({
label: this.dataSeries[ndx].label,
data: this.dataSeries[ndx].data,
fill: this.fill,
});
});
return JSON.stringify(datasets)
.replace(/"([^"]+)":/g, "$1:")
.replace(/"/g, "'");
}
getJson(data) {
if (data) {
// debug(data);
const response = {};
// debug(Object.keys(data).join(","));
Object.keys(data).forEach(key => {
response[removeSpaces(key)] = data[key]
})
response['x'] = `[${'^' + this.dataCategories.join('^,^') + '^'}]`
return(JSON.stringify(response).replace(/"/g, '').replace(/\^/g, '"'))
} else {
throw new Error("getJson called without data");
}
}
getGroups(data) {
// debug(`getGroups called: `, data)
return(JSON.stringify(`[${'^' + Object.keys(data).map((x) => removeSpaces(x)).join('^,^') + '^'}]`).replace(/"/g, '').replace(/\^/g, '"'))
}
enoughDataToPrintChart(data) {
let goodToGo = false
const k = Object.keys(data)
if (k.length) { // Have entries
// But are they non-zero
let sumOfValues = 0
for (let keyNdx = 0; keyNdx < k.length; keyNdx++) {
let key = k[keyNdx]
// k.forEach((key) => {
// debug(`...data[${key}]: (typeof: ${typeof data[key]})`, data[key])
if (typeof data[key] == 'number') {
sumOfValues += data[key]
} else {
sumOfValues += data[key].reduce((acc, cur) => { return acc + cur})
}
debug(`sumOfValues after ${key}: ${sumOfValues}`)
if (sumOfValues) { return(true) }
}
}
return(goodToGo)
}
/**
* Create the HTML tag for a single chart
*
* @param {string} title
* @param {object} data
* @param {string} [chartType="area"]
* @returns
* @memberof ChartLinkMaker
*/
async buildChartImgTag(title, data, chartType = "area", xLabel = "") {
return new Promise((resolve, reject) => {
// debug(
// `buildChartImgTag(${title}, data, ${chartType}) called with data: `,
// data, '; this.dataSeries: ', this.dataSeries
// );
// TODO: Update chart to show 2nd axis moving averge
if (Object.keys(data).includes(`mvgAvg7`)) {
delete data[`mvgAvg7`]
}
if (Object.keys(data).includes(`mvgAvg30`)) {
delete data[`mvgAvg30`]
}
if (!this.enoughDataToPrintChart(data)) { // No data in function call
if (!this.dataSeries.length) { // No data previously provided, so return error msg
resolve({ err: 'No data available' })
} else {
debug(`this.dataSeries.length: `, this.dataSeries.length)
}
// No data, so build from local data
this.h = DEFAULT_HEIGHT;
this.w = DEFAULT_WIDTH;
// TODO: Set these via the config file
let inReview = this.dataSeries.filter((x) => {
return x.label == "IN REVIEW";
})[0].data;
let blocked = this.dataSeries.filter((x) => {
return x.label == "BLOCKED";
})[0].data;
data = {
Open: this.dataSeries.filter((x) => {
return x.label == "DEFINED";
})[0].data,
Active: this.dataSeries
.filter((x) => {
return x.label == "IN PROGRESS";
})[0]
.data.map((num, idx) => {
return num + inReview[idx];
}),
Closed: this.dataSeries.filter((x) => {
return x.label == "DONE";
})[0].data,
Stopped: this.dataSeries
.filter((x) => {
return x.label == "EMERGENCY";
})[0]
.data.map((num, idx) => {
return num + blocked[idx];
}),
};
}
if (!title) {
title = DEFAULT_CHART_TITLE;
}
if (Object.keys(data)) {
debug(`data keys: ${Object.keys(data).join(',')}`)
let forecastType = ''
let dataGroups = ""
// Workaround for specifying stacked bar chart
if (chartType === "stacked-bar") {
if (Object.keys(data).includes("Forecast")) {
const subData = {...data}
delete subData["Forecast"]
delete subData["Forecast_TeamSize"]
dataGroups = `groups: [ ${this.getGroups(subData)} ],`
forecastType = `types: { Forecast: "line", Forecast_TeamSize: "line" },`
} else {
dataGroups = `groups: [ ${this.getGroups(data)} ],`
}
chartType = "bar"
}
const subchartContent = chartType === "area" || chartType === "line" ? 'subchart: { show: true, }' : ''
const id = rando(99999);
let chartHtml = `<div id='chart-${id}' class='miniJSRChart'></div><script>
var chart = bb.generate({
bindto: "#chart-${id}",
size: { height: ${this.h ? this.h : DEFAULT_HEIGHT}, width: ${
this.w ? this.w : DEFAULT_WIDTH
} },
title: { text: '${title}' },
axis: {
${xLabel ? 'y: { label: "' + xLabel + '" },' : '' }
x: {
type: "timeseries",
}
},
bar: {
width: {
ratio: 0.9,
max: 30
}
},
data: {
x: "x",
type: "${chartType}",
json: ${this.getJson(data)},
${forecastType}
${dataGroups}
},
color: { pattern: [ 'lightblue', 'MediumSeaGreen', 'CornflowerBlue', 'Pink', 'Orange' ] },
xFormat: "%Y-%m-%d",
${subchartContent}
});
// window.onload = saveStaticChart();
// function saveStaticChart() {
// chart.config("title.text", "asdf", true);
// setTimeout(function() {
// chart.export(null, function(dataUrl) {
// var img = document.getElementById("exported");
// img.src = dataUrl;
// })
// }, 500);
// }
</script>`;
resolve(chartHtml);
} else {
debug(`ERR in buildChartImgTag: No data set for ${title}`);
reject("no data set");
}
});
}
}
module.exports = ChartLinkMaker;