This repository was archived by the owner on Oct 22, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy pathrefresh.js
More file actions
125 lines (110 loc) · 3.4 KB
/
Copy pathrefresh.js
File metadata and controls
125 lines (110 loc) · 3.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
/* Copyright 2015 Amazon.com, Inc. or its affiliates. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License"). You may not use
this file except in compliance with the License. A copy of the License is
located at
http://aws.amazon.com/apache2.0/
or in the "license" file accompanying this file. This file is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
implied. See the License for the specific language governing permissions and
limitations under the License. */
// Region and IdentityPoolId should be set to your own values
AWS.config.region = '<your-region-here>'; // Region
AWS.config.credentials = new AWS.CognitoIdentityCredentials({
IdentityPoolId: '<your-identity-pool-id-here>',
});
var dynamodb = new AWS.DynamoDB();
var params = { TableName: 'VoteAppAggregates' };
/* Create the context for applying the chart to the HTML canvas */
var ctx = $("#graph").get(0).getContext("2d");
/* Set the options for our chart */
var options = { segmentShowStroke : false,
animateScale: true,
percentageInnerCutout : 50,
showToolTips: true,
tooltipEvents: ["mousemove", "touchstart", "touchmove"],
tooltipFontColor: "#fff",
animationEasing : 'easeOutCirc'
}
/* Set the initial data */
var init = [
{
value: 1,
color: "#e74c3c",
highlight: "#c0392b",
label: "Red"
},
{
value: 1,
color: "#2ecc71",
highlight: "#27ae60",
label: "Green"
},
{
value: 1,
color: "#3498db",
highlight: "#2980b9",
label: "Blue"
}
];
graph = new Chart(ctx).Doughnut(init, options);
$(function() {
getData();
$.ajaxSetup({ cache: false });
/* Get the data every 3 seconds */
setInterval(getData, 3000);
});
/* Makes a scan of the DynamoDB table to set a data object for the chart */
function getData() {
dynamodb.scan(params, function(err, data) {
if (err) {
console.log(err);
return null;
} else {
var redCount = 0;
var greenCount = 0;
var blueCount = 0;
for (var i in data['Items']) {
if (data['Items'][i]['VotedFor']['S'] == "RED") {
redCount = parseInt(data['Items'][i]['Vote']['N']);
}
if (data['Items'][i]['VotedFor']['S'] == "GREEN") {
greenCount = parseInt(data['Items'][i]['Vote']['N']);
}
if (data['Items'][i]['VotedFor']['S'] == "BLUE") {
blueCount = parseInt(data['Items'][i]['Vote']['N']);
}
}
var data = [
{
value: redCount,
color:"#e74c3c",
highlight: "#c0392b",
label: "Red"
},
{
value: greenCount,
color: "#2ecc71",
highlight: "#27ae60",
label: "Green"
},
{
value: blueCount,
color: "#3498db",
highlight: "#2980b9",
label: "Blue"
}
];
/* Only update if we have new values (preserves tooltips) */
if ( graph.segments[0].value != data[0].value ||
graph.segments[1].value != data[1].value ||
graph.segments[2].value != data[2].value
)
{
graph.segments[0].value = data[0].value;
graph.segments[1].value = data[1].value;
graph.segments[2].value = data[2].value;
graph.update();
}
}
});
}