Calculation in expression for Charts

Hi All

I would like to make some dynamic settings to a chart in OH3 - basically what the Y-Axix MIN value is.

This is so that the MIN value is dependent on the values being charted.

  yAxis:
    - component: oh-value-axis
      config:
        gridIndex: 0
        name: kWh
        min: "12000"

I would like to use an expression, something like:

min: =items.ShellyEM_MAINS_TotalEnergy.state - 5 000kWh

I however ca’t get the expression syntax correct.

=items.ShellyEM_MAINS_TotalEnergy.state

gives me: 15155.631 kWh

But not sure if (and how) I can perform the calculation.

Thanks
Mark

In the widgets all items.X.state calls return a string, even if the original item is a number type. The expression parser for the widgets has no concept of OH types and UoM so asking it to try and do math on the string 15155.631 kWh isn’t meaningful to it. For the widgets, your calculations simply have to be devoid of unit labels and cast into js number types.

In this case, your best option is the parseFloat method of the js Number object. parseFloat will read a string, starting at the first character, and go character by character until it encounters the first non-numerical character in the string at which point it converts the numerical characters already encountered into a float value and discards the rest. So you want:

min: =Number.parseFloat(items.ShellyEM_MAINS_TotalEnergy.state) - 5000

Thanks Justin.
Expression results in the correct value but when I do it that way the labels don’t work?

Is there a way to convert the result back into a string? The min: value appears to have to be a string as in “10000”. Using " " doesnt seem to help

Well…that’s odd…

I admit, I’ve never noticed this behavior before. The only places I have expressions in my axes min and max properties I’ve turned off the tick marks and labels anyway.

Yes there is =(calculation).toString(), but I doubt it’s a string vs number type issue. The echarts axis min and max values support string and numerical values (and functions, but you can’t pass those via the OH yaml system as far as I can tell). You’ll find that 10000 works just as well as "10000". It must be that the expression is returning the data in some way that is confusing echarts.

This is probably worth a github issue, because there shouldn’t, in theory, be any difference between what the expression evaluates to and a regular value.

In the meantime, if your goal is just to have the axis perfectly scaled to your data, you don’ t need an expression at all. There are built-in keywords for that.

min: dataMin
max: dataMax

will automatically set the axis min and max values to the the min and max of the data.

If it’s of critical importance that you have that particular calculated axis minimum, then you can leave the expression and see if it is possible to re-engage the axis labels via the axisLabel options.

1 Like

Thanks Justin, as usual you have helped a huge amount.

The above achieve what I was after really well, so will skip the GitHub logging - sadly I have not had much success doing that anyway.

So thank you!