From 60365f7ee8b34a3d4c92fde708e5fa7110e0ce34 Mon Sep 17 00:00:00 2001 From: Daniel Friesel Date: Mon, 23 Feb 2015 10:47:44 +0100 Subject: proper number formatting --- index.pl | 8 ++++---- public/js/d3funcs.js | 14 ++++++++------ templates/main.html.ep | 52 +++++++++++++++++++++++++------------------------- 3 files changed, 38 insertions(+), 36 deletions(-) diff --git a/index.pl b/index.pl index ff1cf65..2188c21 100644 --- a/index.pl +++ b/index.pl @@ -96,16 +96,16 @@ get '/2ddata.tsv' => sub { count(is_canceled) from $table where $where_clause group by aggregate }; } - when ('cancel_percent') { + when ('cancel_rate') { $query = qq{ select $format as aggregate, - avg(is_canceled) * 100 from $table where $where_clause group by aggregate + avg(is_canceled) from $table where $where_clause group by aggregate }; } - when ('message_percent') { + when ('message_rate') { $query = qq{ select $format as aggregate, - avg(msgtable.train_id is not null) * 100 from departures + avg(msgtable.train_id is not null) from departures left outer join msg_$msgnum as msgtable using (scheduled_time, train_id) where $where_clause group by aggregate }; diff --git a/public/js/d3funcs.js b/public/js/d3funcs.js index 4fafb42..15fe5eb 100644 --- a/public/js/d3funcs.js +++ b/public/js/d3funcs.js @@ -1,10 +1,9 @@ -show_bargraph = function(datasource, title, xlab, ylab) { +show_bargraph = function(datasource, title, xLabel, yLabel, yFormat) { + var margin = {top: 40, right: 20, bottom: 30, left: 40}, width = 960 - margin.left - margin.right, height = 500 - margin.top - margin.bottom; - var formatPercent = d3.format(".0%"); - var x = d3.scale.ordinal() .rangeRoundBands([0, width], .1); @@ -18,7 +17,10 @@ show_bargraph = function(datasource, title, xlab, ylab) { var yAxis = d3.svg.axis() .scale(y) .orient("left"); - // .tickFormat(formatPercent); + + if (yFormat) { + yAxis = yAxis.tickFormat(d3.format(yFormat)); + } var tip = d3.tip() .attr('class', 'd3-tip') @@ -48,7 +50,7 @@ show_bargraph = function(datasource, title, xlab, ylab) { .attr("x", width / 2) .attr("y", 30) .style("text-anchor", "middle") - .text(xlab); + .text(xLabel); svg.append("g") .attr("class", "y axis") @@ -58,7 +60,7 @@ show_bargraph = function(datasource, title, xlab, ylab) { .attr("y", 6) .attr("dy", ".71em") .style("text-anchor", "end") - .text(ylab); + .text(yLabel); svg.append("text") .attr("x", (width / 2)) diff --git a/templates/main.html.ep b/templates/main.html.ep index 6294a18..6038879 100644 --- a/templates/main.html.ep +++ b/templates/main.html.ep @@ -1,33 +1,33 @@ %= javascript begin show_bargraph('/2ddata.tsv?aggregate=hour&metric=delay', 'Durchschnittliche Verspätung nach Uhrzeit', - 'Angebrochene Stunde', 'Minuten'); + 'Angebrochene Stunde', 'Minuten', '.1f'); -show_bargraph('/2ddata.tsv?aggregate=hour&metric=cancel_percent', 'Anteil Zugausfälle nach Uhrzeit', - 'Angebrochene Stunde', 'Ausfälle [%]'); +show_bargraph('/2ddata.tsv?aggregate=hour&metric=cancel_rate', 'Anteil Zugausfälle nach Uhrzeit', + 'Angebrochene Stunde', 'Ausfälle', '.1%'); show_bargraph('/2ddata.tsv?aggregate=weekday&metric=delay', 'Durchschnittliche Verspätung nach Wochentag', - 'Wochentag', 'Minuten'); + 'Wochentag', 'Minuten', '.1f'); -show_bargraph('/2ddata.tsv?aggregate=weekday&metric=cancel_percent', 'Anteil Zugausfälle nach Wochentag', - 'Wochentag', 'Ausfälle [%]'); +show_bargraph('/2ddata.tsv?aggregate=weekday&metric=cancel_rate', 'Anteil Zugausfälle nach Wochentag', + 'Wochentag', 'Ausfälle', '.1%'); show_bargraph('/2ddata.tsv?aggregate=weekhour&metric=delay', 'Durchschnittliche Verspätung nach Wochentag und Uhrzeit', - 'Tag/Stunde', 'Minuten'); + 'Tag/Stunde', 'Minuten', '.1f'); show_bargraph('/2ddata.tsv?aggregate=line&metric=delay', 'Durchschnittliche Verspätung nach Linie', - 'Typ', 'Minuten'); + 'Typ', 'Minuten', '.1f'); -show_bargraph('/2ddata.tsv?aggregate=line&metric=cancel_percent', 'Anteil Zugausfälle nach Linie', - 'typ', 'Ausfälle [%]'); +show_bargraph('/2ddata.tsv?aggregate=line&metric=cancel_rate', 'Anteil Zugausfälle nach Linie', + 'typ', 'Ausfälle', '.1%'); show_bargraph('/2ddata.tsv?aggregate=train_type&metric=delay', 'Durchschnittliche Verspätung nach Zugtyp', - 'Typ', 'Minuten'); + 'Typ', 'Minuten', '.1f'); -show_bargraph('/2ddata.tsv?aggregate=train_type&metric=cancel_percent', 'Anteil Zugausfälle nach Zugtyp', - 'typ', 'Ausfälle [%]'); +show_bargraph('/2ddata.tsv?aggregate=train_type&metric=cancel_rate', 'Anteil Zugausfälle nach Zugtyp', + 'typ', 'Ausfälle', '.1%'); -msglist = [[2, 'Polizeiliche Ermittlung'], +var msglist = [[2, 'Polizeiliche Ermittlung'], [3, 'Feuerwehreinsatz neben der Strecke'], [5, 'Aerztliche Versorgung eines Fahrgastes'], [6, 'Betaetigen der Notbremse'], @@ -74,21 +74,21 @@ msglist = [[2, 'Polizeiliche Ermittlung'], [60, 'Reduzierte Geschwindigkeit wegen Sturm'], [61, 'Tuerstoerung']]; for (msg in msglist) { - show_bargraph('/2ddata.tsv?aggregate=hour&metric=message_percent&msgnum=' + msg[0], - 'Anteil "'+msg[1]+'"', 'Angebrochene Stunde', '%'); - show_bargraph('/2ddata.tsv?aggregate=weekday&metric=message_percent&msgnum=' + msg[0], - 'Anteil "'+msg[1]+'"', 'Wochentag', '%'); + show_bargraph('/2ddata.tsv?aggregate=hour&metric=message_rate&msgnum=' + msg[0], + 'Anteil "'+msg[1]+'"', 'Angebrochene Stunde', '%', '.1%'); + show_bargraph('/2ddata.tsv?aggregate=weekday&metric=message_rate&msgnum=' + msg[0], + 'Anteil "'+msg[1]+'"', 'Wochentag', '%', '.1%'); } -show_bargraph('/2ddata.tsv?aggregate=hour&metric=message_percent&msgnum=80', - 'Anteil "Abweichende Wagenreihung"', 'Angebrochene Stunde', '%'); -show_bargraph('/2ddata.tsv?aggregate=weekday&metric=message_percent&msgnum=80', - 'Anteil "Abweichende Wagenreihung"', 'Wochentag', '%'); +show_bargraph('/2ddata.tsv?aggregate=hour&metric=message_rate&msgnum=80', + 'Anteil "Abweichende Wagenreihung"', 'Angebrochene Stunde', '%', '.1%'); +show_bargraph('/2ddata.tsv?aggregate=weekday&metric=message_rate&msgnum=80', + 'Anteil "Abweichende Wagenreihung"', 'Wochentag', '%', '.1%'); -show_bargraph('/2ddata.tsv?aggregate=hour&metric=message_percent&msgnum=85', - 'Anteil "Ein Wagen fehlt"', 'Angebrochene Stunde', '%'); -show_bargraph('/2ddata.tsv?aggregate=weekday&metric=message_percent&msgnum=85', - 'Anteil "Ein Wagen fehlt"', 'Wochentag', '%'); +show_bargraph('/2ddata.tsv?aggregate=hour&metric=message_rate&msgnum=85', + 'Anteil "Ein Wagen fehlt"', 'Angebrochene Stunde', '%', '.1%'); +show_bargraph('/2ddata.tsv?aggregate=weekday&metric=message_rate&msgnum=85', + 'Anteil "Ein Wagen fehlt"', 'Wochentag', '%', '.1%'); % end -- cgit v1.2.3