-
Notifications
You must be signed in to change notification settings - Fork 76
/
comboChartHelpers.js
316 lines (295 loc) · 9.56 KB
/
comboChartHelpers.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
import { useMemo } from 'react';
import { isEmpty, isNil, merge, omit, filter, capitalize } from 'lodash-es';
import {
chartValueFormatter,
getUpdatedCardSize,
handleTooltip,
} from '../../utils/cardUtilityFunctions';
import { CHART_COLORS } from '../../constants/CardPropTypes';
import { formatGraphTick } from '../TimeSeriesCard/timeSeriesUtils';
import { CARD_SIZES, TIME_SERIES_TYPES } from '../../constants/LayoutConstants';
import dayjs from '../../utils/dayjs';
/**
* Formats and maps the colors to their corresponding datasets in the carbon charts tabular data format
* @param {Array} series an array of dataset group classifications
* @returns {Object} colors - formatted
*/
const formatColors = (series = []) => {
const colors = {
scale: {},
};
if (Array.isArray(series)) {
series.forEach((dataset, index) => {
colors.scale[dataset.label] = dataset.color || CHART_COLORS[index % CHART_COLORS.length];
});
} else {
colors.scale[series.label] = series.color || CHART_COLORS[0];
}
return colors;
};
/**
* Configures the axes object for the comboChart
*/
const configureAxes = (content) => {
const {
domainRange,
xLabel,
yLabel,
unit,
chartType,
decimalPrecision,
includeZeroOnXaxis,
interval,
locale,
series,
showTimeInGMT,
size,
includeZeroOnYaxis,
previousTick,
thresholds,
} = content;
/** *
* timestamp of current value
* index of current value
* ticks: array of current ticks or a single string with part of the date, e.g. month or year & month
*/
const formatTick = (timestamp, index, ticks) => {
const previousTimestamp = previousTick.current;
// store current in the previous tick
previousTick.current = timestamp;
return formatGraphTick(
timestamp,
index,
ticks,
interval,
locale,
previousTimestamp,
showTimeInGMT
);
};
// Checks size property against new size naming convention and reassigns to closest supported size if necessary.
const newSize = getUpdatedCardSize(size);
const getMaxTicksPerSize = (s) => {
switch (s) {
case CARD_SIZES.MEDIUMTHIN:
return 2;
case CARD_SIZES.MEDIUM:
return 4;
case CARD_SIZES.MEDIUMWIDE:
case CARD_SIZES.LARGE:
return 6;
case CARD_SIZES.LARGEWIDE:
return 14;
default:
return 10;
}
};
return {
bottom: {
title: xLabel || ' ',
mapsTo: 'date',
scaleType: 'time',
ticks: {
max: getMaxTicksPerSize(newSize),
formatter: formatTick,
},
includeZero: includeZeroOnXaxis,
...(domainRange ? { domain: domainRange } : {}),
},
left: {
title: `${yLabel || ''} ${unit ? `(${unit})` : ''}`,
mapsTo: 'value',
ticks: {
formatter: (axisValue) =>
chartValueFormatter(axisValue, newSize, null, locale, decimalPrecision),
},
stacked: chartType === TIME_SERIES_TYPES.BAR && series.length > 1,
includeZero: includeZeroOnYaxis,
scaleType: 'linear',
thresholds,
},
};
};
/**
* Translates our raw data into a language the carbon-charts understand
* @param {string} timeDataSourceId, the field that identifies the timestamp value in the data
* @param {array} series, an array of lines to create in our chart
* @param {array} values, the array of values from our data layer
*
* TODO: Handle empty data lines gracefully and notify the user of data lines that did not
* match the dataFilter
*
* @returns {object} with a labels array and a datasets array
*/
export const useChartData = (values, { series = [], timeDataSourceId, showTimeInGMT }) => {
// Carbon charts is incredibly slow with large datasets sometimes, so let's shave off a little time where we can
return useMemo(() => {
// Generate a set of unique timestamps for the values
const timestamps = [...new Set(values.map((val) => val[timeDataSourceId]))];
const data = [];
const seriesArray = Array.isArray(series) ? series : [series];
// Series is the different groups of datasets
seriesArray.forEach(({ dataSourceId, dataFilter = {}, label }) => {
timestamps.forEach((timestamp) => {
// First filter based on on the dataFilter
const filteredData = filter(values, dataFilter);
if (!isEmpty(filteredData)) {
// have to filter out null values from the dataset, as it causes Carbon Charts to break
filteredData
.filter((dataItem) => {
// only allow valid timestamp matches
return !isNil(dataItem[timeDataSourceId]) && dataItem[timeDataSourceId] === timestamp;
})
.forEach((dataItem) => {
// Check to see if the data Item actually exists in this timestamp before adding to data (to support sparse data in the values)
if (dataItem[dataSourceId]) {
data.push({
date:
dataItem[timeDataSourceId] instanceof Date
? dataItem[timeDataSourceId]
: showTimeInGMT
? dayjs.utc(dataItem[timeDataSourceId])
: dayjs(dataItem[timeDataSourceId]),
value: dataItem[dataSourceId],
group: label,
});
}
});
}
});
});
return data;
}, [values, series, timeDataSourceId, showTimeInGMT]);
};
export const useChartOptions = (content) => {
return useMemo(() => {
const {
comboChartTypes,
decimalPrecision,
i18n,
isEditable,
isLoading,
locale,
series,
showTimeInGMT,
size,
timeDataSourceId,
tooltipDateFormatPattern,
defaultDateFormatPattern,
unit,
values,
} = content;
const options = {
experimental: true,
animations: false,
accessibility: true,
axes: configureAxes(content),
legend: {
position: 'bottom',
enabled: values.length > 1,
clickable: !isEditable,
},
containerResizable: true,
comboChartTypes,
series,
color: formatColors(series),
timeScale: {
addSpaceOnEdges: 0,
},
tooltip: {
valueFormatter: (tooltipValue) =>
chartValueFormatter(tooltipValue, size, unit, locale, decimalPrecision),
customHTML: (...args) =>
handleTooltip(...args, timeDataSourceId, showTimeInGMT, tooltipDateFormatPattern, locale),
groupLabel: i18n.tooltipGroupLabel,
totalLabel: i18n.tooltipTotalLabel,
},
toolbar: {
enabled: false,
},
defaultDateFormatPattern,
};
options.data = merge(options.data, { loading: isLoading });
return { ...options, ...content };
}, [content]);
};
const extractDataAndColumnNames = (values, chartOptions) => {
const { timeDataSourceId, showTimeInGMT, defaultDateFormatPattern } = chartOptions;
let maxColumnNames = [];
const tableValues = values.map((value, index) => {
const currentValueColumns = Object.keys(omit(value, timeDataSourceId));
maxColumnNames =
currentValueColumns.length > maxColumnNames.length ? currentValueColumns : maxColumnNames;
return {
id: `dataindex-${index}`,
values: {
...omit(value, timeDataSourceId), // skip the timestamp so we can format it locally
[timeDataSourceId]: (showTimeInGMT
? dayjs.utc(value[timeDataSourceId])
: dayjs(value[timeDataSourceId])
).format(defaultDateFormatPattern),
},
isSelectable: false,
};
});
return { tableData: tableValues, columnNames: maxColumnNames };
};
export const useTableData = (values, chartOptions) => {
const { tableData } = useMemo(() => extractDataAndColumnNames(values, chartOptions), [
values,
chartOptions,
]);
return tableData;
};
export const useTableColumns = (values, chartOptions) => {
const { timeDataSourceId, decimalPrecision, i18n, size, series, unit, locale } = chartOptions;
const { columnNames } = extractDataAndColumnNames(values, chartOptions);
// In expanded mode we show the data underneath the linechart in a table so need to build the columns
const tableColumns = useMemo(() => {
// First column is timestamp
const columns = [
{
id: timeDataSourceId,
name: capitalize(timeDataSourceId),
isSortable: true,
type: 'TIMESTAMP',
},
];
// then the rest in series order
return columns.concat(
columnNames.map((columnName) => {
const matchingDataSource = Array.isArray(series)
? series.find((d) => d.dataSourceId === columnName)
: series;
return {
id: columnName,
// use the label if one exists as it will be the user-defined, readable name
// UNLESS dataFilter is enabled as the matchingDataSource will only find the first match
name: matchingDataSource
? matchingDataSource.dataFilter
? matchingDataSource.dataSourceId
: matchingDataSource.label
: columnName,
isSortable: true,
filter: { placeholderText: i18n.defaultFilterStringPlaceholdText },
renderDataFunction: ({ value }) => {
if (typeof value === 'number' && !isNil(decimalPrecision)) {
return chartValueFormatter(value, size, unit, locale, decimalPrecision);
}
return value;
},
};
})
);
}, [
columnNames,
decimalPrecision,
i18n.defaultFilterStringPlaceholdText,
locale,
series,
size,
timeDataSourceId,
unit,
]);
return tableColumns;
};