reactjs - 如何让 VictoryAxis tickValues 在victory-native 的轴上均匀分布?
问题描述
我正在尝试制作一个看起来像水银温度计的温度计。为此,我使用了强大实验室的胜利本机库。
我遇到麻烦的地方是让组件沿 y 轴均匀VictoryAxis
分布其组件。VictoryLabel
我的循环componentDidMount
正在生成一个数字数组,增量为 0.2,介于this.state.cLower
和之间this.state.cUpper
,应该设置为 y 轴上的刻度标签。
export default class Thermometer extends React.Component {
constructor (props) {
super(props)
this.state = {
temp: [0],
cUpper: 29,
cLower: 24,
tickValues: []
}
DB.ref('tempLog').limitToLast(1).on('value', snap => {
this.setState({ temp: [{ x: 0, y: [snap.val()[Object.keys(snap.val())[0]].temp] }], y0: this.state.cLower })
})
}
componentDidMount () {
let temps = new Set()
for (let i = parseFloat(this.state.cLower); i <= parseFloat(this.state.cUpper); i += 0.2) {
temps.add(Math.round(i * 10) / 10)
}
this.setState({ tickValues: Array.from(temps) })
}
render () {
return (
<VictoryChart
height={300}
width={65}>
<VictoryAxis
style={{ ticks: { size: 12 } }}
orientation={'left'}
tickValues={this.state.tickValues}
dependentAxis />
<VictoryBar
style={{ data: { fill: 'rgb(255, 0, 0)' } }}
data={this.state.temp} />
</VictoryChart>
)
}
}
我已经验证了呈现的值是正确的,但是正如您在此处看到的,所有标签几乎都呈现在彼此之上。
解决方案
因为this.state.cLower
并this.state.cUpper
分别设置了下限和上限数据的界限,所以我不得不将VictoryBar
's y0
prop 设置为this.state.cLower
<VictoryBar
y0={() => this.state.cLower}
style={{ data: { fill: 'rgb(255, 0, 0)' } }}
data={this.state.temp} />
VictoryAxis
然后开始处理这个问题,大概是通过VictoryChart
,然后VictoryAxis
' VictoryLabel
s 得到分发,并且渲染的VictoryBar
数据按预期对齐
推荐阅读
- javascript - 如何将javascript对象存储为自定义类
- python - python write_videofile 导致黑屏视频
- javascript - 如何使用 jquery ajax 在 nodejs 中获取警报?
- java - 升级到 Spring 5.2.2.RELEASE 时 AnnotationUtils.isCandidateClass(java.lang.Class, java.util.Collection) 的 NoSuchMethodError
- angularjs - 没有 .htaccess 的 Angular 部署
- java - 为什么日期转换在 Spring 中对内容类型为 json-patch 的请求不起作用?
- javascript - MongoDB 聚合 $lookup $match
- android - 使用自定义字体并根据语言环境更新 textview 的文本,除非重新启动应用程序,否则不会显示预期的文本
- asp.net-web-api - MVC\WEB Api 默认路由应该在路由列表的底部?对吧?
- python - Django模型没有将日期字段转换为datetime.date?