I am trying to create a chart.js graph in my .Net Core Web app, with data from the database. I am using ajax to call a method that will pull the data from the database, but I’m unsure how to group the data to display in the graph.
At the moment I have a database that looks like so:
I am looking to show the time along the bottom and count how many jobs success and how m any jobs exception. At the moment my graph is hard coded.
JavaScript
x
58
58
1
// Area Chart Example
2
var ctx = document.getElementById("canvas")
3
var lineChartData = {
4
labels: ['January', 'February', 'March', 'April', 'May', 'June', 'July'],
5
datasets: [{
6
label: 'Failed',
7
borderColor: "MediumSeaGreen",
8
backgroundColor: "MediumSeaGreen",
9
fill: false,
10
data: [
11
30000, 30162, 26263, 18394, 18287, 28682, 31274, 33259, 25849,
12
24159, 32651, 31984, 38451
13
],
14
yAxisID: 'y-axis-1',
15
}, {
16
label: 'Exceptioned',
17
borderColor: "Tomato",
18
backgroundColor: "Tomato",
19
fill: false,
20
data: [
21
20000, 30162, 26263, 33259, 18287, 28682, 25849, 18394, 25849,
22
24159, 32651, 31984, 38451
23
],
24
yAxisID: 'y-axis-2'
25
}]
26
};
27
28
window.myLine = Chart.Line(ctx, {
29
data: lineChartData,
30
options: {
31
responsive: true,
32
hoverMode: 'index',
33
stacked: false,
34
title: {
35
display: true,
36
text: 'Processes'
37
},
38
scales: {
39
yAxes: [{
40
type: 'linear',
41
display: true,
42
position: 'left',
43
id: 'y-axis-1',
44
}, {
45
type: 'linear',
46
display: true,
47
position: 'right',
48
id: 'y-axis-2',
49
50
// grid line settings
51
gridLines: {
52
drawOnChartArea: false, // only want the grid lines for one axis to show up
53
},
54
}],
55
}
56
}
57
});
58
Advertisement
Answer
Here is a simple demo like below:
1.Model:
JavaScript
1
8
1
public class Job
2
{
3
public int JobId { get; set; }
4
public string JobName { get; set; }
5
public string JobStatus { get; set; }
6
public DateTime JobCompletion { get; set; }
7
}
8
2.View:
JavaScript
1
81
81
1
<canvas id="canvas" width="400" height="400"></canvas>
2
@section Scripts{
3
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.8.0/Chart.bundle.js" integrity="sha256-qSIshlknROr4J8GMHRlW3fGKrPki733tLq+qeMCR05Q=" crossorigin="anonymous"></script>
4
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.8.0/Chart.bundle.min.js" integrity="sha256-xKeoJ50pzbUGkpQxDYHD7o7hxe0LaOGeguUidbq6vis=" crossorigin="anonymous"></script>
5
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.8.0/Chart.css" integrity="sha256-IvM9nJf/b5l2RoebiFno92E5ONttVyaEEsdemDC6iQA=" crossorigin="anonymous" />
6
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.8.0/Chart.js" integrity="sha256-arMsf+3JJK2LoTGqxfnuJPFTU4hAK57MtIPdFpiHXOU=" crossorigin="anonymous"></script>
7
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.8.0/Chart.min.css" integrity="sha256-aa0xaJgmK/X74WM224KMQeNQC2xYKwlAt08oZqjeF0E=" crossorigin="anonymous" />
8
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.8.0/Chart.min.js" integrity="sha256-Uv9BNBucvCPipKQ2NS9wYpJmi8DTOEfTA/nH2aoJALw=" crossorigin="anonymous"></script>
9
<script>
10
function GetJSON_Simple() {
11
var resp = [];
12
$.ajax({
13
type: "GET",
14
url: '/Jobs/Index',
15
async: false,
16
contentType: "application/json",
17
success: function (data) {
18
resp.push(data);
19
},
20
error: function (req, status, error) {
21
// do something with error
22
alert("error");
23
}
24
});
25
return resp;
26
}
27
var simpleData = GetJSON_Simple();
28
var ctx = document.getElementById("canvas")
29
var lineChartData = {
30
labels: simpleData[0].myDate,
31
datasets: [{
32
label: 'Sucess',
33
borderColor: "MediumSeaGreen",
34
backgroundColor: "MediumSeaGreen",
35
fill: false,
36
data: simpleData[0].mySuccess,
37
yAxisID: 'y-axis-1',
38
}, {
39
label: 'Exceptioned',
40
borderColor: "Tomato",
41
backgroundColor: "Tomato",
42
fill: false,
43
data: simpleData[0].myException,
44
yAxisID: 'y-axis-2'
45
}]
46
};
47
48
window.myLine = Chart.Line(ctx, {
49
data: lineChartData,
50
options: {
51
responsive: true,
52
hoverMode: 'index',
53
stacked: false,
54
title: {
55
display: true,
56
text: 'Processes'
57
},
58
scales: {
59
yAxes: [{
60
type: 'linear',
61
display: true,
62
position: 'left',
63
id: 'y-axis-1',
64
}, {
65
type: 'linear',
66
display: true,
67
position: 'right',
68
id: 'y-axis-2',
69
70
// grid line settings
71
gridLines: {
72
drawOnChartArea: false, // only want the grid lines for one axis to show up
73
},
74
}],
75
}
76
}
77
});
78
79
</script>
80
}
81
3.Controller:
JavaScript
1
33
33
1
public class JobsController : Controller
2
{
3
private readonly YourContext _context;
4
5
public JobsController(YourContext context)
6
{
7
_context = context;
8
}
9
10
// GET: Jobs
11
public async Task<ActionResult> Index()
12
{
13
var date = await _context.Job.Select(j => j.JobCompletion).Distinct().ToListAsync();
14
var success =_context.Job
15
.Where(j => j.JobStatus == "Success")
16
.GroupBy(j => j.JobCompletion)
17
.Select(group=>new {
18
JobCompletion = group.Key,
19
Count=group.Count()
20
});
21
var countSuccess = success.Select(a => a.Count).ToArray();
22
var exception = _context.Job
23
.Where(j => j.JobStatus == "Exception")
24
.GroupBy(j => j.JobCompletion)
25
.Select(group => new {
26
JobCompletion = group.Key,
27
Count = group.Count()
28
});
29
var countException = exception.Select(a => a.Count).ToArray();
30
return new JsonResult(new { myDate=date,mySuccess= countSuccess, myException= countException });
31
}
32
}
33
4.Database:
5.Result: