Skip to content

Commit 3d75836

Browse files
committed
Added chart.js example
1 parent dcfe688 commit 3d75836

File tree

3 files changed

+87
-0
lines changed

3 files changed

+87
-0
lines changed

MVC_Start/Controllers/HomeController.cs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,30 @@ public IActionResult IndexWithLayout()
1818
{
1919
return View();
2020
}
21+
22+
/// <summary>
23+
/// Replicate the chart example in the JavaScript presentation
24+
///
25+
/// Typically LINQ and SQL return data as collections.
26+
/// Hence we start the example by creating collections representing the x-axis labels and the y-axis values
27+
/// However, chart.js expects data as a string, not as a collection.
28+
/// Hence we join the elements in the collections into strings in the view model
29+
/// </summary>
30+
/// <returns>View that will display the chart</returns>
31+
public ViewResult DemoChart()
32+
{
33+
string[] ChartLabels = new string[] { "Africa", "Asia", "Europe", "Latin America", "North America" };
34+
int[] ChartData = new int[] { 2478, 5267, 734, 784, 433 };
35+
36+
ChartModel Model = new ChartModel
37+
{
38+
ChartType = "bar",
39+
Labels = String.Join(",", ChartLabels.Select(d => "'" + d + "'")),
40+
Data = String.Join(",", ChartData.Select(d => d)),
41+
Title = "Predicted world population (millions) in 2050"
42+
};
43+
44+
return View(Model);
45+
}
2146
}
2247
}

MVC_Start/Models/App_Models.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,12 @@ public class GuestContact
1111
public string Email { get; set; }
1212
public string Phone { get; set; }
1313
}
14+
15+
public class ChartModel
16+
{
17+
public string ChartType { get; set; }
18+
public string Labels { get; set; }
19+
public string Data { get; set; }
20+
public string Title { get; set; }
21+
}
1422
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<!--
2+
Example to introduce charting in MVC using chart.js
3+
4+
Author: Manish Agrawal
5+
Date: Feb 25, 2019
6+
7+
Many examples use or are inspired by
8+
http://tobiasahlin.com/blog/chartjs-charts-to-get-you-started/
9+
-->
10+
11+
@model ChartModel
12+
13+
@{
14+
Layout = null;
15+
}
16+
17+
<!DOCTYPE html>
18+
19+
<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
20+
21+
<head>
22+
<meta charset="utf-8" />
23+
<title>Chart.js examples</title>
24+
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.7.3/Chart.js"></script>
25+
</head>
26+
<body>
27+
<h1> Bar chart example</h1>
28+
29+
<canvas id="barchartexample" width="800" height="450"></canvas>
30+
31+
<script>
32+
new Chart(document.getElementById("barchartexample"), {
33+
type: '@Model.ChartType',
34+
data: {
35+
labels: [@Html.Raw(@Model.Labels)],
36+
datasets: [
37+
{
38+
label: "Population (millions)",
39+
backgroundColor: ["#3e95cd", "#8e5ea2", "#3cba9f", "#e8c3b9", "#c45850"],
40+
data: [@Model.Data]
41+
}
42+
]
43+
},
44+
options: {
45+
legend: { display: false },
46+
title: {
47+
display: true,
48+
text: '@Model.Title'
49+
}
50+
}
51+
});
52+
</script>
53+
</body>
54+
</html>

0 commit comments

Comments
 (0)