$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>javascript - d3.csv returns index.html content in console.log - Stack Overflow|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

javascript - d3.csv returns index.html content in console.log - Stack Overflow

matteradmin15PV0评论

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
Add a ment  | 

3 Answers 3

Reset to default 4

You 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

Post a comment

comment list (0)

  1. No comments so far