React,对来自不同数据源的 Rechart 中的 X 轴标签进行排序

wok*_*uel 5 charts reactjs recharts

我需要帮助了解如何在 Rechart 图表中按升序对 X 轴标签值进行排序。目前,该图表的数据来自两个来源。

下面是给出的代码。

const series = [
  { name: 'SourceA: ', data: reportsFromSourceA },
  { name: 'SourceB: ', data: reportsFromSourceB },
]

const selectedCategories= [...]

 selectedCategories.map((category, i) => (
    <ResponsiveContainer height={350} width='50%' key={category}>
      <LineChart
        margin={{ top: 40, right: 30, left: 70, bottom: 5 }}
        syncId='category'
      >
        <XAxis dataKey='hour' allowDuplicatedCategory={false} />
        <YAxis
          label={{
            value: category,
            position: 'top',
            fontSize: 18,
            offset: 20,
            fontWeight: 'bold',
            fill: COLORS[i % 8]
          }}
          tickFormatter={formatUSD}
        />
        <CartesianGrid strokeDasharray='3 3' />
        <Tooltip formatter={formatUSD} />
        {series.map((s, i) => (
          <Line dataKey='rpc' stroke={COLORS[i % 2]} data={s.data[category]} name={s.name} key={s.name} />
        ))}
        <ReferenceArea y2={0} stroke='red' color='red' strokeOpacity={0.5} />
      </LineChart>
    </ResponsiveContainer>
  )))
Run Code Online (Sandbox Code Playgroud)

未分类的 X 标签

小智 0

我有一个类似的问题,但解决方案是:

  • 首先整理来自数据库的数据
  • 或按键值对整个数组进行排序(按升序或降序)