6

I am using a nivo line chart and want to use the x-Axis as a timeline, down to a minute. Unfortunatelly I can not render that chart because it fails to read the date properly. For example this is part of my data:

{ x: "2020-04-24T13:07:44.000+0000", y: 0.8063946735624102 }

this is the data the chart gets, generated with the following code:

let cpuEntry = {
             x: data[i].created,
             y: data[i].system_cpu
         };

When I try to open the chart I get this error message:

Uncaught TypeError: v.getTime is not a function

After a bit of research I found out, the chart needs a Date obejct. I wrapped it like this:

x: new Date(data[i].created),

which gives me a result like this:

Fri Apr 24 2020 15:07:44 GMT+0200

and this error:

Uncaught Error: Objects are not valid as a React child (found: Fri Apr 24 2020 15:25:00 GMT+0200). If you meant to render a collection of children, use an array instead.

This is part of my configuration in the ResponsiveLine:

                    xScale={{
                    format: 'native',
                    type: 'time'
                }}

I have read something about trying to use "toString()" but thats just a circle of the same errors. I hope someone can help me. If needed I will provide further information.

CrystalCase
  • 137
  • 2
  • 11

1 Answers1

9

You should to specify xScale format as

xScale={{ format: "%Y-%m-%dT%H:%M:%S.%L%Z", type: "time" }}

And xFormat (as an example)

xFormat="time:%Y-%m-%dT%H:%M:%S.%L%Z"

Or (if you want just time)

xFormat="time:%H:%M:%S.%L"

Also based on your time intervals you can set axisBottom settings as follow:

axisBottom={{
          tickValues: "every 1 second",
          tickSize: 5,
          tickPadding: 5,
          tickRotation: 0,
          format: "%S.%L",
          legend: "Time",
          legendOffset: 36,
          legendPosition: "middle"
        }}

Here is a complete working example: https://codesandbox.io/s/react-hooks-counter-demo-dbr34?file=/src/index.js

And for time formats refer to https://github.com/d3/d3-time-format

MohammadAmin Mohammadi
  • 1,183
  • 3
  • 14
  • 23
  • 3
    In combination with the axisBottom settings in the example it worked parfectly fine. Thank you very much! – CrystalCase Apr 27 '20 at 06:58
  • After configuring axisBottom, it works for me I am using nivo 0.80.0. Setting axisBottom with format and "Time" legend is very important. Without it, nothing is rendered (blank white rectangle). – Christian Esken Apr 15 '23 at 09:27