图表工具提示显示相同的值

时间:2019-08-05 13:43:47

标签: javascript recharts

我遇到一个问题,即工具提示针对不同系列显示相同的值。因此,每当我将鼠标悬停在弹出框上时,都会得到以下信息: enter image description here

这是我的实现方式

  <LineChart margin={{ top: 15, right: 5, bottom: 5, left: 10 }}>
    <XAxis
      type='number'
      dataKey='timestamp'
      padding={{ left: 30, right: 30 }}
      domain={['dataMin', 'dataMax']}
      height={90}
      tickFormatter={(unixTime) => dayjs(unixTime).format('MM/DD h:mm A')}
      tickMargin={30}
    />

    <YAxis
      dataKey='Demand'
      tickFormatter={(val, _axis) => numeral(val).format('0,0') + ' kW'}
    />

    {chartData && this.renderLines(chartData, theme)}

    <CartesianGrid strokeDasharray='3 3' />
    <Legend />
    <Tooltip
      content={<LiveDailyDemandTooltip
        format={{
        Demand: '0.0'
          }} />}
    />
  </LineChart>

数据如下:

{
  "dataID-1": [
    {Demand: 4237, timestamp: 1564977600000}
    ...
  ],
  "dataID-2": [
    {Demand: 112, timestamp: 1564977600000}
    ...
  ]
}

2 个答案:

答案 0 :(得分:3)

如此处所述:https://github.com/recharts/recharts/issues/1625 您应该在XAxis中将allowDuplicatedCategory设置为false:

<XAxis allowDuplicatedCategory={false}/>

这将解决工具提示中重复值的问题。

答案 1 :(得分:0)

我能够通过以其他格式提供数据来解决此问题。图表似乎需要按其X轴值分组数据:

[
  { timestamp: 1564977600000, Demand1: 4237, Demand2: 112 },
  ...
]
相关问题