Javascript 如何使用json在amcharts线条图中绘制多条线条?

Javascript 如何使用json在amcharts线条图中绘制多条线条?,javascript,json,amcharts,Javascript,Json,Amcharts,我有一个AM图表折线图,其中包含我从url加载的多个图形。这在dataloader为数据提取json时非常有效,但是我也在尝试让脚本加载动态“图形”数据。我尝试了该解决方案,但它没有在浏览器中呈现 编辑-我修复了javascript(有一个额外的括号),现在它在浏览器中呈现,数据点在那里,但不可见,也不画线 有人有什么建议吗 css 工作时的示例: 问题在于如何创建图形-AmGraph对象中没有类型:“serial”,因为该值用于图表类型。在AmGraph对象中,可以设置为“线”、“列”、“步长

我有一个AM图表折线图,其中包含我从url加载的多个图形。这在dataloader为数据提取json时非常有效,但是我也在尝试让脚本加载动态“图形”数据。我尝试了该解决方案,但它没有在浏览器中呈现

编辑-我修复了javascript(有一个额外的括号),现在它在浏览器中呈现,数据点在那里,但不可见,也不画线

有人有什么建议吗

css

工作时的示例:

问题在于如何创建图形-AmGraph对象中没有
类型:“serial”
,因为该值用于图表类型。在AmGraph对象中,可以设置为“线”、“列”、“步长”、“平滑线”、“烛台”和“ohlc”。修正类型将修正图表

"complete": function(chart) {
   //get potential valueFields after filtering out the categoryField
   var valueFields = Object.keys(chart.dataProvider[0]).filter(function(value) {
     return value !== chart.categoryField;
   })
  //create the graphs
   valueFields.forEach(function(valueField) {
     chart.graphs.push({
       "valueField": valueField,
       "title": valueField, //added to show labels next to legend markers
       "type": "line", //type should be line, not serial
       "balloonText": "[[category]]: <b>[[value]]</b>",
       "lineAlphas": 0.2
     })
   });
 }
“完成”:功能(图表){
//过滤掉categoryField后获取潜在的valueFields
var valueFields=Object.keys(chart.dataProvider[0]).filter(函数(值){
返回值!==chart.categoryField;
})
//创建图形
valueFields.forEach(函数(valueField){
chart.graphs.push({
“valueField”:valueField,
“title”:valueField,//添加以显示图例标记旁边的标签
“type”:“line”,//类型应为line,而不是serial
“文本”:“[[category]]:[[value]]”,
“lineAlphas”:0.2
})
});
}

非常感谢!仍在学习这些图表,比绘图更灵活!有没有一种方法可以像您在上面的回答中那样循环使用AMSCharts 4中的json?我正在将v3图表转换为v4(我认为这是我最感兴趣的),但不太擅长JS。是的,尽管将此作为一个单独的问题发布更合适,因为v4版本更复杂,而且v4代码与v3完全不同。如果你想在发布问题之前先尝试一下,然后。您需要查看加载程序中的
done
事件,以修改加载后的图表设置。如果仍然无法理解,我可以这样做。比你
 <!-- Chart code -->
 <script>
 var chart = AmCharts.makeChart("chartdiv", {
  "type": "serial",
  "theme": "light",
  "dataLoader": {
    "url": "http://127.0.0.1:5000/rate_json.json",
    "format": "json",
    "complete": function(chart) {
       //get potential valueFields after filtering out the categoryField
       var valueFields = Object.keys(chart.dataProvider[0]).filter(function(value) {
         return value !== chart.categoryField;
       })
      //create the graphs
       valueFields.forEach(function(valueField) {
         chart.graphs.push({
           "valueField": valueField,
           "type": "serial",
           "balloonText": "[[category]]: <b>[[value]]</b>",
           "fillAlphas": 0.8,
           "lineAlphas": 0.2
         })
       });
     }
   },
  "valueAxes": [{
    "gridColor": "#FFFFFF",
    "gridAlpha": 0.2,
    "dashLength": 0
  }],
  "gridAboveGraphs": true,
  "startDuration": 1,
  "graphs": [],
  "chartCursor": {
    "categoryBalloonEnabled": false,
    "cursorAlpha": 0,
    "zoomable": false
  },
  "categoryField": "month",
  "categoryAxis": {
    "gridPosition": "start",
    "gridAlpha": 0
  },
  "legend": {},
  "export": {
    "enabled": true
    },
  "menu": [ {
    "class": "export-main",
    "menu": [ "PNG", "JPG", "CSV" ]
  } ]
});

