From eb2a98721d692532c6f7a5f038628e7db71e3424 Mon Sep 17 00:00:00 2001 From: CMDR furrycat Date: Fri, 12 Feb 2016 06:40:31 -0500 Subject: [PATCH] Move timeline into race view. --- lib/header.php | 2 +- lib/race.php | 248 +++++++++++++++++++++++++++++++++++++++++++++++-- lib/timeline.php | 278 ------------------------------------------------------- race.js | 110 +++++++++++++++------- timeline.js | 45 --------- 5 files changed, 319 insertions(+), 364 deletions(-) delete mode 100644 lib/timeline.php delete mode 100644 timeline.js diff --git a/lib/header.php b/lib/header.php index 872be85..5e09447 100644 --- a/lib/header.php +++ b/lib/header.php @@ -9,7 +9,7 @@ Buckyball Racing Club " . ucfirst($title) . "s"; } diff --git a/lib/race.php b/lib/race.php index d9ee633..2135a88 100644 --- a/lib/race.php +++ b/lib/race.php @@ -420,6 +420,11 @@ return false; } + function ordinal($n) { + if (($n % 100) >= 11 && ($n % 100) <= 13) return "${n}th"; + else return $n . array('th', 'st', 'nd', 'rd', 'th', 'th', 'th', 'th', 'th', 'th')[$n % 10]; + } + function find_prizewinners($race, $entries) { $competitors = array(); $prizes = array(); @@ -466,6 +471,234 @@ return ($A < $B) ? 1 : -1; } + function show_timeline($race_id, $filtered_entry_ids) { + $cq = new CourseQuery; + $cq->joinWith('Course.Race'); + $cq->joinWith('Course.Station'); + $course = $cq->filterByRaceId($race_id)->find(); + $race = $start_line = $finish_line = null; + $system_ids = array(); + foreach ($course as $leg) { + if (! $race) $race = $leg->getRace(); + $station_id = $leg->getStationId(); + if ($leg->getFinishLine()) $finish_line = $station_id; + if ($leg->getStartLine()) $start_line = $station_id; + $system_ids[] = $leg->getStation()->getSystemId(); + } + $system_ids = array_unique($system_ids); + + $rq = new RaikogramQuery; + $rq->filterBySystem1($system_ids); + $rq->filterBySystem2($system_ids); + $raikograms = $rq->find(); + $distances = array(); + foreach ($raikograms as $raikogram) { + $system1 = $raikogram->getSystem1(); + $system2 = $raikogram->getSystem2(); + $distance = $raikogram->getDistance(); + $distances["from{$system1}to${system2}"] = $distance; + } + + $allotted_time = $race->getAllottedTime(); + echo "

Timeline

