banner



How To Add Script Src To Body Asp.net

  • Updated date Apr 26, 2018
  • 132k
  • 18

In this article, I will exist explaining how to integrate chart.js with your Asp.Cyberspace Core application with 4 different types of charts.

In this article, I volition exist explaining how to integrate chart.js with your Asp.Internet Cadre awarding with four different types of charts, which are:

  1. Pie
  2. Bar
  3. Line
  4. Stacked Bar

Creation and usage of the Pie, Bar and Line Charts are very similar only the stacked nautical chart is a bit more complex. Basically, a chart is a key-value listing grouped in an blithe style making it easier to empathise and see the numbers, except for the stacked chart which has two keys and a value. A more detailed explanation will be placed with each case.

What is chart.js?"Elementary, clean and engaging HTML5 based JavaScript charts. Chart.js is an piece of cake way to include animated, interactive graphs on your website for free."  https://world wide web.chartjs.org/ Pre-requisite?We have two prerequisites that must be loaded at every each view with a chart equally content, and they are the Jquery and Chart.js libraries:

  1. <script src= "https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.7.2/Chart.bundle.min.js" ></script>
  2. <script src="https://code.jquery.com/jquery-three.3.1.min.js" ></script>

Models used here

SimpleReportViewModel

  1. public class  SimpleReportViewModel
  2.    {
  3. public string  DimensionOne { get ; gear up ; }
  4. public int  Quantity { get ; set ; }
  5.    }

StackedViewModel

  1. public class  StackedViewModel
  2. {
  3. public string  StackedDimensionOne { go ; fix ; }
  4. public  List<SimpleReportViewModel> LstData { get ; set ; }
  5. }

Bar Chart

Controller Action

  1. public  IActionResult Bar()
  2.         {
  3. individual  Random rnd = new  Random();
  4.             var lstModel =new  List<SimpleReportViewModel>();
  5.             lstModel.Add together(new  SimpleReportViewModel
  6.             {
  7.                 DimensionOne ="Technology" ,
  8.                 Quantity = rnd.Next( ten )
  9.             } );
  10.             lstModel.Add together(new  SimpleReportViewModel
  11.             {
  12.                 DimensionOne ="Sales" ,
  13.                 Quantity = rnd.Next( 10 )
  14.             } );
  15.             lstModel.Add(new  SimpleReportViewModel
  16.             {
  17.                 DimensionOne ="Marketing" ,
  18.                 Quantity = rnd.Next( ten )
  19.             } );
  20.             lstModel.Add(new  SimpleReportViewModel
  21.             {
  22.                 DimensionOne ="Human Resources" ,
  23.                 Quantity = rnd.Next( 10 )
  24.             } );
  25.             lstModel.Add(new  SimpleReportViewModel
  26.             {
  27.                 DimensionOne ="Research and Development" ,
  28.                 Quantity = rnd.Next( 10 )
  29.             } );
  30.             lstModel.Add(new  SimpleReportViewModel
  31.             {
  32.                 DimensionOne ="Acconting" ,
  33.                 Quantity = rnd.Adjacent( 10 )
  34.             } );
  35.             lstModel.Add(new  SimpleReportViewModel
  36.             {
  37.                 DimensionOne ="Support" ,
  38.                 Quantity = rnd.Adjacent( 10 )
  39.             } );
  40.             lstModel.Add(new  SimpleReportViewModel
  41.             {
  42.                 DimensionOne ="Logistics" ,
  43.                 Quantity = rnd.Adjacent( ten )
  44.             } );
  45. return  View( lstModel );
  46.         }

