New to Telerik UI for ASP.NET MVC? Download free 30-day trial

Getting Started with the Linear Gauge

This tutorial explains how to set up a basic Telerik UI for ASP.NET MVC Linear Gauge and highlights the major steps in the configuration of the component.

You will initialize a Linear Gauge and set up the Scale property, which configures the visualization of the gauge. Then, you will use JavaScript to update the value of the Linear Gauge using its client-side reference.

Sample Telerik UI for ASP.NET MVC Linear Gauge

Prerequisites

To successfully complete the tutorial, you need a project that is already configured to use the Telerik UI for ASP.NET MVC components:

1. Prepare the CSHTML File

The first step is to add the required directives at the top of the .cshtml document:

  • To use the Telerik UI for ASP.NET MVC HtmlHelpers:

    @using Kendo.Mvc.UI
    

Optionally, you can structure the document by adding the desired HTML elements like headings, divs, and paragraphs. In this tutorial, you will also apply some styles to the gauge and its container.

    <h2>Tempretature scale of the Pasteurization Process</h2>
    <div class="legend">
        <div class="legend-item">
            <span class="color red"></span>
                Temperature is too high.
        </div>
        <div class="legend-item">
            <span class="color yellow"></span>
                Temperature is right for pasteurization.
        </div>
        <div class="legend-item">
            <span class="color blue"></span>
                Temperature is too low.
        </div>
    </div>
    <div id="gauge-container">
            <!-- Component Configuration -->
    </div>
    <style>
        #gauge-container {
            text-align: center;
            margin: 0 auto;
            background: transparent url("@Url.Content("~/shared/dataviz/gauge/linear-gauge-container.png")") no-repeat 50% 50%;
            background-size: contain;
            background-position:bottom;
            padding: 18px;
            width: 320px;
            height: 320px;
        }

        #gauge {
            height: 100%;
            display: inline-block;
            zoom: 1;
        }
        h2{
            display:flex;
            justify-content:center;
        }
        .legend-item{
            display:flex;
            flex-direction: row;
        }
        .color{
            display:flex;
            width: 15px;
            height: 15px;
            margin-right:5px;
            border-radius:30%;
        }
        .red{
            background-color:#c20000;
        }
        .yellow{
            background-color:#ffc700;
        }
        .blue{
            background-color:#2798df;
        }
    </style>

2. Initialize the Linear Gauge

Use the Linear Gauge HtmlHelper to add the component to a page:

  • The Name() configuration method is mandatory as its value is used for the id and the name attributes of the Linear Gauge element.
  • Utilize the LinearGaugePointerBuilder's Value method to set an initial value for the Linear Gauge.
    @using Kendo.Mvc.UI

   @(Html.Kendo().LinearGauge()
          .Name("gauge")
          .Pointer(pointer => pointer.Value(56))
    )

3. Configure the Linear Gauge

Configure the Scale configuration method of the Linear Gauge. It exposes the LinearGaugeScaleSettingsBuilder which allows you to set up the Min, Max, MajorTicks, MinorTicks, Labels and Ranges properties.

The Ranges property exposes the following configuration methods that are responsible for the graphics rendering of the component:

  • From — The start position of the range in scale units.
  • To — The end position of the range in scale units.
  • Color — The color of the range. Any valid CSS color string will work here, including hex and rgb.
  • Opacity — The opacity of the range.

Finally, fine tune the size and the position of the component with the GaugeAreas's Height and Margin properties.

    @using Kendo.Mvc.UI

   @(Html.Kendo().LinearGauge()
          .Name("gauge")
          .Pointer(pointer => pointer.Value(56))
          .Scale(scale => scale
              .MajorUnit(20)
              .MinorUnit(2)
              .Min(0)
              .Max(130)
              .Ranges(ranges =>
              {
                  ranges.Add().From(0).To(70).Color("#2798df");
                  ranges.Add().From(70).To(100).Color("#ffc700");
                  ranges.Add().From(100).To(130).Color("#c20000");
              }
              )
          )
          .GaugeArea(g=>g.Height(280).Margin(m=>m.Top(30)))
    )

4. (Optional) Reference Existing Linear Gauge Instances

You can reference the Linear Gauge instances that you have created and build on top of their existing configuration:

  1. Use the id attribute of the component instance to establish a reference.

        $(document).ready( function (e) {
            var lineargaugeReference = $("#gauge").data("kendoLinearGauge"); // lineargaugeReference is a reference to the existing Linear Gauge instance of the helper.
        });
    
  2. Use the Linear Gauge client-side API to control the behavior of the component. In this example, you will use the value method to change the value of the Linear Gauge every second to ensure that the visualized thermometer is exact.

        setInterval(function(){
            updateValue(temperatureSensor); // update the value of the Linear Gauge based on external input every second
        },1000)
    
        function updateValue(number) {
            var gauge = $("#gauge").data("kendoLinearGauge");
            gauge.value(number);
        }
    

Next Steps

See Also

In this article