markdown-up 1.5.0 → 1.7.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/lib/model.js ADDED
@@ -0,0 +1,298 @@
1
+ // Licensed under the MIT License
2
+ // https://github.com/craigahobbs/markdown-up/blob/main/LICENSE
3
+
4
+ /** @module lib/model */
5
+
6
+ import {SchemaMarkdownParser} from 'schema-markdown/lib/parser.js';
7
+ import {validateType} from 'schema-markdown/lib/schema.js';
8
+
9
+
10
+ /**
11
+ * Validate a bar chart model
12
+ *
13
+ * @param {Object} barChart - The bar chart model
14
+ * @returns {Object}
15
+ */
16
+ export function validateBarChart(barChart) {
17
+ return validateType(chartModel.types, 'BarChart', barChart);
18
+ }
19
+
20
+
21
+ /**
22
+ * Validate a data table model
23
+ *
24
+ * @param {Object} dataTable - The data table model
25
+ * @returns {Object}
26
+ */
27
+ export function validateDataTable(dataTable) {
28
+ return validateType(chartModel.types, 'DataTable', dataTable);
29
+ }
30
+
31
+
32
+ /**
33
+ * Validate a line chart model
34
+ *
35
+ * @param {Object} lineChart - The line chart model
36
+ * @returns {Object}
37
+ */
38
+ export function validateLineChart(lineChart) {
39
+ return validateType(chartModel.types, 'LineChart', lineChart);
40
+ }
41
+
42
+
43
+ /**
44
+ * The chart model defined using Schema Markdown
45
+ */
46
+ export const chartModelSmd = `\
47
+ # Base struct for all chart types
48
+ struct ChartBase
49
+
50
+ # The data specification
51
+ Data data
52
+
53
+ # The map of variable name to variable expression
54
+ optional string{len > 0} var
55
+
56
+ # The calculated fields
57
+ optional CalculatedField[len > 0] calc
58
+
59
+ # The data row boolean filter expression. Omit any row that does not match.
60
+ optional string filter
61
+
62
+ # The data aggregation specification
63
+ optional Aggregation agg
64
+
65
+ # The post-aggregation calculated fields
66
+ optional CalculatedField[len > 0] aggcalc
67
+
68
+ # The data's sort specification
69
+ optional SortField[len > 0] sort
70
+
71
+ # The data's top specification
72
+ optional Top top
73
+
74
+ # The numeric formatting precision (default is 2)
75
+ optional int(>= 0) precision
76
+
77
+ # The datetime format
78
+ optional DatetimeFormat datetime
79
+
80
+
81
+ # A chart data specification
82
+ struct Data
83
+
84
+ # The data resource URL. The data resource is formatted either as a CSV or as a JSON array of row objects.
85
+ string url
86
+
87
+ # Data joins
88
+ optional DataJoin[len > 0] join
89
+
90
+
91
+ # A data join specification
92
+ struct DataJoin
93
+
94
+ # The data resource URL. The data resource is formatted either as a CSV or as a JSON array of row objects.
95
+ string url
96
+
97
+ # If true, the join is a left join (default is right join)
98
+ optional bool leftJoin
99
+
100
+ # The join expression for the left row
101
+ string left
102
+
103
+ # The join expression for the right row
104
+ optional string right
105
+
106
+
107
+ # A datetime format
108
+ enum DatetimeFormat
109
+
110
+ # ISO datetime year format
111
+ Year
112
+
113
+ # ISO datetime month format
114
+ Month
115
+
116
+ # ISO datetime day format
117
+ Day
118
+
119
+
120
+ # A calculated field specification
121
+ struct CalculatedField
122
+
123
+ # The calculated field name
124
+ string name
125
+
126
+ # The calculation expression
127
+ string expr
128
+
129
+
130
+ # A data aggregation specification
131
+ struct Aggregation
132
+
133
+ # The aggregation category fields
134
+ string[len > 0] category
135
+
136
+ # The aggregation measures
137
+ AggregationMeasure[len > 0] measure
138
+
139
+
140
+ # An aggregation measure specification
141
+ struct AggregationMeasure
142
+
143
+ # The aggregated-measure field name
144
+ optional string name
145
+
146
+ # The aggregation measure field
147
+ string field
148
+
149
+ # The aggregation function
150
+ AggregationFunction func
151
+
152
+
153
+ # An aggregation function
154
+ enum AggregationFunction
155
+
156
+ # The average of the measure's values
157
+ Average
158
+
159
+ # The count of the measure's values
160
+ Count
161
+
162
+ # The greatest of the measure's values
163
+ Max
164
+
165
+ # The least of the measure's values
166
+ Min
167
+
168
+ # The sum of the measure's values
169
+ Sum
170
+
171
+
172
+ # A sort field specification
173
+ struct SortField
174
+
175
+ # The field to sort by
176
+ string field
177
+
178
+ # If true, sort this field in descending order
179
+ optional bool desc
180
+
181
+
182
+ # A data top specification
183
+ struct Top
184
+
185
+ # The maximum number of rows to keep per category
186
+ optional int(> 0) count
187
+
188
+ # The category fields
189
+ optional string[len > 0] category
190
+
191
+
192
+ # Base struct for line and bar charts
193
+ struct ChartCommon
194
+
195
+ # The chart title expression
196
+ optional string title
197
+
198
+ # The chart width expression
199
+ optional string width
200
+
201
+ # The chart height expression
202
+ optional string height
203
+
204
+
205
+ # A bar chart specification
206
+ struct BarChart (ChartCommon, ChartBase)
207
+
208
+ # The bar measure fields
209
+ string[len > 0] measure
210
+
211
+ # The bar category fields
212
+ optional string[len > 0] bar
213
+
214
+ # The category fields
215
+ optional string[len > 0] category
216
+
217
+ # The color encoding field
218
+ optional string[len > 0] color
219
+
220
+ # If true, the bar chart is rendered "reversed". The default is false.
221
+ optional bool reverse
222
+
223
+
224
+ # A data table specification
225
+ struct DataTable (ChartBase)
226
+
227
+ # The table's category fields
228
+ optional string[len > 0] category
229
+
230
+ # The table's fields
231
+ optional string[len > 0] field
232
+
233
+ # The "categoryFields" and "fields" to be rendered as Markdown text
234
+ optional string[len > 0] markdown
235
+
236
+
237
+ # A line chart specification
238
+ struct LineChart (ChartCommon, ChartBase)
239
+
240
+ # The line chart's X-axis field
241
+ string x
242
+
243
+ # The line chart's Y-axis fields
244
+ string[len > 0] y
245
+
246
+ # The color encoding field
247
+ optional string color
248
+
249
+ # The X-axis tick mark specification
250
+ optional AxisTicks xtick
251
+
252
+ # The Y-axis tick mark specification
253
+ optional AxisTicks ytick
254
+
255
+ # The X-axis annotations
256
+ optional AxisAnnotation[len > 0] xline
257
+
258
+ # The Y-axis annotations
259
+ optional AxisAnnotation[len > 0] yline
260
+
261
+
262
+ # An axis annotation
263
+ struct AxisAnnotation
264
+
265
+ # The axis field value expression
266
+ string value
267
+
268
+ # The annotation label expression
269
+ optional string label
270
+
271
+
272
+ # Automatically-generated, evenly-spaced tick marks specification
273
+ struct AxisTicks
274
+
275
+ # The tick mark count expression. Default is 3.
276
+ optional string count
277
+
278
+ # The field value expression of the first tick mark. Default is the minimum field value.
279
+ optional string start
280
+
281
+ # The field value expression of the last tick mark. Default is the maximum field value.
282
+ optional string end
283
+
284
+ # The number of tick mark labels to skip after a rendered label
285
+ optional int(> 0) skip
286
+ `;
287
+
288
+
289
+ /**
290
+ * The chart model
291
+ *
292
+ * @property {string} title - The model's title
293
+ * @property {Object} types - The model's referenced types dictionary
294
+ */
295
+ export const chartModel = {
296
+ 'title': 'The Chart Model',
297
+ 'types': (new SchemaMarkdownParser(chartModelSmd)).types
298
+ };