View

  1. @model List<SimpleReportViewModel>
  2. @{
  3.     var XLabels = Newtonsoft.Json.JsonConvert.SerializeObject( Model.Select( 10 => x.DimensionOne ).ToList() );
  4.     var YValues = Newtonsoft.Json.JsonConvert.SerializeObject( Model.Select( x => 10.Quantity ).ToList() );
  5.     ViewData["Title" ] = "Bar Nautical chart" ;
  6. }
  7. <!DOCTYPE html>
  8. <html>
  9. <caput>
  10.     <meta proper name="viewport"  content= "width=device-width"  />
  11.     <title>Bar</championship>
  12. </caput>
  13. <trunk>
  14.     <divcourse = "box-body" >
  15.         <divform = "chart-container" >
  16.             <canvas id="chart"  fashion= "width:100%; height:500px" ></sheet>
  17.         </div>
  18.     </div>
  19. </body>
  20. </html>
  21. <script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.seven.2/Nautical chart.bundle.min.js" ></script>
  22. <script src="https://code.jquery.com/jquery-3.3.1.min.js" ></script>
  23. <script type="text/javascript" >
  24.         $(function () {
  25.     var chartName ="nautical chart" ;
  26.         var ctx = document.getElementById(chartName).getContext('2d' );
  27.         var information = {
  28.                 labels: @Html.Raw(XLabels),
  29.                 datasets: [{
  30.                     label:"Departments Nautical chart" ,
  31.                     backgroundColor: [
  32. 'rgba(255, 99, 132, 0.2)' ,
  33. 'rgba(54, 162, 235, 0.ii)' ,
  34. 'rgba(255, 206, 86, 0.2)' ,
  35. 'rgba(75, 192, 192, 0.two)' ,
  36. 'rgba(153, 102, 255, 0.2)' ,
  37. 'rgba(255, 159, 64, 0.2)' ,
  38. 'rgba(255, 0, 0)' ,
  39. 'rgba(0, 255, 0)' ,
  40. 'rgba(0, 0, 255)' ,
  41. 'rgba(192, 192, 192)' ,
  42. 'rgba(255, 255, 0)' ,
  43. 'rgba(255, 0, 255)'
  44.                     ],
  45.                     borderColor: [
  46. 'rgba(255,99,132,1)' ,
  47. 'rgba(54, 162, 235, 1)' ,
  48. 'rgba(255, 206, 86, ane)' ,
  49. 'rgba(75, 192, 192, 1)' ,
  50. 'rgba(153, 102, 255, ane)' ,
  51. 'rgba(255, 159, 64, 1)' ,
  52. 'rgba(255, 0, 0)' ,
  53. 'rgba(0, 255, 0)' ,
  54. 'rgba(0, 0, 255)' ,
  55. 'rgba(192, 192, 192)' ,
  56. 'rgba(255, 255, 0)' ,
  57. 'rgba(255, 0, 255)'
  58.                     ],
  59.                     borderWidth: 1,
  60.                     data: @Html.Raw(YValues)
  61.     }]
  62.             };
  63. var options = {
  64.                 maintainAspectRatio:faux ,
  65.                 scales: {
  66.                     yAxes: [{
  67.                         ticks: {
  68.                             min: 0,
  69.                             beginAtZero:true
  70.                         },
  71.                         gridLines: {
  72.                             display:true ,
  73.                             color:"rgba(255,99,164,0.2)"
  74.                         }
  75. }],
  76.                     xAxes: [{
  77.                         ticks: {
  78.                             min: 0,
  79.                             beginAtZero:true
  80.                         },
  81.                         gridLines: {
  82.                             display:false
  83.                         }
  84.                     }]
  85.                 }
  86.             };
  87.        var myChart =new   Chart(ctx, {
  88.                 options: options,
  89.                 data: data,
  90.                 type:'bar'
  91.             });
  92.         });
  93. </script>

 Consequence

 Result

ExplanationHere nosotros utilize the SimpleReportViewModel considering we accept a simple bar chart with a department list and their head number

Pie ChartController Action

  1. public  IActionResult Pie()
  2.         {
  3. individual  Random rnd = new  Random();
  4.             var lstModel =new  List<SimpleReportViewModel>();
  5.             lstModel.Add(new  SimpleReportViewModel
  6.             {
  7.                 DimensionOne ="Beer" ,
  8.                 Quantity = rnd.Next( 10 )
  9.             } );
  10.             lstModel.Add(new  SimpleReportViewModel
  11.             {
  12.                 DimensionOne ="Wine" ,
  13.                 Quantity = rnd.Side by side( ten )
  14.             } );
  15.             lstModel.Add(new  SimpleReportViewModel
  16.             {
  17.                 DimensionOne ="Whisky" ,
  18.                 Quantity = rnd.Side by side( 10 )
  19.             } );
  20.             lstModel.Add(new  SimpleReportViewModel
  21.             {
  22.                 DimensionOne ="Water" ,
  23.                 Quantity = rnd.Side by side( 10 )
  24.             } );
  25. render  View( lstModel );
  26.         }

