Edit this page

Chart HtmlHelper Overview

The Chart HtmlHelper extension is a server-side wrapper for the Kendo UI Chart widgets.

Getting Started

The Basics

There are two ways to bind a Kendo UI Chart for ASP.NET MVC:

  • server—The Chart is bound to a supplied model.
  • ajax—The Chart makes Ajax requests to get the data.

Server Binding

Below are listed the steps for you to follow when configuring the Kendo UI Chart for server binding to the to the list of InternetUsers.

  1. Make sure you followed all the steps from the introductory article on Telerik UI for ASP.NET MVC.

  2. Create a new action method and pass the InternetUsers list as the model.

    Example
        public ActionResult Index()
        {
            return View(ChartDataRepository.InternetUsers());
        }
    
  3. Make your view strongly typed.

    Example
    
            <%@ Page Language="C#" MasterPageFile="~/Views/Shared/Site.Master"
               Inherits="System.Web.Mvc.ViewPage<IEnumerable<MvcApplication1.Models.InternetUsers>>" %>
    
    
            @model IEnumerable<MvcApplication1.Models.InternetUsers>
    
  4. Add a server-bound Chart.

    Example
    
            <%: Html.Kendo().Chart(Model) //The Chart will be bound to the Model which is the InternetUsers list.
                    .Name("internetUsersChart") //The name of the Chart is mandatory. It specifies the "id" attribute of the widget.
                    .Title("Internet Users")
                    .Series(series => {
                        series.Bar(model => model.Value) //Create a Bar Chart series bound to the "Value" property.
                              .Name("United States");
                    })
                    .CategoryAxis(axis => axis
                        .Categories(model => model.Year)
                    )
            %>
    
    
            @(Html.Kendo().Chart(Model) //The Chart will be bound to the Model which is the InternetUsers list
                  .Name("internetUsersChart") //The name of the Chart is mandatory. It specifies the "id" attribute of the widget.
                  .Title("Internet Users")
                  .Series(series => {
                      series.Bar(model => model.Value) //Create a Bar Chart series bound to the "Value" property.
                            .Name("United States");
                  })
                  .CategoryAxis(axis => axis
                      .Categories(model => model.Year)
                  )
            )
    

Event Handling

You can subscribe to all Chart events.

By Handler Name

The following example demonstrates how to subscribe to events by a handler name.

Example

      <%: Html.Kendo().Chart(Model)
              .Name("internetUsersChart")
              .Events(e => e
                  .DataBound("internetUsersChart_dataBound")
                  .SeriesClick("internetUsersChart_seriesClick")
              )
      %>

      <script>
          function internetUsersChart_dataBound() {
              //Handle the dataBound event.
          }

          function internetUsersChart_seriesClick() {
              //Handle the series click event.
          }
      </script>

      @(Html.Kendo().Chart(Model)
            .Name("internetUsersChart")
            .Events(e => e
                .DataBound("internetUsersChart_dataBound")
                .SeriesClick("internetUsersChart_seriesClick")
            )
      )

      <script>
          function internetUsersChart_dataBound() {
              //Handle the dataBound event.
          }

          function internetUsersChart_seriesClick() {
              //Handle the seriesClick event.
          }
      </script>

By Template Delegate

The following example demonstrates how to subscribe to events by a template delegate.

Example

      @(Html.Kendo().Chart(Model)
            .Name("internetUsersChart")
            .Events(e => e
                .DataBound(@<text>
                     function() {
                         //Handle the dataBound event inline.
                     }
                </text>)
                .SeriesClick(@<text>
                     function() {
                         //Handle the seriesClick event inline.
                     }
                </text>)
            )
      )

Reference

Existing Instances

To reference an existing Kendo UI Chart instance, use the jQuery.data() configuration option. Once a reference is established, use the Chart API to control its behavior.

Example
  //Put this after your Kendo UI Chart for ASP.NET MVC declaration.
  <script>
      $(function() {
          //Notice that the Name() of the Chart is used to get its client-side instance.
          var chart = $("#internetUsersChart").data("kendoChart");
      });
  </script>

See Also