【问题标题】:show date in( MM-DD) format in x-axis using react-vis使用 react-vis 在 x 轴中以(MM-DD)格式显示日期
【发布时间】:2019-05-08 12:13:13
【问题描述】:

我正在尝试在我的项目中实现 react-vis。我需要根据日期显示数据。我正在使用 tickFormat,但它在 x 轴上显示了两次相同的日期。我在这里添加了几行代码。

       <XYPlot
        width={1140}
        height={440}
        >
        <LineMarkSeries
           lineStyle={{stroke: '#e0e0e0'}}
            markStyle={{stroke: '#6dc6c1'}}
            style={{
                strokeWidth: '2px'
            }}
            strokeStyle="solid"
            data={[
                {
                    x: Date.parse("05/05/2019"),
                    y: 0
                },
                {
                    x: Date.parse("05/12/2019"),
                    y: 12
                },
                {
                    x: Date.parse("05/13/2019"),
                    y: 16
                }
            ]}
         />
          <XAxis
            attr="x"
            attrAxis="y"
            orientation="bottom"
            tickFormat={function tickFormat(d){return 
                           moment(d).format("MMM DD")}}
            tickLabelAngle={0}
         />
        <YAxis />
    </XYPlot>

【问题讨论】:

  • 在数据数组中保持moment.format(MM-DD)相同的日期格式。
  • 我试过了,但它不起作用。显示此错误(错误: 属性 cx:预期长度,“NaN”。)我认为数据数组内部可能不支持这种格式。

标签: reactjs react-vis


【解决方案1】:

如果您将xType 指定为ordinal,它将使用您的x 值作为刻度标签(例如:像条形图)。因此,您不必使用 Date.parse 对 x 值进行冗余转换。

<XYPlot
  width={1140}
  height={440}
  xType='ordinal'
>
    <LineMarkSeries
      lineStyle={{stroke: '#e0e0e0'}}
      markStyle={{stroke: '#6dc6c1'}}
      style={{ strokeWidth: '2px' }}
      strokeStyle="solid"
      data={[
        {
          x: "05/05/2019",
          y: 0
        },
        {
          x: "05/12/2019",
          y: 12
        },
        {
          x: "05/13/2019",
          y: 16
        }
      ]}
    />
    <XAxis />
</XYPlot>

【讨论】:

  • 这将生成一个图表,其中所有日期都彼此相邻,无论两个日期之间经过了多少时间。
猜你喜欢
  • 2018-06-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多