</script>
<!-- Resources -->
<script src="//www.amcharts.com/lib/3/amcharts.js"></script>
<script src="//www.amcharts.com/lib/3/serial.js"></script>
<script src="//www.amcharts.com/lib/3/themes/light.js"></script>
<script type="text/javascript" src="https://www.amcharts.com/lib/3/plugins/export/export.js"></script>
<link rel="stylesheet" href='https://www.amcharts.com/lib/3/plugins/export/export.css' type='text/css' media='all'/>
<script src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/t-160/dataloader-3.19.0.min.js"></script>

<div id="chartdiv"></div>
<!-- Chart code -->
<script>
var chart = AmCharts.makeChart("chartdiv", {
  "type": "serial",
  "theme": "light",
  "dataLoader": {
    "url": "http://127.0.0.1:5000/rate_json.json",
    "format": "json"
  },
  "valueAxes": [{
    "gridColor": "#FFFFFF",
    "gridAlpha": 0.2,
    "dashLength": 0
  }],
  "gridAboveGraphs": true,
  "startDuration": 1,
  "graphs": [{
    "title": "Store 1216",
    "balloonText": "[[title]]: <b>[[value]]</b>",
    "bullet": "round",
    "bulletSize": 10,
    "bulletBorderColor": "#ffffff",
    "bulletBorderAlpha": 1,
    "bulletBorderThickness": 2,
     "lineThickness": 3,
    "valueField": "1216"
  }, {
    "title": "Store 6528",
    "balloonText": "[[title]]: <b>[[value]]</b>",
    "bullet": "round",
    "bulletSize": 10,
    "bulletBorderColor": "#ffffff",
    "bulletBorderAlpha": 1,
    "bulletBorderThickness": 2,
     "lineThickness": 3,
    "valueField": "6528"
  },
  {
    "title": "Store 230",
    "balloonText": "[[title]]: <b>[[value]]</b>",
    "bullet": "round",
    "bulletSize": 10,
    "bulletBorderColor": "#ffffff",
    "bulletBorderAlpha": 1,
    "bulletBorderThickness": 2,
     "lineThickness": 3,
    "valueField": "230"
  },{
    "title": "Store 6538",
    "balloonText": "[[title]]: <b>[[value]]</b>",
    "bullet": "round",
    "bulletSize": 10,
    "bulletBorderColor": "#ffffff",
    "bulletBorderAlpha": 1,
    "bulletBorderThickness": 2,
     "lineThickness": 3,
    "valueField": "6538"
  },{
    "title": "Store 6514",
    "balloonText": "[[title]]: <b>[[value]]</b>",
    "bullet": "round",
    "bulletSize": 10,
    "bulletBorderColor": "#ffffff",
    "bulletBorderAlpha": 1,
    "bulletBorderThickness": 2,
     "lineThickness": 3,
    "valueField": "6514"
  },{
    "title": "Store 1297",
    "balloonText": "[[title]]: <b>[[value]]</b>",
    "bullet": "round",
    "bulletSize": 10,
    "bulletBorderColor": "#ffffff",
    "bulletBorderAlpha": 1,
    "bulletBorderThickness": 2,
     "lineThickness": 3,
    "valueField": "1297"
  },
  {
    "title": "Store 6299",
    "balloonText": "[[title]]: <b>[[value]]</b>",
    "bullet": "round",
    "bulletSize": 10,
    "bulletBorderColor": "#ffffff",
    "bulletBorderAlpha": 1,
    "bulletBorderThickness": 2,
     "lineThickness": 3,
    "valueField": "6299"
  },{
    "title": "Store 5830",
    "balloonText": "[[title]]: <b>[[value]]</b>",
    "bullet": "round",
    "bulletSize": 10,
    "bulletBorderColor": "#ffffff",
    "bulletBorderAlpha": 1,
    "bulletBorderThickness": 2,
     "lineThickness": 3,
    "valueField": "5830"
  },{
    "title": "Average",
    "balloonText": "[[title]]: <b>[[value]]</b>",
    "bullet": "round",
    "bulletSize": 10,
    "bulletBorderColor": "#ffffff",
    "bulletBorderAlpha": 1,
    "bulletBorderThickness": 2,
     "lineThickness": 3,
    "valueField": "Average"
  }],
  "chartCursor": {
    "categoryBalloonEnabled": false,
    "cursorAlpha": 0,
    "zoomable": false
  },
  "categoryField": "month",
  "categoryAxis": {
    "gridPosition": "start",
    "gridAlpha": 0
  },
  "legend": {},
  "export": {
    "enabled": true
    },
  "menu": [ {
    "class": "export-main",
    "menu": [ "PNG", "JPG", "CSV" ]
  } ]
});

