Search code examples
amcharts

How to insert string into graphs in amchart


I have a string variable myGraph:

newarr.push({"type": "smoothedLine", 
             "valueField": selected_ids1[i]});

var myGraph ='[' + newarr.map(JSON.stringify).join() + ']';
myGraph = myGraph.replace(/\s/g, '');   

How i can insert this variable to amchart graphs

    var NewChart = AmCharts.makeChart("chartdiv", {
  "type": "serial",
  "addClassNames": true,
  "startDuration": 0.4,
  "theme": "light",
  "dataDateFormat": "HH:mm:ss",
  "trendLines": [],
  "applyGapValue": 0,


  "graphs": myGraph.charAt,
  "guides": [],
    "categoryField": "date

",

Solution

  • graphs is an array as outlined in the documentation. You can't assign a string directly to it. You need to parse the string back into an array:

    AmCharts.makeChart("chartdiv", {
      // ...
      graphs: JSON.parse(myGraphs),
      // ...
    });
    

    I don't understand the reasoning behind why you went through the trouble of pushing items into newArr and stringify-ing it into a separate variable when you can simply just assign it directly anyway.

    AmCharts.makeChart("chartdiv", {
      // ...
      graphs: newarr,
      // ...
    });