Javascript 不确定是否调用了d3.json内的回调

Javascript 不确定是否调用了d3.json内的回调,javascript,reactjs,d3.js,Javascript,Reactjs,D3.js,我试图使用d3选择一些元素,附加g,并在svg内部生成一个轴 我已经调试了renderWeather中的每个步骤,似乎执行了d3.json,但我没有看到渲染元素。我不明白为什么 import React, { Component } from 'react' import * as d3 from 'd3' export default class Weather extends Component { componentDidMount () { this.renderWe

我试图使用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>
)
import React,{Component}来自“React”
从“d3”导入*作为d3
导出默认类扩展组件{
组件安装(){
this.renderWeather()
}
组件更新(){
this.renderWeather()
}
渲染天气=()=>{
常数tmp=(d)=>{
常数xScale=d3
.scaleLinear()
.domain([1,10])
.范围([20470])
常数yScale=d3
.scaleLinear()
.domain([0,40])
.范围([480,20])
常数xAxis=d3
.axisBottom()
.scale(xScale)
.尺寸(480)
.滴答声(8)
d3.选择(此.refs.container)
.append('g')
.attr('id','xAxisG')
.呼叫(xAxis)
d3.选择(此.refs.container)
.append('rect')
.style('宽度',100)
.style('height',100)
//我没有使用任何数据,只是想先让它工作
}
//此处为虚拟url
d3.json('https://google.com",tmp)
}
render(){
返回(
)
} }

我希望xAxis在底部,但是在里面什么也看不到。这是为什么呢?

D3.js使用API来处理HTTP请求

因此,要加载和解析JSON对象,需要解析
then()
语句中返回的承诺

d3.json('some-path').then(response => {
  console.log(response); 
  // handle the rest here
  tmp(response);
});

同样,
renderWeather()
方法中
tmp
的匿名函数应该在
then()
语句中调用,因为图表的呈现依赖于
d3.json()
This
d3.json()返回的数据https://google.com)。然后(d=>tmp(d))
works@jen007啊,,是的,我刚刚意识到你创建了一个匿名函数。我将相应地更新我的答案。是否有任何方法将json响应
d
公开为全局变量,或者这是访问
d
的唯一方法?您可以尝试将其分配给类中的属性!这样,您就可以在类
d3.json()中的任何其他地方访问它https://google.com)。然后(d=>this.data=d)
将为整个组件提供
this.data
。谢谢