【发布时间】:2022-01-06 20:55:47
【问题描述】:
我有这个代码笔https://codepen.io/a166617/pen/NWvZGLd,我正在尝试为图表添加一个 x 轴标签。如您所见,y 轴标签显示了 0,25,50,75,100。这些是静态值。但是 x 轴标签名称是动态的,我无法放置标签名称。
我尝试将这行代码<div style={xtitle}>{entry.name}</div> 放入下面的代码中,但我看不到 x 轴标签。有人可以建议我哪里出错了。
const rows = (entry, rowIndex) => {
return entry.bars.map((bar, index) => {
console.log('bar', entry, bar)
const graphHeight = 490 - 5;
const height = (bar.value / entry.total) * graphHeight;
const y = (bar.y / entry.total) * graphHeight;
return (
<>
<g key={Math.random()}>
<rect
width={50}
height={height}
fill={bar.color}
x={100 + rowIndex * 60} // multiply with the width (50) + 10 for space
y={490 - y - height}
/>
<text
x={125 + rowIndex * 60} // visible centre point of your bar
y={490 - y - height/2}
dy="0.5em" // adjusts the text y position to adjust for text descenders.
textAnchor="middle" // centre the text horizontall at x
class="bar-label"
style={{ fill: 'white',
fontSize: '12px' }}// styling for this text
>{`${bar.color === '#ffcc00' && bar.value === 100 ? 'X': bar.value}`}</text>
</g>
<div style={xtitle}>{entry.name}</div>
</>
);
});
};
我正在尝试实现此屏幕截图的最终结果。屏幕截图的 x 轴标签名称为“Tom、Emma、Lucy、Kim、Steve”。同样,我试图在我的 codepen 上显示 x 轴标签。
【问题讨论】:
-
你好,检查一下,codepen.io/Free_Soul/pen/WNZNdJo
-
@AmanSharma- 嗨,您能否确认一下您的上述代码笔。我看到的代码和我的一样。
-
我刚刚编辑了您的代码,您没有看到栏下方的任何标签吗?
-
我明白了,图表的高度是 500 像素并且隐藏了文本。现在我可以看到您的代码,并且更改会反映在图表上。您可以只发布您的代码作为答案,以便我接受吗
-
好吧............
标签: javascript html css reactjs svg