Refactor: Graph Dataset functions (#2690)

* Refactor graph dataset building function

This commit refactors the graph dataset building functions into three
separate functions, one for each plot: main plot, comparison plot, and
dashed plot.

* Merge default graph dataset options

This commit moves default options from graph building functions to the
main function.

* Remove truncateToPresentIndex function

* Include dashed part when comparing periods

This commit fixes a bug where some period + interval combinations were
not working properly with comparisons. This is because `present_index`
is not available in every period, e.g. Last Month.
ua-device-type
Vini Brasil 4 years ago committed by Uku Taht
parent 813fa85a73
commit 7b7231677c

@ -34,8 +34,49 @@ export const LoadingState = {
isLoadedOrRefreshing: function (state) { return [this.loaded, this.refreshing].includes(state) }
}
const truncateToPresentIndex = function(array, presentIndex) {
return array.slice(0, presentIndex)
const buildComparisonDataset = function(comparisonPlot, presentIndex) {
if (!comparisonPlot) return []
let data = [...comparisonPlot]
if (presentIndex) {
const dashedPartIncludedIndex = presentIndex + 1
data = data.slice(0, dashedPartIncludedIndex)
}
return [{
data: data,
borderColor: 'rgba(60,70,110,0.2)',
pointBackgroundColor: 'rgba(60,70,110,0.2)',
pointHoverBackgroundColor: 'rgba(60, 70, 110)',
yAxisID: 'yComparison',
}]
}
const buildDashedDataset = function(plot, presentIndex) {
if (!presentIndex) return []
const dashedPart = plot.slice(presentIndex - 1, presentIndex + 1);
const dashedPlot = (new Array(presentIndex - 1)).concat(dashedPart)
return [{
data: dashedPlot,
borderDash: [3, 3],
borderColor: 'rgba(101,116,205)',
pointHoverBackgroundColor: 'rgba(71, 87, 193)',
yAxisID: 'y',
}]
}
const buildMainPlotDataset = function(plot, presentIndex) {
const data = presentIndex ? plot.slice(0, presentIndex) : plot
return [{
data: data,
borderColor: 'rgba(101,116,205)',
pointBackgroundColor: 'rgba(101,116,205)',
pointHoverBackgroundColor: 'rgba(71, 87, 193)',
yAxisID: 'y',
}]
}
export const buildDataSet = (plot, comparisonPlot, present_index, ctx, label) => {
@ -46,72 +87,13 @@ export const buildDataSet = (plot, comparisonPlot, present_index, ctx, label) =>
prev_gradient.addColorStop(0, 'rgba(101,116,205, 0.075)');
prev_gradient.addColorStop(1, 'rgba(101,116,205, 0)');
let comparisonDataSet = []
if (comparisonPlot) {
comparisonDataSet = [
{
label,
data: truncateToPresentIndex(comparisonPlot, present_index),
borderWidth: 3,
borderColor: 'rgba(60,70,110,0.2)',
pointBackgroundColor: 'rgba(60,70,110,0.2)',
pointHoverBackgroundColor: 'rgba(60, 70, 110)',
pointBorderColor: 'transparent',
pointHoverRadius: 4,
backgroundColor: gradient,
fill: true,
yAxisID: 'yComparison',
}
]
}
const defaultOptions = { label, borderWidth: 3, pointBorderColor: "transparent", pointHoverRadius: 4, backgroundColor: gradient, fill: true }
if (present_index) {
var dashedPart = plot.slice(present_index - 1, present_index + 1);
var dashedPlot = (new Array(present_index - 1)).concat(dashedPart)
const _plot = truncateToPresentIndex([...plot], present_index)
return [
{
label,
data: _plot,
borderWidth: 3,
borderColor: 'rgba(101,116,205)',
pointBackgroundColor: 'rgba(101,116,205)',
pointHoverBackgroundColor: 'rgba(71, 87, 193)',
pointBorderColor: 'transparent',
pointHoverRadius: 4,
backgroundColor: gradient,
fill: true,
yAxisID: 'y',
},
{
label,
data: dashedPlot,
borderWidth: 3,
borderDash: [3, 3],
borderColor: 'rgba(101,116,205)',
pointHoverBackgroundColor: 'rgba(71, 87, 193)',
pointBorderColor: 'transparent',
pointHoverRadius: 4,
backgroundColor: gradient,
fill: true,
yAxisID: 'y',
}
].concat(comparisonDataSet)
} else {
return [
{
label,
data: plot,
borderWidth: 3,
borderColor: 'rgba(101,116,205)',
pointHoverBackgroundColor: 'rgba(71, 87, 193)',
pointBorderColor: 'transparent',
pointHoverRadius: 4,
backgroundColor: gradient,
fill: true,
yAxisID: 'y',
}
].concat(comparisonDataSet)
}
const dataset = [
...buildMainPlotDataset(plot, present_index),
...buildDashedDataset(plot, present_index),
...buildComparisonDataset(comparisonPlot, present_index)
]
return dataset.map((item) => Object.assign(item, defaultOptions))
}

Loading…
Cancel
Save