Skip to content

Commit

Permalink
feat: KDataTable must display number using the default unit (close #882)
Browse files Browse the repository at this point in the history
  • Loading branch information
cnouguier committed Jun 19, 2024
1 parent f50aa0e commit 85dbb9e
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 7 deletions.
14 changes: 10 additions & 4 deletions core/client/components/chart/KDataTable.vue
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,11 @@ import _ from 'lodash'
import moment from 'moment'
import Papa from 'papaparse'
import { ref, watch } from 'vue'
import { downloadAsBlob } from '../../utils'
import { downloadAsBlob, convertTimeSerie } from '../../utils'
import { useSchema } from '../../composables'
import { Units } from '../../units'
import { Time } from '../../time'
import { i18n } from '../../i18n'
import { Units } from '../../units.js'
import { Time } from '../../time.js'
import { i18n } from '../../i18n.js'
// Props
const props = defineProps({
Expand Down Expand Up @@ -71,6 +71,7 @@ const height = ref(0)
// Used to store template compilers per field
const compilers = {}
const exportCompilers = {}
let propertiesToConvert = []
// Watch
watch(() => props.tables, update)
Expand All @@ -81,11 +82,14 @@ async function update () {
await compile(props.schema)
columns.value = []
const invisibleColumns = []
propertiesToConvert = []
_.forOwn(schema.value.properties, (value, key) => {
const type = _.get(value, 'type')
// FIXME: allow for custom representation of complex objects
if (type === 'object') return
const label = _.get(value, 'field.label', _.get(value, 'field.helper', key))
const convertToDefaultUnit = _.get(value, 'field.defaultUnit', false)
if (convertToDefaultUnit) propertiesToConvert.push(key)
const visible = _.get(value, 'field.visible', true)
if (!visible) invisibleColumns.push(key)
const formatter = _.has(value, 'field.formatter') ? _.get(props.formatters, value.field.formatter) : null
Expand Down Expand Up @@ -131,6 +135,7 @@ async function update () {
rows.value = []
for (const table of props.tables) {
const data = await table.data
convertTimeSerie(data, table.variable, propertiesToConvert)
rows.value = rows.value.concat(data)
}
}
Expand All @@ -143,6 +148,7 @@ async function exportData (options = {}) {
for (let i = 0; i < props.tables.length; i++) {
const table = props.tables[i]
const data = await table.data
await convertTimeSerie(data, table.variable, propertiesToConvert)
for (const item of data) {
const row = {}
_.forOwn(schema.value.properties, (value, key) => {
Expand Down
6 changes: 4 additions & 2 deletions core/client/units.js
Original file line number Diff line number Diff line change
Expand Up @@ -322,10 +322,12 @@ export const Units = {
// If target unit is not specified will use default unit (if any) for source unit
convert (value, sourceUnit, targetUnit) {
if (_.isNil(value)) {
logger.warn('[KDK] cannont convert an nil value')
logger.warn('[KDK] cannont convert a nil value')
return
}
}
if (value === Number.MIN_VALUE || value === Number.MAX_VALUE) return value
// If target unit is same as source unit does nothing
if (targetUnit === sourceUnit) return value
// If target unit is not given use default one
if (!targetUnit) targetUnit = this.getDefaultUnit(sourceUnit)
// Check if the target unit does exist
Expand Down
4 changes: 3 additions & 1 deletion core/client/utils/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,18 @@ import { Notify, Dialog, Loading, exportFile } from 'quasar'
import { defineAsyncComponent, markRaw } from 'vue'

export * from './utils.account.js'
export * from './utils.actions.js'
export * from './utils.colors.js'
export * from './utils.content.js'
export * from './utils.data.js'
export * from './utils.locale.js'
export * from './utils.platform.js'
export * from './utils.push.js'
export * from './utils.pwa.js'
export * from './utils.shapes.js'
export * from './utils.session.js'
export * from './utils.time.js'
export * from './utils.actions.js'


Notify.setDefaults({
position: 'bottom-left',
Expand Down
22 changes: 22 additions & 0 deletions core/client/utils/utils.data.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import _ from 'lodash'
import { Units } from '../units.js'

export function convertData (data, valuePaths, sourceUnit, targetUnit) {
if (!Array.isArray(valuePaths)) valuePaths = [valuePaths]
_.forEach(data, document => {
_.forEach(valuePaths, valuePath => {
const value = _.get(document, valuePath)
if (value) _.set(document, valuePath, Units.convert(value, sourceUnit, targetUnit))
})
})
}

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

0 comments on commit 85dbb9e

Please sign in to comment.