简体   繁体   English

无法将日期传递给 nivo 折线图

[英]Can not pass date to nivo line chart

I am using a nivo line chart and want to use the x-Axis as a timeline, down to a minute.我正在使用 nivo 折线图,并希望将 x 轴用作时间线,最多一分钟。 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:这是我在 ResponsiveLine 中的配置的一部分:

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

I have read something about trying to use "toString()" but thats just a circle of the same errors.我读过一些关于尝试使用“toString()”的内容,但这只是一个相同错误的圈子。 I hope someone can help me.我希望有一个人可以帮助我。 If needed I will provide further information.如果需要,我将提供更多信息。

You should to specify xScale format as您应该将 xScale 格式指定为

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

And xFormat (as an example)和 xFormat(例如)

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设置如下:

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这是一个完整的工作示例: 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对于时间格式,请参阅https://github.com/d3/d3-time-format

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM