2

I am trying to create Dynamic DataTable for drawing line chart.

This is my Data Structure for Line chart, In my scenario columns will not be same for all users. For example

User - ABC will have
array_json =
[["date","CA","SF"],
["May-2024",100,200],
["Jun-2024",200,300]]
User - XYZ will have
array_json=
[["date","CA","SF","IL"],
["May-2024",100,200,400],
["Jun-2024",200,300,500]]

I tried creating Data Table like below

dynamic_col_names = array_json.shift(1);
dtbl = new google.visualization.DataTable();
for(var i in dynamic_col_names){
        //console.log(dynamic_col_names[i]);
        if(i==0){
          dtbl.addColumn({label:dynamic_col_names[i],id:dynamic_col_names[i],type:'string'});                
        }
        else{          
          dtbl.addColumn({label:dynamic_col_names[i],id:dynamic_col_names[i],type:'number'});          
        }
      }

I have probelm in adding my column to rows, my code for adding values to data Table

combined[0] = dynamic_col_names;
      for (var i = 1; i < array_json.length; i++){
        var dt_ = convertdate(array_json[i][0]);  // convertdate is function I created to format the date  
// In this Line dt_ column is always fixed, but other number of columns will change based on user
        combined[i + 1] = [dt_,Number(array_json[i][i]),Number(array_json[i][i]),Number(array_json[i][i])];                       
      }  

In this line of code , I want to add this particular column "Number(array_json[i][i])" specific number of times for example user ABC has 2 number data type columns "CA","SF"

combined[i + 1] = [dt_,Number(array_json[i][i]),Number(array_json[i][i])]; 

User XYZ has 3 data type number columns "CA","SF","IL", my code should be loke this

combined[i + 1] = [dt_,Number(array_json[i][i]),Number(array_json[i][i]),Number(array_json[i][i])]; 

How can I do this in an efficient way. Thanks in advance!!

2
  • use static method arrayToDataTable() Commented Sep 16, 2024 at 21:52
  • Thank you @WhiteHat. I tried it works fine. Commented Sep 17, 2024 at 8:59

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.