当您有很多数组元素时,过滤数据以在 react-chartjs-2 中更加可视化数据

问题描述

我想在我的图表上获得适当的可视化数据,但它很混乱,因为有 1000 个数组元素。我不想减少数据,但实际上是为了获得适当的可视化,因为在图表的 x 轴上它显示的是 NaN 值。

fetch("./data.json")
      .then((res) => {
        return res.json();
      })
      .then((data) => {
        console.log(data);
        for (const dataObj of data) {
          empSal.push(parseInt(dataObj.intensity));
          empAge.push(parseInt(dataObj.start_year));
        }.catch((err) => {
    console.log(err);
  });

(1000) [{…},{…}, …]

enter image description here

样本数据(data.json):

 [
        {
            "end_year": "","intensity": 6,"sector": "Energy","topic": "gas","insight": "Annual Energy Outlook","url": "http://www.eia.gov/outlooks/aeo/pdf/0383(2017).pdf","region": "northern America","start_year": "","impact": "","added": "January,20 2017 03:51:25","published": "January,09 2017 00:00:00","country": "United States of America","relevance": 2,"pestle": "Industries","source": "EIA","title": "U.S. natural gas consumption is expected to increase during much of the projection period.","likelihood": 3
        },

....

希望输出为:

enter image description here

解决方法

您可以filter data 数组同时具有 start_yearintensity 以及 map 两个值:

const intensity = data.filter(el => el.intensity && el.start_year).map(val => val.intensity);
const year = data.filter(el => el.intensity && el.start_year).map(val => val.start_year);

使用这两个,您可以根据您的要求绘制图形

样本数据过滤示例here

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...