-->

How to create Intensity Chart using MVC4 & Jquery.


Introduction

In this post, I explain How to create Intensity Chart using MVC4 & Jquery.
We all know, An intensity map of Google Chart which is very useful one, that's highlights regions or countries based on relative values. Here I have explained how to apply Intensity Chart in a table. An intensity Chart that highlights table cells based on relative values.

Steps :

Step - 1: Create New Project.

Go to File > New > Project > Select asp.net MVC4 web application > Entry Application Name > Click OK > Select Internet Application > Select view engine Razor > OK

Step-2: Add a new Controller.

Go to Solution Explorer > Right Click on Controllers folder form Solution Explorer > Add > Controller > Enter Controller name > Select Templete "empty MVC Controller"> Add.

Step-3: Add new action into your controller for Get Method.

Here I have added "Chart" Action into "Home" Controller. Please write this following code

  1. public ActionResult Chart()
  2. {
  3. return View();
  4. }

Step-4: Add view for the Action & design.

Right Click on Action Method (here right click on form action) > Add View... > Enter View Name > Select View Engine (Razor) > Check "Create a strong-typed view" > Select your model class > Add.

Step-5: Add jQuery code for creating the Intensity Chart.

Jquery Code
  1. <script>
  2. $(document).ready(function () {
  3. var Color = '#FF9966';
  4. var isReverse = false;
  5. SetColor(Color, isReverse);
  6.  
  7. // For Reverse Color Button Click
  8. $("#rev").click(function () {
  9. isReverse = !isReverse;
  10. SetColor(Color, isReverse);
  11. });
  12.  
  13. });
  14. function SetColor(Color, isReverse) {
  15. //Find Max Value
  16. var maxValue = 1;
  17. $("#data tr").each(function (i, val) {
  18. var $tr = this;
  19. $("td", $tr).each(function (i, val) {
  20. var value = parseInt($(this).text());
  21. maxValue = value > maxValue ? value : maxValue;
  22. });
  23. });
  24.  
  25. // Set Color Here
  26. $("#data tr").each(function (i, val) {
  27. var $tr = this;
  28. $("td", $tr).each(function (i, val) {
  29. var value = parseInt($(this).text());
  30. value = value == 0 ? 1 : value;
  31. var percent = ((1 / maxValue) * value);
  32. $(this).css('background-color', GetColor(Color.substr(1), (isReverse ? percent : 1 - percent)));
  33. });
  34. });
  35. }
  36.  
  37. function GetColor(colorHex, percent) {
  38. colorHex = String(colorHex).replace(/^0-9a-f/gi, '');
  39. if (colorHex.length < 6) {
  40. colorHex = colorHex[0] + colorHex[0] + colorHex[1] + colorHex[1] + colorHex[2] + colorHex[2];
  41. }
  42. percent = percent || 0;
  43. var rgb = "#", c, i;
  44. for (var i = 0; i < 3; i++) {
  45. c = parseInt(colorHex.substr(i * 2, 2), 16);
  46. c = Math.round(Math.min(Math.max(0, c + (c * percent)), 255)).toString(16);
  47. rgb += ("00" + c).substr(c.length);
  48. }
  49. return rgb;
  50. }
  51. </script>
Complete View
  1. @{
  2. ViewBag.Title = "Chart";
  3. }
  4.  
  5. <h2> Intensity Chart</h2>
  6.  
  7. <div>
  8. <input type="button" id="rev" value="Reverse Color" />
  9. @* Create a table for hold your data *@
  10. <table id="data" border="0" cellpadding="0" cellspacing="0">
  11. @{
  12. Random r = new Random();
  13. for (int i = 0; i < 5; i++)
  14. {
  15. <tr>
  16. @for (int j = 0; j < 5; j++)
  17. {
  18. <td><span style="display:inline-block;padding:10px;min-width:80px;">@(r.Next(100))</span></td>
  19. }
  20. </tr>
  21. }
  22. }
  23. </table>
  24. </div>
  25.  
  26. @* Here I am going to add some js code for the intensity chart *@
  27.  
  28. @section Scripts{
  29. <script>
  30. $(document).ready(function () {
  31. var Color = '#FF9966';
  32. var isReverse = false;
  33. SetColor(Color, isReverse);
  34.  
  35. // For Reverse Color Button Click
  36. $("#rev").click(function () {
  37. isReverse = !isReverse;
  38. SetColor(Color, isReverse);
  39. });
  40.  
  41. });
  42. function SetColor(Color, isReverse) {
  43. //Find Max Value
  44. var maxValue = 1;
  45. $("#data tr").each(function (i, val) {
  46. var $tr = this;
  47. $("td", $tr).each(function (i, val) {
  48. var value = parseInt($(this).text());
  49. maxValue = value > maxValue ? value : maxValue;
  50. });
  51. });
  52.  
  53. // Set Color Here
  54. $("#data tr").each(function (i, val) {
  55. var $tr = this;
  56. $("td", $tr).each(function (i, val) {
  57. var value = parseInt($(this).text());
  58. value = value == 0 ? 1 : value;
  59. var percent = ((1 / maxValue) * value);
  60. $(this).css('background-color', GetColor(Color.substr(1), (isReverse ? percent : 1 - percent)));
  61. });
  62. });
  63. }
  64.  
  65. function GetColor(colorHex, percent) {
  66. colorHex = String(colorHex).replace(/^0-9a-f/gi, '');
  67. if (colorHex.length < 6) {
  68. colorHex = colorHex[0] + colorHex[0] + colorHex[1] + colorHex[1] + colorHex[2] + colorHex[2];
  69. }
  70. percent = percent || 0;
  71. var rgb = "#", c, i;
  72. for (var i = 0; i < 3; i++) {
  73. c = parseInt(colorHex.substr(i * 2, 2), 16);
  74. c = Math.round(Math.min(Math.max(0, c + (c * percent)), 255)).toString(16);
  75. rgb += ("00" + c).substr(c.length);
  76. }
  77. return rgb;
  78. }
  79. </script>
  80. }

Step-8: Run Application.



Download Application     Live Demo

How to create Intensity Chart using MVC4 & Jquery.


Related Post : 

Hello ! My name is Sourav Mondal. I am a software developer working in Microsoft .NET technologies since 2010.

I like to share my working experience, research and knowledge through my site.

I love developing applications in Microsoft Technologies including Asp.Net webforms, mvc, winforms, c#.net, sql server, entity framework, Ajax, Jquery, web api, web service and more.

Related Posts: