【发布时间】:2019-12-12 10:22:14
【问题描述】:
我正在尝试使用 d3 选择一些元素,附加 g 并在 svg 内生成一个轴。
我已经调试了renderWeather 内部的每个步骤,似乎d3.json 确实被执行了,但我没有看到任何元素呈现。我不知道为什么。
import React, { Component } from 'react'
import * as d3 from 'd3'
export default class Weather extends Component {
componentDidMount () {
this.renderWeather()
}
componentDidUpdate () {
this.renderWeather()
}
renderWeather = () => {
const tmp = (d) => {
const xScale = d3
.scaleLinear()
.domain([1, 10])
.range([20, 470])
const yScale = d3
.scaleLinear()
.domain([0, 40])
.range([480, 20])
const xAxis = d3
.axisBottom()
.scale(xScale)
.tickSize(480)
.ticks(8)
d3.select(this.refs.container)
.append('g')
.attr('id', 'xAxisG')
.call(xAxis)
d3.select(this.refs.container)
.append('rect')
.style('width', 100)
.style('height', 100)
// I haven't used any data, just trying to make it work first
}
// dummy url here
d3.json('https://google.com', tmp)
}
render() {
return (
<svg ref="container">
</svg>
)
} }
我希望 xAxis 在底部,但在 <svg></svg> 内部什么也看不到,这是为什么?
【问题讨论】:
标签: javascript reactjs d3.js