-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
chart.js
507 lines (442 loc) 路 14.5 KB
/
chart.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
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
/**
* @fileoverview tsv, csv format tui.chart plugin
* consumes tab separated values and make data/options for tui chart
* @author NHN Ent. FE Development Lab <dl_javascript@nhnent.com>
*/
/**
* @example
* tsv, csv format tui.chart plugin
* consumes tab separated values and make data/options for tui chart
*
* ```chart
* \tcat1\tcat2 => tsv, csv format chart data
* jan\t21\t23
* feb\t351\t45
* => space required as a separator
* type: area => tui.chart.areaChart()
* url: http://url.to/csv => fetch data from the url
* width: 700 => chart.width
* height: 300 => chart.height
* title: Monthly Revenue => chart.title
* format: 1000 => chart.format
* x.title: Amount => xAxis.title
* x.min: 0 => xAxis.min
* x.max 9000 => xAxis.max
* x.suffix: $ => xAxis.suffix
* y.title: Month => yAxis.title
* ```
*/
import $ from 'jquery';
import util from 'tui-code-snippet';
const chart = window.tui.chart;
import Editor from '../editorProxy';
import csv from './csv';
const {WwCodeBlockManager, codeBlockManager} = Editor;
const LANG = 'chart';
// csv configuration
csv.IGNORE_QUOTE_WHITESPACE = false;
csv.IGNORE_RECORD_LENGTH = true;
csv.DETECT_TYPES = false;
const REGEX_LINE_ENDING = /[\n\r]/;
const DSV_DELIMITERS = [',', '\t', /\s+/];
const OPTION_DELIMITER = ':';
const SUPPORTED_CHART_TYPES = ['barChart', 'columnChart', 'lineChart', 'areaChart', 'pieChart'];
const CATEGORY_CHART_TYPES = ['lineChart', 'areaChart'];
/**
* parse data and options for tui.chart
* data format can be csv, tsv
* options format is colon separated keys & values
* @param {string} code - plain text format data & options
* @param {Function} callback - callback which provides json format data & options
* @ignore
*/
function parseCode2DataAndOptions(code, callback) {
code = trimKeepingTabs(code);
const [firstCode, secondCode] = code.split(/\n{2,}/);
// try to parse first code block as `options`
let options = parseCode2ChartOption(firstCode);
const url = options && options.editorChart && options.editorChart.url;
// if first code block is `options` and has `url` option, fetch data from url
let dataAndOptions;
if (util.isString(url)) {
// url option provided
// fetch data from url
const success = dataCode => {
dataAndOptions = _parseCode2DataAndOptions(dataCode, firstCode);
callback(dataAndOptions);
};
const fail = () => callback(null);
$.get(url).done(success).fail(fail);
} else {
// else first block is `data`
dataAndOptions = _parseCode2DataAndOptions(firstCode, secondCode);
callback(dataAndOptions);
}
}
/**
* parse codes to chart data & options Object
* @param {string} dataCode - code block containing chart data
* @param {string} optionCode - code block containing chart options
* @returns {Object} - tui.chart data & options
* @see https://nhnent.github.io/tui.chart/latest/tui.chart.html
* @ignore
*/
function _parseCode2DataAndOptions(dataCode, optionCode) {
const data = parseDSV2ChartData(dataCode);
const options = parseCode2ChartOption(optionCode);
return {
data,
options
};
}
/**
* detect delimiter the comma, tab, regex
* @param {string} code - code to detect delimiter
* @returns {string|RegExp} - detected delimiter
* @ignore
*/
function detectDelimiter(code) {
code = trimKeepingTabs(code);
// chunk first max 10 lines to detect
const chunk = code.split(REGEX_LINE_ENDING).slice(0, 10).join('\n');
// calc delta for each delimiters
// then pick a delimiter having the minimum value delta
return DSV_DELIMITERS.map(delimiter => ({
delimiter,
delta: calcDSVDelta(chunk, delimiter)
})).sort((a, b) => (
a.delta - b.delta
))[0].delimiter;
}
/**
* calculate delta(sum of length difference of rows) values of given DSV
* @param {string} code - code to be test
* @param {string|RegExp} delimiter - delimiter to test
* @returns {number} delta value for code
* @ignore
*/
function calcDSVDelta(code, delimiter) {
let rows, delta;
try {
csv.COLUMN_SEPARATOR = delimiter;
rows = csv.parse(code);
if (rows[0].length < 2) {
// parsing completely failed
throw new Error('parser fail');
}
// sum of all length difference of all rows
delta = rows.map(row => row.length)
.reduce((a, b) => ({
deltaSum: a.deltaSum + Math.abs(a.length - b),
length: b
}), {
deltaSum: 0,
length: rows[0].length
}).deltaSum;
} catch (e) {
delta = Infinity;
}
return delta;
}
/**
* parse csv, tsv to chart data
* @param {string} code - data code
* @param {string|RegExp} delimiter - delimiter
* @returns {Object} - tui.chart data
* @see https://nhnent.github.io/tui.chart/latest/tui.chart.html
* @ignore
*/
function parseDSV2ChartData(code, delimiter) {
// trim all heading/trailing blank lines
code = trimKeepingTabs(code);
csv.COLUMN_SEPARATOR = delimiter || detectDelimiter(code);
let dsv = csv.parse(code);
// trim all values in 2D array
dsv = dsv.map(arr => arr.map(val => val.trim()));
// test a first row for legends. ['anything', '1', '2', '3'] === false, ['anything', 't1', '2', 't3'] === true
const hasLegends = dsv[0].filter((v, i) => i > 0).reduce((hasNaN, item) => hasNaN || !isNumeric(item), false);
const legends = hasLegends ? dsv.shift() : [];
// test a first column for categories
const hasCategories = dsv.slice(1).reduce((hasNaN, row) => hasNaN || !isNumeric(row[0]), false);
const categories = hasCategories ? dsv.map(arr => arr.shift()) : [];
if (hasCategories) {
legends.shift();
}
// transpose dsv, parse number
// [['1','2','3'] [[1,4,7]
// ['4','5','6'] => [2,5,8]
// ['7','8','9']] [3,6,9]]
dsv = dsv[0].map((t, i) => dsv.map(x => parseFloat(x[i])));
// make series
const series = dsv.map((data, i) => hasLegends ? {
name: legends[i],
data
} : {
data
});
return {
categories,
series
};
}
/**
* parse code from url
* @param {string} url - remote csv/tsv file url
* @param {Function} callback - callback function
* @ignore
*/
function parseURL2ChartData(url, callback) {
const success = code => {
const chartData = parseDSV2ChartData(code);
callback(chartData);
};
const fail = () => callback(null);
$.get(url).done(success).fail(fail);
}
/**
* parse option code
* @param {string} optionCode - option code
* @returns {Object} - tui.chart option string
* @see https://nhnent.github.io/tui.chart/latest/tui.chart.html
* @ignore
*/
function parseCode2ChartOption(optionCode) {
const reservedKeys = ['type', 'url'];
let options = {};
if (util.isUndefined(optionCode)) {
return options;
}
const optionLines = optionCode.split(REGEX_LINE_ENDING);
optionLines.forEach(line => {
let [keyString, ...values] = line.split(OPTION_DELIMITER);
let value = values.join(OPTION_DELIMITER);
keyString = keyString.trim();
if (value.length === 0) {
return;
}
try {
value = JSON.parse(value.trim());
} catch (e) {
value = value.trim();
}
// parse keys
let [...keys] = keyString.split('.');
let topKey = keys[0];
if (util.inArray(topKey, reservedKeys) >= 0) {
// reserved keys for chart plugin option
keys.unshift('editorChart');
} else if (keys.length === 1) {
// short names for `chart`
keys.unshift('chart');
} else if (topKey === 'x' || topKey === 'y') {
// short-handed keys
keys[0] = `${topKey}Axis`;
}
let option = options;
for (let i = 0; i < keys.length; i += 1) {
let key = keys[i];
option[key] = option[key] || (keys.length - 1 === i ? value : {});
option = option[key];
}
});
return options;
}
/**
* trim whitespace and newlines at head/tail
* it should not trim \t in tsv
* @param {string} code - code to trim
* @returns {string} - trimmed code
* @ignore
*/
function trimKeepingTabs(code) {
return code.replace(/(^(\s*[\n\r])+)|([\n\r]+\s*$)/g, '');
}
/**
* test given string is numeric
* @param {string} str - string to be tested
* @returns {boolean} - true for numeric string
* @ignore
*/
function isNumeric(str) {
return !isNaN(str) && isFinite(str);
}
/**
* set default options
* @param {Object} options - tui.chart options
* @param {HTMLElement} chartContainer - chart container
* @returns {Object} - options
* @see https://nhnent.github.io/tui.chart/latest/tui.chart.html
* @ignore
*/
function setDefaultOptions(options, chartContainer) {
options = util.extend({
editorChart: {},
chart: {},
chartExportMenu: {}
}, options);
let {width, height} = options.chart;
const isWidthUndefined = util.isUndefined(width);
const isHeightUndefined = util.isUndefined(height);
if (isWidthUndefined || isHeightUndefined) {
// if no width or height specified, set width and height to container width
const {
width: containerWidth
} = chartContainer.getBoundingClientRect();
options.chart.width = isWidthUndefined ? containerWidth : width;
options.chart.height = isHeightUndefined ? containerWidth : height;
}
options.editorChart.type = options.editorChart.type ? `${options.editorChart.type}Chart` : 'columnChart';
options.chartExportMenu.visible = options.chartExportMenu.visible || false;
return options;
}
/**
* replace html from chart data
* @param {string} codeBlockChartDataAndOptions - chart data text
* @returns {string} - rendered html
* @ignore
*/
function chartReplacer(codeBlockChartDataAndOptions) {
const randomId = `chart-${Math.random().toString(36).substr(2, 10)}`;
let renderedHTML = `<div id="${randomId}" class="chart" />`;
setTimeout(() => {
const chartContainer = document.querySelector(`#${randomId}`);
try {
parseCode2DataAndOptions(codeBlockChartDataAndOptions, ({data, options}) => {
options = setDefaultOptions(options, chartContainer);
const chartType = options.editorChart.type;
if (SUPPORTED_CHART_TYPES.indexOf(chartType) < 0) {
chartContainer.innerHTML = `invalid chart type. type: bar, column, line, area, pie`;
} else if (CATEGORY_CHART_TYPES.indexOf(chartType) > -1 &&
data.categories.length !== data.series[0].data.length) {
chartContainer.innerHTML = 'invalid chart data';
} else {
chart[chartType](chartContainer, data, options);
}
});
} catch (e) {
chartContainer.innerHTML = 'invalid chart data';
}
}, 0);
return renderedHTML;
}
/**
* reduce 2D array to TSV rows
* @param {Array.<Array.<string>>} arr - 2d array
* @returns {Array.<string>} - TSV row array
* @ignore
*/
function _reduceToTSV(arr) {
// 2D array => quoted TSV row array
// [['a', 'b b'], [1, 2]] => ['a\t"b b"', '1\t2']
return arr.reduce((acc, row) => {
// ['a', 'b b', 'c c'] => ['a', '"b b"', '"c c"']
const quoted = row.map(text => {
if (!isNumeric(text) && text.indexOf(' ') >= 0) {
text = `"${text}"`;
}
return text;
});
// ['a', '"b b"', '"c c"'] => 'a\t"b b"\t"c c"'
acc.push(quoted.join('\t'));
return acc;
}, []);
}
/**
* override WwCodeBlockManager to enclose pasting data strings from wysiwyg in quotes
* @param {Editor} editor - editor
* @ignore
*/
function _setWwCodeBlockManagerForChart(editor) {
const componentManager = editor.wwEditor.componentManager;
componentManager.removeManager('codeblock');
componentManager.addManager(class extends WwCodeBlockManager {
/**
* Wrap table nodes into code block as TSV
* @memberof WwCodeBlockManager
* @param {Array.<Node>} nodes Node array
* @returns {HTMLElement} Code block element
*/
convertToCodeblock(nodes) {
if (nodes.length !== 1 || nodes[0].tagName !== 'TABLE') {
return super.convertToCodeblock(nodes);
}
const $codeblock = $('<pre />');
const node = nodes.shift();
// convert table to 2-dim array
const cells = [].slice.call(node.rows).map(
row => [].slice.call(row.cells).map(
cell => cell.innerText.trim()
)
);
const tsvRows = _reduceToTSV(cells);
$codeblock.append(tsvRows.reduce((acc, row) => acc + `<div>${row}</div>`, []));
$codeblock.attr('data-te-codeblock', '');
return $codeblock[0];
}
});
}
/**
* determine the event is from codeblock in markdown/codeblock editor
* @param {CodeMirror} cm - markdown codemirror editor
* @param {string} source - event source
* @param {Object} eventData - event data
* @returns {boolean} - true for the event from codeblock in markdown/codeblock editor
* @ignore
*/
function _isFromCodeBlockInCodeMirror(cm, source, eventData) {
// cursor in codeblock in markdown editor
let fromCodeBlockInCodeMirror = source === 'markdown' && cm.getTokenAt(eventData.from).state.overlay.codeBlock;
// or codeblock editor
fromCodeBlockInCodeMirror = fromCodeBlockInCodeMirror || (source === 'codeblock');
// but not from wysiwyg
fromCodeBlockInCodeMirror = fromCodeBlockInCodeMirror && (source !== 'wysiwyg');
return fromCodeBlockInCodeMirror;
}
/**
* enclose pasting data strings from markdown in quotes
* wysiwyg event should be treated separately.
* because pasteBefore event from wysiwyg has been already processed table data to string,
* on the other hand we need a table element
* @param {CodeMirror} cm - markdown codemirror editor
* @param {string} source - event source
* @param {Object} data - event data
* @ignore
*/
function _onMDPasteBefore(cm, {source, data: eventData}) {
if (!_isFromCodeBlockInCodeMirror(cm, source, eventData)) {
return;
}
const code = eventData.text.join('\n');
const delta = calcDSVDelta(code, '\t');
if (delta === 0) {
csv.COLUMN_SEPARATOR = '\t';
const parsed = _reduceToTSV(csv.parse(code));
eventData.update(eventData.from, eventData.to, parsed);
}
}
/**
* chart plugin
* @param {Editor} editor - editor
* @ignore
*/
function chartExtension(editor) {
const optionLanguages = editor.options.codeBlockLanguages;
if (optionLanguages && optionLanguages.indexOf(LANG) < 0) {
optionLanguages.push(LANG);
}
codeBlockManager.setReplacer(LANG, chartReplacer);
if (!editor.isViewer()) {
// treat wysiwyg paste event
_setWwCodeBlockManagerForChart(editor);
// treat markdown paste event
editor.eventManager.listen('pasteBefore', ev => _onMDPasteBefore(editor.mdEditor.cm, ev));
}
}
Editor.defineExtension('chart', chartExtension);
export {
parseCode2DataAndOptions,
parseURL2ChartData,
parseCode2ChartOption,
parseDSV2ChartData,
detectDelimiter
};