-
Notifications
You must be signed in to change notification settings - Fork 2
/
barchart.php
44 lines (41 loc) · 1.3 KB
/
barchart.php
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
<?php
function RenderBarChart($data, $width_pixels) {
$party_order = ["con", "lib", "ndp", "grn", "bq", "oth"];
$party_names = [
"con" => "Conservative",
"lib" => "Liberal",
"ndp" => "NDP",
"grn" => "Green",
"bq" => "Bloc Québecois",
"oth" => "Independent",
];
$max_value = max($data);
foreach ($party_order as $party) {
if (array_key_exists($party, $data)) {
$value = $data[$party];
} else {
$value = 0;
}
$party_name = $party_names[$party];
$bar_length = $value / $max_value * $width_pixels;
echo ("<div class=\"riding $party\"><span class=\"ridingname\">" .
"$party_name</span>" .
"<span class=\"sequence\">" .
"<span class=\"projection barchart $party-proj\" " .
"style=\"width: $bar_length\"> </span>" .
"<span class=\"estimate $party\">" .
"$value</span></span></div>\n");
}
}
function SqlBarChart($sql, $width_pixels) {
$result = query($sql);
$data = [];
while ($row = mysql_fetch_row($result)) {
$key = strtolower($row[0]);
$value = $row[1];
$data[$key] = $value;
}
mysql_free_result($result);
RenderBarChart($data, $width_pixels);
}
?>