作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个 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"
]
}
我路过
data
和
keys
作为
ResponsiveBar
的 Prop .目前,条形图看起来像这样,颜色错误:
<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
结尾在您的数据中
关于reactjs - Nivo ResponsiveBar 中未显示数据的颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66167554/
我有一个 ResponsiveBar我的 React 应用程序中的组件,但颜色未按预期显示。我已遵循 official docs 中的指南.数据如下所示: { "data": [ {
我是一名优秀的程序员,十分优秀!