The Ignite UI Data Chart is a chart control that uses the Canvas tag in HTML5 to plot different kinds of chart into web applications and pages. The control has numerous series which can be combined and this way you can create even more series such as a tornado chart. The tornado chart is a special type of bar chart, which present values in a horizontal rather than vertical orientation. Those values are usually sorted from the largest bar at the top to the smallest one at the bottom. The final appearance of the chart is in a tornado shape and hence it is called. Tornado charts are useful for comparing relative variables or for sensitive analysis. The current blog is based on Graham Murray' s ideas and help for the sample.
Step one
As always the first step to configure the widget is to add the required references as well as the JS and CSS files. If you are creating a JavaScript project you will need two div tags – one for the control and one for the legend. After you are done doing that you can start configuring the data chart. For the purpose of our sample we are going to use movies data from Freebase.
Source: Freebase, licensed under CC-BY. We are going to compare the number of comedy and drama movies that came out in theaters for the years: 2000 to 2010.
JS:
- <scriptsrc="../../Scripts/jquery-ui-1.8.20.min.js"></script>
- <scriptsrc="../../Scripts/infragistics.core.js"></script>
- <scriptsrc="../../Scripts/infragistics.dv.js"></script>
- <divid="chart"></div>
- <divid="legend"></div>
Data:
- var data = [
- { 'Year': 2010, 'Comedy': 449, 'Drama': 780},
- { 'Year': 2009, 'Comedy': 682, 'Drama': 1143 },
- { 'Year': 2008, 'Comedy': 729, 'Drama': 1034 },
- { 'Year': 2007, 'Comedy': 660, 'Drama': 982 },
- { 'Year': 2006, 'Comedy': 765, 'Drama': 1117 },
- { 'Year': 2005, 'Comedy': 686, 'Drama': 1042 },
- { 'Year': 2004, 'Comedy': 639, 'Drama': 998 },
- { 'Year': 2003, 'Comedy': 593, 'Drama': 864},
- { 'Year': 2002, 'Comedy': 543, 'Drama': 850},
- { 'Year': 2001, 'Comedy': 467, 'Drama': 732 },
- { 'Year': 2000, 'Comedy': 417, 'Drama': 653, }
- ];
MVC:
- @using Infragistics.Web.Mvc
- @model IQueryable<TornadoChart.Models.Movies>
Model:
- publicclassMovies
- {
- publicint Year { get; set; }
- publicint Drama { get; set; }
- publicint Comedy { get; set; }
- }
HomeController:
- publicActionResult MVC()
- {
- var data = newList<Movies>()
- {
- newMovies{Year=2010,Comedy=449,Drama=780},
- newMovies{Year=2009,Comedy=682,Drama=1143},
- newMovies{Year=2008,Comedy=729,Drama=1034},
- newMovies{Year=2007,Comedy=660,Drama=982},
- newMovies{Year=2006,Comedy=765,Drama=1117},
- newMovies{Year=2005,Comedy=686,Drama=1042},
- newMovies{Year=2004,Comedy=639,Drama=998},
- newMovies{Year=2003,Comedy=593,Drama=864},
- newMovies{Year=2002,Comedy=543,Drama=850},
- newMovies{Year=2001,Comedy=467,Drama=732},
- newMovies{Year=2000,Comedy=417,Drama=653},
- };
- return View(data.AsQueryable());
- }
Step two
We have everything needed to proceed with the configuration of the widget. Let’s start with configuring the basic – we need to set height, width and dataSource which we are going to connect with the data array we defined. Then we need to configure the chart axis options. The axis type determines if this is the category X-axis or a numeric (value) Y-axis, and the axes names are used in data series configuration to refer to which axis to map a particular data series. We will define two Y-axes and one X-axis. The X-axis main role in the case is to determine the referenceValue (the starting point for our bars).By default this value is 0. We have two Y-values: one for each bar. Although you can place as many bars as you need at one Y-axis they will be positioned one above the other. For the tornado chart we will need the opposite bars to be displayed at the same visual line (to align the series) that is why we need two Y-values. Using the labelVisibility property we can collapse the labels of one of the Y-axes.
The crossing axis property can be used for changing where the X-Axis crosses the Y-Axis. You can set one of the Y-axis to cross the X-axis, and assign to the stroke and strokeThickness properties some values. This way when the tornado chart is ready the bars will be separated visually by a line (the Y-axis).
The last thing to do is configure the series. They are mapped to the corresponding Y-axes because the values in the two data series exist in different value ranges and we want to display them in a different way. We choose the bar type for the series because we want the bars to be displayed horizontally. We can customize the bars by changing their brush or outline. The valueMemberPath connects the series to the data property we want it to display. If we want a legend we should assigned it with the legend option which points to the div element we created earlier.
JS:
- $("#chart").igDataChart({
- dataSource: data,
- width: "500px",
- heigh: "250px",
- title: "Movies",
- subtitle: "Number of comedies and drama movies for the years: 2000-2010",
- axes: [{
- name: "yAxis",
- type: "categoryY",
- label: "Year",
- crossingAxis: "xAxis",
- stroke: "black",
- strokeThickness: 2,
- isInverted: true,
- gap: 10,
- interval: 1
- }, {
- name: "yAxis2",
- type: "categoryY",
- label: "Year",
- isInverted: true,
- labelVisibility: "collapsed",
- gap: 10
- }, {
- name: "xAxis",
- type: "numericX",
- labelExtent: 30,
- formatLabel: function (val) {
- return Math.abs(val);
- }
- }], series: [{
- name: "Comedy",
- type: "bar",
- xAxis: "xAxis",
- yAxis: "yAxis",
- valueMemberPath: "Comedy",
- title: "Comedy",
- brush: "#666699",
- outline: "#666699"
- }, {
- name: "Drama",
- type: "bar",
- xAxis: "xAxis",
- yAxis: "yAxis2",
- valueMemberPath: "Drama",
- title: "Drama",
- brush: "#336699",
- outline: "#336699",
- }],
- legend: {
- element: "legend"
- }
- });
MVC:
- @(Html.Infragistics().DataChart(Model)
- .ID("chart")
- .Height("400")
- .Width("500")
- .Title("Movies")
- .Subtitle("Number of comedies and drama movies for the years: 2000-2010")
- .Axes(a =>
- {
- a.CategoryY("yAxis")
- .Label(item => item.Year)
- .IsInverted(true)
- .Gap(10)
- .Interval(1);
- a.CategoryY("yAxis2")
- .Label(item => item.Year)
- .IsInverted(true)
- .LabelVisibility(Visibility.Collapsed)
- .Stroke("#000")
- .Gap(10);
- a.NumericX("xAxis")
- .CrossingAxis("categodyY")
- .ReferenceValue(0)
- .LabelExtent(60)
- .FormatLabel("function(val){return Math.abs(val);}");
- })
- .Series(s => {
- s.Bar("Drama")
- .XAxis("xAxis")
- .YAxis("yAxis2")
- .ValueMemberPath("Drama")
- .Title("Drama")
- .ShowTooltip(true)
- .TooltipTemplate("tooltipTemplateDrama")
- .IsTransitionInEnabled(false);
- s.Bar("Comedy")
- .XAxis("xAxis")
- .YAxis("yAxis")
- .ValueMemberPath("Comedy")
- .Title("Comedy")
- .ShowTooltip(true)
- .TooltipTemplate("tooltipTemplateComedy")
- .IsTransitionInEnabled(false)
- .Brush("#666699")
- .Outline("#666699");
- })
- .Legend(legend => legend.ID("legend"))
- .DataBind()
- .Render())
Step Three
Building a tornado chart out of the data chart is not that hard. We want the different bars of the chart to be displayed in opposite directions and for that reason we call the convert function which transform one of the data properties’ values into negative integers. After the call of the converting function we will have negative values in the data and for that reason the labels will have negative values as well which doesn’t fit the data we are presenting. That is why we convert the labels using the formatLabel property and we will display the numbers’ absolute value. As we said to achieve a tornado appearance for the chart we need the series to be aligned that is why we use two Y-axes.
- function convert() {
- var i;
- for (i = 0; i < data.length; i++) {
- if (data[i].Drama) {
- data[i].Drama = data[i].Drama * -1;
- }
- }
- }
- formatLabel: function (val) {
- return Math.abs(val);
- }
This was the last step for the creation of the the tornado chart.
If you want to add tooltips to your control you just have to add the showTooltip property and set it to true. That way you will see the default template.You can create a custom template using the tooltipTemplate property. In our case we will see the negative values that correspond to the drama bars that is why we will have to handle this with the tooltipShown event.
Tooltip templates:
- <scriptid="tooltipTemplateDrama"type="text/x-jquery-tmpl">
- <span>${item.Drama}</span>
- </script>
- <scriptid="tooltipTemplateComedy"type="text/x-jquery-tmpl">
- <span>${item.Comedy}</span>
- </script>
JS:
- series: [{
- name: "Comedy",
- type: "bar",
- xAxis: "xAxis",
- yAxis: "yAxis",
- valueMemberPath: "Comedy",
- title: "Comedy",
- showTooltip: true,
- isHighlightingEnabled: true,
- tooltipTemplate: "tooltipTemplateComedy",
- brush: "#666699",
- outline: "#666699"
- }, {
- name: "Drama",
- type: "bar",
- xAxis: "xAxis",
- yAxis: "yAxis2",
- valueMemberPath: "Drama",
- showTooltip: true,
- isHighlightingEnabled: true,
- tooltipTemplate: "tooltipTemplateDrama",
- title: "Drama",
- brush: "#336699",
- outline: "#336699",
- }],
Event:
- tooltipShown: function (evt, ui) {
- if (ui.tempId == "Drama") {
- ui.element.text(parseInt(ui.element.text()) * -1)
- }
- },
Image:
To conclude
In few simple steps you can easily create a tornado chart using the Ignite UI data chart , displaying comparative values. This chart provides an easy to understand presentation of the data and an attractive addition to any web application or site.
Check out the live demo on jsFiddle or download the MVC sample
You can follow us on Twitter @Infragistics and stay in touch on Facebook, Google+ and LinkedIn!