reactjs - How to pull an SVG linearGradient id from a map function
问题描述
I need to create a dynamic svg semi circle gauge with a linearGradient that reacts to changes in value. For the time being, these angles are passed as props and hardcoded.
I have an arc component that defines the semi circle. The arc is then rendered into the parent component that receives additional props.
I have defined a color's array which holds my gradient colors, in the parent component, I map through the colors array (three colors) and for each one, i render a element with the contained attributes. I set the id dynamic using id={linearColors${index}
}.
Now still in my parent component I again map through the colors and for each, render an arc component then pass it the attributes.
My question is, how can i access the linearGradient id from outside the map function to use as a stroke property in my arc map method?
The angle points for each stop are already defined. Will this method work?
var colForGradient = <G>{colors.map((color, index) => {
const nextCol = colors[index + 1]
if (!nextCol) {
return null
}
console.log(nextCol, 'nextCol')
console.log(color, 'color')
return (
<linearGradient key={index} id={`linearColors${index}`} x1={angles[0]} y1={angles[1]} x2={angles[2]} y2={angles[3]}>
<stop offset="0" stop-color={color}></stop>
<stop offset="1" stop-color={nextCol}></stop>
</linearGradient>
)
// convert value to boolean and negate to true
}).filter((comp) => !!comp)
}</G>
var arcs = <G>{colors.map((color, i) => <Arc
// style={"stroke: "}
cx={256}
cy={256}
key={i}
stroke={(id) => colForGradient(id)}
strokeWidth={strokeWidth}
color={color}
startAngle={angles[i]}
endAngle={angles[i + 1]}
transform={transformCircleToDialFromDown}
onClick={() => arcClick(i)}
className="hover-highlight"
/>)}</G>;
解决方案
所以我设法通过渲染 colForGradient 并将 arcs 中的 color prop 引用为 color={ url(#linearColors${i})
} 来将 id 引用传递给 arcs。我现在遇到的问题是渐变没有动态显示。在日志中,弧似乎接受角度点作为偏移量和 linearGradient 的 id
var colForGradient = <G>{colors.map((color, index) => {
const nextCol = index < index.length - 1 ? colors[index + 1] : colors[index]
if (!nextCol) {
return null
}
console.log(nextCol, 'nextCol')
console.log(color, 'color')
return (
<linearGradient key={index} id={`linearColors${index}`} x1="1" y1="0" x2="0" y2="1">
<stop offset={angles[index]} stop-color={color}></stop>
<stop offset={angles[index + 1]} stop-color={nextCol}></stop>
</linearGradient>
)
// convert value to boolean and negate to true
}).filter((comp) => !!comp)
}</G>
console.log(colForGradient, 'colgrad')
// define linear gradients in array and map over them, rendering each path?
var arcs = <G>{colors.map((color, i) => <Arc
cx={256}
cy={256}
key={i}
strokeWidth={strokeWidth}
color={`url(#linearColors${i})`}
stroke={`url(linearColors${i})`}
startAngle={angles[i]}
endAngle={angles[i + 1]}
transform={transformCircleToDialFromDown}
onClick={() => arcClick(i)}
className="hover-highlight"
/>
)
}</G>;
推荐阅读
- tomcat - /* 之后的 urlPatterns 不是在 servlet 中不起作用
- wordpress - 如何在 wordpress 标题中保留“plugins/contact-form-7/includes/js/scripts.js”,同时通过“wp_deregister_script('jquery');”删除未使用的 js?
- python - Spotify 中的显式曲目总是返回 None?
- python - 如何使用英特尔 realsense d435 创建高度图
- sql - 条件范围内相邻列的总和
- typescript - Angular 如何显示带有动态数据的饼图?
- flutter - flutter_bloc - 如何在屏幕之间将变量保存在内存中
- ruby-on-rails - RSpec:hash_include 没有按我预期的那样工作
- python - 如何将numpy数组重塑为ise KNN分类器?
- c# - EF Core、DI、存储库模式和基本存储库结构问题