\n"; + echo "
\n"; + $plots = array(); + + $json_racers = array(); + $json_events = array(); + $eq = new EntryQuery; + $eq->joinWith('Entry.Ship'); + $eq->joinWith('Ship.Competitor'); + $eq->joinWith('Ship.Hull'); + $eq->filterByRaceId($race_id); + $eq->filterById($filtered_entry_ids); + $entry_objects = $eq->find(); + foreach ($entry_objects as $entry) $entries[] = $entry; + $prizewinners = find_prizewinners($race, $entries); + uasort($entries, "Entry::StandardSort"); + $ranked_entries = array(); + $competitor_entries = array(); + foreach ($entries as $entry) { + if ($entry->isExcluded()) continue; + $entry_id = $entry->getId(); + $ship =$entry->getShip(); + $competitor = $ship->getCompetitor(); + $competitor_id = $competitor->getId(); + $competitor_entries[$competitor_id] = $entry_id; + $ranked_entries[$entry_id] = true; + $json_racer = array("join" => count($json_racers) + 1); + $json_racer["owner"] = $competitor->getCmdrName(); + $json_racer["user"] = $competitor->getForumName(); + if (! $json_racer["user"]) $json_racer["user"] = $json_racer["owner"]; + $json_racer["ship"] = $ship->getName(); + $json_racer["hull"] = $ship->getHull()->getName(); + $json_racer["lap_distance"] = $entry->getLapDistance(); + $json_racer["complete_laps"] = $entry->getCompleteLaps(); + $json_racer["allotted_time"] = $allotted_time + $entry->getBonusTime(); + $json_racer["bonus_time"] = (int) $entry->getBonusTime(); + $json_racer["raced_time"] = $entry->getRacedTime(); + $json_racer["total_time"] = $entry->getTotalTime(); + $json_racer["joker"] = (bool) $entry->usedJoker(); + $json_racer["shields"] = (bool) $entry->shields(); + $json_racer["docking_computer"] = (bool) $entry->usedDockingComputer(); + $json_racer["fsd_injection"] = (bool) $entry->usedInjection(); + $json_racer["cut_short"] = (bool) $entry->cutShort(); + $json_racers[] = $json_racer; + } + + $q = new LaptimeQuery; + $q->joinWith('Laptime.Station'); + $q->joinWith('Station.System'); + $q->joinWith('Laptime.Lap'); + $q->joinWith('Lap.Entry'); + $q->joinWith('Entry.Ship'); + $q->joinWith('Ship.Competitor'); + $q->joinWith('Ship.Hull'); + $q->where('Entry.RaceId=?', $race_id); + $q->where('Entry.Id in (' . implode(',', $filtered_entry_ids) . ')'); + $legs = $q->orderByArrival()->orderBy('Lap.LapNumber')->orderByStationOrder()->orderBy('Competitor.CmdrName')->orderBy('Entry.Id')->find(); + + $start_times = array(); + $ordered_arrivals = array(); + $entry_system_ids = array(); + $entry_distance = array(); + foreach ($legs as $leg) { + $id = $leg->getId(); + $arrival = strtotime($leg->getArrival()); + $station_order = $leg->getStationOrder(); + $lap = $leg->getLap(); + $lap_number = $lap->getLapNumber(); + $entry = $lap->getEntry(); + $entry_id = $entry->getId(); + if (! is_array($entry_system_ids[$entry_id])) $entry_system_ids[$entry_id] = array(null); + $entry_system_ids[$entry_id][] = $leg->getStation()->getSystemId(); + if ($lap_number == 1 && $station_order == 1) $start_times[$entry_id] = $arrival; + $delta = $arrival - $start_times[$entry_id]; + if (! is_array($ordered_arrivals[$delta])) $ordered_arrivals[$delta] = array(); + $ordered_arrivals[$delta][] = $leg; + if (! $entry_distance[$entry_id]) $entry_distance[$entry_id] = 0.0; + $plot_key = $entry->getShip()->getCompetitor()->getCmdrName() . " #$entry_id"; + if (! is_array($plots[$plot_key])) $plots[$plot_key] = array( + "x" => array("2001-01-01 00:00:00"), + "y" => array(0), + "name" => $plot_key, + "text" => array("") + ); + } + + ksort($ordered_arrivals); + + foreach($ordered_arrivals as $time => $ordered_legs) { + foreach ($ordered_legs as $leg) { + $delta = format_time($time); + $arrival = elite_time($leg->getArrival()); + $station_order = $leg->getStationOrder(); + $nth = ordinal($station_order); + $station = $leg->getStation(); + $station_id = $station->getId(); + $station_name = $station->getName(); + $type = strtolower($station->getType()); + $system = $station->getSystem(); + $system_name = $system->getName(); + $lap = $leg->getLap(); + $lap_number = $lap->getLapNumber(); + $hull_start = $lap->getHullStart(); + $hull_end = $lap->getHullEnd(); + $entry = $lap->getEntry(); + $entry_id = $entry->getId(); + $start_time = $entry->getLink(elite_time(date('Y-m-d H:i:s', $start_times[$entry_id]))); + $ship = $entry->getShip(); + $ship_name = $ship->getLink(); + $competitor = $ship->getCompetitor(); + $cmdr_name = $competitor->getLink(); + $hull = $ship->getHull(); + $hull_name = $hull->getLink(); + + $distance = 0.0; + $previous_system_id = array_shift($entry_system_ids[$entry_id]); + if (! is_null($previous_system_id)) { + $system_id = $system->getId(); + if ($system_id != $previous_system_id) { + $system1 = min($system_id, $previous_system_id); + $system2 = max($system_id, $previous_system_id); + $distance = (float) $distances["from${system1}to${system2}"]; + $entry_distance[$entry_id] += $distance; + } + $total_distance = $entry_distance[$entry_id]; + $plot_key = $competitor->getCmdrName() . " #$entry_id"; + $plots[$plot_key]["x"][] = "2001-01-01 $delta"; + $plots[$plot_key]["y"][] = $total_distance; + $order = "station $station_order"; + if ($allotted_time) $order = "lap $lap_number $order"; + $plots[$plot_key]["text"][] = array($system_name, $station_name, $delta, $order); + } + + $style_cmdr = "cmdr" . $competitor->getId(); + $style_entry = "entry$entry_id"; + $style_hull = "hull" . $hull->getId(); + $style_ship = "ship" . $ship->getId(); + $style_lap = "lap$lap_number"; + $style_order = "order$station_order"; + $style_station = "station$station_id"; + $style_system = "system" . $system->getId(); + $classes = array( + "leg", + $style_cmdr, + $style_entry, + $style_hull, + $style_lap, + $style_order, + $style_ship, + $style_station, + $style_system + ); + + $verb = ($station_order > 1) ? "arrived at" : "left"; + echo "

