reactjs - Nivo ResponsiveBar 中未显示数据的颜色

标签 reactjs bar-chart nivo-react

我有一个 ResponsiveBar我的 React 应用程序中的组件,但颜色未按预期显示。我已遵循 official docs 中的指南.数据如下所示:

{
  "data": [
    {
      "bucket": "Prospects Imported",
      "Oliver Queen": 10,
      "Oliver QueenColor": "hsl(177.06123087721252,18.89068530574307%,16.53273587631816%)",
      "Ray Palmer": 10,
      "Ray PalmerColor": "hsl(287.8197874078934,62.18829850513416%,13.604789291455033%)"
    },
    {
      "bucket": "Assigned",
      "Oliver Queen": 158,
      "Oliver QueenColor": "hsl(130.29946551632844,21.88588940442986%,85.78716216902176%)",
      "Ray Palmer": 2,
      "Ray PalmerColor": "hsl(192.1716868789712,1.8797192964029374%,7.158239130186517%)"
    },
    {
      "bucket": "Calls Made",
      "Oliver Queen": 24,
      "Oliver QueenColor": "hsl(164.04170858156496,47.95430493222506%,79.37512126292603%)",
      "Ray Palmer": 0,
      "Ray PalmerColor": "hsl(38.148811405937096,23.83917214972724%,37.366418973565544%)"
    },
    {
      "bucket": "Emails Sent",
      "Oliver Queen": 120,
      "Oliver QueenColor": "hsl(316.96830604521773,3.6081626612787465%,54.49458825991964%)",
      "Ray Palmer": 0,
      "Ray PalmerColor": "hsl(223.97078722168806,98.41710495281106%,47.3590863956761%)"
    },
    {
      "bucket": "Texts Sent",
      "Oliver Queen": 0,
      "Oliver QueenColor": "hsl(83.50359081784423,77.57558426662207%,3.9445038120280884%)",
      "Ray Palmer": 0,
      "Ray PalmerColor": "hsl(127.2531925420342,14.870911431246302%,27.26153404734799%)"
    }
  ],
  "keys": [
    "Oliver Queen",
    "Ray Palmer"
  ]
}
我路过datakeys作为 ResponsiveBar 的 Prop .目前,条形图看起来像这样,颜色错误:
enter image description here
正如您所看到的,数据中的颜色没有得到反射(reflect)。
这是我的 ResponsiveBar 组件:
<ResponsiveBar
                    data={chartListSection.data}
                    keys={chartListSection.keys}
                    indexBy="bucket"
                    margin={{ top: 50, right: 50, bottom: 110, left: 60 }}
                    padding={0.3}
                    valueScale={{ type: 'linear' }}
                    indexScale={{ type: 'band', round: true }}
                    colors={{ scheme: 'nivo' }}
                    defs={[
                      {
                        id: 'dots',
                        type: 'patternDots',
                        background: 'inherit',
                        color: '#38bcb2',
                        size: 4,
                        padding: 1,
                        stagger: true,
                      },
                      {
                        id: 'lines',
                        type: 'patternLines',
                        background: 'inherit',
                        color: '#eed312',
                        rotation: -45,
                        lineWidth: 6,
                        spacing: 10,
                      },
                    ]}
                    borderColor={{
                      from: 'color',
                      modifiers: [['darker', 1.6]],
                    }}
                    axisTop={null}
                    axisRight={null}
                    axisBottom={{
                      tickSize: 5,
                      tickPadding: 5,
                      tickRotation: 0,
                      legend: '',
                      legendPosition: 'middle',
                      legendOffset: 32,
                    }}
                    axisLeft={{
                      tickSize: 5,
                      tickPadding: 5,
                      tickRotation: 0,
                      legend: 'Prospects',
                      legendPosition: 'middle',
                      legendOffset: -53,
                    }}
                    labelSkipWidth={12}
                    labelSkipHeight={12}
                    labelTextColor={{
                      from: 'color',
                      modifiers: [['darker', 1.6]],
                    }}
                    legends={[
                      {
                        dataFrom: 'keys',
                        anchor: 'bottom-left',
                        direction: 'row',
                        justify: false,
                        translateX: 0,
                        translateY: 90,
                        itemsSpacing: 2,
                        itemWidth: 100,
                        itemHeight: 20,
                        itemDirection: 'left-to-right',
                        itemOpacity: 0.85,
                        symbolSize: 20,
                        effects: [
                          {
                            on: 'hover',
                            style: {
                              itemOpacity: 1,
                            },
                          },
                        ],
                      },
                    ]}
                    animate={true}
                    motionStiffness={90}
                    motionDamping={15}
                  />
如何使数据中的颜色显示在栏中?

最佳答案

您可以设置 colors带有映射到您的颜色字​​段的函数的 Prop :

<ResponsiveBar
    ......
    colors={({id, data}) => data[`${id}Color`]}
    ......
/>
在上面的代码中,颜色以 Color 结尾在您的数据中
示例结果:
enter image description here
Sandbox

关于reactjs - Nivo ResponsiveBar 中未显示数据的颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66167554/

相关文章:

javascript - 将 React 添加到大型 PHP 项目 (ZF3)

reactjs - React Redux 更新来自笨 child 的状态

javascript - ReactJS material-ui TextField 应用css

pandas - 在 pandas barplot 中的每个子组中的条形之间添加空格

javascript - 更新 Redux/React 中 value = x 的状态

plot - 无法设置行堆叠直方图条的边框线宽

reactjs - 如何在所有图表上制作 React Recharts 工具提示模式?

javascript - 如何在 nivo 图表设置中添加自定义颜色主题

javascript - 尝试以设定的时间间隔在 nivo 中创建图表