Javascript React/D3:图形将不显示
在另一个组件中,我在屏幕上毫无问题地显示数据内容。然而,我在使用相同的数据构建图表时遇到了问题。当我单击“提交”时,屏幕上会显示一个图形,但浏览器一直在崩溃。有人能帮我找出代码中的原因吗,谢谢 在控制台中,数据包含类似于JSON文件的内容:Javascript React/D3:图形将不显示,javascript,reactjs,d3.js,Javascript,Reactjs,D3.js,在另一个组件中,我在屏幕上毫无问题地显示数据内容。然而,我在使用相同的数据构建图表时遇到了问题。当我单击“提交”时,屏幕上会显示一个图形,但浏览器一直在崩溃。有人能帮我找出代码中的原因吗,谢谢 在控制台中,数据包含类似于JSON文件的内容: {data:{action:[action: "changed", timestamp: 12345678], ...} 显示组件: class Graph extends Component { constructor({nodes}) {
{data:{action:[action: "changed", timestamp: 12345678], ...}
显示组件:
class Graph extends Component {
constructor({nodes}) {
const times = d3.extent(nodes.action.map(action => {action.action}))
const range = [50, 450]
super({nodes})
this.state = {nodes, times, range}
}
componentDidMount() {
let group
const { nodes, times, range } = this.state
const { target } = this.refs
const scale = d3.scaleTime().domain(times).range(range)
d3.select(target)
.append('svg')
.attr('height', 200)
.attr('width', 500)
group = d3.select(target.children[0])
.selectAll('g')
.data(nodes)
.enter()
.append('g')
.attr(
'transform',
(d, i) => 'translate(' + scale(d.timestamp) + ', 0)'
)
group.append('circle')
.attr('cy', 160)
.attr('r', 5)
.style('fill', 'blue')
group.append('text')
.text(d => d.timestamp + " - " + d.action)
.style('font-size', 10)
.attr('y', 115)
.attr('x', -95)
.attr('transform', 'rotate(-45)')
}
render() {
return (
<Graph name="Timeline"/>
)
}
}
export default Graph;
类图扩展组件{
构造函数({nodes}){
const times=d3.extent(nodes.action.map(action=>{action.action}))
常数范围=[50450]
超级({nodes})
this.state={nodes,times,range}
}
componentDidMount(){
let组
const{nodes,times,range}=this.state
const{target}=this.refs
常量scale=d3.scaleTime().domain(次).range(范围)
d3.选择(目标)
.append('svg')
.attr('height',200)
.attr('width',500)
group=d3.select(target.children[0])
.selectAll('g')
.数据(节点)
.输入()
.append('g')
艾特先生(
“转换”,
(d,i)=>'translate('+scale(d.timestamp)+',0'
)
group.append('circle'))
.attr('cy',160)
.attr('r',5)
.style('填充','蓝色')
group.append('text')
.text(d=>d.timestamp+“-”+d.action)
.style('font-size',10)
.attr('y',115)
.attr('x',-95)
.attr('transform'、'rotate(-45'))
}
render(){
返回(
)
}
}
导出默认图形;
这个问题需要更好的标题。这不是JSON(甚至不是javascript),数组不能包含key:value
对..@jeffreyroosendal那么它是什么?我不知道!试试这个:console.log(JSON.stringify(_-var))代码>(_var_是'JSON'变量)。这样行吗?也许这只是因为控制台上的复制粘贴错误。Chrome开发者工具说它的JSON。