-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathquickchart.js
413 lines (394 loc) · 14.9 KB
/
quickchart.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
import * as coda from "@codahq/packs-sdk";
export const pack = coda.newPack();
const textParam = coda.makeParameter({
type: coda.ParameterType.String,
name: "text",
description: "The text to turn into a word cloud."
});
const formatParam = coda.makeParameter({
type: coda.ParameterType.String,
name: "format",
description: "Image output format - svg or png",
optional: true,
defaultValue: "png"
});
const widthParam = coda.makeParameter({
type: coda.ParameterType.Number,
name: "width",
description: "Image width",
optional: true,
defaultValue: 600
});
const heightParam = coda.makeParameter({
type: coda.ParameterType.Number,
name: "height",
description: "Image height",
optional: true,
defaultValue: 600
});
const backgroundColorParam = coda.makeParameter({
type: coda.ParameterType.String,
name: "backgroundColor",
description: "Background color of image (rgb, hsl, hex, or name value)",
optional: true,
defaultValue: "transparent"
});
const fontFamilyParam = coda.makeParameter({
type: coda.ParameterType.String,
name: "fontFamily",
description: "Font family to use",
optional: true,
defaultValue: "serif"
});
const fontScaleParam = coda.makeParameter({
type: coda.ParameterType.Number,
name: "fontScale",
description: "Size of the largest font (roughly)",
optional: true,
defaultValue: 25
});
const scaleParam = coda.makeParameter({
type: coda.ParameterType.String,
name: "scale",
description: "Frequency scaling method - linear, sqrt, or log",
optional: true,
defaultValue: "linear"
});
const paddingParam = coda.makeParameter({
type: coda.ParameterType.Number,
name: "padding",
description: "Padding between words, in pixels",
optional: true,
defaultValue: 1
});
const rotationParam = coda.makeParameter({
type: coda.ParameterType.Number,
name: "rotation",
description: "Maximum angle of rotation for words",
optional: true,
defaultValue: 0
});
const maxNumWordsParam = coda.makeParameter({
type: coda.ParameterType.Number,
name: "maxNumWords",
description: "Maximum number of words to show. Note that fewer may be shown depending on size.",
optional: true,
defaultValue: 200
});
const minNumWorsdParam = coda.makeParameter({
type: coda.ParameterType.Number,
name: "minNumWords",
description: "Minimum character length of each word to include",
optional: true,
defaultValue: 1
});
const caseParam = coda.makeParameter({
type: coda.ParameterType.String,
name: "case",
description: "Force words to this case - upper, lower, or none",
optional: true,
defaultValue: "lower"
});
const colorsParam = coda.makeParameter({
type: coda.ParameterType.String,
name: "colors",
description: "List of colors for words in JSON format, assigned randomly. e.g. [“red”, “#00ff00”, “rgba(0, 0, 255, 1.0)”]",
optional: true,
defaultValue: "random"
});
const removeStopwordsParam = coda.makeParameter({
type: coda.ParameterType.Boolean,
name: "removeStopwords",
description: "If true, remove common words from the cloud",
optional: true,
defaultValue: false
});
const languageParam = coda.makeParameter({
type: coda.ParameterType.String,
name: "language",
description: "Two-letter language code of stopwords to remove",
optional: true,
defaultValue: "en"
});
const useWordListParam = coda.makeParameter({
type: coda.ParameterType.Boolean,
name: "useWordList",
description: "If true, treat text as a comma-separated list of words or phrases instead of trying to split the text on our side",
optional: true,
defaultValue: false
});
function getUrl(url) {
return url.replace("[\"\s]", "%22");
}
pack.addFormula({
name: "WordCloud",
description: "Generate a QuickChart Word Cloud image. More documentation: https://quickchart.io/documentation/word-cloud-api/",
parameters: [textParam, formatParam, widthParam, heightParam, backgroundColorParam, fontFamilyParam, fontScaleParam, scaleParam, paddingParam, rotationParam, maxNumWordsParam, minNumWorsdParam, caseParam, colorsParam, removeStopwordsParam, languageParam, useWordListParam],
execute: async function (inputs, helper) {
var base_url = "https://quickchart.io/wordcloud?";
var params = [];
for (var i = 0; i < inputs.length; i++) {
var input = inputs[i];
// TODO: default values not being used when param is null
if (input != null) {
params.push(this.parameters[i].name + "=" + input);
}
}
return encodeURI(base_url + params.join('&'));
},
resultType: coda.ValueType.String,
codaType: coda.ValueHintType.ImageAttachment
});
const qrCodeTextParam = coda.makeParameter({
type: coda.ParameterType.String,
name: "text",
description: "The text to turn into a QR code link."
});
const marginParam = coda.makeParameter({
type: coda.ParameterType.Number,
name: "margin",
description: "Specify the whitespace around the QR image",
optional: true,
defaultValue: 4
});
const ecLevelParam = coda.makeParameter({
type: coda.ParameterType.String,
name: "ecLevel",
description: "Error correction level (Low, Medium, Quartile, High - default is Medium)",
optional: true,
defaultValue: "Medium"
});
const foregroundColorParam = coda.makeParameter({
type: coda.ParameterType.String,
name: "foregroundColor",
description: "Change the color of the dark foreground (default black). Specify using hex code (eg: 000000).",
optional: true,
defaultValue: "000000"
});
const qrBackgroundColorParam = coda.makeParameter({
type: coda.ParameterType.String,
name: "backgroundColor",
description: "Change the color of the light background (default white). Specify using hex code (eg: ffffff).",
optional: true,
defaultValue: "ffffff"
});
const qrSizeParam = coda.makeParameter({
type: coda.ParameterType.Number,
name: "size",
description: "Height and width in pixels",
optional: true
});
pack.addFormula({
name: "QRCode",
description: "Generate a QR Code image. More documentation: https://quickchart.io/documentation/#qr",
parameters: [qrCodeTextParam, formatParam, marginParam, widthParam, heightParam, ecLevelParam, foregroundColorParam, qrBackgroundColorParam, qrSizeParam],
execute: async function (inputs, helper) {
var base_url = "https://quickchart.io/qr?";
var params = [];
for (var i = 0; i < inputs.length; i++) {
var input = inputs[i];
var name = this.parameters[i].name;
// TODO: default values not being used when param is null
if (input != null) {
// Rewrite user friendly input to character version.
if (name == "ecLevel") {
input = input[0];
}
if (name == "foregroundColor") {
name = "dark";
// TODO: could also accept non-hex values and rewrite.
}
if (name == "backgroundColor") {
name = "light";
// TODO: could also accept non-hex values and rewrite.
}
params.push(name + "=" + input);
}
}
return getUrl(base_url + params.join('&'));
},
resultType: coda.ValueType.String,
codaType: coda.ValueHintType.ImageAttachment
});
const nodesParam = coda.makeParameter({
type: coda.ParameterType.StringArray,
name: "nodes",
description: "Node labels."
});
const graphTypeParam = coda.makeParameter({
type: coda.ParameterType.String,
name: "graphType",
description: "'directed' or 'undirected'",
optional: true,
defaultValue: "undirected"
});
const edgesParam = coda.makeParameter({
type: coda.ParameterType.StringArray,
name: "edges",
description: "Edges per node",
optional: true
});
const colorNodesParam = coda.makeParameter({
type: coda.ParameterType.StringArray,
name: "colorNodes",
description: "Color nodes based on unique labels in this array. Random colors assigned, unique per label.",
optional: true
});
const layoutParam = coda.makeParameter({
type: coda.ParameterType.String,
name: "layout",
description: "'dot', 'fdp', 'neato', 'circo', 'twopi', 'osage', 'patchwork'",
optional: true,
defaultValue: "dot"
});
const allowOverlapParam = coda.makeParameter({
type: coda.ParameterType.Boolean,
name: "allowOverlap",
description: "By default the layout engine will try not to have overlap. But you can further force no overlap",
optional: true,
defaultValue: true
});
pack.addFormula({
// This is the name that will be called in the formula builder. Remember, your formula name cannot have spaces in it.
name: "GraphNodes",
description: "Generate a node graph image. More documentation: https://quickchart.io/documentation/graphviz-api/",
parameters: [nodesParam, graphTypeParam, edgesParam, colorNodesParam, layoutParam, allowOverlapParam, formatParam, heightParam, widthParam],
execute: async function ([nodes, graphType, edges, colorNodes, layout, allowOverlap, format, height, width], helper) {
var edge = "--";
if (graphType == 'directed') {
edge = "->";
graphType = "digraph";
} else {
graphType = "graph";
}
var url = "https://quickchart.io/graphviz?";
if (layout != null && layout.length > 0) {
url += "layout=" + layout + "&";
}
url += "graph=" + graphType + "{";
var rows = ""
var colorList = colors.map((x) => x);
var colorMap = {};
for (var i = 0; i < nodes.length; i++) {
var rowLabel = nodes[i];
// Slice creates a copy: https://reactgo.com/javascript-string-copy/
var rowID = rowLabel.slice().replace(" ", "");
var rowEdges = edges[i];
var edgeList = rowEdges.replace(" ", "").split(',');
for (var edgeId = 0; edgeId < edgeList.length; edgeId++) {
if (edgeList[edgeId].length > 0) {
rows += rowID + edge + edgeList[edgeId] + ";";
}
}
rows += rowID + " [label=\"" + rowLabel + "\"";
if (colorNodes != null && colorNodes.length > 0 && colorNodes[i] != null && colorNodes[i].length > 0 && colorNodes[i].trim().length > 0) {
var colorID = colorNodes[i].slice().replace(" ", "");
var color;
if (colorID in colorMap) {
color = colorMap[colorID];
} else {
color = colorList.pop();
colorMap[colorID] = color;
if (colorList.length == 0) {
// re-upp the color list if empty.
colorList = colors.map((x) => x);
}
}
rows += ' style=filled,color=\"' + color + '\"';
}
rows += "];";
}
var overlap = "";
if (!allowOverlap) {
overlap = "overlap=false;"
}
url = url + rows + overlap + "}&";
if (format != null && format.length > 0) {
url += "format=" + format + "&";
}
if (height != null) {
url += "height=" + height + "&";
}
if (width != null) {
url += "width=" + width + "&";
}
// Trim last '&'
url = url.substr(0, url.length - 1);
return url;
},
resultType: coda.ValueType.String,
codaType: coda.ValueHintType.ImageAttachment
});
const genericChartTypeParam = coda.makeParameter({
type: coda.ParameterType.String,
name: "chartType",
description: "Chart type (line, bar, radar, doughnut, pie, bubble, scatter, violin, horizontalViolin, sparkline, progressBar, radialGauge)"
});
const xAxis = coda.makeParameter({
type: coda.ParameterType.StringArray,
name: "xaxis",
description: "X-axis labels labels",
optional: true
});
const genericChartBackgroundColorParam = coda.makeParameter({
type: coda.ParameterType.String,
name: "backgroundColor",
description: "Background of the chart canvas. Accepts rgb (rgb(255,255,120)), colors (red), and url-encoded hex values (%23ff00ff)",
optional: true,
defaultValue: "transparent"
});
const datasetNameParam = coda.makeParameter({
type: coda.ParameterType.String,
name: "datasetName",
description: "Name for this column",
optional: true
});
const datasetDataParam = coda.makeParameter({
type: coda.ParameterType.NumberArray,
name: "data",
description: "Array of data",
optional: true
});
pack.addFormula({
name: "Chart",
description: "Generate a generic Chart image. More documentation: https://quickchart.io/documentation/#parameters",
parameters: [genericChartTypeParam, xAxis, formatParam, widthParam, heightParam, genericChartBackgroundColorParam],
varargParameters: [datasetNameParam, datasetDataParam],
execute: async function (inputs, helper) {
var base_url = "https://quickchart.io/chart?c=";
var datasets = [];
for (var datasetIndex = this.parameters.length; datasetIndex < inputs.length; datasetIndex += this.varargParameters.length) {
datasets.push({"label": inputs[datasetIndex], "data": inputs[datasetIndex + 1]});
}
var data = {"labels": inputs[1], "datasets": datasets};
var params = {"type": inputs[0], "data": data};
return base_url + JSON.stringify(params, null, 2);
},
resultType: coda.ValueType.String,
codaType: coda.ValueHintType.ImageAttachment,
cacheTtlSecs: 0,
});
// https://graphviz.org/doc/info/colors.html
var colors = ["aqua", "azure", "beige", // "black",
"blue", "brown", "cyan", "darkblue", "darkcyan", "darkgrey", "darkgreen", "darkkhak", "darkmagenta", "darkolivegreenf", "darkorang", "darkorchid", "darkred", "darksalmon", "darkviolet", "fuchsia", "gold", "green", "indigo", "khaki", "lightblue", "lightcyan", "lightgreen", "lightgrey", "lightpink", "lightyellow", "lime", "magenta", "maroon", "navy", "olive", "orange", "pink", "purple", "violet", "red", "silver", // "white",
"yellow", "palegoldenrod", "palegreen", "paleturquoise", "palevoiletred1", "skyblue1", "slategray1", "seashell1", "lightslateblue", "darkseagreen", "peachpuff", "plum1", "plum3", "khaki1",];
// https://blog.trannhat.xyz/generate-a-hash-from-string-in-javascript/
function hashCode(s) {
return Math.abs(s.split('').reduce((a, b) => {
a = ((a << 5) - a) + b.charCodeAt(0);
return a & a
}, 0));
};
// Returns in 0-1 range compoment.
function hexToR(h) {
return parseInt((cutHex(h)).substring(0, 2), 16) / 255.0
}
function hexToG(h) {
return parseInt((cutHex(h)).substring(2, 4), 16) / 255.0
}
function hexToB(h) {
return parseInt((cutHex(h)).substring(4, 6), 16) / 255.0
}
function cutHex(h) {
return (h.charAt(0) == "#") ? h.substring(1, 7) : h
}