使图表在放置在另一个 SVG 下时可点击 <View>
Make a chart clickable when placed under another SVG <View>
我在 React Native 中使用一些 SVG 时遇到问题。
我有一个可点击且运行良好的图表,然后我需要在图表顶部放置另一个 SVG 以绘制一条代表极限分值的线。我现在面临的问题是我无法再单击图表,因为 SVG 视图位于图表之上。
我将 SVG 的背景颜色设为透明,这样我至少可以看到它后面的图表,但是,我不知道如何让它再次可点击。
是否有任何解决方法可以让我通过放置在顶部的透明视图使图表可点击?
这可能是一个愚蠢的问题,但总的来说我对 React 和 JS 都很陌生,所以我真的可以使用任何类型的帮助。 :)
图表图片如下:
Polar Chart and Svg circle
这里是具有非透明背景的相同 Svg,如您所见,它几乎覆盖了孔图。
Svg covering the chart
这是 Svg 代码:
export class NutritionChartSvg extends Component {
render() {
return (
<View style={styles.container} >
<Svg height={height * 0.5} width={width * 0.5} viewBox="0 0 100 100">
<Svg.Circle
id="circle"
cx="50"
cy="13"
r="40"
stroke="gray"
strokeWidth="0.6"
fill="none"
/>
<Text fill="black" fontSize="8" dy="-2">
<TextPath href="#circle" startOffset='181'>
100
</TextPath>
</Text>
</Svg>
</View>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
alignContent: 'center',
justifyContent: 'center' ,
position: 'absolute',
left: '25%',
height: 'auto',
width: 'auto',
},
});'
这是图表形式 chartjs:
export const NutritionChart = (props) => {
return (
<Chart
chartConfiguration={
{
type: 'polarArea',
data: {
labels: ['Fiber', 'Protein', 'Healthy Oil', 'Good Energy', 'Immune
Defense'],
datasets: [
{
label: '# of Votes',
data: [
50,
140,
90,
120,
100,
],
backgroundColor: backgroundColor,
borderColor: borderColor,
borderWidth: datasets.border,
hoverBackgroundColor: hoverBackgroundColor,
},
],
},
options: {
layout: {
padding: {
top: options.layout.padding.top,
bottom: options.layout.padding.bottom,
}
},
legend: {
display: false,
fullWidth: false,
labels: {
fontSize: options.legend.labels.fontSize,
boxWidth: options.legend.labels.boxWidth,
padding: options.legend.labels.padding,
}
},
scale: {
display: false,
ticks: {
min:0,
max:200,
}
},
responsive: true,
maintainAspectRatio: false,
},
}
}
defaultFontSize={10}
/>
);
};
并且他们在一个视图中:
<View style={styles.nutritionChart} key={3}>
<NutritionChart/>
<NutritionChartSvg/>
</View>
或者:
- 将限制线移动到图表 SVG 中,而不是将其单独放在顶部,或者
- 在顶部 SVG 上设置
pointer-events: none
。这将使点击直接通过它。
我在 React Native 中使用一些 SVG 时遇到问题。 我有一个可点击且运行良好的图表,然后我需要在图表顶部放置另一个 SVG 以绘制一条代表极限分值的线。我现在面临的问题是我无法再单击图表,因为 SVG 视图位于图表之上。 我将 SVG 的背景颜色设为透明,这样我至少可以看到它后面的图表,但是,我不知道如何让它再次可点击。
是否有任何解决方法可以让我通过放置在顶部的透明视图使图表可点击?
这可能是一个愚蠢的问题,但总的来说我对 React 和 JS 都很陌生,所以我真的可以使用任何类型的帮助。 :)
图表图片如下:
Polar Chart and Svg circle
这里是具有非透明背景的相同 Svg,如您所见,它几乎覆盖了孔图。
Svg covering the chart
这是 Svg 代码:
export class NutritionChartSvg extends Component {
render() {
return (
<View style={styles.container} >
<Svg height={height * 0.5} width={width * 0.5} viewBox="0 0 100 100">
<Svg.Circle
id="circle"
cx="50"
cy="13"
r="40"
stroke="gray"
strokeWidth="0.6"
fill="none"
/>
<Text fill="black" fontSize="8" dy="-2">
<TextPath href="#circle" startOffset='181'>
100
</TextPath>
</Text>
</Svg>
</View>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
alignContent: 'center',
justifyContent: 'center' ,
position: 'absolute',
left: '25%',
height: 'auto',
width: 'auto',
},
});'
这是图表形式 chartjs:
export const NutritionChart = (props) => {
return (
<Chart
chartConfiguration={
{
type: 'polarArea',
data: {
labels: ['Fiber', 'Protein', 'Healthy Oil', 'Good Energy', 'Immune
Defense'],
datasets: [
{
label: '# of Votes',
data: [
50,
140,
90,
120,
100,
],
backgroundColor: backgroundColor,
borderColor: borderColor,
borderWidth: datasets.border,
hoverBackgroundColor: hoverBackgroundColor,
},
],
},
options: {
layout: {
padding: {
top: options.layout.padding.top,
bottom: options.layout.padding.bottom,
}
},
legend: {
display: false,
fullWidth: false,
labels: {
fontSize: options.legend.labels.fontSize,
boxWidth: options.legend.labels.boxWidth,
padding: options.legend.labels.padding,
}
},
scale: {
display: false,
ticks: {
min:0,
max:200,
}
},
responsive: true,
maintainAspectRatio: false,
},
}
}
defaultFontSize={10}
/>
);
};
并且他们在一个视图中:
<View style={styles.nutritionChart} key={3}>
<NutritionChart/>
<NutritionChartSvg/>
</View>
或者:
- 将限制线移动到图表 SVG 中,而不是将其单独放在顶部,或者
- 在顶部 SVG 上设置
pointer-events: none
。这将使点击直接通过它。