使用react-native-chart-kit包为React创建了一个基本的行/区域图,如下所示。
问题:,我们如何能够独立地改变情节组成的样式/颜色?如点、线笔划、面积、轴、勾标等。
当color参数在chartConfig中更改时,它似乎会影响到几乎整个图表,包括轴勾标签、网格线、线下区域。
是否可以为图表的每个属性定义颜色?

代码:
import { LineChart } from 'react-native-chart-kit';
import { Dimensions} from 'react-native';
const screenWidth = Dimensions.get('window').width
const data = {
labels: ['January', 'February', 'March', 'April', 'May', 'June'],
datasets: [{
data: [ 20, 45, 28, 80, 99, 43 ],
color: (opacity = 1) => `rgba(134, 65, 244, ${opacity})`,
strokeWidth: 2 // optional
}]
}
const chartConfig = {
backgroundGradientFrom: '#fff',
backgroundGradientTo: '#fff',
color: (opacity = 1) => `rgba(63, 143, 244, ${opacity})`,
strokeWidth: 2 // optional, default 3
}
class Test extends Component {
render() {
return (
<LineChart
data={data}
width={screenWidth}
height={400}
chartConfig={chartConfig}
/>
)
}
}发布于 2020-04-14 20:26:09
下面是我用来创建图表的代码。请参阅“propsForDots”部分,以对数据点进行样式化。
我知道这只是你寻求帮助定制的一部分,但这是一个开始。
<LineChart
data={{
labels: labelArray,
datasets: [
{
data: valueArray
}
]
}}
width={Dimensions.get("window").width} // from react-native
height={220}
yAxisInterval={1} // optional, defaults to 1
chartConfig={{
backgroundColor: "#444",
backgroundGradientFrom: '#444',
backgroundGradientTo: '#444',
decimalPlaces: 2, // optional, defaults to 2dp
color: (opacity = 1) => `rgba(0, 110, 199, ${opacity})`,
labelColor: (opacity = 1) => `rgba(255, 255, 255, ${opacity})`,
style: {
borderRadius: 16
},
propsForDots: {
r: "6",
strokeWidth: "2",
stroke: "#fff"
}
}}
bezier
style={{
marginVertical: 8,
borderRadius: 16
}}
/>在圆点道具中,r是圆点的半径,笔画的宽度是边框的大小,笔画是边框的颜色。
我还在用这个图书馆做实验,这就是我遇到这个问题的原因。希望这能帮助你和其他人寻找同样的东西。
发布于 2021-04-07 10:04:42
对于点颜色,我们可以使用getDotColor道具,用它我们可以编辑每个点,并为每个点定义一个颜色。
getDotColor定义了点颜色函数,该函数用于计算线条图中的点的颜色并取取(dataPoint,dataPointIndex)。
getDotColor={(dataPoint, dataPointIndex) => {
console.log('dataPoint ---->', dataPoint);
console.log('dataPointIndex --->', dataPointIndex);
//based on condition we return the color as string
if (dataPointIndex === 0) return 'red';
}}https://stackoverflow.com/questions/57062646
复制相似问题