简体   繁体   English

使用 D3.js 使 React 中的 SVG 块具有渐变颜色的简单方法

[英]Simple way of making an SVG block in React have a gradient of color using D3.js

I,m trying to create a bar chart in a React component using D3.js, but can't seem to find simple instructions to create a rectangle that starts with a solid color at one end and fades away towards the other end.我正在尝试使用 D3.js 在 React 组件中创建条形图,但似乎无法找到简单的说明来创建一个矩形,该矩形的一端以纯色开始,向另一端逐渐消失。

So given a simple 30 x 300 rect:所以给定一个简单的 30 x 300 矩形:

const bar = () => {
  const b = select('svg')
    .append('rect')
    .attr('width', 300)
    .attr('height', 30)
}

const App = () => {
  useEffect(() => {
    bar()
  })
    return (
      <div>
        <svg width={200} height={200}>

        </svg>
      </div>
    )
}

render(<App />, document.getElementById('root'))

How do I make the block fade from solid black to grey?如何使块从纯黑色渐变为灰色?

You can use liner gradients with defs for that purpose.为此,您可以使用带有 defs 的线性渐变。 here is the detailed article for your reference.这是详细的文章供您参考。 D3 Charts with Gradient Color 带有渐变色的 D3 图表

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM