0

我试图在其中实现预期结果的项目是运行 Storybook.js 的 TypeScript、React 项目。

我有一个 csv 文件,存储在我的 React 应用程序的公共文件夹中。csv 包含我想用来绘制图表以进行测试的日期和值数据。

就目前而言,我目前使用以下函数来获取数据:

private fetchFromUrl = async() => {
    const response = fetch(`${process.env.PUBLIC_URL}/tempData/data_for_damian.csv`)
        .then(res => res.text())
        .then(res => readString(res))
        .then(res =>{
            return(res.data)
        })
        
        return await response
}

返回:

Promise {<pending>}
__proto__: Promise
[[PromiseStatus]]: "fulfilled"
[[PromiseValue]]: Array(143816)

Plottable.js 需要这种格式的坐标表:

[ 
  { "x": 0, "y": 1 },
  { "x": 1, "y": 2 },
  { "x": 2, "y": 4 },
  { "x": 5, "y": 20 },
]

如何从 Promise 中提取数据表?

或者,我可以填充一个值表作为 fetch 函数的一部分吗?

4

1 回答 1

0

最后,我选择了一种不同的方法,在 fetch 函数中使用值填充数组。

    private fetchFromUrl = () => {
        type dateValue = {
            x: Date,
            y: number
        }
        
        var data: Array<dateValue> = Array();

        fetch(`${process.env.PUBLIC_URL}/tempData/monthly.csv`)
            .then(res => res.text())
            .then(res => readString(res))
            .then(res =>{
                const Coords = res.data    
                Coords.forEach(x => data.push( {x: new Date(x[0]), y: Number(x[1]) } ))
            })
        
        return(data)
    }
于 2020-08-11T12:00:39.527 回答