0

I am looking at using the HTML widget in a drag n drop form to show a horizontal bar the length of which is based on a field. Not being a programmer of any sort I don't know the html syntax to use - a simple bar would be great but the widget isn't well documented - anyone help?

1 Answer 1

2

The HTML widget accepts HTML language and JavaScript.

To get a value from a layer field, the following JS code is :

<script>expression.evaluate("\"my_field\"")</script>

To build a simple horizontal bar in HTML (color red, length 75% of the total width of the page) :

<div style='background-color:red; width:75%'>&nbsp;</div>

So, now, we will build HTML with JS by replacing values with JS variables :

<p id="my_bar"></p>
<script>
var percent_length = expression.evaluate("\"my_length_field\"") / expression.evaluate("array_max(array_agg(\"my_length_field\"))") * 100;
var color = "#00FF00"
document.getElementById("my_bar").innerHTML = "<div style='background-color:" + color + "; width:" + percent_length + "%'>&nbsp;</div>";
</script>

Step-by-step :

  • retrieve the current length from the field "my_length_field" : expression.evaluate("\"my_length_field\"")
  • retrieve the max length value from this same field : expression.evaluate("array_max(array_agg(\"my_length_field\"))")
  • get the current value in percent : @current_value / @max_value * 100
  • store this percent length in a variable : var percent_length =
  • store for more readability the bar color in a variable, here Lime : var color = "#00FF00"
  • build the HTML horizontal bar code with the two variables : "<div style='background-color:" + color + "; width:" + percent_length + "%'>&nbsp;</div>"
  • get the HTML paragraph identified as "my_bar" and replace its (empty) content (innerHTML) with the generated HTML code.

The result will be a Lime (super green) horizontal bar with a length varying between ]0 - 100] % of the width of the form dialog, according to your "my_length_field" value.

2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.