"; + echo "$delta: CMDR $cmdr_name flying $hull_name $ship_name from $start_time $verb $nth station "; + if ($allotted_time) { + echo "of lap $lap_number, "; + if ($time <= $allotted_time) $period = "regulation"; + else if ($time <= $allotted_time + $entry->getBonusTime()) $period = "bonus"; + else $period = "penalty"; + } + echo "$station_name $type in $system_name at $arrival"; + if ($station_id == $finish_line && $station_number > 1 && is_numeric($hull_end)) echo " with $hull_end% hull remaining"; + else if ($station_id == $start_line && is_numeric($hull_start)) echo " with $hull_start% hull"; + if ($distance) echo " covering ${distance}Ly of total ${total_distance}Ly"; + if ($allotted_time) echo " during $period"; + echo "

"; + + if (array_key_exists($entry_id, $ranked_entries)) { + $json_events[] = array( + "ship" => $ship->getName(), + "event_type" => explode(" ", $verb)[0], + "time" => $time, + "timestamp" => $arrival, + "time_period" => $period, + "location" => $station->getName(), + "lap" => $lap_number, + "leg" => $station_order, + "leg_distance" => (float) $distance, + "total_distance" => (float) $total_distance + ); + } + } + } + + echo "\n"; + + echo "

JSON

\n"; + echo "
\n";
+    echo json_encode(array(
+      "racers" => $json_racers,
+      "events" => $json_events,
+    ));
+    echo "
\n"; + } + function module_race($action, $args) { if ($_POST['add_race']) { if (add_race($_POST["name"], $_POST["allotted_time"])) unset($_POST); @@ -533,12 +766,6 @@ foreach ($race->getPrizeKeys() as $p) $prizes[] = $race->PrizeDescription($p); echo "

Prizes for " . implode(", ", $prizes) . ".

\n"; } - echo "
\n"; - echo "
\n"; - echo "
\n"; - echo "
\n"; - echo "
\n"; - echo "
\n"; echo "
\n"; /* Sort options. */ @@ -882,6 +1109,15 @@ if ($show_raced_days) echo "var raced_days = " . json_encode(array_values($raced_days)) . ";\n"; else echo "var raced_days = null;\n"; echo "//-->\n"; + + echo "
\n"; + echo "
\n"; + echo "
\n"; + echo "
\n"; + echo "
\n"; + echo "
\n"; + + show_timeline($id, $filtered_entry_ids); } echo "
\n"; $sq = new StationQuery; diff --git a/lib/timeline.php b/lib/timeline.php deleted file mode 100644 index 64630fa..0000000 --- a/lib/timeline.php +++ /dev/null @@ -1,278 +0,0 @@ - -

Race timelines

