-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathPieChart.php
executable file
·156 lines (140 loc) · 4.04 KB
/
PieChart.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
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
<?php
namespace KateMorley\Grid\UI;
use KateMorley\Grid\State\Datum;
use KateMorley\Grid\State\Map;
/** Outputs a pie chart. */
class PieChart {
private const OUTER_RADIUS = 0.75;
private const INNER_RADIUS = 0.50;
/**
* Outputs a pie chart.
*
* @param Datum $datum The datum
*/
public static function output(Datum $datum): void {
$generation = $datum->generation->getTotal();
$demand = $datum->getTotal();
$generationPower = Value::formatTotalPower($datum->demand->getGeneration());
$generationPercentage = Value::formatPercentage($generation / $demand);
echo '<div class="pie-chart"><div><div>Generation</div><div class="generation"></div><div><span>';
echo $generationPower;
echo '</span>GW</div><div><span>';
echo $generationPercentage;
echo '</span>%</div></div><svg viewBox="-1 -1 2 2" data-power="';
echo $generationPower;
echo '" data-percentage="';
echo $generationPercentage;
echo '">';
self::outputRing(
$datum->generation,
$generation,
$demand,
self::OUTER_RADIUS,
1
);
self::outputRing(
$datum->types,
$generation,
$demand,
self::INNER_RADIUS,
self::OUTER_RADIUS,
true
);
echo "</svg></div>\n";
}
/**
* Outputs a ring.
*
* @param Map $sources The sources
* @param float $generation The total generation
* @param float $demand The total demand
* @param float $innerRadius The inner radius
* @param float $outerRadius The outer radius
* @param bool $isTotal Whether this ring shows power totals
*/
private static function outputRing(
Map $sources,
float $generation,
float $demand,
float $innerRadius,
float $outerRadius,
bool $isTotal = false
): void {
$offset = 0;
foreach ($sources::KEYS as $key => $description) {
$power = $sources->get($key);
if ($power > 0) {
$fraction = $power / $generation;
self::outputArc(
$key,
($isTotal ? Value::formatTotalPower($power) : Value::formatPower($power)),
Value::formatPercentage($power / $demand),
$fraction,
$offset,
$innerRadius,
$outerRadius
);
$offset += $fraction;
}
}
}
/**
* Outputs an arc.
*
* @param string $source The source
* @param string $power The power
* @param string $percentage The percentage
* @param float $faction The fraction of the circle
* @param float $offset The faction offset
* @param float $innerRadius The inner radius
* @param float $outerRadius The outer radius
*/
private static function outputArc(
string $source,
string $power,
string $percentage,
float $faction,
float $offset,
float $innerRadius,
float $outerRadius
): void {
echo '<path class="';
echo $source;
echo '" d="M';
self::outputArcPoint($offset, $outerRadius);
echo 'A';
echo $outerRadius;
echo ',';
echo $outerRadius;
echo ' 0 ';
echo ($faction < 0.5 ? 0 : 1);
echo ' 1 ';
self::outputArcPoint($offset + $faction, $outerRadius);
echo 'L';
self::outputArcPoint($offset + $faction, $innerRadius);
echo 'A';
echo $innerRadius;
echo ',';
echo $innerRadius;
echo ' 0 ';
echo ($faction < 0.5 ? 0 : 1);
echo ' 0 ';
self::outputArcPoint($offset, $innerRadius);
echo 'z" data-power="';
echo $power;
echo '" data-percentage="';
echo $percentage;
echo '"/>';
}
/**
* Outputs the co-ordinates of a point on an arc.
*
* @param float $faction The fraction of the circle
* @param float $radius The radius
*/
private static function outputArcPoint(float $faction, float $radius): void {
printf('%0.4f', $radius * sin($faction * 2 * M_PI));
echo ',';
printf('%0.4f', $radius * -cos($faction * 2 * M_PI));
}
}