简体   繁体   English

Javascript从外部json读取HighCharts图表数据

[英]Javascript Read HighCharts Chart data from external json

At the moment I am reading the HighCharts Chart data having all the data inside the actual page. 目前,我正在读取HighCharts图表数据,其中所有数据都在实际页面中。

Here is the full code: 这是完整的代码:

<!DOCTYPE html>
<html><head>
<script type="text/javascript" src="http://code.jquery.com/jquery-latest.min.js"></script>
<script src="https://code.highcharts.com/highcharts.js"></script>
<script src="https://code.highcharts.com/highcharts-more.js"></script>
<script src="https://code.highcharts.com/modules/exporting.js"></script>
<script src="http://highcharts.github.io/export-csv/export-csv.js"></script>
<script type="text/javascript">
        parent.$('iframe').height('256');
    </script>

<style>
  .highcharts-tooltip h3 {
    margin: 0.3em 0;
}
</style>

<script>


$(function () {

    $('#container').highcharts({
        chart: {
            type: 'column',
            height: 220
        },
        title: {
            text: ''
        },
        xAxis: {
            categories: ['Data 1', 'Data 2', 'Data 3', 'Data 4', 'Data 5']
        },
        yAxis: {
            min: 0,
            title: {
                text: 'Data by Name'
            },
            stackLabels: {
                enabled: true,
                style: {
                    fontWeight: 'bold',
                    color: (Highcharts.theme && Highcharts.theme.textColor) || 'gray'
                }
            }
        },
        legend: {
            align: 'right',
            x: -30,
            verticalAlign: 'top',
            y: 25,
            floating: true,
            backgroundColor: (Highcharts.theme && Highcharts.theme.background2) || 'white',
            borderColor: '#CCC',
            borderWidth: 1,
            shadow: false
        },
        tooltip: {
            headerFormat: '<b>{point.x}</b><br/>',
            pointFormat: '{series.name}: {point.y}<br/>Total: {point.stackTotal}'
        },
        plotOptions: {
            column: {
                stacking: 'normal',
                dataLabels: {
                    enabled: true,
                    color: (Highcharts.theme && Highcharts.theme.dataLabelsColor) || 'white',
                    style: {
                        textShadow: '0 0 3px black'
                    }
                }
            }
        },
        series: [{
            name: 'Name 3',
            data: [5, 3, 4, 7, 2]
        }, {
            name: 'Name 2',
            data: [2, 2, 3, 2, 1]
        }, {
            name: 'Name 1',
            data: [3, 4, 4, 2, 5]
        }]
    });

});

</script>
  </head>
  <body style="overflow:hidden">


<div id="container" style="height: 400px; margin: 0 auto"></div>


</body></html>

My question is, how can I modify the code so I can read the data from an external json file? 我的问题是,如何修改代码,以便可以从外部json文件读取数据?

Here is an example fiddle 这是一个小提琴的例子

Also see This answer 另请参阅此答案

$.getJSON('your_json_file.json', function(data) { 
    options.series=data; 
     chart = new Highcharts.Chart(options);            

});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM