question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Set domain for date type

See original GitHub issue

Do you want to request a feature or report a bug?

Not sure it’s bug or not, but every try was failed.

What is the current behavior?

I want to set minimum/maximum value for XAxis which is date type.

<ResponsiveContainer width="100%" height={200}>
  <BarChart
    width={400}
    height={200}
    data={data}
    margin={{
      top: 20,
      left: 5,
      bottom: 5,
      right: 5
    }}
  >
    <CartesianGrid strokeDasharray="3 3" />
    <XAxis dataKey="time" tickFormatter={timeStr => moment(timeStr).format(timeFormat)} />
    <YAxis dateKey="count" type="number" allowDecimals={false} tickCount={10} />
    <Tooltip formatter={(value, name) => [value, startCase(name)]} />
    <Bar dataKey="count" fill="#8884d8" />
  </BarChart>
</ResponsiveContainer>

Key part is <XAxis /> :

<XAxis dataKey="time" tickFormatter={timeStr => moment(timeStr).format(timeFormat)} />

Let’s say I want to set minimum date to 2018-01-01 00:00:00 and max date to 2019-12-31 23:59:59. All of these won’t worked to me:

domain={['2018-01-01', '2019-12-31']}
domain={['2018-01-01 00:00:00', '2019-12-31 00:00:00']}
domain={['2018-01-01T00:00:00.000+09:00', '2019-12-31T23:59:59.999+09:00']}
domain={[new Date('2018-01-01'), new Date('2019-12-31')]}
domain={[new Date('2018-01-01').getTime(), new Date('2019-12-31').getTime()]}
domain={[moment('2018-01-01').format('YYYY-MM-DD'), moment('2019-12-31').format('YYYY-MM-DD')]}

Which versions of Recharts, and which browser / OS are affected by this issue? Did this work in previous versions of Recharts?

Recharts@1.6.2, Chrome 75, MacOS 10.13.6. Didn’t tried with previous version.

Issue Analytics

  • State:open
  • Created 4 years ago
  • Reactions:6
  • Comments:8 (1 by maintainers)

github_iconTop GitHub Comments

5reactions
jweinstein92commented, May 12, 2020

For anyone looking for a workaround, I was able to convert my date strings into a timestamp using moment and use those as a type="number" for my XAxis

For example if my data was

[{ date: 'May 10 2020', count: 70}, { date: 'May 11 2020', count: 20}, { date: 'May 12 2020', count: 50}]

I changed it so it was

[{ date: moment.utc('May 10 2020').valueOf(), count: 70}, { date: moment.utc('May 11 2020').valueOf(), count: 20}, { date: moment.utc('May 12 2020').valueOf(), count: 50}]

and my XAxis was

<XAxis
          label={false}
          type="number"
          dataKey="date"
          domain={[left, right]}
          tickFormatter={v => moment.utc(v).format(format())} />

Where left and right are the timestamp ranges. Keep in mind though that without a domain, it is identifying the dataMin as 0, so timestamps like 1589299080 will show very far on the right. I basically took the first and last timestamps of my data and. used those for the domain range.

1reaction
T1bbYcommented, Apr 12, 2022

Have just faced same issue. Chart size is rendered correctly according to the domain but time axis renders ticks only within the range from data set ignoring domain setting. https://codesandbox.io/s/gifted-resonance-9opey?file=/src/App.js ReCharts 2.1.6, React 17.0.2

Did you try allowDataOverflow That should help.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to change time format all over the domain for all users
Dear Community, We have date format like month/day/year. In order to change it to day/month/year for all users in my domain, what sould...
Read more >
Using domain with datetime and date - python - Stack Overflow
Using domain with datetime and date ... I have tried using strptime , strftime no luck. How is datetime/date filtering done in Odoo?...
Read more >
Date-Range Domains - what gives - Esri Community
In ArcGIS Pro, I created a new Range Domain in a file gdb configured to use the Date field type. fgdb = r"C:\Users\johnmdye\Documents\ArcGIS\ ......
Read more >
how to format oracle.jbo.domain.date
Hi,. when you need to set attribute with datetime you don't need to set format. ... row.setAttribute("ShippedDate",new oracle.jbo.domain.Date( ...
Read more >
Working With Dates - Using D3.js
Convert string representations of the temporal data to Date objects ... scaleTime, We set the domain of the scale by passing the array...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found