Skip to content

Commit

Permalink
use common model for multiple series
Browse files Browse the repository at this point in the history
  • Loading branch information
zbigg committed Sep 6, 2023
1 parent f89589f commit 4b8bccd
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -198,39 +198,40 @@ function TimeSeriesWidgetUIContent({
const categories = [];
const colorMapping = {};

const firstSerieEntry = data[0];
if (
firstSerieEntry &&
Array.isArray(firstSerieEntry.data) &&
firstSerieEntry.category
) {
// multiple series model
// array of objects {data, category}
for (const { data: seriesDataRaw, category } of data) {
// const firstSerieEntry = data[0];
// if (
// firstSerieEntry &&
// Array.isArray(firstSerieEntry.data) &&
// firstSerieEntry.category
// ) {
// // multiple series model
// // array of objects {data, category}
// for (const { data: seriesDataRaw, category } of data) {
// categories.push(category);
// series.push({
// category,
// data: seriesDataRaw.map(({ name, value }) => [name, value])
// });
// }
// } else {

// splitByCategory model
// one array, with category embedded: { name: 1009843200000, category: 'DECEPTIVE PRACTICE', value: 6 }
for (const { name, value, category } of data) {
let dataSeriesIndex = category ? categories.indexOf(category) : 0;
if (dataSeriesIndex === -1) {
dataSeriesIndex = categories.length;
categories.push(category);
series.push({
category,
data: seriesDataRaw.map(({ name, value }) => [name, value])
});
}
} else {
// splitByCategory model
// one array, with category embedded: { name: 1009843200000, category: 'DECEPTIVE PRACTICE', value: 6 }
for (const { name, value, category } of data) {
let dataSeriesIndex = category ? categories.indexOf(category) : 0;
if (dataSeriesIndex === -1) {
dataSeriesIndex = categories.length;
categories.push(category);
}
if (!series[dataSeriesIndex]) {
series[dataSeriesIndex] = {
category,
data: []
};
}
series[dataSeriesIndex].data.push([name, value]);
if (!series[dataSeriesIndex]) {
series[dataSeriesIndex] = {
category,
data: []
};
}
series[dataSeriesIndex].data.push([name, value]);
}
// }

series.forEach(({ category }, i) => {
series[i].color = getColorByCategory(category, {
Expand Down Expand Up @@ -565,13 +566,11 @@ function defaultTooltipFormatter(unsortedParams, stepSize, valueFormatter, showN
}'></div>
</div>
<p style='line-height: 1; flex: 1; margin-left: 0.5em; min-width: 20px; max-width: 200px; overflow: hidden; text-overflow: ellipsis; align-self: left;'>
${
showNames && serie.seriesName
? `${serie.seriesName}</p>`
: ''
}
${showNames && serie.seriesName ? `${serie.seriesName}</p>` : ''}
</p>
<p style='line-height: 1; justify-self: flex-end;'>${valueFormatter(serie.data[1])}</p>
<p style='line-height: 1; justify-self: flex-end;'>${valueFormatter(
serie.data[1]
)}</p>
</div>`;
acc.push(HTML);
}
Expand Down
11 changes: 9 additions & 2 deletions packages/react-widgets/src/models/TimeSeriesModel.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ import { Methods, executeTask } from '@carto/react-workers';
import { normalizeObjectKeys, wrapModelCall } from './utils';
import { AggregationTypes } from '@carto/react-core';

export function getTimeSeries(props) {
export async function getTimeSeries(props) {
if (props.series) {
const { series, propsNoSeries } = props;
return Promise.all(
const rawSeriesData = await Promise.all(
series.map(({ operation, operationColumn }) => {
const category = getCategory(operation, operationColumn, series);
return {
Expand All @@ -19,6 +19,13 @@ export function getTimeSeries(props) {
};
})
);
let aggregatedData = [];
for (const { data, category } of rawSeriesData) {
for (const { name, value } of data) {
aggregatedData.push({ name, value, category });
}
}
return aggregatedData;
} else {
return wrapModelCall(props, fromLocal, fromRemote);
}
Expand Down

0 comments on commit 4b8bccd

Please sign in to comment.