AmCharts 数据加载器不适用于使用股票图表生成系列插件

AmCharts dataloader doesn't work with generate series plugin using stock chart

我正在加载在 AmCharts 插件中工作的 JSON,但我无法使其与外部 JSON 和股票图表一起工作。我在这里使用此示例中的代码:https://www.amcharts.com/kbase/automatically-generate-series-graphs-data-stock-chart/

我正在使用下面的 dataLoader 配置:

"dataLoader": {
  "url": "data/MSFT.json", 
  "format": "json", 
  "showCurtain": true, 
  "showErrors": true, 
  "async": true
}

当我的数据直接在 dataProvider 中作为静态 JSON 提供但不使用 dataLoader 时,插件按预期工作。

您链接到的插件不是为与数据加载器一起工作而设计的,但是您可以按照 datePadding plugin 所做的将主要逻辑包装到一个可以稍后调用的函数,然后添加一些代码以在将调用附加到 dataLoader 的完整回调之前检查 dataLoader 是否存在:

AmCharts.addInitHandler(function(chart) {  
  function generateGraphsFromSeries(chart) { 
    //original logic here
  }
  //determine whether the dataloader is used or not
  /**
     * Stock or Serial chart?
     */
    if ( chart.type === "stock" ) {

        // check each data set if there's a Data Loader active
        var loader;
        for ( var i = 0; i < chart.dataSets.length; i++ ) {
            if ( chart.dataSets[ i ].dataLoader !== undefined && chart.dataSets[ i ].dataLoader.url !== undefined ) {
                loader = chart.dataSets[ i ].dataLoader;
            }
        }

        // is there at least one data loader?
        if ( loader === undefined ) {
            // nope - let's go with processing
            generateGraphsFromSeries( chart );
        } else {
            // yeah - let's use the last data loader's complete event
            if ( loader.complete ) {
                loader._complete = loader.complete;
            }
            loader.complete = function( chart ) {
                // call original complete
                if ( loader._complete )
                    loader._complete.call( this, chart );

                // now let's do our thing
                generateGraphsFromSeries( chart );
            }
        }

    } else if ( chart.stockChart === undefined && chart.id !== "scrollbarChart" ) {

        // check for Data Loader
        var loader = chart.dataLoader;
        if ( loader !== undefined && loader.url !== undefined ) {
            if ( loader.complete ) {
                loader._complete = loader.complete;
            }
            loader.complete = function( chart ) {
                // call original complete
                if ( loader._complete )
                    loader._complete.call( this, chart );

                // now let's do our thing
                generateGraphsFromSeries( chart );
            }
        } else {
            generateGraphsFromSeries( chart );
        }

    }
}, ["serial", "stock"]);

这里有一个 updated demo 演示如何使用外部 JSON 文件。

另请注意,股票图表需要 dataLoader 位于 dataSets 数组中的 dataSet 对象内,这与系列图表不同:

AmCharts.makeChart("...", {
  type: stock,
  // ...
  dataSets: [{
    dataLoader: {
      // config
    },
    //rest of the config here.
  },
    // repeat for each dataSet
  ],
  // ...
});