-= 11 && ($n % 100) <= 13) return "${n}th"; - else return $n . array('th', 'st', 'nd', 'rd', 'th', 'th', 'th', 'th', 'th', 'th')[$n % 10]; - } - - function find_prizewinners($race, $entries) { - $competitors = array(); - $prizes = array(); - $last_sort_method = null; - foreach ($race->getPrizeKeys() as $p) { - $sort_method = $race->PrizeSortMethod($p); - if ($sort_method != $last_sort_method) uasort($entries, "Entry::$sort_method"); - $last_sort_method = $sort_method; - foreach ($entries as $entry) { - if ($entry->isExcluded()) continue; - $entry_id = $entry->getId(); - if (array_key_exists($entry_id, $prizes)) continue; - $competitor_id = $entry->getShip()->getCompetitorId(); - if (array_key_exists($competitor_id, $competitors)) continue; - $prizes[$entry_id] = $p; - $competitors[$competitor_id] = $entry_id; - break; - } - } - return array( - "competitors" => $competitors, - "entries" => $prizes - ); - } - - function show_timeline($race_id) { - $cq = new CourseQuery; - $cq->joinWith('Course.Race'); - $cq->joinWith('Course.Station'); - $course = $cq->filterByRaceId($race_id)->find(); - $race = $start_line = $finish_line = null; - $system_ids = array(); - foreach ($course as $leg) { - if (! $race) $race = $leg->getRace(); - $station_id = $leg->getStationId(); - if ($leg->getFinishLine()) $finish_line = $station_id; - if ($leg->getStartLine()) $start_line = $station_id; - $system_ids[] = $leg->getStation()->getSystemId(); - } - $system_ids = array_unique($system_ids); - - $rq = new RaikogramQuery; - $rq->filterBySystem1($system_ids); - $rq->filterBySystem2($system_ids); - $raikograms = $rq->find(); - $distances = array(); - foreach ($raikograms as $raikogram) { - $system1 = $raikogram->getSystem1(); - $system2 = $raikogram->getSystem2(); - $distance = $raikogram->getDistance(); - $distances["from{$system1}to${system2}"] = $distance; - } - - $allotted_time = $race->getAllottedTime(); - echo "

" . $race->getName() . "

\n"; - echo "
\n"; - $plots = array(); - - $json_racers = array(); - $json_events = array(); - $eq = new EntryQuery; - $eq->joinWith('Entry.Ship'); - $eq->joinWith('Ship.Competitor'); - $eq->joinWith('Ship.Hull'); - $eq->filterByRaceId($race_id); - $entry_objects = $eq->find(); - foreach ($entry_objects as $entry) $entries[] = $entry; - $prizewinners = find_prizewinners($race, $entries); - uasort($entries, "Entry::StandardSort"); - $ranked_entries = array(); - $competitor_entries = array(); - foreach ($entries as $entry) { - if ($entry->isExcluded()) continue; - $entry_id = $entry->getId(); - $ship =$entry->getShip(); - $competitor = $ship->getCompetitor(); - $competitor_id = $competitor->getId(); - if (array_key_exists($entry_id, $prizewinners["entries"]) || (! array_key_exists($competitor_id, $prizewinners["competitors"]) && ! array_key_exists($competitor_id, $competitor_entries))) { - $competitor_entries[$competitor_id] = $entry_id; - $ranked_entries[$entry_id] = true; - $json_racer = array("join" => count($json_racers) + 1); - $json_racer["owner"] = $competitor->getCmdrName(); - $json_racer["user"] = $competitor->getForumName(); - if (! $json_racer["user"]) $json_racer["user"] = $json_racer["owner"]; - $json_racer["ship"] = $ship->getName(); - $json_racer["hull"] = $ship->getHull()->getName(); - $json_racer["lap_distance"] = $entry->getLapDistance(); - $json_racer["complete_laps"] = $entry->getCompleteLaps(); - $json_racer["allotted_time"] = $allotted_time + $entry->getBonusTime(); - $json_racer["bonus_time"] = (int) $entry->getBonusTime(); - $json_racer["raced_time"] = $entry->getRacedTime(); - $json_racer["total_time"] = $entry->getTotalTime(); - $json_racer["joker"] = (bool) $entry->usedJoker(); - $json_racer["shields"] = (bool) $entry->shields(); - $json_racer["docking_computer"] = (bool) $entry->usedDockingComputer(); - $json_racer["fsd_injection"] = (bool) $entry->usedInjection(); - $json_racer["cut_short"] = (bool) $entry->cutShort(); - $json_racers[] = $json_racer; - } - } - - $q = new LaptimeQuery; - $q->joinWith('Laptime.Station'); - $q->joinWith('Station.System'); - $q->joinWith('Laptime.Lap'); - $q->joinWith('Lap.Entry'); - $q->joinWith('Entry.Ship'); - $q->joinWith('Ship.Competitor'); - $q->joinWith('Ship.Hull'); - $q->where('Entry.RaceId=?', $race_id); - $legs = $q->orderByArrival()->orderBy('Lap.LapNumber')->orderByStationOrder()->orderBy('Competitor.CmdrName')->orderBy('Entry.Id')->find(); - - $start_times = array(); - $ordered_arrivals = array(); - $entry_system_ids = array(); - $entry_distance = array(); - foreach ($legs as $leg) { - $id = $leg->getId(); - $arrival = strtotime($leg->getArrival()); - $station_order = $leg->getStationOrder(); - $lap = $leg->getLap(); - $lap_number = $lap->getLapNumber(); - $entry = $lap->getEntry(); - $entry_id = $entry->getId(); - if (! is_array($entry_system_ids[$entry_id])) $entry_system_ids[$entry_id] = array(null); - $entry_system_ids[$entry_id][] = $leg->getStation()->getSystemId(); - if ($lap_number == 1 && $station_order == 1) $start_times[$entry_id] = $arrival; - $delta = $arrival - $start_times[$entry_id]; - if (! is_array($ordered_arrivals[$delta])) $ordered_arrivals[$delta] = array(); - $ordered_arrivals[$delta][] = $leg; - if (! $entry_distance[$entry_id]) $entry_distance[$entry_id] = 0.0; - $plot_key = $entry->getShip()->getCompetitor()->getCmdrName() . " #$entry_id"; - if (! is_array($plots[$plot_key])) $plots[$plot_key] = array( - "x" => array("2001-01-01 00:00:00"), - "y" => array(0), - "name" => $plot_key, - "text" => array("") - ); - } - - ksort($ordered_arrivals); - - foreach($ordered_arrivals as $time => $ordered_legs) { - foreach ($ordered_legs as $leg) { - $delta = format_time($time); - $arrival = elite_time($leg->getArrival()); - $station_order = $leg->getStationOrder(); - $nth = ordinal($station_order); - $station = $leg->getStation(); - $station_id = $station->getId(); - $station_name = $station->getName(); - $type = strtolower($station->getType()); - $system = $station->getSystem(); - $system_name = $system->getName(); - $lap = $leg->getLap(); - $lap_number = $lap->getLapNumber(); - $hull_start = $lap->getHullStart(); - $hull_end = $lap->getHullEnd(); - $entry = $lap->getEntry(); - $entry_id = $entry->getId(); - $start_time = $entry->getLink(elite_time(date('Y-m-d H:i:s', $start_times[$entry_id]))); - $ship = $entry->getShip(); - $ship_name = $ship->getLink(); - $competitor = $ship->getCompetitor(); - $cmdr_name = $competitor->getLink(); - $hull = $ship->getHull(); - $hull_name = $hull->getLink(); - - $distance = 0.0; - $previous_system_id = array_shift($entry_system_ids[$entry_id]); - if (! is_null($previous_system_id)) { - $system_id = $system->getId(); - if ($system_id != $previous_system_id) { - $system1 = min($system_id, $previous_system_id); - $system2 = max($system_id, $previous_system_id); - $distance = (float) $distances["from${system1}to${system2}"]; - $entry_distance[$entry_id] += $distance; - } - $total_distance = $entry_distance[$entry_id]; - $plot_key = $competitor->getCmdrName() . " #$entry_id"; - $plots[$plot_key]["x"][] = "2001-01-01 $delta"; - $plots[$plot_key]["y"][] = $total_distance; - $order = "station $station_order"; - if ($allotted_time) $order = "lap $lap_number $order"; - $plots[$plot_key]["text"][] = array($system_name, $station_name, $delta, $order); - } - - $style_cmdr = "cmdr" . $competitor->getId(); - $style_entry = "entry$entry_id"; - $style_hull = "hull" . $hull->getId(); - $style_ship = "ship" . $ship->getId(); - $style_lap = "lap$lap_number"; - $style_order = "order$station_order"; - $style_station = "station$station_id"; - $style_system = "system" . $system->getId(); - $classes = array( - "leg", - $style_cmdr, - $style_entry, - $style_hull, - $style_lap, - $style_order, - $style_ship, - $style_station, - $style_system - ); - - $verb = ($station_order > 1) ? "arrived at" : "left"; - echo "

"; - echo "$delta: CMDR $cmdr_name flying $hull_name $ship_name from $start_time $verb $nth station "; - if ($allotted_time) { - echo "of lap $lap_number, "; - if ($time <= $allotted_time) $period = "regulation"; - else if ($time <= $allotted_time + $entry->getBonusTime()) $period = "bonus"; - else $period = "penalty"; - } - echo "$station_name $type in $system_name at $arrival"; - if ($station_id == $finish_line && $station_number > 1 && is_numeric($hull_end)) echo " with $hull_end% hull remaining"; - else if ($station_id == $start_line && is_numeric($hull_start)) echo " with $hull_start% hull"; - if ($distance) echo " covering ${distance}Ly of total ${total_distance}Ly"; - if ($allotted_time) echo " during $period"; - echo "

"; - - if (array_key_exists($entry_id, $ranked_entries)) { - $json_events[] = array( - "ship" => $ship->getName(), - "event_type" => explode(" ", $verb)[0], - "time" => $time, - "timestamp" => $arrival, - "time_period" => $period, - "location" => $station->getName(), - "lap" => $lap_number, - "leg" => $station_order, - "leg_distance" => (float) $distance, - "total_distance" => (float) $total_distance - ); - } - } - } - - echo "\n"; - - echo "

JSON

\n"; - echo "
\n";
-    echo json_encode(array(
-      "racers" => $json_racers,
-      "events" => $json_events,
-    ));
-    echo "
\n"; - } - - function module_timeline($action, $args) { - if ($action) show_timeline($action); - $rq = new RaceQuery; - $rq->joinWith('Race.Entry'); - $races = $rq->find(); - foreach ($races as $race) { - if (! count($race->getEntries())) continue; - $id = $race->getId(); - $name = $race->getName(); - /* XXX */ - $url = preg_replace('/&action=\d+/', '', $_SERVER['REQUEST_URI']) . "&action=$id"; - echo "

$name

\n"; - } - } - -?> diff --git a/race.js b/race.js index 1072e3f..580b89b 100644 --- a/race.js +++ b/race.js @@ -2,6 +2,33 @@ var start_flag = 1 << 1; var finish_flag = 1 << 2; var mandatory_flags = start_flag | finish_flag; +/* Set the background for a leg element. */ +function leg_background(classes, colour) { + var changed = false; + for (var c of classes) { + for (var element of document.getElementsByClassName(c)) { + if (! element.classList.contains('leg')) continue; + element.style.backgroundColor = colour; + changed = true; + } + } + return changed; +} + +/* Hover over an element. */ +function enter(event) { + if (leg_background(event.target.classList, 'rgba(255, 100, 24, 0.4)')) { + event.target.style.textDecoration = 'underline'; + } +} + +/* Mouse out of an element. */ +function leave(event) { + if (leg_background(event.target.classList, 'inherit')) { + event.target.style.textDecoration = 'inherit'; + } +} + /* Copy table to clipboard. */ function copy_classification(event) { try { @@ -125,9 +152,6 @@ function changed_order(event) { /* Document onload. */ function loaded() { - for (var classification of document.getElementsByClassName('classification')) { - classification.addEventListener('dblclick', copy_classification, true); - } var waypoints = document.getElementsByClassName('station'); if (waypoints.length) { var div = document.getElementById('waypoints'); @@ -150,37 +174,55 @@ function loaded() { } validate(); } - var canvas = document.getElementById('plot_distribution'); - if (canvas && pies) Plotly.plot(canvas, pies, { - title: 'Entry distribution' - }); - canvas = document.getElementById('plot_legs'); - if (canvas && legs) Plotly.plot(canvas, legs, { - title: 'Average station to station time', - yaxis: { title: 'Time', tickformat: '%H:%M:%S' } - }); - canvas = document.getElementById('plot_speeds'); - if (canvas && speeds) Plotly.plot(canvas, speeds, { - title: 'Average station to station speed', - yaxis: { title: 'Speed (Ly/h)' } - }); - canvas = document.getElementById('plot_laps'); - if (canvas && laps) Plotly.plot(canvas, laps, { - title: 'Fastest individual laps', - }); - canvas = document.getElementById('plot_raced_times'); - if (canvas && raced_times) Plotly.plot(canvas, raced_times, { - barmode: 'stack', - title: 'Time raced', - yaxis: { title: 'Time (s)' } - }); - canvas = document.getElementById('plot_raced_days'); - if (canvas && raced_days) Plotly.plot(canvas, raced_days, { - barmode: 'stack', - title: 'Entries per day', - xaxis: { tickformat: '%b %e' }, - yaxis: { title: 'Entries' } - }); + else { + for (var classification of document.getElementsByClassName('classification')) { + classification.addEventListener('dblclick', copy_classification, true); + } + for (var span of document.getElementsByTagName('span')) { + if (! span.classList.length) continue; + span.addEventListener('mouseenter', enter, true); + span.addEventListener('mouseleave', leave, true); + } + var canvas = document.getElementById('plot_timeline'); + if (canvas && points) { + Plotly.plot(canvas, points, { + title: 'Competitor distance', + xaxis: { title: 'Time', autorange: true, rangemode: 'nonnegative', tickformat: '%H:%M:%S' }, + yaxis: { title: 'Distance (Ly)', autorange: true, rangemode: 'nonnegative' } + }); + } + canvas = document.getElementById('plot_distribution'); + if (canvas && pies) Plotly.plot(canvas, pies, { + title: 'Entry distribution' + }); + canvas = document.getElementById('plot_legs'); + if (canvas && legs) Plotly.plot(canvas, legs, { + title: 'Average station to station time', + yaxis: { title: 'Time', tickformat: '%H:%M:%S' } + }); + canvas = document.getElementById('plot_speeds'); + if (canvas && speeds) Plotly.plot(canvas, speeds, { + title: 'Average station to station speed', + yaxis: { title: 'Speed (Ly/h)' } + }); + canvas = document.getElementById('plot_laps'); + if (canvas && laps) Plotly.plot(canvas, laps, { + title: 'Fastest individual laps', + }); + canvas = document.getElementById('plot_raced_times'); + if (canvas && raced_times) Plotly.plot(canvas, raced_times, { + barmode: 'stack', + title: 'Time raced', + yaxis: { title: 'Time (s)' } + }); + canvas = document.getElementById('plot_raced_days'); + if (canvas && raced_days) Plotly.plot(canvas, raced_days, { + barmode: 'stack', + title: 'Entries per day', + xaxis: { tickformat: '%b %e' }, + yaxis: { title: 'Entries' } + }); + } } document.onload = loaded(); diff --git a/timeline.js b/timeline.js deleted file mode 100644 index 286e429..0000000 --- a/timeline.js +++ /dev/null @@ -1,45 +0,0 @@ -/* Set the background for a leg element. */ -function leg_background(classes, colour) { - var changed = false; - for (var c of classes) { - for (var element of document.getElementsByClassName(c)) { - if (! element.classList.contains('leg')) continue; - element.style.backgroundColor = colour; - changed = true; - } - } - return changed; -} - -/* Hover over an element. */ -function enter(event) { - if (leg_background(event.target.classList, 'rgba(255, 100, 24, 0.4)')) { - event.target.style.textDecoration = 'underline'; - } -} - -/* Mouse out of an element. */ -function leave(event) { - if (leg_background(event.target.classList, 'inherit')) { - event.target.style.textDecoration = 'inherit'; - } -} - -/* Document onload. */ -function loaded() { - for (var span of document.getElementsByTagName('span')) { - if (! span.classList.length) continue; - span.addEventListener('mouseenter', enter, true); - span.addEventListener('mouseleave', leave, true); - } - var canvas = document.getElementById('plot'); - if (canvas && points) { - Plotly.plot(canvas, points, { - title: 'Competitor distance', - xaxis: { title: 'Time', autorange: true, rangemode: 'nonnegative', tickformat: '%H:%M:%S' }, - yaxis: { title: 'Distance (Ly)', autorange: true, rangemode: 'nonnegative' } - }); - } -} - -document.onload = loaded(); -- 2.7.4