gpt4 book ai didi

javascript - d3 React JS : d3. csv正在导入index.html而不是csv文件

转载 作者:行者123 更新时间:2023-11-28 14:34:36 25 4
gpt4 key购买 nike

我正在尝试在 React App.js 文件中使用 d3-request 库来导入和解析 .csv 文件中的数据。当我将结果数据输出到控制台时,它从 App.js 文件而不是从 objective-c sv 文件返回行数组。我错过了什么?

App.js

import React, {Component} from 'react';
import {csv} from 'd3-request';
import './App.css';
import Chart from './components/chart'

const API_URL = "https://nataliia-radina.github.io/react-vis-example/";

class App extends Component {
constructor(props) {
super(props)
this.state = {
results: [],
};
}
render() {
csv("./data/test.csv", function(error, data) {
if (error) throw error;
console.log(data);
});

const {results} = this.state;

return (
<div className="App">
<Chart data={results}
/>
</div>
);
}
}

export default App;

测试.csv

col1,col2,col3
hi,there,buddy

 控制台输出

(39) [{…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, columns: Array(1)]
0:{<!DOCTYPE html>: "<html lang="en">"}
1:{<!DOCTYPE html>: " <head>"}
2:{<!DOCTYPE html>: " <meta charset="utf-8">"}
3:{<!DOCTYPE html>: " <meta name="viewport" content="width=device-width"}
4:{<!DOCTYPE html>: " <meta name="theme-color" content="#000000">"}
5:{<!DOCTYPE html>: " <link rel="manifest" href="/manifest.json">"}
6:{<!DOCTYPE html>: " <link rel="shortcut icon" href="/favicon.ico">"}
7:{<!DOCTYPE html>: " <title>React App</title>"}
8:{<!DOCTYPE html>: " </head>"}
9:{<!DOCTYPE html>: " <body>"}
10:{<!DOCTYPE html>: " <noscript>"}
11:{<!DOCTYPE html>: " You need to enable JavaScript to run this app."}
12:{<!DOCTYPE html>: " </noscript>"}
13:{<!DOCTYPE html>: " <div id="root"></div>"}
14:{<!DOCTYPE html>: " <script type="text/javascript" src="/static/js/bundle.js"></script></body>"}
15:{<!DOCTYPE html>: "</html>"}
columns:["<!DOCTYPE html>"]
length:39
__proto__:Array(0)

最佳答案

首先尝试导入数据:

import React, {Component} from 'react';
import {csv} from 'd3-request';
import './App.css';
import Chart from './components/chart';
import data from './data/test.csv';

const API_URL = "https://nataliia-radina.github.io/react-vis-example/";

class App extends Component {
constructor(props) {
super(props)
this.state = {
results: [],
};
}
render() {
csv(data, function(error, data) {
if (error) throw error;
console.log(data);
});

const {results} = this.state;

return (
<div className="App">
<Chart data={results}
/>
</div>
);
}
}

export default App;

关于javascript - d3 React JS : d3. csv正在导入index.html而不是csv文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50242933/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com