从多个csv文件中绘制网站中的数据 [英] Plot data in website from multiple csv file
问题描述
我制作了一个静态网站
我不了解围绕Web开发的工具和技术,但是我举了一个 index.html 代码示例,
I do not know the tools and technology around web development, but I took an example of index.html code allowing me to plot data from a single file named "my_data_file1.csv".
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta http-equiv="X-UA-Compatible" content="ie=edge" />
<title>Coding Train: Data and APIs Project 1</title>
<script src="https://cdn.jsdelivr.net/npm/chart.js@2.8.0"></script>
</head>
<body>
<h1>Global Temperatures</h1>
<canvas id="myChart" width="400" height="200"></canvas>
<script>
window.addEventListener('load', setup);
async function setup() {
const ctx = document.getElementById('myChart').getContext('2d');
const globalTemps = await getData();
const myChart = new Chart(ctx, {
type: 'line',
data: {
labels: globalTemps.years,
datasets: [
{
label: 'Temperature in °C',
data: globalTemps.temps,
fill: false,
borderColor: 'rgba(255, 99, 132, 1)',
backgroundColor: 'rgba(255, 99, 132, 0.5)',
borderWidth: 1
}
]
},
options: {}
});
}
async function getData() {
const response = await fetch('my_data_file1.csv');
const data = await response.text();
const years = [];
const temps = [];
const rows = data.split('\n').slice(1);
rows.forEach(row => {
const cols = row.split(',');
years.push(cols[0]);
temps.push(parseFloat(cols[2]));
});
return { years, temps };
}
</script>
</body>
</html>
我的所有数据都被拆分为多个文件,因此我希望能够考虑到其中的所有CSV文件目录,而不只是一个目录。我的文件名是可变的,因此无法一一列出。
是否可以将过滤器或RegEx用作 * .csv ?
预先感谢
All of my data is split into multiple files, so I would like to be able to account for all the CSV files in a directory, rather than just one. The name of my files is variable, so I cannot list them one by one. Is it possible to use a filter or RegEx as "*.csv"? Thanks in advance,
推荐答案
是否可以使用过滤器或RegEx作为 * .csv?
Is it possible to use a filter or RegEx as "*.csv"?
否。
虽然可以生成通过将文件和目录从文件系统映射到URL,URL不是目录。
While URLs can be generated by mapping files and directories from a filesystem to URLs, a URL isn't a directory.
没有办法全局网址。
您可以确保在询问服务器时for ./
返回URL列表,然后使用客户端JS进行解析和过滤,然后请求每个URL(可能使用 Promise .all
来确定您对每个请求的响应时间。
You could ensure that the server, when asked for ./
returns a list of URLs and then parse and filter it with client-side JS, and then request each of those URLs (probably using Promise.all
to determine when you had a response for every one of those requests).
您还可以编写服务器端代码来连接所有CSV数据,因此您只需提出一个请求即可。
You could also write server-side code to concatenate all the CSV data so you only have to make one request.
这篇关于从多个csv文件中绘制网站中的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!