admin管理员组文章数量:1026989
I am using d3 v5 with reactJS. I am calling d3.csv inside react 'List' class like following:
import React from 'react';
import * as d3 from 'd3';
class List extends React.Component{
ponentDidMount(){
d3.csv("./data/data.csv").then(function(d, error){
console.log(d);
});
}
render(){
return(
<div> </div>
);
}
}
export default List;
and List is being imported in following 'App' class
import React, { Component } from 'react';
import logo from './logo.svg';
import List from './ponents/list/List';
import './App.css';
class App extends Component {
render() {
return (
<div className="App">
<header className="App-header">
<img src={logo} className="App-logo" alt="logo" />
<h1 className="App-title">Wele to React</h1>
</header>
<List/>
</div>
);
}
}
export default App;
Following is the 'index.js' code:
import React from 'react';
import ReactDOM from 'react-dom';
import './index.css';
import App from './App';
import registerServiceWorker from './registerServiceWorker';
ReactDOM.render(<App />, document.getElementById('root'));
registerServiceWorker();
'root' is a div id in index.html. By calling console.log in List file inside d3.csv I am getting content of index.html file in the console. But I want CSV content.
I am using d3 v5 with reactJS. I am calling d3.csv inside react 'List' class like following:
import React from 'react';
import * as d3 from 'd3';
class List extends React.Component{
ponentDidMount(){
d3.csv("./data/data.csv").then(function(d, error){
console.log(d);
});
}
render(){
return(
<div> </div>
);
}
}
export default List;
and List is being imported in following 'App' class
import React, { Component } from 'react';
import logo from './logo.svg';
import List from './ponents/list/List';
import './App.css';
class App extends Component {
render() {
return (
<div className="App">
<header className="App-header">
<img src={logo} className="App-logo" alt="logo" />
<h1 className="App-title">Wele to React</h1>
</header>
<List/>
</div>
);
}
}
export default App;
Following is the 'index.js' code:
import React from 'react';
import ReactDOM from 'react-dom';
import './index.css';
import App from './App';
import registerServiceWorker from './registerServiceWorker';
ReactDOM.render(<App />, document.getElementById('root'));
registerServiceWorker();
'root' is a div id in index.html. By calling console.log in List file inside d3.csv I am getting content of index.html file in the console. But I want CSV content.
Share Improve this question edited Apr 24, 2018 at 11:54 Ahmed Ashour 5,61110 gold badges39 silver badges62 bronze badges asked Apr 24, 2018 at 11:49 IgniterIgniter 6151 gold badge5 silver badges13 bronze badges 1
- @altocumulus now I have posted clearly what the issue is. Please help if you understand the issue. – Igniter Commented Apr 25, 2018 at 14:49
3 Answers
Reset to default 4You could try using the import statement to load the CSV file at first. :)
import React from 'react';
import * as d3 from 'd3';
import data from "./data/data.csv";
class List extends React.Component{
ponentDidMount(){
d3.csv(data).then(function(d, error){
if (error) {
console.log(error);
} else {
console.log(d);
};
});
}
render(){
return(
<div> </div>
);
}
}
export default List;
I had the same issue and here's how I solved it. Check if the data is in the same directory as the index.html
file. In my case, my index.html
file is in the public/
folder, so I created public/data/data.csv
. You can then try d3.csv('./data/data', function(data){...})
to read in the data.
If you are using hooks and useState, ensure that the URL is a string and not resolved as an object before using the data.
Before Input
Before Output
After Input
After Output
I am using d3 v5 with reactJS. I am calling d3.csv inside react 'List' class like following:
import React from 'react';
import * as d3 from 'd3';
class List extends React.Component{
ponentDidMount(){
d3.csv("./data/data.csv").then(function(d, error){
console.log(d);
});
}
render(){
return(
<div> </div>
);
}
}
export default List;
and List is being imported in following 'App' class
import React, { Component } from 'react';
import logo from './logo.svg';
import List from './ponents/list/List';
import './App.css';
class App extends Component {
render() {
return (
<div className="App">
<header className="App-header">
<img src={logo} className="App-logo" alt="logo" />
<h1 className="App-title">Wele to React</h1>
</header>
<List/>
</div>
);
}
}
export default App;
Following is the 'index.js' code:
import React from 'react';
import ReactDOM from 'react-dom';
import './index.css';
import App from './App';
import registerServiceWorker from './registerServiceWorker';
ReactDOM.render(<App />, document.getElementById('root'));
registerServiceWorker();
'root' is a div id in index.html. By calling console.log in List file inside d3.csv I am getting content of index.html file in the console. But I want CSV content.
I am using d3 v5 with reactJS. I am calling d3.csv inside react 'List' class like following:
import React from 'react';
import * as d3 from 'd3';
class List extends React.Component{
ponentDidMount(){
d3.csv("./data/data.csv").then(function(d, error){
console.log(d);
});
}
render(){
return(
<div> </div>
);
}
}
export default List;
and List is being imported in following 'App' class
import React, { Component } from 'react';
import logo from './logo.svg';
import List from './ponents/list/List';
import './App.css';
class App extends Component {
render() {
return (
<div className="App">
<header className="App-header">
<img src={logo} className="App-logo" alt="logo" />
<h1 className="App-title">Wele to React</h1>
</header>
<List/>
</div>
);
}
}
export default App;
Following is the 'index.js' code:
import React from 'react';
import ReactDOM from 'react-dom';
import './index.css';
import App from './App';
import registerServiceWorker from './registerServiceWorker';
ReactDOM.render(<App />, document.getElementById('root'));
registerServiceWorker();
'root' is a div id in index.html. By calling console.log in List file inside d3.csv I am getting content of index.html file in the console. But I want CSV content.
Share Improve this question edited Apr 24, 2018 at 11:54 Ahmed Ashour 5,61110 gold badges39 silver badges62 bronze badges asked Apr 24, 2018 at 11:49 IgniterIgniter 6151 gold badge5 silver badges13 bronze badges 1
- @altocumulus now I have posted clearly what the issue is. Please help if you understand the issue. – Igniter Commented Apr 25, 2018 at 14:49
3 Answers
Reset to default 4You could try using the import statement to load the CSV file at first. :)
import React from 'react';
import * as d3 from 'd3';
import data from "./data/data.csv";
class List extends React.Component{
ponentDidMount(){
d3.csv(data).then(function(d, error){
if (error) {
console.log(error);
} else {
console.log(d);
};
});
}
render(){
return(
<div> </div>
);
}
}
export default List;
I had the same issue and here's how I solved it. Check if the data is in the same directory as the index.html
file. In my case, my index.html
file is in the public/
folder, so I created public/data/data.csv
. You can then try d3.csv('./data/data', function(data){...})
to read in the data.
If you are using hooks and useState, ensure that the URL is a string and not resolved as an object before using the data.
Before Input
Before Output
After Input
After Output
本文标签: javascriptd3csv returns indexhtml content in consolelogStack Overflow
版权声明:本文标题:javascript - d3.csv returns index.html content in console.log - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745665306a2162141.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论