diff --git a/assets/js/dashboard/stats/graph/graph-util.js b/assets/js/dashboard/stats/graph/graph-util.js index 1b6b58e63..539d90545 100644 --- a/assets/js/dashboard/stats/graph/graph-util.js +++ b/assets/js/dashboard/stats/graph/graph-util.js @@ -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)) }