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 1 commit
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
30 changes: 28 additions & 2 deletions 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 @@ -36,8 +36,33 @@ class TimeSeriesChart extends React.Component {
resolution,
} = this.props;

console.log(data);
Copy link
Contributor

Choose a reason for hiding this comment

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

Remove this.


const labelFormat = resolutionToLabelFormat.get(resolution);

const getTicks = (resolution) => {
const ticks = {
"all": undefined,
"year": [...Array(13).keys()].map(index => subMonths(new Date(), index)).reverse(),
"month": [...Array(getDaysInMonth(new Date()) + 1).keys()].map(index => subDays(new Date(), index)).reverse(),
"week": [...Array(8).keys()].map(index => subDays(new Date(), index)).reverse(),
"day": [...Array(25).keys()].map(index => subHours(new Date(), index)).reverse(),
"hour": [...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.

Are these numbers arbitrary or where do they come from?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Maybe I should have explained this.
To prevent duplicate labels I set the ticks manually.
The hour chart should have a maximum of 61 ticks, meaning 1 for every minute in an hour plus one more so the first and last ticks have the same time.
Likewise, the day chart should have 25 ticks since there are 24 hours in a day + 1 hour for the first tick.
It works analogously for the week and the month chart.

}
return ticks[resolution]
}
const getDomain = (resolution) => {
const domain = {
"all": ['auto', 'auto'],
"year": [subMonths(new Date(), 13), 'auto'],
"month": [subDays(new Date(), getDaysInMonth(new Date()) + 1), 'auto'],
"week": [subDays(new Date(), 8), 'auto'],
"day": [subHours(new Date(), 25), 'auto'],
"hour": [subMinutes(new Date(), 61), 'auto']
}
return domain[resolution]
}

return (
<div className="TimeSeriesChart">
<ResponsiveContainer width="100%" minHeight={100}>
Expand All @@ -54,10 +79,11 @@ class TimeSeriesChart extends React.Component {
dataKey="time"
name="Time"
scale="time"
domain={['auto', 'auto']}
domain={getDomain(resolution)}
type="number"
interval="preserveStartEnd"
tickFormatter={unixTime => formatDate(new Date(unixTime), labelFormat)}
ticks={getTicks(resolution)}
axisLine={{
stroke: 'transparent',
}}
Expand Down