Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(113) adjust location of annotation; shrink x axis range if screen… #121

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 22 additions & 5 deletions src/components/stat-visuals/TripRatioGraph.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { React, useState, useEffect } from 'react';
import Plot from 'react-plotly.js';

import tripData from "../../Routes/schedule_vs_realtime_all_day_types_routes.json";
import { subtractOneYear } from '../../utils/dateUtils';

function rollingAverage(values, windowSize) {
const result = [];
Expand Down Expand Up @@ -146,6 +147,16 @@ function TripRatioGraph({ route_id }) {
},
];

// this modifies the axis if there is data and the screen is small
// it applies the same axis to both charts assuming that their data ranges are the same
// it takes the most recent date and subtracts a year from it to show the last 12 months
let xaxis_range = [];
if (data_ratio && data_ratio[1] && data_ratio[1].x.length > 0 && window.screen.width < 500) {
const x_values = data_ratio[1].x;
const last_date = x_values[x_values.length - 1];
xaxis_range = [subtractOneYear(last_date), last_date]
}

const layout_trips = {
responsive: true,
showLegend: false,
Expand All @@ -159,11 +170,13 @@ function TripRatioGraph({ route_id }) {
yaxis: {
title: "Trips"
},

xaxis: {
range: xaxis_range
},
annotations: [
{
x: "2022-11-01",
y: 20,
x: "2023-05-01",
y: 75,
xref: "x",
yref: "y",
showarrow: false,
Expand Down Expand Up @@ -197,11 +210,15 @@ function TripRatioGraph({ route_id }) {
yaxis: {
title: 'Ratio of Actual vs. Scheduled Trips',
range: [0.0, 1.2],
zeroline: false
zeroline: false,
tickformat: ',.0%'
},
xaxis: {
range: xaxis_range
},
annotations: [
{
x: "2022-11-01",
x: "2023-05-01",
y: 0.3,
xref: "x",
yref: "y",
Expand Down
9 changes: 9 additions & 0 deletions src/utils/dateUtils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/*
* This takes in a date string such as 2023-01-01
* and outputs a data from a year ago such as 2022-01-01
*/
export const subtractOneYear = (date) => {
const jsDate = new Date(date);
jsDate.setFullYear(jsDate.getFullYear() - 1);
return jsDate.toISOString()
}