I'm trying to make a graph like this using D3 and React Native
Right now I'm trying to define my Y axis using a list of start and end dates.
I'm making my yScale using the code:
const yScale = scaleTime()
.domain([new Date(MIN_VALUE), new Date(MAX_VALUE)])
.range([height, 0])
But Im having trouble figuring out an algorithm to define what to input for MIN_VALUE and MAX_VALUE
Here are example dates:
const dates = [
{
start: 2018-03-04T00:28:23-08:00 //Mar 4 12:28am
end: 2018-03-04T09:44:46-08:00 //Mar 4 09:44 am
},
{
start: 2018-03-02T22:25:24-08:00 //Mar 2 10:25 pm
end: 2018-03-03T08:26:58-08:00 //Mar 3 08:26 am
},
{
start: 2018-03-01T23:24:48-08:00 //Mar 1 11:24 pm
end: 2018-03-02T07:44:27-08:00 //Mar 2 07:44 am
}
]
From this example, Min time should be 10:25pm and Max time should be 8:26am
This is what I tried:
I first convert all the dates to milliseconds relative to start date
const allYValues = [] data.map((report, idx) => { var start = moment(data.end).startOf('day').subtract(1, 'day') allYValues.push(moment(data.start).diff(start)) allYValues.push(moment(data.end).diff(start)) })
Then I pass the min and max in milliseconds
const yScale = scaleLinear() .domain([new Date(Math.min(...allYValues)), new Date(Math.max(...allYValues))]) .range([height - paddingBottom, 0])
However I get the wrong date. For example If Math.min(...allYValues) = 80213000 (milliseconds). If I convert this to hours and minutes it should be 22:16, but new Date(Math.min(...allYValues)) gives me "01 Jan 1970 02:16 pm"