-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpocp_transmission.html
678 lines (513 loc) · 18 KB
/
pocp_transmission.html
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
<!DOCTYPE html>
<meta charset="utf-8">
<link rel=”icon” href="/favicon.ico" type=”image/x-icon”>
<link rel=”shortcut icon” type=”image/x-icon” >
<style>
@import url(http://fonts.googleapis.com/css?family=Yanone+Kaffeesatz:400,700);
.title {
font-family: "Yanone Kaffeesatz";
text-rendering: optimizeLegibility;
font-size: 42px;
margin-top: .3em;
margin-bottom: 0;
}
.notes {
font-family: "Yanone Kaffeesatz";
text-rendering: optimizeLegibility;
font-size: 14px;
margin-top: .3em;
margin-bottom: 0;
}
svg {
font: 10px sans-serif;
}
path {
fill: lightgrey;
stroke: black;
}
line {
fill: none;
stroke: red;
}
.axis path,
.axis line {
fill: none;
stroke: black;
shape-rendering: crispEdges;
}
.brush .extent {
stroke: #fff;
fill-opacity: .125;
shape-rendering: crispEdges;
}
#pocp-list {
min-height: 1024px;
}
#pocp-list .date,
#pocp-list .day {
margin-bottom: .4em;
}
#pocp-list .day {
font-family: "Yanone Kaffeesatz";
text-rendering: optimizeLegibility;
}
#pocp-list .outage {
line-height: 1.5em;
background: #eee;
width: 840px;
margin-bottom: 1px;
}
#pocp-list .outage div {
display: inline-block;
width: 100px;
}
#pocp-list div.loss,
#pocp-list div.id {
width: 50px;
padding-right: 10px;
padding-left: 10px;
text-align: left;
}
#pocp-list div.End {
color: #999;
width: 150px;
padding-right: 10px;
padding-left: 10px;
text-align: left;
}
#pocp-list div.Outage {
width: 200px;
padding-right: 10px;
padding-left: 10px;
text-align: left;
}
#pocp-list .early {
color: green;
}
aside {
position: absolute;
left: 840px;
font-size: smaller;
width: 220px;
font-family: "Yanone Kaffeesatz";
text-rendering: optimizeLegibility;
}
</style>
<body>
<div id="charts">
<div id="line-chart" class="chart">
<div class="title">POCP transmission outage data</div>
<div class="notes"> <p>Notes: </p>
<ol class="notes_list">
<li>best viewed with Chrome or Firefox;</li>
<li>selected outage list currently by Start datetime only;</li>
<li>maximum of 100 outages listed at anyone time;</li>
<li>POCP data is horribly inconsistant (various time-dependent logic has been applied to get the data in this form - there may be issues);</li>
<li>Coming soon to a browser near you, selectors for company/island and generation type...</li>
<li>Automatic updates *should* occur daily at 1am/7am/1pm and 7pm.</li>
</ol></div>
<!--Add buttons for each generation type in the POCP database-->
<table border="0">
<tr>
<td><div id="Today">
<input name="updateButton"
type="button"
value="Today"
onclick="brushToday()"/></td>
<td><div id="Two weeks">
<input name="updateButton"
type="button"
value="Two weeks"
onclick="brushTwoWeeks()"/></td>
<td><div id="TotalNZ">
<input name="updateButton"
type="button"
value="Total NZ"
onclick="checkBothIslands();updateSelection()"/></td>
<td><div id="TotalNI">
<input name="updateButton"
type="button"
value="Total NI"
onclick="checkIsland('NI');updateSelection()"/></td>
<td><div id="TotalSI">
<input name="updateButton"
type="button"
value="Total SI"
onclick="checkIsland('SI');updateSelection()"/></td>
<table id=checkboxTable>
<tr>
<td>
<form id="island">
<input type="checkbox" checked id="NI">North Island<br>
<input type="checkbox" checked id="SI">South Island
</form>
</td>
</tr>
</table>
</div>
</div>
<aside id="totals"><span id="active">-</span> of <span id="total_outages">-</span> outages selected.</aside>
<div id="lists">
<div id="pocp-list" class="list"></div>
</div>
<!--Now for the fun stuff, d3/Javascript code -->
<script src="http://ajax.googleapis.com/ajax/libs/jquery/2.0.3/jquery.js"></script>
<script src="http://d3js.org/d3.v3.js"></script>
<script src="crossfilter.v1.js"></script>
<script src="irregular_time_series.js"></script>
<script>
// Need to create a dummy console object so that console.log doesn't cause errors in IE
if (!window.console) {
window.console = {};
}
if (!window.console.log) {
window.console.log = function () {};
}
var MIN_DATE = new Date(-8640000000000000);
var MAX_DATE = new Date(8640000000000000);
var MIN_DATE_TS = { date: MIN_DATE, price: 0 };
var MAX_DATE_TS = { date: MAX_DATE, price: 0 };
var parseDate = d3.time.format("%Y-%m-%d %H:%M:%S").parse;
var parseShortDate = d3.time.format("%d/%m/%Y").parse;
var outages; // global variables to store crossfilters and various dimenisions
//var outagesByIsland;
var outagesByStart;
var outageTimeSeries;
var timeSeriesArray;
var startDate;
var endDate;
var selectedOutages;
var selectedOutagesByStart;
var selectedOutagesByEnd;
var margin = {top: 10, right: 10, bottom: 100, left: 40},
margin2 = {top: 430, right: 10, bottom: 20, left: 40},
width = 960 - margin.left - margin.right,
height = 500 - margin.top - margin.bottom,
height2 = 500 - margin2.top - margin2.bottom;
var x = d3.time.scale().range([0, width]),
x2 = d3.time.scale().range([0, width]),
y = d3.scale.linear().range([height, 0]),
y2 = d3.scale.linear().range([height2, 0]);
var xAxis = d3.svg.axis().scale(x).orient("bottom"),
xAxis2 = d3.svg.axis().scale(x2).orient("bottom"),
yAxis = d3.svg.axis().scale(y).orient("left");
var brush = d3.svg.brush()
.x(x2)
.on("brush", brushed);
var area = d3.svg.area()
.interpolate("step-after")
.x(function(d) { return x(d.date); })
.y0(height)
.y1(function(d) { return y(d.price); });
var area2 = d3.svg.area()
.interpolate("step-after")
.x(function(d) { return x2(d.date); })
.y0(height2)
.y1(function(d) { return y2(d.price); });
var svg = d3.select("#line-chart").append("svg")
.attr("width", width + margin.left + margin.right)
.attr("height", height + margin.top + margin.bottom);
var svgDefs = svg.append("defs");
svgDefs.append("clipPath")
.attr("id", "clip")
.append("rect")
.attr("width", width)
.attr("height", height);
svgDefs.append("clipPath")
.attr("id", "clip2")
.append("rect")
.attr("width", width)
.attr("height", height2);
var focus = svg.append("g")
.attr("transform", "translate(" + margin.left + "," + margin.top + ")");
var context = svg.append("g")
.attr("transform", "translate(" + margin2.left + "," + margin2.top + ")");
//Load in the pocp time series data, and the outage listing data
var pocp, remaining = 2;
d3.csv("pocp_transmission_data_year.json", function(error, csv) {
pocp = csv;
if (!--remaining) doSomething(); //when remaining = 0 this becomes true, and we then run doSomething,
});
d3.json("metadata.json", function(error, metadata) {
startDate = parseShortDate(metadata.startTime);
endDate = parseShortDate(metadata.endTime);
updateTime = parseDate(metadata.updateTime);
if (!--remaining) doSomething(); //when remaining = 0 this becomes true, and we then run doSomething,
});
function doSomething() {
// A nest operator, for grouping the pocp list.
var nestByDate = d3.nest()
.key(function(d) { return d3.time.day(d.Start); });
// Various formatters.
var formatNumber = d3.format(",d");
var formatChange = d3.format("+,d");
var formatDateday = d3.time.format("%Y-%m-%d");
var formatDatetime = d3.time.format("%H:%M:%S");
var formatDate = d3.time.format("%Y-%m-%d %H:%M:%S");
d3.select("ol.notes_list").append("li")
.text("Data last updated at " + formatDate(updateTime));
//// A little coercion, since both CSVs are untyped.
//data.forEach(function(d) {
//d.date = parseDate(d.datetime);
//d['North_Contact_Energy'] = +d['North_Contact_Energy'];
////d.price = +d['North_Contact_Energy'];
//d['North_Genesis'] = +d['North_Genesis'];
//d['North_Meridian'] = +d['North_Meridian'];
//d['North_Mighty_River'] = +d['North_Mighty_River'];
//d['North_Trustpower'] = +d['North_Trustpower'];
//d['South_Contact_Energy'] = +d['South_Contact_Energy'];
//d['South_Genesis'] = +d['South_Genesis'];
//d['South_Meridian'] = +d['South_Meridian'];
////d.price = +d['South_Meridian'];
//d['South_Trustpower'] = +d['South_Trustpower'];
//d['North_Total'] = +d['North_Total'];
//d['South_Total'] = +d['South_Total'];
//d.price = +d['North_Total'];
//});
//startDate = new Date(2013, 08-1, 15);
//endDate = new Date(2014, 08-1, 15+1);
pocp.forEach(function(d, i) {
d.index = i;
d.Start = parseDate(d.Start);
d.End = parseDate(d.End);
d.Value = 1;
d.id = d.id;
d['Last Modified'] = parseDate(d['Last Modified']);
d['Outage Block'] = d['Outage Block'];
d.Owner = d.Owner;
d.type = d.type;
});
outages = crossfilter(pocp);
//outagesByIsland = outages.dimension(function(d) { return d.Island; });
outagesByStart = outages.dimension(function(d) { return d.Start; });
outageTimeSeries = outages.groupAll().reduce(
function(prev, curr) {
return addIrregularTimeSeries(prev, constantIrregularTimeSeries(curr));
},
function(prev, curr) {
return subtractIrregularTimeSeries(prev, constantIrregularTimeSeries(curr));
},
function() {
return [];
}
);
//debugger;
focus.append("path")
.attr("clip-path", "url(#clip)");
focus.append("line")
.attr("clip-path", "url(#clip)")
.attr("y1", 0)
.attr("y2", height);
focus.append("g")
.attr("class", "x axis")
.attr("transform", "translate(0," + height + ")");
focus.append("g")
.attr("class", "y axis");
context.append("path")
.attr("clip-path", "url(#clip2)");
context.append("line")
.attr("clip-path", "url(#clip2)")
.attr("y1", 0)
.attr("y2", height2);
context.append("g")
.attr("class", "x axis")
.attr("transform", "translate(0," + height2 + ")");
context.append("g")
.attr("class", "x brush");
updateSelection();
$("#checkboxTable").on("click", ":checkbox", updateSelection);
};
//In this function we can return the brush elements, in this case brush.extent()[0] is the zoomed start.
//We now want to use this with the POCP csv data to list the outages for this duration
//What is the best method to achieve this?
function brushed() {
x.domain(brush.empty() ? x2.domain() : brush.extent());
focus.select("path").attr("d", area);
focus.select(".x.axis").call(xAxis);
var currentTime = new Date();
focus.select("line")
.datum(currentTime)
.attr("x1", x)
.attr("x2", x);
var start = brush.extent()[0]
var end = brush.extent()[1]
//filter pocp list data
// Create the crossfilter for the relevant dimensions and groups.
var n = selectedOutages.groupAll().reduceCount().value();
var all = selectedOutages.groupAll();
//var top_owner_loss=Owners.top(1)
//Attempt to filter current outages with crossfilter filter
//var outage_by_start = outage.dimension(function(d){return d.Start});
//var outage_by_end = outage.dimension(function(d){return d.End});
//outage_by_start.filter([-Infinity, now])
//outage_by_end.filter([now,Infinity])
//console.log("There is a total of " + n + " generation outages")
//Filter out the current outages from the data set
//filter dimension to get outages occuring now
selectedOutagesByStart.filter([-Infinity, end]);
selectedOutagesByEnd.filter([start,Infinity]);
var current_outages = selectedOutagesByStart.top(Infinity); //sorted by ['Start']
//console.log(current_outages)
//console.log(sf)
//console.log("Of which " + outage.groupAll().reduceCount().value() + " are within selected date span")
//console.log("The current largest outage is " + Loss.top(4)[0]['Outage Block'] + " = " + Loss.top(4)[0]['MW Loss'] + "MW")
//********************************************************************//
var nestByDate = d3.nest()
.key(function(d) { return d3.time.day(d.Start); });
// Various formatters.
var formatNumber = d3.format(",d");
var formatChange = d3.format("+,d");
var formatDateday = d3.time.format("%Y-%m-%d");
var formatDatetime = d3.time.format("%H:%M:%S");
var formatDate = d3.time.format("%Y-%m-%d %H:%M:%S");
//Try and add a list of outages
// Render the initial lists.
var list = d3.selectAll(".list")
.data([pocpList]);
// Render the total.
d3.selectAll("#total_outages")
.text(formatNumber(outages.size()));
var outage = crossfilter(current_outages),
all = outage.groupAll(),
Startdate = outage.dimension(function(d) { return d3.time.day(d.Start); });
renderAll();
// Renders the specified chart or list.
function render(method) {
d3.select(this).call(method);
}
// Whenever the brush moves, re-rendering everything.
function renderAll() {
//chart.each(render);
list.each(render);
d3.select("#active").text(formatNumber(all.value()));
}
function pocpList(div) {
var outagesByDate = nestByDate.entries(Startdate.top(100));
div.each(function() {
var Startdate = d3.select(this).selectAll(".date")
.data(outagesByDate, function(d) { return d.key; });
Startdate.enter().append("div")
.attr("class", "date")
.append("div")
.attr("class", "day")
.text(function(d) { return formatDateday(d.values[0].Start); });
Startdate.exit().remove();
var outage = Startdate.order().selectAll(".outage")
.data(function(d) {
// Sort values for each group, since they aren't guaranteed to be in order within each group.
return d.values.sort(function(a, b) { return b.Start - a.Start; });
}, function(d) { return d.index; });
var outageEnter = outage.enter().append("div")
.attr("class", "outage");
outageEnter.append("div")
.attr("class", "Start")
.text(function(d) { return formatDatetime(d.Start); });
outageEnter.append("div")
.attr("class", "End")
.text(function(d) { return formatDate(d.End); });
outageEnter.append("div")
.attr("class", "Outage")
.text(function(d) { return d['Outage Block']; });
outageEnter.append("div")
.attr("class", "Owner")
.text(function(d) { return d.Owner; });
outageEnter.append("div")
.attr("class", "ID")
.text(function(d) { return d.id; });
outage.exit().remove();
outage.order();
});
}
//********************************************************************//
}
function brushToday() {
var today = new Date();
var dayStart = new Date(today.getFullYear(), today.getMonth(), today.getDate());
var dayEnd = new Date(today.getFullYear(), today.getMonth(), today.getDate() + 1);
brush.extent([dayStart, dayEnd]);
context.select(".x.brush")
.call(brush);
brushed();
}
function brushTwoWeeks() {
var today = new Date();
var periodStart = new Date(today.getFullYear(), today.getMonth(), today.getDate() - 7);
var periodEnd = new Date(today.getFullYear(), today.getMonth(), today.getDate() + 8);
brush.extent([periodStart, periodEnd]);
context.select(".x.brush")
.call(brush);
brushed();
}
//function checkBothIslands() {
//$("#island > input:checkbox").prop("checked", true);
//}
//function checkIsland(island) {
//var islands = $("#island > input:checkbox");
//islands.prop("checked", false);
//islands.filter("#" + island).prop("checked", true);
//}
function addHalfHour(date) {
return new Date(date.getTime() + 1000*60*30);
}
function updateSelection() {
console.log("Update selection");
//var islands = $("#island > input:checkbox");
//var ni = islands.filter("#NI").is(":checked");
//var si = islands.filter("#SI").is(":checked");
//outagesByIsland.filterFunction(function(i) {
//return ni && i === "NI" ||
//si && i === "SI";
//});
var timeSeries = outageTimeSeries.value().slice(0);
//debugger;
if (timeSeries.length === 0 || timeSeries[0].date > startDate)
timeSeries.unshift({ date: startDate, price: 0 });
if (timeSeries[timeSeries.length-1].date < endDate)
timeSeries.push({ date: endDate, price: 0 });
//timeSeriesArray = new Array(timeSeries.values.length);
//for (var i = 0, date = timeSeries.startTime; i < timeSeries.values.length; i++, date = addHalfHour(date)) {
//timeSeriesArray[i] = {
//date: date,
//price: timeSeries.values[i]
//};
//}
//debugger;
//Setup scales
x.domain(brush.empty() ? [startDate, endDate] : brush.extent());
y.domain([0, d3.max(timeSeries, function(d) { return d.price; } )]);
x2.domain([startDate, endDate]);
y2.domain(y.domain());
var currentTime = new Date();
focus.select("path")
.datum(timeSeries)
.attr("d", area);
focus.select("line")
.datum(currentTime)
.attr("x1", x)
.attr("x2", x);
focus.select(".x.axis")
.call(xAxis);
focus.select(".y.axis")
.call(yAxis);
context.select("path")
.datum(timeSeries)
.attr("d", area2);
context.select("line")
.datum(currentTime)
.attr("x1", x2)
.attr("x2", x2);
context.select(".x.axis")
.call(xAxis2);
context.select(".x.brush")
.call(brush)
.selectAll("rect")
.attr("y", -6)
.attr("height", height2 + 7);
selectedOutages = crossfilter(outagesByStart.top(Infinity));
//selectedOutagesByOwner = selectedOutages.dimension(function(d) { return d.Owner; });
//selectedOutagesByLoss = selectedOutages.dimension(function(d) { return d['MW Loss']; });
selectedOutagesByStart = selectedOutages.dimension(function(d) { return d.Start; });
selectedOutagesByEnd = selectedOutages.dimension(function(d) { return d.End; });
brushed();
}
</script>