AmCharts4 - 无法加载文件 - 从 SQL 数据库生成的外部 JSON 文件

AmCharts4 - Unable to load file - external JSON file produced from SQL database

我很难在 AmCharts4 中打开 json 排列的数据。在我以前的图表中,我使用了非常简单的脚本 (chart.data = ;),不幸的是这次它不起作用。所以我正在使用 AmCharts 文档提出的 chart.dataSource.url 函数。当我加载在网络上找到的示例文件时,一切正常,一旦我切换到我的文件,图表就无法加载文件。我在网上找不到类似的问题,因此非常感谢您的帮助。

这是我的示例,其中工作 url 而我的文件不工作。

提前致谢:

<!doctype html>
<html>
<head>
 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<script src="https://www.amcharts.com/lib/4/core.js"></script>
<script src="https://www.amcharts.com/lib/4/charts.js"></script>
<script src="https://www.amcharts.com/lib/4/themes/animated.js"></script>
<style>


</style>

</head>

<body>
     <div id="chartdiv"></div>
</body>
</html>


<!-- Styles -->
<style>
#chartdiv {
  width: 100%;
  height: 500px;
}
</style>

<!-- Resources -->
<script src="https://cdn.amcharts.com/lib/4/core.js"></script>
<script src="https://cdn.amcharts.com/lib/4/charts.js"></script>
<script src="https://cdn.amcharts.com/lib/4/themes/animated.js"></script>

<!-- Chart code -->
<script>
am4core.ready(function() {

// Themes begin
am4core.useTheme(am4themes_animated);
// Themes end



var chart = am4core.create('chartdiv', am4charts.XYChart)
// Modify chart's colors
chart.colors.list = [
  am4core.color("#264B29"),
  am4core.color("#94B255"),
  am4core.color("#456C39"),
  am4core.color("#C4D563"),
  am4core.color("#698F47"),
  am4core.color("#F9F871"),
];

chart.legend = new am4charts.Legend()
chart.legend.position = 'top'
chart.legend.paddingBottom = 20
chart.legend.labels.template.maxWidth = 95

var xAxis = chart.xAxes.push(new am4charts.CategoryAxis())
xAxis.dataFields.category = 'year'
xAxis.renderer.cellStartLocation = 0.1
xAxis.renderer.cellEndLocation = 0.9
xAxis.renderer.grid.template.location = 0;

var yAxis = chart.yAxes.push(new am4charts.ValueAxis());


function createSeries(value, name) {
    var series = chart.series.push(new am4charts.ColumnSeries())
    series.dataFields.valueY = value
    series.dataFields.categoryX = 'year'
    series.name = name

    series.events.on("hidden", arrangeColumns);
    series.events.on("shown", arrangeColumns);

    var bullet = series.bullets.push(new am4charts.LabelBullet())
    bullet.interactionsEnabled = false
    bullet.dy = 30;
    bullet.label.text = '{valueY}'
    bullet.label.fill = am4core.color('#ffffff')

    return series;
}


// Add data
//Working url
//chart.dataSource.url = "https://s3-us-west-2.amazonaws.com/s.cdpn.io/t-160/sample_data_serial.json";

//My SQL produced JSON file is not working
chart.dataSource.url = "data/my-file.php";
chart.dataSource.adapter.add("parsedData", function(data) {
  var newData = [];
  data.forEach(function(dataItem) {
    var newDataItem = {};
    Object.keys(dataItem).forEach(function(key) {
      if (typeof dataItem[key] === "object") {
        newDataItem["_id"] = dataItem[key]["@id"];
        dataItem[key]["Column"].forEach(function(dataItem) {
          newDataItem[dataItem["@name"]] = dataItem["@id"];
        });
      } else {
        newDataItem[key] = dataItem[key];
      }
    });
    newData.push(newDataItem);
  });
  data = newData;
  return data;
});



createSeries('cars', 'The First');
createSeries('motorcycles', 'The Second');
createSeries('bicycles', 'The Third');
//createSeries('bilanca_lsk_lst', 'T4');

function arrangeColumns() {

    var series = chart.series.getIndex(0);

    var w = 1 - xAxis.renderer.cellStartLocation - (1 - xAxis.renderer.cellEndLocation);
    if (series.dataItems.length > 1) {
        var x0 = xAxis.getX(series.dataItems.getIndex(0), "yearX");
        var x1 = xAxis.getX(series.dataItems.getIndex(1), "yearX");
        var delta = ((x1 - x0) / chart.series.length) * w;
        if (am4core.isNumber(delta)) {
            var middle = chart.series.length / 2;

            var newIndex = 0;
            chart.series.each(function(series) {
                if (!series.isHidden && !series.isHiding) {
                    series.dummyData = newIndex;
                    newIndex++;
                }
                else {
                    series.dummyData = chart.series.indexOf(series);
                }
            })
            var visibleCount = newIndex;
            var newMiddle = visibleCount / 2;

            chart.series.each(function(series) {
                var trueIndex = chart.series.indexOf(series);
                var newIndex = series.dummyData;

                var dx = (newIndex - trueIndex + middle - newMiddle) * delta

                series.animate({ property: "dx", to: dx }, series.interpolationDuration, series.interpolationEasing);
                series.bulletsContainer.animate({ property: "dx", to: dx }, series.interpolationDuration, series.interpolationEasing);
            })
        }
    }
}

}); 



// end am4core.ready()
</script>

我在 my-file.php

中发现了一个打字错误

无论如何,在我解决了输入问题后,chart.dataSource.url 功能仍然无法使用,但它使用下一个 php include 脚本工作。

chart.data = <?php include './data/my-file.php'; ?>;