View

  1. @ using  System.Linq;
  2. @model Listing<SimpleReportViewModel>
  3. @{
  4.     var XLabels = Newtonsoft.Json.JsonConvert.SerializeObject( Model.Select( x => x.DimensionOne ).ToList() );
  5.     var YValues = Newtonsoft.Json.JsonConvert.SerializeObject( Model.Select( x => x.Quantity ).ToList() );
  6.     ViewData["Title" ] = "Pie Chart" ;
  7. }
  8. <!DOCTYPE html>
  9. <html>
  10. <head>
  11.     <meta proper noun="viewport"  content= "width=device-width"  />
  12.     <championship>Pie</championship>
  13. </head>
  14. <torso>
  15.     <divclass = "box-body" >
  16.         <divclass = "chart-container" >
  17.             <canvas id="chart"  style= "width:100%; peak:500px" ></sheet>
  18.         </div>
  19.     </div>
  20. </trunk>
  21. </html>
  22. <script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.7.2/Nautical chart.packet.min.js" ></script>
  23. <script src="https://code.jquery.com/jquery-3.three.1.min.js" ></script>
  24. <script type="text/javascript" >
  25.         $(function () {
  26.     var chartName ="nautical chart" ;
  27.         var ctx = certificate.getElementById(chartName).getContext('2d' );
  28.         var information = {
  29.                 labels: @Html.Raw(XLabels),
  30.                 datasets: [{
  31.                     label:"Drinks Nautical chart" ,
  32.                     backgroundColor: [
  33. 'rgba(255, 99, 132, 0.2)' ,
  34. 'rgba(54, 162, 235, 0.2)' ,
  35. 'rgba(255, 206, 86, 0.2)' ,
  36. 'rgba(75, 192, 192, 0.2)' ,
  37. 'rgba(153, 102, 255, 0.2)' ,
  38. 'rgba(255, 159, 64, 0.ii)' ,
  39. 'rgba(255, 0, 0)' ,
  40. 'rgba(0, 255, 0)' ,
  41. 'rgba(0, 0, 255)' ,
  42. 'rgba(192, 192, 192)' ,
  43. 'rgba(255, 255, 0)' ,
  44. 'rgba(255, 0, 255)'
  45.                     ],
  46.                     borderColor: [
  47. 'rgba(255,99,132,1)' ,
  48. 'rgba(54, 162, 235, one)' ,
  49. 'rgba(255, 206, 86, 1)' ,
  50. 'rgba(75, 192, 192, 1)' ,
  51. 'rgba(153, 102, 255, 1)' ,
  52. 'rgba(255, 159, 64, i)' ,
  53. 'rgba(255, 0, 0)' ,
  54. 'rgba(0, 255, 0)' ,
  55. 'rgba(0, 0, 255)' ,
  56. 'rgba(192, 192, 192)' ,
  57. 'rgba(255, 255, 0)' ,
  58. 'rgba(255, 0, 255)'
  59.                     ],
  60.                     borderWidth: 1,
  61.                     information: @Html.Raw(YValues)
  62.     }]
  63.             };
  64. var options = {
  65.                 maintainAspectRatio:false ,
  66.                 scales: {
  67.                     yAxes: [{
  68.                         ticks: {
  69.                             min: 0,
  70.                             beginAtZero:true
  71.                         },
  72.                         gridLines: {
  73.                             brandish:true ,
  74.                             color:"rgba(255,99,164,0.2)"
  75.                         }
  76. }],
  77.                     xAxes: [{
  78.                         ticks: {
  79.                             min: 0,
  80.                             beginAtZero:true
  81.                         },
  82.                         gridLines: {
  83.                             display:false
  84.                         }
  85.                     }]
  86.                 }
  87.             };
  88.        var myChart =new   Nautical chart(ctx, {
  89.                 options: options,
  90.                 information: data,
  91.                 type:'pie'
  92.             });
  93.         });
  94. </script>

Result

 Result

