Loading in data for multiple queries in Apache Echarts

Hey there,

Right now I want to load in data from multiple different queries. The issue is I’m not quite sure how to code this up. My code works if only using 1 query however I’m unsure on how to adapt the code to handle multiple queries. This can be seen below as I get the following error:

My code is as follows:

let day_of_week = [],
  total_data_volume_gb = [],
  total_3g_data_volume_gb = [],
  day_of_week1 = [],
  total_users1 = [];


data.series.forEach((test) => {
  day_of_week = test.fields.find((f) => f.name === "day_of_week").values;
  total_data_volume_gb = test.fields.find((f) => f.name === "total_data_volume_gb").values;
}); 

data.series.forEach((test1) => {
  day_of_week1 = test1.fields.find((f) => f.name === "day_of_week1").values;
  total_users1 = test1.fields.find((f) => f.name === "total_users1").values;
});

Always check the root data via
console.log(data) and what it spits out in your dev console

1 Like

Thanks, I’ve got it working now :grinning:

would help others to post how you fixed it. You asked and you were helped right? So pass it along

1 Like

Fixed using the code below:

// Iterate over each section in the series (0 and 1)
for (let i = 0; i <= 1; i++) {
  const test = data.series[i]; // Establishing path as 'data' is a JSON object
 
  if (test) {
    // For the first query
    if (i === 0) { // Checking the first query which is labelled as '0'
      const dayOfWeekField = test.fields.find((f) => f.name === "day_of_week");
      const dataVolumeField = test.fields.find((f) => f.name === "total_data_volume_gb");
 
      if (dayOfWeekField && dataVolumeField) {
        day_of_week = dayOfWeekField.values;
        total_data_volume_gb = dataVolumeField.values;
      }
    }
 
    // For the second query-> Can copy + paste for additional queries as it follows this exact format
    if (i === 1) { // Checking second query which is labelled as '1'
      const totalDataVolume1Field = test.fields.find((f) => f.name === "total_data_volume_gb1");
 
      if (totalDataVolume1Field) {
        total_data_volume_gb1 = totalDataVolume1Field.values; // Doesn't need y axis values as it should share the same as the first query
      }
    }
  }
}