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:
- Pie
- Bar
- Line
- 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:
- <script src= "https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.7.2/Chart.bundle.min.js" ></script>
- <script src="https://code.jquery.com/jquery-three.3.1.min.js" ></script>
Models used here
SimpleReportViewModel
- public class SimpleReportViewModel
- {
- public string DimensionOne { get ; gear up ; }
- public int Quantity { get ; set ; }
- }
StackedViewModel
- public class StackedViewModel
- {
- public string StackedDimensionOne { go ; fix ; }
- public List<SimpleReportViewModel> LstData { get ; set ; }
- }
Bar Chart
Controller Action
- public IActionResult Bar()
- {
- individual Random rnd = new Random();
- var lstModel =new List<SimpleReportViewModel>();
- lstModel.Add together(new SimpleReportViewModel
- {
- DimensionOne ="Technology" ,
- Quantity = rnd.Next( ten )
- } );
- lstModel.Add together(new SimpleReportViewModel
- {
- DimensionOne ="Sales" ,
- Quantity = rnd.Next( 10 )
- } );
- lstModel.Add(new SimpleReportViewModel
- {
- DimensionOne ="Marketing" ,
- Quantity = rnd.Next( ten )
- } );
- lstModel.Add(new SimpleReportViewModel
- {
- DimensionOne ="Human Resources" ,
- Quantity = rnd.Next( 10 )
- } );
- lstModel.Add(new SimpleReportViewModel
- {
- DimensionOne ="Research and Development" ,
- Quantity = rnd.Next( 10 )
- } );
- lstModel.Add(new SimpleReportViewModel
- {
- DimensionOne ="Acconting" ,
- Quantity = rnd.Adjacent( 10 )
- } );
- lstModel.Add(new SimpleReportViewModel
- {
- DimensionOne ="Support" ,
- Quantity = rnd.Adjacent( 10 )
- } );
- lstModel.Add(new SimpleReportViewModel
- {
- DimensionOne ="Logistics" ,
- Quantity = rnd.Adjacent( ten )
- } );
- return View( lstModel );
- }
View
- @model List<SimpleReportViewModel>
- @{
- var XLabels = Newtonsoft.Json.JsonConvert.SerializeObject( Model.Select( 10 => x.DimensionOne ).ToList() );
- var YValues = Newtonsoft.Json.JsonConvert.SerializeObject( Model.Select( x => 10.Quantity ).ToList() );
- ViewData["Title" ] = "Bar Nautical chart" ;
- }
- <!DOCTYPE html>
- <html>
- <caput>
- <meta proper name="viewport" content= "width=device-width" />
- <title>Bar</championship>
- </caput>
- <trunk>
- <divcourse = "box-body" >
- <divform = "chart-container" >
- <canvas id="chart" fashion= "width:100%; height:500px" ></sheet>
- </div>
- </div>
- </body>
- </html>
- <script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.seven.2/Nautical chart.bundle.min.js" ></script>
- <script src="https://code.jquery.com/jquery-3.3.1.min.js" ></script>
- <script type="text/javascript" >
- $(function () {
- var chartName ="nautical chart" ;
- var ctx = document.getElementById(chartName).getContext('2d' );
- var information = {
- labels: @Html.Raw(XLabels),
- datasets: [{
- label:"Departments Nautical chart" ,
- backgroundColor: [
- 'rgba(255, 99, 132, 0.2)' ,
- 'rgba(54, 162, 235, 0.ii)' ,
- 'rgba(255, 206, 86, 0.2)' ,
- 'rgba(75, 192, 192, 0.two)' ,
- 'rgba(153, 102, 255, 0.2)' ,
- 'rgba(255, 159, 64, 0.2)' ,
- 'rgba(255, 0, 0)' ,
- 'rgba(0, 255, 0)' ,
- 'rgba(0, 0, 255)' ,
- 'rgba(192, 192, 192)' ,
- 'rgba(255, 255, 0)' ,
- 'rgba(255, 0, 255)'
- ],
- borderColor: [
- 'rgba(255,99,132,1)' ,
- 'rgba(54, 162, 235, 1)' ,
- 'rgba(255, 206, 86, ane)' ,
- 'rgba(75, 192, 192, 1)' ,
- 'rgba(153, 102, 255, ane)' ,
- 'rgba(255, 159, 64, 1)' ,
- 'rgba(255, 0, 0)' ,
- 'rgba(0, 255, 0)' ,
- 'rgba(0, 0, 255)' ,
- 'rgba(192, 192, 192)' ,
- 'rgba(255, 255, 0)' ,
- 'rgba(255, 0, 255)'
- ],
- borderWidth: 1,
- data: @Html.Raw(YValues)
- }]
- };
- var options = {
- maintainAspectRatio:faux ,
- scales: {
- yAxes: [{
- ticks: {
- min: 0,
- beginAtZero:true
- },
- gridLines: {
- display:true ,
- color:"rgba(255,99,164,0.2)"
- }
- }],
- xAxes: [{
- ticks: {
- min: 0,
- beginAtZero:true
- },
- gridLines: {
- display:false
- }
- }]
- }
- };
- var myChart =new Chart(ctx, {
- options: options,
- data: data,
- type:'bar'
- });
- });
- </script>
Consequence
ExplanationHere nosotros utilize the SimpleReportViewModel considering we accept a simple bar chart with a department list and their head number
Pie ChartController Action
- public IActionResult Pie()
- {
- individual Random rnd = new Random();
- var lstModel =new List<SimpleReportViewModel>();
- lstModel.Add(new SimpleReportViewModel
- {
- DimensionOne ="Beer" ,
- Quantity = rnd.Next( 10 )
- } );
- lstModel.Add(new SimpleReportViewModel
- {
- DimensionOne ="Wine" ,
- Quantity = rnd.Side by side( ten )
- } );
- lstModel.Add(new SimpleReportViewModel
- {
- DimensionOne ="Whisky" ,
- Quantity = rnd.Side by side( 10 )
- } );
- lstModel.Add(new SimpleReportViewModel
- {
- DimensionOne ="Water" ,
- Quantity = rnd.Side by side( 10 )
- } );
- render View( lstModel );
- }
View
- @ using System.Linq;
- @model Listing<SimpleReportViewModel>
- @{
- var XLabels = Newtonsoft.Json.JsonConvert.SerializeObject( Model.Select( x => x.DimensionOne ).ToList() );
- var YValues = Newtonsoft.Json.JsonConvert.SerializeObject( Model.Select( x => x.Quantity ).ToList() );
- ViewData["Title" ] = "Pie Chart" ;
- }
- <!DOCTYPE html>
- <html>
- <head>
- <meta proper noun="viewport" content= "width=device-width" />
- <championship>Pie</championship>
- </head>
- <torso>
- <divclass = "box-body" >
- <divclass = "chart-container" >
- <canvas id="chart" style= "width:100%; peak:500px" ></sheet>
- </div>
- </div>
- </trunk>
- </html>
- <script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.7.2/Nautical chart.packet.min.js" ></script>
- <script src="https://code.jquery.com/jquery-3.three.1.min.js" ></script>
- <script type="text/javascript" >
- $(function () {
- var chartName ="nautical chart" ;
- var ctx = certificate.getElementById(chartName).getContext('2d' );
- var information = {
- labels: @Html.Raw(XLabels),
- datasets: [{
- label:"Drinks Nautical chart" ,
- backgroundColor: [
- 'rgba(255, 99, 132, 0.2)' ,
- 'rgba(54, 162, 235, 0.2)' ,
- 'rgba(255, 206, 86, 0.2)' ,
- 'rgba(75, 192, 192, 0.2)' ,
- 'rgba(153, 102, 255, 0.2)' ,
- 'rgba(255, 159, 64, 0.ii)' ,
- 'rgba(255, 0, 0)' ,
- 'rgba(0, 255, 0)' ,
- 'rgba(0, 0, 255)' ,
- 'rgba(192, 192, 192)' ,
- 'rgba(255, 255, 0)' ,
- 'rgba(255, 0, 255)'
- ],
- borderColor: [
- 'rgba(255,99,132,1)' ,
- 'rgba(54, 162, 235, one)' ,
- 'rgba(255, 206, 86, 1)' ,
- 'rgba(75, 192, 192, 1)' ,
- 'rgba(153, 102, 255, 1)' ,
- 'rgba(255, 159, 64, i)' ,
- 'rgba(255, 0, 0)' ,
- 'rgba(0, 255, 0)' ,
- 'rgba(0, 0, 255)' ,
- 'rgba(192, 192, 192)' ,
- 'rgba(255, 255, 0)' ,
- 'rgba(255, 0, 255)'
- ],
- borderWidth: 1,
- information: @Html.Raw(YValues)
- }]
- };
- var options = {
- maintainAspectRatio:false ,
- scales: {
- yAxes: [{
- ticks: {
- min: 0,
- beginAtZero:true
- },
- gridLines: {
- brandish:true ,
- color:"rgba(255,99,164,0.2)"
- }
- }],
- xAxes: [{
- ticks: {
- min: 0,
- beginAtZero:true
- },
- gridLines: {
- display:false
- }
- }]
- }
- };
- var myChart =new Nautical chart(ctx, {
- options: options,
- information: data,
- type:'pie'
- });
- });
- </script>
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
- public IActionResult Line()
- {
- private Random rnd = new Random();
- var lstModel =new List<SimpleReportViewModel>();
- lstModel.Add(new SimpleReportViewModel
- {
- DimensionOne ="Brazil" ,
- Quantity = rnd.Next( 10 )
- } );
- lstModel.Add together(new SimpleReportViewModel
- {
- DimensionOne ="Usa" ,
- Quantity = rnd.Side by side( 10 )
- } );
- lstModel.Add(new SimpleReportViewModel
- {
- DimensionOne ="Portugal" ,
- Quantity = rnd.Side by side( 10 )
- } );
- lstModel.Add together(new SimpleReportViewModel
- {
- DimensionOne ="Russia" ,
- Quantity = rnd.Next( 10 )
- } );
- lstModel.Add together(new SimpleReportViewModel
- {
- DimensionOne ="Ireland" ,
- Quantity = rnd.Next( 10 )
- } );
- lstModel.Add together(new SimpleReportViewModel
- {
- DimensionOne ="Germany" ,
- Quantity = rnd.Adjacent( 10 )
- } );
- render View( lstModel );
- }
View
- @model List<SimpleReportViewModel>
- @{
- var XLabels = Newtonsoft.Json.JsonConvert.SerializeObject( Model.Select( x => x.DimensionOne ).ToList() );
- var YValues = Newtonsoft.Json.JsonConvert.SerializeObject( Model.Select( ten => x.Quantity ).ToList() );
- ViewData["Title" ] = "Line Chart" ;
- }
- <!DOCTYPE html>
- <html>
- <caput>
- <meta proper noun="viewport" content= "width=device-width" />
- <championship>Line</championship>
- </head>
- <body>
- <divform = "box-torso" >
- <divclass = "chart-container" >
- <canvas id="chart" style= "width:100%; height:500px" ></sheet>
- </div>
- </div>
- </body>
- </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.3.1.min.js" ></script>
- <script type="text/javascript" >
- $(function () {
- var chartName ="nautical chart" ;
- var ctx = document.getElementById(chartName).getContext('2d' );
- var data = {
- labels: @Html.Raw(XLabels),
- datasets: [{
- label:"Countries Chart" ,
- backgroundColor: [
- 'rgba(255, 99, 132, 0.two)' ,
- 'rgba(54, 162, 235, 0.2)' ,
- 'rgba(255, 206, 86, 0.ii)' ,
- 'rgba(75, 192, 192, 0.2)' ,
- 'rgba(153, 102, 255, 0.ii)' ,
- 'rgba(255, 159, 64, 0.2)' ,
- 'rgba(255, 0, 0)' ,
- 'rgba(0, 255, 0)' ,
- 'rgba(0, 0, 255)' ,
- 'rgba(192, 192, 192)' ,
- 'rgba(255, 255, 0)' ,
- 'rgba(255, 0, 255)'
- ],
- borderColor: [
- 'rgba(255,99,132,1)' ,
- 'rgba(54, 162, 235, 1)' ,
- 'rgba(255, 206, 86, 1)' ,
- 'rgba(75, 192, 192, 1)' ,
- 'rgba(153, 102, 255, 1)' ,
- 'rgba(255, 159, 64, 1)' ,
- 'rgba(255, 0, 0)' ,
- 'rgba(0, 255, 0)' ,
- 'rgba(0, 0, 255)' ,
- 'rgba(192, 192, 192)' ,
- 'rgba(255, 255, 0)' ,
- 'rgba(255, 0, 255)'
- ],
- borderWidth: 1,
- data: @Html.Raw(YValues)
- }]
- };
- var options = {
- maintainAspectRatio:fake ,
- scales: {
- yAxes: [{
- ticks: {
- min: 0,
- beginAtZero:true
- },
- gridLines: {
- display:true ,
- color:"rgba(255,99,164,0.ii)"
- }
- }],
- xAxes: [{
- ticks: {
- min: 0,
- beginAtZero:true
- },
- gridLines: {
- display:false
- }
- }]
- }
- };
- var myChart =new Chart(ctx, {
- options: options,
- data: data,
- type:'line'
- });
- });
- </script>
Effect
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
- public IActionResult Stacked()
- {
- private Random rnd = new Random();
- var lstModel =new List<StackedViewModel>();
- lstModel.Add(new StackedViewModel
- {
- StackedDimensionOne ="First Quarter" ,
- LstData =new List<SimpleReportViewModel>()
- {
- new SimpleReportViewModel()
- {
- DimensionOne="Idiot box" ,
- Quantity = rnd.Side by side(10)
- },
- new SimpleReportViewModel()
- {
- DimensionOne="Games" ,
- Quantity = rnd.Next(10)
- },
- new SimpleReportViewModel()
- {
- DimensionOne="Books" ,
- Quantity = rnd.Next(10)
- }
- }
- } );
- lstModel.Add together(new StackedViewModel
- {
- StackedDimensionOne ="2d Quarter" ,
- LstData =new List<SimpleReportViewModel>()
- {
- new SimpleReportViewModel()
- {
- DimensionOne="TV" ,
- Quantity = rnd.Next(10)
- },
- new SimpleReportViewModel()
- {
- DimensionOne="Games" ,
- Quantity = rnd.Next(ten)
- },
- new SimpleReportViewModel()
- {
- DimensionOne="Books" ,
- Quantity = rnd.Side by side(ten)
- }
- }
- } );
- lstModel.Add(new StackedViewModel
- {
- StackedDimensionOne ="Third Quarter" ,
- LstData =new List<SimpleReportViewModel>()
- {
- new SimpleReportViewModel()
- {
- DimensionOne="TV" ,
- Quantity = rnd.Next(10)
- },
- new SimpleReportViewModel()
- {
- DimensionOne="Games" ,
- Quantity = rnd.Next(ten)
- },
- new SimpleReportViewModel()
- {
- DimensionOne="Books" ,
- Quantity = rnd.Next(10)
- }
- }
- } );
- lstModel.Add(new StackedViewModel
- {
- StackedDimensionOne ="Fourth Quarter" ,
- LstData =new List<SimpleReportViewModel>()
- {
- new SimpleReportViewModel()
- {
- DimensionOne="TV" ,
- Quantity = rnd.Next(10)
- },
- new SimpleReportViewModel()
- {
- DimensionOne="Games" ,
- Quantity = rnd.Side by side(10)
- },
- new SimpleReportViewModel()
- {
- DimensionOne="Books" ,
- Quantity = rnd.Next(10)
- }
- }
- } );
- return View( lstModel );
- }
View
- @model List < StackedViewModel > @ {
- var XLabels = Newtonsoft.Json.JsonConvert.SerializeObject(Model.FirstOrDefault().LstData.Select(x => x.DimensionOne).ToList());
- var YValues = Newtonsoft.Json.JsonConvert.SerializeObject(Model.Select(10 => x.LstData.Select(w => west.Quantity)).ToList());
- var label2 = Newtonsoft.Json.JsonConvert.SerializeObject(Model.Select(x => x.StackedDimensionOne).ToList());
- ViewData["Title" ] = "Stacked Chart" ;
- } < !DOCTYPE html > < html > < head > < meta name ="viewport"
- content ="width=device-width" / > < title > Stacked < /championship> < /caput> < body > < div class = "box-torso" > < div form = "nautical chart-container" > < canvas id = "chartStacked"
- 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 () {
- var chartName = "chartStacked" ;
- var ctx = document.getElementById(chartName).getContext( '2d' );
- var XLabels = @Html.Raw(XLabels);
- var YValues = @Html.Raw(YValues);
- var characterization = @Html.Raw(label2);
- var aux = 0;
- var barChartData = {
- labels: @Html.Raw(label2),
- datasets: []
- }
- XLabels.forEach(function (a, i) {
- var data = [];
- YValues.forEach(function (a, i) {
- information.push button(a[aux]);
- });
- barChartData.datasets.button({
- label: XLabels[aux],
- backgroundColor: random_rgba(),
- data: data
- });
- aux++;
- });
- var options = {
- maintainAspectRatio:false ,
- scales: {
- yAxes: [{
- ticks: {
- min: 0,
- beginAtZero:truthful
- },
- stacked:true ,
- gridLines: {
- display:truthful ,
- colour:"rgba(255,99,164,0.two)"
- }
- }],
- xAxes: [{
- stacked:true ,
- gridLines: {
- display:false
- }
- }]
- }
- };
- function random_rgba() {
- var o = Math.round,
- r = Math.random,
- s = 255;
- render 'rgba(' + o(r() * s) + ',' + o(r() * s) + ',' + o(r() * s) + ',' + r().toFixed(ane) + ')' ;
- }
- var myChart = new Chart(ctx, {
- options: options,
- data: barChartData,
- type:'bar'
- });
- }); < /script>
Event
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