Explanation

Here we besides employ the SimpleReportViewModel because we have a simple pie chart with a drinkable listing and their quantity number.

Line ChartController Action

  1. public  IActionResult Line()
  2.        {
  3. private  Random rnd = new  Random();
  4.            var lstModel =new  List<SimpleReportViewModel>();
  5.            lstModel.Add(new  SimpleReportViewModel
  6.            {
  7.                DimensionOne ="Brazil" ,
  8.                Quantity = rnd.Next( 10 )
  9.            } );
  10.            lstModel.Add together(new  SimpleReportViewModel
  11.            {
  12.                DimensionOne ="Usa" ,
  13.                Quantity = rnd.Side by side( 10 )
  14.            } );
  15.            lstModel.Add(new  SimpleReportViewModel
  16.            {
  17.                DimensionOne ="Portugal" ,
  18.                Quantity = rnd.Side by side( 10 )
  19.            } );
  20.            lstModel.Add together(new  SimpleReportViewModel
  21.            {
  22.                DimensionOne ="Russia" ,
  23.                Quantity = rnd.Next( 10 )
  24.            } );
  25.            lstModel.Add together(new  SimpleReportViewModel
  26.            {
  27.                DimensionOne ="Ireland" ,
  28.                Quantity = rnd.Next( 10 )
  29.            } );
  30.            lstModel.Add together(new  SimpleReportViewModel
  31.            {
  32.                DimensionOne ="Germany" ,
  33.                Quantity = rnd.Adjacent( 10 )
  34.            } );
  35. render  View( lstModel );
  36.        }

View

  1. @model List<SimpleReportViewModel>
  2. @{
  3.     var XLabels = Newtonsoft.Json.JsonConvert.SerializeObject( Model.Select( x => x.DimensionOne ).ToList() );
  4.     var YValues = Newtonsoft.Json.JsonConvert.SerializeObject( Model.Select( ten => x.Quantity ).ToList() );
  5.     ViewData["Title" ] = "Line Chart" ;
  6. }
  7. <!DOCTYPE html>
  8. <html>
  9. <caput>
  10.     <meta proper noun="viewport"  content= "width=device-width"  />
  11.     <championship>Line</championship>
  12. </head>
  13. <body>
  14.     <divform = "box-torso" >
  15.         <divclass = "chart-container" >
  16.             <canvas id="chart"  style= "width:100%; height:500px" ></sheet>
  17.         </div>
  18.     </div>
  19. </body>
  20. </html>
  21. <script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.7.two/Chart.bundle.min.js" ></script>
  22. <script src="https://code.jquery.com/jquery-3.3.1.min.js" ></script>
  23. <script type="text/javascript" >
  24.         $(function () {
  25.     var chartName ="nautical chart" ;
  26.         var ctx = document.getElementById(chartName).getContext('2d' );
  27.         var data = {
  28.                 labels: @Html.Raw(XLabels),
  29.                 datasets: [{
  30.                     label:"Countries Chart" ,
  31.                     backgroundColor: [
  32. 'rgba(255, 99, 132, 0.two)' ,
  33. 'rgba(54, 162, 235, 0.2)' ,
  34. 'rgba(255, 206, 86, 0.ii)' ,
  35. 'rgba(75, 192, 192, 0.2)' ,
  36. 'rgba(153, 102, 255, 0.ii)' ,
  37. 'rgba(255, 159, 64, 0.2)' ,
  38. 'rgba(255, 0, 0)' ,
  39. 'rgba(0, 255, 0)' ,
  40. 'rgba(0, 0, 255)' ,
  41. 'rgba(192, 192, 192)' ,
  42. 'rgba(255, 255, 0)' ,
  43. 'rgba(255, 0, 255)'
  44.                     ],
  45.                     borderColor: [
  46. 'rgba(255,99,132,1)' ,
  47. 'rgba(54, 162, 235, 1)' ,
  48. 'rgba(255, 206, 86, 1)' ,
  49. 'rgba(75, 192, 192, 1)' ,
  50. 'rgba(153, 102, 255, 1)' ,
  51. 'rgba(255, 159, 64, 1)' ,
  52. 'rgba(255, 0, 0)' ,
  53. 'rgba(0, 255, 0)' ,
  54. 'rgba(0, 0, 255)' ,
  55. 'rgba(192, 192, 192)' ,
  56. 'rgba(255, 255, 0)' ,
  57. 'rgba(255, 0, 255)'
  58.                     ],
  59.                     borderWidth: 1,
  60.                     data: @Html.Raw(YValues)
  61.     }]
  62.             };
  63. var options = {
  64.                 maintainAspectRatio:fake ,
  65.                 scales: {
  66.                     yAxes: [{
  67.                         ticks: {
  68.                             min: 0,
  69.                             beginAtZero:true
  70.                         },
  71.                         gridLines: {
  72.                             display:true ,
  73.                             color:"rgba(255,99,164,0.ii)"
  74.                         }
  75. }],
  76.                     xAxes: [{
  77.                         ticks: {
  78.                             min: 0,
  79.                             beginAtZero:true
  80.                         },
  81.                         gridLines: {
  82.                             display:false
  83.                         }
  84.                     }]
  85.                 }
  86.             };
  87.        var myChart =new   Chart(ctx, {
  88.                 options: options,
  89.                 data: data,
  90.                 type:'line'
  91.             });
  92.         });
  93. </script>

