从多个 csv 文件中绘制网站中的数据

2022-01-20 00:00:00 csv web fetch javascript html

我创建了一个 静态网站 托管在 AWS s3 存储桶上.

I made a static website hosted on an AWS s3 bucket.

我不了解有关 Web 开发的工具和技术,但我举了一个 index.html 代码示例,该代码允许我从名为my_data_file1.csv"的单个文件中绘制数据.

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('
').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 文件,而不仅仅是一个.我的文件名是可变的,所以我不能一一列出.是否可以将过滤器或正则表达式用作*.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,但 URL 不是目录.

While URLs can be generated by mapping files and directories from a filesystem to URLs, a URL isn't a directory.

没有办法glob URL.

您可以确保服务器在请求 ./ 时返回一个 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.

相关文章