Highcharts(特别是mapChart)如何在数据中使用csvURL加载?

lztngnrs  于 2022-11-10  发布在  Highcharts
关注(0)|答案(1)|浏览(181)

Highcharts(特别是mapChart)如何从csv加载?
我从一个基本的例子开始:https://jsfiddle.net/gh/get/library/pure/highcharts/highcharts/tree/master/samples/maps/demo/color-axis
...这是基于.getJSON()和修改它,以匹配类似的chartstockChart工作的例子,我与csv...
例如,我尝试了以下简单的替换:

series: [{
    //... demo code ...

    //data: data,

    data: {
        csvURL: 'http://localhost/testData.csv',
        enablePolling: false
    },

    //... demo code ...

...它适用于其他图表类型(折线图、条形图等),但不适用于mapChart?当我尝试时,出现以下错误:


highmaps.src.js:54109:20 ===未捕获(在承诺中)类型错误:c.forEach不是函数
testData.csv包含以下内容以供参考:

code,value,label
ND,13,42
SD,42,69

......我也试了这个,以防万一:

"code","value","label"
"ND",13,"Thirteen"
"SD",42,"FortyTwo"

我在文档中查找过,但找不到任何针对mapChart的特殊“标注”,说明此功能不适用于mapChart
https://api.highcharts.com/highmaps/data.csvURL
我想使用data.csvURL,因为它与其他类型图表的所有其他代码流相匹配。
任何帮助都将不胜感激。

o2gm4chl

o2gm4chl1#

虽然我还不明白为什么:

series:           ...
    data:         ...
        csvURL:   ...

......不起作用,以下方法将起作用:

(async () => {

    const topology = await fetch(
        'https://code.highcharts.com/mapdata/countries/us/us-all.topo.json'
    ).then(response => response.json());

    // Instantiate the map
    Highcharts.mapChart('container', {

        chart: {
            map: topology,
            borderWidth: 1
        },

        data: {
            //csv: document.getElementById('csv').innerHTML,
            csvURL: 'http://localhost/testData.csv',
            seriesMapping: [{
                code: 0,
                value: 1,
                label: 2
            }]
        },

        title: {
            text: 'US population density (/km²)'
        },

        exporting: {
            sourceWidth: 600,
            sourceHeight: 500
        },

        legend: {
            layout: 'horizontal',
            borderWidth: 0,
            backgroundColor: 'rgba(255,255,255,0.85)',
            floating: true,
            verticalAlign: 'top',
            y: 25
        },

        mapNavigation: {
            enabled: true
        },

        colorAxis: {
            min: 1,
            type: 'logarithmic',
            minColor: '#EEEEFF',
            maxColor: '#000022',
            stops: [
                [0, '#EFEFFF'],
                [0.67, '#4444FF'],
                [1, '#000022']
            ]
        },

        plotOptions: {
            series: {
                animation: {
                    duration: 1000
                },
                joinBy: ['postal-code', 'code'],
                dataLabels: {
                    enabled: true,
                    color: '#FFFFFF',
                    format: '{point.code}'
                },
                name: 'Population density',
                tooltip: {
                    pointFormat: '{point.code}: {point.value}/km²'
                }
            }
        }
    });

})();

虽然我还是很想知道为什么它在我最初的问题中不起作用。

相关问题