Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
117 views
in Technique[技术] by (71.8m points)

javascript - How to fetch all data from API files and assign their values to chart?

I am trying to create a bubble chart that is going to show how many comments have been made in the files. However, my API doesn't provide the chunk of the information in one end-point, but it's more like a repository from which I have to fetch data from the different end-points. How should I approach this kind of thing?

const getData = async () => {
    console.log("Processing");
    const request = await fetch("http://25.102.238.73/api/repos/RxJava/");
    const data = await request.json();
    return data;
};

console.log(getData());

var popCanvas = document.getElementById("myChart");

Chart.defaults.global.defaultFontFamily = "Lato";
Chart.defaults.global.defaultFontSize = 18;


//  y = Line of code in the file
//  x = Line of comments in the file
var popData = {
datasets: [{
    label: ['How many comments have been made in the code?'],
    data: [{
        x:getData(),
        y:getData(),
        r:getData()
    }, 

    ],
    backgroundColor: "#FF9966"
}]
};

var bubbleChart = new Chart(popCanvas, {
type: 'bubble',
data: popData
});
</script>
question from:https://stackoverflow.com/questions/65842671/how-to-fetch-all-data-from-api-files-and-assign-their-values-to-chart

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

There are 2 problems in this code.

Problem 1: using async and sync code in one place. Here is an example snippet (for testing I removed fetch, but replaced it with its data, because your fetch is not working in snippet here).

Compare results of DATA1 and DATA2. The first one is sync, the second - async. You can see, that your sync variant is not working. So, you need async (using .then).

const getData = async() => {
  //console.log("Processing");
  //const request = await fetch("http://46.101.95.202/api/repos/RxJava/");
  //const data = await request.json();
  const data = {
    "key": "RxJava", "name": "RxJava", "language": "java",
    "path": "/RxJava_c", "links": { "self": "/api/repos/RxJava/" },
    "contents": [{
        "path": "/RxJava_c/docs", "name": "docs",
        "type": "directory",
        "follow": "/api/repos/RxJava?data_path=/RxJava_c/docs"
      },
      {
        "path": "/RxJava_c/gradle", "name": "gradle",
        "type": "directory",
        "follow": "/api/repos/RxJava?data_path=/RxJava_c/gradle"
      },
      {
        "path": "/RxJava_c/src", "name": "src",
        "type": "directory",
        "follow": "/api/repos/RxJava?data_path=/RxJava_c/src"
      }
    ]};
  return data;
};

console.log("DATA1 =", getData());

getData().then((mydata) => {
  console.log("DATA2 =", mydata);
});

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...