Effect

 Result

Caption

Here we likewise utilize the SimpleReportViewModel considering we take a simple line chart with a state list and their quantity number.

Stacked ChartController Activeness

  1. public  IActionResult Stacked()
  2.        {
  3. private  Random rnd = new  Random();
  4.            var lstModel =new  List<StackedViewModel>();
  5.            lstModel.Add(new  StackedViewModel
  6.            {
  7.                StackedDimensionOne ="First Quarter" ,
  8.                LstData =new  List<SimpleReportViewModel>()
  9.                {
  10. new  SimpleReportViewModel()
  11.                    {
  12.                        DimensionOne="Idiot box" ,
  13.                        Quantity = rnd.Side by side(10)
  14.                    },
  15. new  SimpleReportViewModel()
  16.                    {
  17.                        DimensionOne="Games" ,
  18.                        Quantity = rnd.Next(10)
  19.                    },
  20. new  SimpleReportViewModel()
  21.                    {
  22.                        DimensionOne="Books" ,
  23.                        Quantity = rnd.Next(10)
  24.                    }
  25.                }
  26.            } );
  27.            lstModel.Add together(new  StackedViewModel
  28.            {
  29.                StackedDimensionOne ="2d Quarter" ,
  30.                LstData =new  List<SimpleReportViewModel>()
  31.                {
  32. new  SimpleReportViewModel()
  33.                    {
  34.                        DimensionOne="TV" ,
  35.                        Quantity = rnd.Next(10)
  36.                    },
  37. new  SimpleReportViewModel()
  38.                    {
  39.                        DimensionOne="Games" ,
  40.                        Quantity = rnd.Next(ten)
  41.                    },
  42. new  SimpleReportViewModel()
  43.                    {
  44.                        DimensionOne="Books" ,
  45.                        Quantity = rnd.Side by side(ten)
  46.                    }
  47.                }
  48.            } );
  49.            lstModel.Add(new  StackedViewModel
  50.            {
  51.                StackedDimensionOne ="Third Quarter" ,
  52.                LstData =new  List<SimpleReportViewModel>()
  53.                {
  54. new  SimpleReportViewModel()
  55.                    {
  56.                        DimensionOne="TV" ,
  57.                        Quantity = rnd.Next(10)
  58.                    },
  59. new  SimpleReportViewModel()
  60.                    {
  61.                        DimensionOne="Games" ,
  62.                        Quantity = rnd.Next(ten)
  63.                    },
  64. new  SimpleReportViewModel()
  65.                    {
  66.                        DimensionOne="Books" ,
  67.                        Quantity = rnd.Next(10)
  68.                    }
  69.                }
  70.            } );
  71.            lstModel.Add(new  StackedViewModel
  72.            {
  73.                StackedDimensionOne ="Fourth Quarter" ,
  74.                LstData =new  List<SimpleReportViewModel>()
  75.                {
  76. new  SimpleReportViewModel()
  77.                    {
  78.                        DimensionOne="TV" ,
  79.                        Quantity = rnd.Next(10)
  80.                    },
  81. new  SimpleReportViewModel()
  82.                    {
  83.                        DimensionOne="Games" ,
  84.                        Quantity = rnd.Side by side(10)
  85.                    },
  86. new  SimpleReportViewModel()
  87.                    {
  88.                        DimensionOne="Books" ,
  89.                        Quantity = rnd.Next(10)
  90.                    }
  91.                }
  92.            } );
  93. return  View( lstModel );
  94.        }

