Formatted date date on x-axis

1.2k views Asked by At

I'm trying to display a line chart based on this data set:

[
  { x: new Date(2018, 1, 1), y: 0 },
  { x: new Date(2018, 2, 1), y: 50 },
  { x: new Date(2018, 3, 1), y: 150 },
  { x: new Date(2018, 4, 1), y: 200 },
  { x: new Date(2018, 5, 1), y: 340 },
  { x: new Date(2018, 6, 1), y: 490 },
  { x: new Date(2018, 7, 1), y: 590 },
  { x: new Date(2018, 8, 1), y: 700 },
  { x: new Date(2018, 9, 1), y: 1230 },
  { x: new Date(2018, 10, 1), y: 900 },
]

My problem: The x axis labels is duplicated 4 times, so every tick is showed 4 times: feb, feb, feb, feb, mar, mar, mar, mar ...


UPDATE:

Removing the formatting of the date reveals that ´react-vis` renders a tick for each week (feb 4, feb 11, feb 18, feb 25, mar 4, mar 11 ...) But the data has a monthly interval.

How can I make the x axis follow the data?


This is my code:

<FlexibleWidthXYPlot height={300} xType="time">
  <XAxis
    // tslint:disable-next-line jsx-no-lambda
    tickFormat={(d: Date) => formatDate(d, { rawFormat: 'MMM' })}
    tickLabelAngle={-45}
    // xDomain={[new Date(data[0].x), new Date(data[data.length - 1].x)]}
    // xRange={[0, 9]}
    // tickValues={[0, 9]}
  />
  <YAxis />
  <HorizontalGridLines />
  <AreaSeries
    data={data}
    opacity={0.25}
    fill="#556887"
    stroke="#556887"
    // tslint:disable-next-line jsx-no-lambda
    onNearestXY={(value: any) => {
      this.setState({ tooltipValue: value })
    }}
    curve={curveName}
  />
  <LineMarkSeries data={data} stroke="#556887" fill="#556887" strokeWidth={3} size={3} curve={curveName} />
</FlexibleWidthXYPlot>

I tried both setting Range, Domain and tickValues on the xAxis but nothing seems to work...

How can I make the labels on the X Axis only be shown once each?


UPDATE 2:

I can limit the number of ticks by setting attribute totalTicks={data.length} like this:

<XAxis
  // tslint:disable-next-line jsx-no-lambda
  tickFormat={(d: Date) => formatDate(d, { rawFormat: 'MMM' })}
  tickLabelAngle={-45}
  totalTicks={data.length}
  ...
  />

I wonder if there is a more elegant / "correct" way of controlling the intervals of the x axis ticks...??


0

There are 0 answers