Perspective Indirect formatting on a label

I am working on putting together a generic label to use with any of my analog values. On my tags, I always use the numeric properties and fill in the units and format string. I want to use this in a embedded view in perspective similar to the way I use it in Vision on a template. Is there a way to have the format bound to a parameter?

I’m sure there’s probably a better way to manage this, but this solution here works (but obviously update the path as needed for you project). This is an Expression binding on Label.props.text against a Tag in my project. It obtains the tag’s value, formatString, and engUnit and uses those pieces to build a String.

numberFormat(tag("[default]Experimentation/IntRepl"), tag("[default]Experimentation/IntRepl.formatString")) + tag("[default]Experimentation/IntRepl.engUnit")

Result:
8.00psi

If you want the format bound to a parameter instead of the format used on the Tag, you should store the format as a string in a property somewhere, and instead of using the Expression above you would replace the format portion with a reference to your custom property:

numberFormat(tag("[default]Experimentation/IntRepl"), {self.custom.formatString}) + tag("[default]Experimentation/IntRepl.engUnit")

Thank you cmallonee! That worked.

1 Like

For the sake of performance you should replace the tag() calls with indirect tag bindings - that is, make a structure in view.custom something like:

{
  "tagPath": "${some String}",
  "formatString": <indirect binding to formatString>,
  "engUnit": <indirect binding to engUnit>,
}

For best results, make these properties private (right click menu) so that the intermediate values aren’t synchronized to the front-end. Then mash them together in a numberFormat expression as Cody did above.

2 Likes

Thank you PGriffith. I had already put it in the meta properties. I didn’t know about making it private, though. I will do that.

The custom category is where we recommend you put it. There are some properties present in meta that we do not expose for public use. If you happen to provide a key in meta which matches one of the properties we’ve put in place there you could overwrite important values, or ignore security on that property and expose it in the web browser.

Is there a clean solution for this in 2022?

Why isn’t there a tickbox to enable the EngUnit in Label, Text, …?

1 Like

in the above case, did you mean tagPath, is actually the "value" returned by using indirect binding on the tagPath.

Otherwise, you mash up these items using tag() function. Which you also said not to use tag() for best practice.

that would be like
numberFormat(tagValue, formatString) + " " + engUnit
??

If you have a tag path, you can just use {tag_path} in an expression to read it and get its value. No reason to use tag() here.
The reason for the intermediate property is to build the path here if you need to build it from several pieces.
So you could use numberFormat(tagValue, formatString) or numberFormat({tagPath}, formatString).

if my tagpath = {prefix}{tagName}

Can we still do the above, without using tag()?
I tried but didn't work.. as is.

Indirect tag binding.

Yes, you build the full path somewhere else:

Or you use an indirect tag binding and then add a transform.
I didn't read the whole thing so I'm not sure about the use case, but one of those solutions is probably what you're looking for.

Yes, thanks, I was trying to not use transform as much as I could. As with this initial post. I ended with having multiple property and mash them than use transform.

I was hoping I can numberFormat({prefix}{tagName}, formatString)
So I do not have to read tag like below.
image

and then mash them up like this.


But I think this is good.
:slight_smile:

No, you need an indirect tag binding into another property. The indirection combines the prefix and the tag name and then retrieves the actual value. You need to provide the actual numeric value to numberFormat.

Yes, I did that to get the tagValue.

In the end I did, indirect binding, plus, mash them up using expression without transform.

VS (in contrast to)

indirect binding + transform.

I guess both does not differ in terms of performance.

Very little difference, true, I suspect. Unless you need that tag value for other purposes, in which case getting it in one place is best.