質問:データに可変量の日付が含まれている場合、x軸を昇順の日付に「再フォーマット」するにはどうすればよいですか?
さまざまな量の「日付」が関連付けられているデータがいくつかあります。たとえば、LineOneには次のものが含まれます。
{ x: "2019-05-01", y: 2 },
{ x: "2019-06-01", y: 7 },
{ x: "2020-03-01", y: 1 }
2行目には次のものが含まれています。
{ x: "2019-05-01", y: 1 },
{ x: "2019-06-01", y: 5 },
{ x: "2020-05-01", y: 5 }
コードボックス:https://codesandbox.io/s/dreamy-almeida-x7rnd?fontsize = 14&hidenavigation = 1&theme = dark
データはx軸上で「不均一」であるため、奇妙に見えるのはなぜですか。「ダミーの日付」とそれにデータを入力すると、x軸を指定して目的の形式にすることができます。問題は、ダミーの日付とデータです。これは、自分のデータを正確にモデル化していないためです。データベースなので、このようなことは避けたいと思います。
これまでに行ったことは、ドキュメントを確認することです。にtype: "time"
フォーマットを追加しようとしましたがxscale
、エラーが発生します
v.getTime()
関数ではありません。
タイムスケールを使用するには、コンポーネントとプロパティのformat
両方に追加する必要がResponsiveLine
ありxScale
ます
xFormat:
x値のオプションのフォーマッタ。
フォーマットされた値は、ラベルとツールチップに使用できます。
タイムスケールを使用する場合は、値がDateオブジェクトに変換されるときに、時間形式を指定する必要があります。
完全な構成:(作業デモ)
import React from "react";
import { ResponsiveLine } from "@nivo/line";
export default function App() {
return (
<div style={{ height: 350 }} className="App">
<ResponsiveLine
data={[
{
id: "LineOne",
data: [
{ x: "2019-05-01", y: 2 },
{ x: "2019-06-01", y: 7 },
{ x: "2020-03-01", y: 1 }
]
},
{
id: "LineTwo",
data: [
{ x: "2019-05-01", y: 1 },
{ x: "2019-06-01", y: 5 },
{ x: "2020-05-01", y: 5 }
]
},
{
id: "LineThree",
data: [
{ x: "2020-02-01", y: 4 },
{ x: "2020-03-01", y: 6 },
{ x: "2020-04-01", y: 1 }
]
}
]}
margin={{ top: 50, right: 110, bottom: 50, left: 60 }}
xScale={{
type: "time",
format: "%Y-%m-%d"
}}
xFormat="time:%Y-%m-%d"
yScale={{
type: "linear",
min: "auto",
max: "auto",
stacked: false,
reverse: false
}}
axisTop={null}
axisRight={null}
axisLeft={{
orient: "left",
tickSize: 5,
tickPadding: 5,
tickRotation: 0,
legend: "count",
legendOffset: -40,
legendPosition: "middle"
}}
axisBottom={{
format: "%b %d",
//tickValues: "every 2 days",
// tickRotation: -90,
legend: "time scale",
legendOffset: -12
}}
colors={{ scheme: "nivo" }}
pointSize={10}
pointColor={{ theme: "background" }}
pointBorderWidth={2}
pointBorderColor={{ from: "serieColor" }}
pointLabel="y"
pointLabelYOffset={-12}
useMesh={true}
legends={[
{
anchor: "bottom-right",
direction: "column",
justify: false,
translateX: 100,
translateY: 0,
itemsSpacing: 0,
itemDirection: "left-to-right",
itemWidth: 80,
itemHeight: 20,
itemOpacity: 0.75,
symbolSize: 12,
symbolShape: "circle",
symbolBorderColor: "rgba(0, 0, 0, .5)",
effects: [
{
on: "hover",
style: {
itemBackground: "rgba(0, 0, 0, .03)",
itemOpacity: 1
}
}
]
}
]}
/>
)
</div>
);
}
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加