From 6507f6be9a29562cd00a589a74eeb8e2da2b832c Mon Sep 17 00:00:00 2001 From: BatedUrGonnaDie Date: Sat, 29 Dec 2018 21:08:15 -0800 Subject: [PATCH] Combine segment related graphs together (#479) * combine segment related graphs together changes the type of the reset graph from a sum pie style to an individual bar chart to mark how many times you reset on each split * Address CR comments Introduce color file similar to sass version for use in JS, fix some bugs, adds some comments as well * address my poor wording --- app/javascript/colors.js | 12 ++ app/javascript/graph_builder.js | 10 +- app/javascript/graphs/pb_graph.js | 60 -------- app/javascript/graphs/reset_graph.js | 67 --------- app/javascript/graphs/segment_graphs.js | 132 ++++++++++++++++++ .../graphs/segment_history_mean_graph.js | 62 -------- app/javascript/highchart_theme.js | 13 +- app/views/runs/show.slim | 4 +- 8 files changed, 155 insertions(+), 205 deletions(-) create mode 100644 app/javascript/colors.js delete mode 100644 app/javascript/graphs/pb_graph.js delete mode 100644 app/javascript/graphs/reset_graph.js create mode 100644 app/javascript/graphs/segment_graphs.js delete mode 100644 app/javascript/graphs/segment_history_mean_graph.js diff --git a/app/javascript/colors.js b/app/javascript/colors.js new file mode 100644 index 00000000..cabf4617 --- /dev/null +++ b/app/javascript/colors.js @@ -0,0 +1,12 @@ +const blue = 'rgba(14, 144, 210, 1)' +const purple = 'rgba(128, 87, 165, 1)' +const green = 'rgba(94, 185, 94, 1)' +const yellow = 'rgba(250, 200, 50, 1)' +const red = 'rgba(221, 81, 76, 1)' +const orange = 'rgba(243, 123, 29, 1)' + +const logoYellow = 'rgba(245, 185, 70, 1)' +const logoBlue = 'rgba(70, 156, 230, 1)' +const logoGreen = 'rgba(109, 229, 136, 1)' + +export {blue, purple, green, yellow, red, orange, logoYellow, logoBlue, logoGreen} diff --git a/app/javascript/graph_builder.js b/app/javascript/graph_builder.js index 1351748a..8c5d9d94 100644 --- a/app/javascript/graph_builder.js +++ b/app/javascript/graph_builder.js @@ -1,8 +1,6 @@ -import {build_pb_graph} from "graphs/pb_graph.js" import {build_run_duration_graph} from "graphs/run_duration_graph.js" -import {build_reset_graph} from "graphs/reset_graph.js" -import {build_segment_history_mean_graph} from "graphs/segment_history_mean_graph.js" import {build_playtime_graph} from "graphs/playtime_graph.js" +import {buildSegmentGraphs} from "graphs/segment_graphs.js" document.addEventListener('turbolinks:load', function() { if (document.getElementById('graph-holder') === null) { @@ -19,11 +17,9 @@ document.addEventListener('turbolinks:load', function() { }).then(function(run) { document.getElementById('graph-spinner').hidden = true document.getElementById('graph-holder').hidden = false - build_pb_graph(run.run) - if (run.run.program === 'livesplit') { + if (run.run.histories.length !== 0) { build_run_duration_graph(run.run) - build_reset_graph(run.run) - build_segment_history_mean_graph(run.run) + buildSegmentGraphs(run.run) build_playtime_graph(run.run) } }).catch(function(error) { diff --git a/app/javascript/graphs/pb_graph.js b/app/javascript/graphs/pb_graph.js deleted file mode 100644 index 9aa63a68..00000000 --- a/app/javascript/graphs/pb_graph.js +++ /dev/null @@ -1,60 +0,0 @@ -import Highcharts from 'highcharts' -import Exporting from 'highcharts/modules/exporting' -Exporting(Highcharts) - -const build_pb_graph = function(run) { - if (document.getElementById('pb-graph') === null) { - return - } - - const url = new URL(window.location.href) - const duration_string = `${url.searchParams.get('timing') || 'real'}time_duration_ms` - const shortest_string = `${url.searchParams.get('timing') || 'real'}time_shortest_duration_ms` - - let graph_data = [] - run.segments.forEach(function(segment, i) { - let time = (segment[duration_string] - segment[shortest_string]) / 1000 - if (time < 0) { - time = 0 - } - - graph_data.push([segment.name, time]) - }) - - Highcharts.chart('pb-graph', { - exporting: { - chartOptions: { - plotOptions: { - series: { - dataLabels: {enabled: true} - } - } - }, - fallbackToExportServer: false - }, - chart: { - type: 'column', - zoomType: 'x' - }, - title: {text: 'Possible Time Save in PB'}, - subtitle: {text: '(PB - Gold)'}, - tooltip: { - pointFormat: 'Time Save: {point.y:.1f}s', - shared: true - }, - legend: {enabled: false}, - xAxis: { - categories: graph_data.map(function(seg) { return seg[0]; }), - crosshair: true - }, - yAxis: { - title: {text: 'Possible Time Save (s)'} - }, - series: [{ - name: 'PB - Gold', - data: graph_data - }] - }) -} - -export {build_pb_graph} diff --git a/app/javascript/graphs/reset_graph.js b/app/javascript/graphs/reset_graph.js deleted file mode 100644 index 6f409d0f..00000000 --- a/app/javascript/graphs/reset_graph.js +++ /dev/null @@ -1,67 +0,0 @@ -import Highcharts from 'highcharts' -import Exporting from 'highcharts/modules/exporting' -Exporting(Highcharts) - -const build_reset_graph = function(run) { - if (document.getElementById('reset-graph') === null) { - return - } - - let reset_counter = run.attempts - let reset_data = [] - - run.segments.forEach(function(segment, i) { - if (reset_counter - segment.histories.length > 0) { - reset_data.push([segment.name, reset_counter - segment.histories.length]) - reset_counter = segment.histories.length - } - }) - - Highcharts.chart('reset-graph', { - exporting: { - chartOptions: { - plotOptions: { - series: { - dataLabels: {enabled: true} - } - } - }, - fallbackToExportServer: false - }, - chart: {type: 'pie'}, - title: {text: 'Resets Per Split'}, - tooltip: { - pointFormat: '\u25CF {series.name}: {point.y} | {point.percentage:.1f}%', - }, - plotOptions: { - pie: { - showInLegend: true, - events: { - afterAnimate: function() { - let chart = this.chart - let legend = chart.legend - let tooltip = this.chart.tooltip - for (let item of legend.allItems) { - item.legendItem.on('mouseover', function (e) { - let data = item.series.data[item.index] - tooltip.refresh(data) - }).on('mouseout', function (e) { - tooltip.hide() - }) - } - } - } - } - }, - yAxis: { - title: {text: 'Resets'} - }, - series: [{ - name: 'Resets', - data: reset_data, - innerSize: '50%' - }] - }) -} - -export {build_reset_graph} diff --git a/app/javascript/graphs/segment_graphs.js b/app/javascript/graphs/segment_graphs.js new file mode 100644 index 00000000..b8d84573 --- /dev/null +++ b/app/javascript/graphs/segment_graphs.js @@ -0,0 +1,132 @@ +import Highcharts from 'highcharts' +import Exporting from 'highcharts/modules/exporting' +import {logoYellow, logoBlue, logoGreen} from '../colors.js' +Exporting(Highcharts) + +const buildSegmentGraphs = function(run) { + if (document.getElementById('segment-graphs') === null) { + return + } + + const url = new URL(window.location.href) + const durationString = `${url.searchParams.get('timing') || 'real'}time_duration_ms` + const shortestString = `${url.searchParams.get('timing') || 'real'}time_shortest_duration_ms` + + const pbData = [] + const meanData = [] + const finished = run[durationString] > 0 + let resetCounter = run.attempts + const resetData = [] + + run.segments.forEach((segment, i) => { + // Start PB info collection + let time = (segment[durationString] - segment[shortestString]) / 1000 + if (time < 0) { + time = 0 + } + pbData.push([segment.name, time]) + + // Start mean info collection + time = 0 + let counter = 0 + if (segment.histories.length !== 0) { + segment.histories.forEach((attempt) => { + if (attempt[durationString] === 0) { + // Reject attempts that have no duration (skipped splits) + return + } else if (run.segments[i - 1] !== undefined) { + const prevSegAttempt = run.segments[i - 1].histories.find((prevAttempt) => { + // Find the previous splits attempt for this same attempt id to make sure it wasn't skipped + return prevAttempt.attempt_number === attempt.attempt_number + }) + if (prevSegAttempt !== undefined && prevSegAttempt[durationString] === 0) { + // Reject the first split after a series of skipped splits to prevent data pollution + return + } + } + + time += attempt[durationString] + counter++ + }) + + time = ((time / counter) - segment[shortestString]) / 1000 + } + meanData.push([segment.name, time]) + + // Start reset info collection + let attemptTotal = segment.histories.length + if (attemptTotal <= 0 && finished) { + // Add one to the history length if it is 0 and the run is finished because we know there should be at least one + // This means we don't have to check for NaN later because we set resetCounter to this value (divide by 0) + attemptTotal = 1 + } + let resetPercentage = (1 - (attemptTotal / resetCounter)) * 100 + if (resetPercentage < 0) { + resetPercentage = 0 + } + resetData.push([segment.name, resetPercentage]) + resetCounter = attemptTotal + }) + + Highcharts.chart('segment-graphs', { + exporting: { + chartOptions: { + plotOptions: { + series: { + dataLabels: {enabled: true} + } + } + }, + fallbackToExportServer: false + }, + chart: { + type: 'column', + zoomType: 'x' + }, + title: {text: 'Individual Segment Stats'}, + subtitle: {text: 'Lower is Better'}, + tooltip: { + shared: true + }, + legend: {enabled: false}, + xAxis: { + categories: pbData.map(function(seg) { return seg[0] }), + crosshair: true + }, + yAxis: [{ + title: {text: 'Possible Time Save (s)'} + }, { + title: {text: 'Reset Attempts Per Split (%)'}, + min: 0, + max: 100, + opposite: true, + tickColor: logoGreen, + gridLineColor: logoGreen + }], + series: [{ + name: 'Difference of PB and Gold', + data: pbData, + color: logoYellow, + tooltip: { + pointFormat: '\u25CF {series.name}: {point.y:.1f}s
' + } + }, { + name: 'Difference of Average and Gold', + data: meanData, + color: logoBlue, + tooltip: { + pointFormat: '\u25CF {series.name}: {point.y:.1f}s
' + } + }, { + name: 'Reset Rate', + data: resetData, + color: logoGreen, + tooltip: { + pointFormat: '\u25CF {series.name}: {point.y:.1f}%
' + }, + yAxis: 1 + }] + }) +} + +export {buildSegmentGraphs} diff --git a/app/javascript/graphs/segment_history_mean_graph.js b/app/javascript/graphs/segment_history_mean_graph.js deleted file mode 100644 index 0def2231..00000000 --- a/app/javascript/graphs/segment_history_mean_graph.js +++ /dev/null @@ -1,62 +0,0 @@ -import Highcharts from 'highcharts' -import Exporting from 'highcharts/modules/exporting' -Exporting(Highcharts) - -const build_segment_history_mean_graph = function(run) { - if (document.getElementById('segment-history-graph') === null) { - return - } - - const url = new URL(window.location.href) - const duration_string = `${url.searchParams.get('timing') || 'real'}time_duration_ms` - const shortest_string = `${url.searchParams.get('timing') || 'real'}time_shortest_duration_ms` - - - const graph_data = [] - run.segments.forEach(function(segment) { - let time = 0 - if (segment.histories.length !== 0) { - const non_zero_values = segment.histories.filter(attempt => attempt[duration_string] > 0) - time = non_zero_values.map((attempt) => attempt[duration_string]).reduce((a, b) => a + b, 0) / non_zero_values.length - time = (time - segment[shortest_string]) / 1000 - } - graph_data.push([segment.name, time]) - }) - - Highcharts.chart('segment-history-graph', { - exporting: { - chartOptions: { - plotOptions: { - series: { - dataLabels: {enabled: true} - } - } - }, - fallbackToExportServer: false - }, - chart: { - type: 'column', - zoomType: 'x' - }, - title: {text: 'Possible Time Save in Averages'}, - subtitle: {text: '(Average - Gold)'}, - tooltip: { - pointFormat: 'Time Save: {point.y:.1f}s', - shared: true - }, - legend: {enabled: false}, - xAxis: { - categories: graph_data.map(function(seg) { return seg[0] }), - crosshair: true - }, - yAxis: { - title: {text: 'Possible Time Save (s)'} - }, - series: [{ - name: 'History Mean - Gold', - data: graph_data - }] - }) -} - -export {build_segment_history_mean_graph} diff --git a/app/javascript/highchart_theme.js b/app/javascript/highchart_theme.js index a08e609a..0ec98cbb 100644 --- a/app/javascript/highchart_theme.js +++ b/app/javascript/highchart_theme.js @@ -1,14 +1,15 @@ 'use strict' import Highcharts from 'highcharts' +import {blue, purple, green, yellow, red, orange} from 'colors.js' Highcharts.theme = { colors: [ - "rgba(14, 144, 210, 1)", - "rgba(128, 87, 165, 1)", - "rgba(94, 185, 94, 1)", - "rgba(250, 210, 50, 1)", - "rgba(221, 81, 76, 1)", - "rgba(243, 123, 29, 1)" + blue, + purple, + green, + yellow, + red, + orange ], chart: { backgroundColor: 'none', diff --git a/app/views/runs/show.slim b/app/views/runs/show.slim index 25706fcd..48a00d4b 100644 --- a/app/views/runs/show.slim +++ b/app/views/runs/show.slim @@ -33,9 +33,7 @@ #graph-alert.alert.alert-danger.my-3 hidden=true #graph-holder hidden=true #run-duration-graph.card.my-3 - #pb-graph.card.my-3 - #segment-history-graph.card.my-3 - #reset-graph.card.my-3 + #segment-graphs.card.my-3 - if @run.histories.any? - if @run.user.try(:silver_patron?) || @run.user.try(:admin?) .my-3 -- knotmirror 0.0.1