Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: allow blank cumulative values (DHIS2-18976) #1760

Merged
merged 11 commits into from
Feb 25, 2025
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
import { VIS_TYPE_COLUMN } from '../../../../../modules/visTypes.js'
import getCumulativeData from '../getCumulativeData.js'

const testData = [
{
data: [1, 1, 1],
accData: [1, 2, 3],
},
{
data: [1.5, 2.5, 3.5],
accData: [1.5, 4, 7.5],
},
{
data: [-1, -1, -1],
accData: [-1, -2, -3],
},
{
data: [null, 1],
accData: [null, 1],
},
{
data: [1, null],
accData: [1, null],
},
{
data: [null, null],
accData: [null, null],
},
{
data: [1, null, 2],
accData: [1, null, 3],
},
]

describe('getDefaultCumulativeData', () => {
const getDefaultSeries = (test, prop) => [
{
data: test[prop],
},
]

test('series get correct cumulative values', () => {
testData.forEach((test) => {
expect(
getCumulativeData(getDefaultSeries(test, 'data'), {})
).toEqual(getDefaultSeries(test, 'accData'))
})
})
})

describe('getTwoCategoryCumulativeData', () => {
const getTwoCategorySeries = (test, prop) => [
{
data: test[prop].map((value, index) => [index, value]),
custom: {
data: [test[prop]],
},
},
]

const layout = {
type: VIS_TYPE_COLUMN,
rows: [{}, {}],
}

test('series get correct cumulative values', () => {
testData.forEach((test) => {
expect(
getCumulativeData(getTwoCategorySeries(test, 'data'), layout)
).toEqual(getTwoCategorySeries(test, 'accData'))
})
})
})
Original file line number Diff line number Diff line change
Expand Up @@ -3,74 +3,76 @@ import { isTwoCategoryChartType } from '../../../../modules/visTypes.js'
import getTwoCategorySplitSerieData from './getTwoCategorySplitSerieData.js'

function getDefaultCumulativeData(series) {
let decimals = 0
let cumulativeValues = []
let decimals
let accValue
let accData

series.forEach((seriesObj) => {
cumulativeValues = seriesObj.data.reduce(
(accumulator, value, index) => {
decimals = Math.max(decimals, numberDecimals(value))

if (index > 0) {
value += accumulator[index - 1]
}
decimals = 0
accValue = null

accumulator.push(value)
accData = seriesObj.data.reduce((accSeriesData, value) => {
if (value === null) {
accSeriesData.push(value)
} else {
decimals = Math.max(decimals, numberDecimals(value))
accValue += value
accSeriesData.push(accValue)
}

return accumulator
},
[]
)
return accSeriesData
}, [])

// round values to the largest number of decimals found in the serie
// this is to avoid the floating-point problems in JavaScript
// the condition in the return statement is because sometimes value can be null
seriesObj.data = cumulativeValues.map((value) => {
return value ? parseFloat(value.toFixed(decimals)) : value
})

decimals = 0
seriesObj.data = accData.map((value) =>
value ? parseFloat(value.toFixed(decimals)) : value
)
})

return series
}

function getTwoCategoryCumulativeData(series) {
let decimals = 0
let decimals
let accValue
let accSeriesData
let accSeriesDataByCategory

series
.filter((seriesObj) => !seriesObj.custom.isTwoCategoryFakeSerie)
.forEach((seriesObj) => {
const cumulativeValues = []

seriesObj.custom.data.forEach((groupObj) => {
const cumulativeGroupValues = []

groupObj.forEach((value, index) => {
decimals = Math.max(decimals, numberDecimals(value))

if (index > 0) {
value += cumulativeGroupValues[index - 1]
decimals = 0
accSeriesData = []

seriesObj.custom.data.forEach((seriesDataByCategory) => {
accValue = null
accSeriesDataByCategory = []

seriesDataByCategory.forEach((value) => {
if (value === null) {
accSeriesDataByCategory.push(value)
} else {
decimals = Math.max(decimals, numberDecimals(value))
accValue += value
accSeriesDataByCategory.push(accValue)
}

cumulativeGroupValues.push(value)
})

cumulativeValues.push(cumulativeGroupValues)
accSeriesData.push(accSeriesDataByCategory)
})

// round values to the largest number of decimals found in the serie
// this is to avoid the floating-point problems in JavaScript
// the condition in the return statement is because sometimes value can be null
seriesObj.custom.data = cumulativeValues.map((groupObj) =>
groupObj.map((value) =>
seriesObj.custom.data = accSeriesData.map((seriesDataByCategory) =>
seriesDataByCategory.map((value) =>
value ? parseFloat(value.toFixed(decimals)) : value
)
)

seriesObj.data = getTwoCategorySplitSerieData(seriesObj.custom.data)

decimals = 0
})

return series
Expand Down
Loading