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 duplicate labels on the TimeSeriesChart x-axis #443

Merged
merged 4 commits into from
Jul 26, 2018
Merged
Changes from 2 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
14 changes: 13 additions & 1 deletion app/renderer/components/TimeSeriesChart.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React from 'react';
import _ from 'lodash';
import {ResponsiveContainer, AreaChart, Area, XAxis, YAxis, Tooltip} from 'recharts';
import {format as formatDate} from 'date-fns';
import {format as formatDate, subMonths, subDays, subHours, subMinutes, getDaysInMonth} from 'date-fns';
import {formatCurrency} from '../util';
import './TimeSeriesChart.scss';

Expand Down Expand Up @@ -38,6 +38,17 @@ class TimeSeriesChart extends React.Component {

const labelFormat = resolutionToLabelFormat.get(resolution);

const getTicks = resolution => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can move this to the top-level, below the resolutionToLabelFormat function, since it doesn't depend on any internal state.

const ticks = {
year: [...new Array(13).keys()].map(index => subMonths(new Date(), index)).reverse(),
month: [...new Array(getDaysInMonth(new Date()) + 1).keys()].map(index => subDays(new Date(), index)).reverse(),
week: [...new Array(8).keys()].map(index => subDays(new Date(), index)).reverse(),
day: [...new Array(25).keys()].map(index => subHours(new Date(), index)).reverse(),
hour: [...new Array(61).keys()].map(index => subMinutes(new Date(), index)).reverse(),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nitpick, but it's wasteful to create so many date objects. I would define const now = new Date(); above const ticks and use that.

You could also abstract some of the logic into a helper function:

const makeTicks = (count, fn) => {
	return [...new Array(count).keys()].map(index => fn(new Date(), index)).reverse();
};

};
return ticks[resolution];
};

return (
<div className="TimeSeriesChart">
<ResponsiveContainer width="100%" minHeight={100}>
Expand All @@ -58,6 +69,7 @@ class TimeSeriesChart extends React.Component {
type="number"
interval="preserveStartEnd"
tickFormatter={unixTime => formatDate(new Date(unixTime), labelFormat)}
ticks={getTicks(resolution)}
axisLine={{
stroke: 'transparent',
}}
Expand Down