Skip to content

Commit

Permalink
wip: Allow to specify a target unit in KDataTable and KTimeSeriesChart
Browse files Browse the repository at this point in the history
  • Loading branch information
cnouguier committed Jul 9, 2024
1 parent a151ff5 commit 96125cc
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 16 deletions.
29 changes: 16 additions & 13 deletions core/client/components/chart/KTimeSeriesChart.vue
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,9 @@ async function onCanvasRef (ref) {
function getUnit (timeSerie) {
return _.get(timeSerie, 'variable.unit')
}
function getTargetUnit (timeSerie) {
return _.get(timeSerie, 'variable.targetUnit')
}
function setUnit (timeserie, targetUnit) {
_.set(timeserie, 'variable.unit', targetUnit)
}
Expand Down Expand Up @@ -136,10 +139,9 @@ async function makeChartConfig () {
return (x ? `${Time.format(x, 'date.short')} - ${Time.format(x, 'time.long')}` : '')
},
label: (context) => {
const { unit, label } = context.dataset
const defaultUnit = Units.getDefaultUnit(unit)
const { unit, targetUnit, label } = context.dataset
const y = _.get(context, 'parsed.y')
return label + ': ' + Units.format(y, defaultUnit)
return label + ': ' + Units.format(y, targetUnit?.name || unit.name, targetUnit?.name || unit.name)
}
}
},
Expand Down Expand Up @@ -245,17 +247,17 @@ function makeScales () {
let axisId = 0
for (const timeSerie of props.timeSeries) {
const unit = getUnit(timeSerie)
const defaultUnit = Units.getDefaultUnit(unit)
if (!unit2axis.has(defaultUnit)) {
const targetUnit = getTargetUnit(timeSerie) || unit
if (!unit2axis.has(targetUnit.name)) {
const axis = `y${axisId}`
unit2axis.set(defaultUnit, axis)
unit2axis.set(targetUnit.name, axis)
scales[axis] = _.merge({
targetUnit: defaultUnit,
targetUnit: targetUnit.name,
type: props.logarithmic ? 'logarithmic' : 'linear',
position: (axisId + 1) % 2 ? 'left' : 'right',
title: {
display: true,
text: defaultUnit
text: i18n.tie(targetUnit.symbol)
},
ticks: {
callback: function (value, index, values) {
Expand All @@ -279,23 +281,24 @@ async function makeDatasets () {
for (const timeSerie of props.timeSeries) {
const label = _.get(timeSerie, 'variable.label')
const unit = getUnit(timeSerie)
const defaultUnit = Units.getDefaultUnit(unit)
setUnit(timeSerie, defaultUnit)
const targetUnit = getTargetUnit(timeSerie)
if (targetUnit) setUnit(timeSerie, targetUnit)
const data = await timeSerie.data
const dataset = Object.assign({
label,
data,
unit,
yAxisID: unit2axis.get(unit)
targetUnit,
yAxisID: unit2axis.get(unit.name)
}, _.omit(_.get(timeSerie, 'variable.chartjs', {}), 'yAxis'))
const xAxisKey = _.get(dataset, 'parsing.xAxisKey', props.xAxisKey)
const yAxisKey = _.get(dataset, 'parsing.yAxisKey', props.yAxisKey)
// Update time/value range
data.forEach(item => {
const time = moment.utc(_.get(item, xAxisKey))
let value = _.get(item, yAxisKey)
if (unit !== defaultUnit) {
value = Units.convert(value, unit, defaultUnit)
if (targetUnit) {
value = Units.convert(value, unit.name, targetUnit.name)
_.set(item, yAxisKey, value)
}
if (_.isNil(min) || (value < min)) min = value
Expand Down
6 changes: 3 additions & 3 deletions core/client/utils/utils.data.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,16 @@ export function convertData (data, valuePaths, sourceUnit, targetUnit) {
_.forEach(data, document => {
_.forEach(valuePaths, valuePath => {
const value = _.get(document, valuePath)
if (value) _.set(document, valuePath, Units.convert(value, sourceUnit, targetUnit))
if (value) _.set(document, valuePath, Units.convert(value, sourceUnit.name, targetUnit.name))
})
})
}

export function convertTimeSerie (data, variable, valuePaths) {
if (!Array.isArray(valuePaths)) valuePaths = [valuePaths]
const unit = variable.unit
if (unit) {
const targetUnit = Units.getDefaultUnit(unit)
const targetUnit = variable.targetUnit || Units.getDefaultUnit(unit.name)
if (unit.name !== targetUnit.name) {
convertData(data, valuePaths, unit, targetUnit)
variable.unit = targetUnit
}
Expand Down

0 comments on commit 96125cc

Please sign in to comment.