</script>
    [{"month": "February", "1216": 22.8, "6528": 23.65, "230": 23.61, "6538": 23.43, "6514": 23.21, "1297": 23.14, "6299": 23.11, "5830": 22.71, "Average": 23.21}, {"month": "March", "1216": 20.01, "6528": 22.61, "230": 23.67, "6538": 22.54, "6514": 22.91, "1297": 23.59, "6299": 13.87, "5830": 22.7, "Average": 21.49}, {"month": "April", "1216": 24.67, "6528": 24.64, "230": 22.7, "6538": 40.89, "6514": 38.18, "1297": 41.98, "6299": 23.19, "5830": 38.8, "Average": 37.51}, {"month": "May", "1216": 22.8, "6528": 23.65, "230": 23.61, "6538": 23.43, "6514": 23.21, "1297": 23.14, "6299": 23.11, "5830": 22.71, "Average": 23.21}, {"month": "June", "1216": 20.01, "6528": 22.61, "230": 23.67, "6538": 22.54, "6514": 22.91, "1297": 23.59, "6299": 13.87, "5830": 22.7, "Average": 21.49}, {"month": "July", "1216": 21.67, "6528": 21.64, "230": 25.7, "6538": 40.89, "6514": 38.18, "1297": 41.98, "6299": 23.19, "5830": 38.8, "Average": 37.51}, {"month": "August", "1216": 22.8, "6528": 23.65, "230": 23.61, "6538": 23.43, "6514": 23.21, "1297": 23.14, "6299": 23.11, "5830": 22.71, "Average": 23.21}, {"month": "Sept", "1216": 20.01, "6528": 22.61, "230": 23.67, "6538": 22.54, "6514": 22.91, "1297": 23.59, "6299": 13.87, "5830": 22.7, "Average": 21.49}, {"month": "Oct", "1216": 14.67, "6528": 16.64, "230": 27.7, "6538": 20.9, "6514": 22.18, "1297": 21.84, "6299": 15.19, "5830": 18.8, "Average": 21.99}]
"complete": function(chart) {
   //get potential valueFields after filtering out the categoryField
   var valueFields = Object.keys(chart.dataProvider[0]).filter(function(value) {
     return value !== chart.categoryField;
   })
  //create the graphs
   valueFields.forEach(function(valueField) {
     chart.graphs.push({
       "valueField": valueField,
       "title": valueField, //added to show labels next to legend markers
       "type": "line", //type should be line, not serial
       "balloonText": "[[category]]: <b>[[value]]</b>",
       "lineAlphas": 0.2
     })
   });
 }