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

How to format a numerical value? #40

Open
valterc opened this issue Jan 7, 2025 · 3 comments
Open

How to format a numerical value? #40

valterc opened this issue Jan 7, 2025 · 3 comments
Labels
documentation Improvements or additions to documentation question Further information is requested

Comments

@valterc
Copy link

valterc commented Jan 7, 2025

Hi,

I have a datatype that emits a single double value, for example:

    <DataType
        description="Example datatype."
        displayName="Example datatype"
        typeId="DATATYPE_EXAMPLE" />
class ExampleDataType(extension: String) : DataTypeImpl(extension, "DATATYPE_EXAMPLE") {

  override fun startStream(emitter: Emitter<StreamState>) {
    val job = CoroutineScope(Dispatchers.IO).launch {
                while (true) {
                    emitter.onNext(
                            StreamState.Streaming(
                                DataPoint(
                                    dataTypeId,
                                    values = mapOf(DataType.Field.SINGLE to random() * 10),
                                )
                            )
                        )
                    delay(5_000)
                }
            }
    
    emitter.setCancellable {
                job.cancel()
    }
  }

}

If I add this datatype to a profile, only the integer part of the value is displayed (example: 5), no decimal places are presented (example: 5.37). Is there a way to 'format' the value or do we need to use a graphical datatype with a custom view?

I also saw that we can emit an UpdateGraphicConfig with a formatDataTypeId, but it's a bit unclear what do we need to provide in this parameter, tried with SPEED and DISTANCE, but no luck.

    override fun startView(context: Context, config: ViewConfig, emitter: ViewEmitter) {
        emitter.onNext(UpdateGraphicConfig(formatDataTypeId = DataType.Type.SPEED))
    }

Thanks.

@brentnd
Copy link
Member

brentnd commented Jan 7, 2025

Try with UpdateNumericConfig with DataType.Type.INTENSITY_FACTOR as an experiment. This field uses 2 digits of precision with no value conversion (speed and distance will try to go from meters to the appropriate miles/km based on user profile).

@valterc
Copy link
Author

valterc commented Jan 7, 2025

That one works, showing the two decimal places. Thank you!

(It would be great if there was some docs/examples explaining the different behaviors when using the different types. In the future, maybe even allowing for custom format strings format="#.00", format="00.0", ...)

@brentnd brentnd added question Further information is requested documentation Improvements or additions to documentation labels Jan 9, 2025
@refi-git
Copy link

Thanks for reporting, I thought only me had this issue. :D
The workaround works, thanks @brentnd

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation question Further information is requested
Projects
None yet
Development

No branches or pull requests

3 participants