View

  1. @model List < StackedViewModel > @ {
  2. var  XLabels = Newtonsoft.Json.JsonConvert.SerializeObject(Model.FirstOrDefault().LstData.Select(x => x.DimensionOne).ToList());
  3. var  YValues = Newtonsoft.Json.JsonConvert.SerializeObject(Model.Select(10 => x.LstData.Select(w => west.Quantity)).ToList());
  4. var  label2 = Newtonsoft.Json.JsonConvert.SerializeObject(Model.Select(x => x.StackedDimensionOne).ToList());
  5.     ViewData["Title" ] = "Stacked Chart" ;
  6. } < !DOCTYPE html > < html > < head > < meta name ="viewport"
  7. content ="width=device-width"  / > < title > Stacked < /championship> < /caput> < body > < div class  = "box-torso"  > < div form  = "nautical chart-container"  > < canvas id = "chartStacked"
  8. style ="width:100%; height:500px"  > < /canvas> < /div> < /div> < /trunk> < /html> < script src = "https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.7.two/Chart.bundle.min.js"  > < /script> < script src = "https://code.jquery.com/jquery-3.three.ane.min.js"  > < /script> < script type = "text/javascript"  > $( function () {
  9. var  chartName = "chartStacked" ;
  10. var  ctx = document.getElementById(chartName).getContext( '2d' );
  11. var  XLabels = @Html.Raw(XLabels);
  12. var  YValues = @Html.Raw(YValues);
  13. var  characterization = @Html.Raw(label2);
  14. var  aux = 0;
  15. var  barChartData = {
  16.         labels: @Html.Raw(label2),
  17.         datasets: []
  18.     }
  19.     XLabels.forEach(function (a, i) {
  20. var  data = [];
  21.         YValues.forEach(function (a, i) {
  22.             information.push button(a[aux]);
  23.         });
  24.         barChartData.datasets.button({
  25.             label: XLabels[aux],
  26.             backgroundColor: random_rgba(),
  27.             data: data
  28.         });
  29.         aux++;
  30.     });
  31. var  options = {
  32.         maintainAspectRatio:false ,
  33.         scales: {
  34.             yAxes: [{
  35.                 ticks: {
  36.                     min: 0,
  37.                     beginAtZero:truthful
  38.                 },
  39.                 stacked:true ,
  40.                 gridLines: {
  41.                     display:truthful ,
  42.                     colour:"rgba(255,99,164,0.two)"
  43.                 }
  44.             }],
  45.             xAxes: [{
  46.                 stacked:true ,
  47.                 gridLines: {
  48.                     display:false
  49.                 }
  50.             }]
  51.         }
  52.     };
  53. function  random_rgba() {
  54. var  o = Math.round,
  55.             r = Math.random,
  56.             s = 255;
  57. render 'rgba('  + o(r() * s) + ','  + o(r() * s) + ','  + o(r() * s) + ','  + r().toFixed(ane) + ')' ;
  58.     }
  59. var  myChart = new  Chart(ctx, {
  60.         options: options,
  61.         data: barChartData,
  62.         type:'bar'
  63.     });
  64. }); < /script>

Event

 Result

Explanation

Hither we use the StackedViewModel because we have a stacked nautical chart with the number of product sales by quarter.

Ps.: Javascript is needed here to order the data from the model to nautical chart.js dataset.

Congratulations, you take successfully integrated Chart.Js with your Asp.cyberspace Core application.

  • Project on GitHub

How To Add Script Src To Body Asp.net,

Source: https://www.c-sharpcorner.com/article/creating-charts-with-asp-net-core/

Posted by: kellsknours00.blogspot.com

0 Response to "How To Add Script Src To Body Asp.net"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel