javascript - Highstocks 图表无法正确显示

标签 javascript html highcharts

我正在尝试复制我在 fiddle ( link)上找到的高股票图表示例。我将代码复制并粘贴到 html 文件中,但图表没有显示。有人能帮我解决这个问题吗?谢谢。

html 文件内容如下:

<!DOCTYPE html>
<html>
<head>

<!--highStocks start--> 

    <!--script src="./static/lib/js/highstock.js"></script>
    <script src="./static/lib/js/exporting.js"></script-->

    <script src="http://github.highcharts.com/highstock.js"></script>
    <script src="http://code.highcharts.com/stock/modules/exporting.js"></script>
    <script type="text/javascript">
$(function() {
    $.getJSON('http://www.highcharts.com/samples/data/jsonp.php?filename=aapl-ohlcv.json&callback=?', function(data) {

        // split the data set into ohlc and volume
        var ohlc = [],
            volume = [],
            volume2 = [],
            dataLength = data.length;

        for (i = 0; i < dataLength; i++) {
            ohlc.push([
                data[i][0], // the date
                data[i][1], // open
                data[i][2], // high
                data[i][3], // low
                data[i][4] // close
            ]);

            volume.push([
                data[i][0], // the date
                data[i][5] // the volume
            ]);

            volume2.push([
                data[i][0], // the date
                data[i][5] // the volume
            ]);


        }

        // set the allowed units for data grouping
        var groupingUnits = [[
            'week',                         // unit name
            [1]                             // allowed multiples
        ], [
            'month',
            [1, 2, 3, 4, 6]
        ]];

        // create the chart
        chart = new Highcharts.StockChart({
            chart: {
                renderTo: 'container',
                alignTicks: false
            },

            rangeSelector: {
                selected: 1
            },

            title: {
                text: 'AAPL Historical'
            },

            yAxis: [{
                title: {
                    text: 'OHLC'
                },
                height: 200,
                lineWidth: 2
            }, {
                title: {
                    text: 'Volume'
                },
                top: 300,
                height: 100,
                offset: 0,
                lineWidth: 2
            }, {
                title: {
                    text: 'Volume2'
                },
                top: 400,
                height: 100,
                offset: 0,
                lineWidth: 2
            }],

            series: [{
                type: 'candlestick',
                name: 'AAPL',
                data: ohlc,
                dataGrouping: {
                    units: groupingUnits
                }
            }, {
                type: 'column',
                name: 'Volume',
                data: volume,
                yAxis: 1,
                dataGrouping: {
                    units: groupingUnits
                }
            }, {
                type: 'column',
                name: 'Volume2',
                data: volume,
                yAxis: 2,
                dataGrouping: {
                    units: groupingUnits
                }
            }]
        });
    });
});
    </script>


<!--highStocks end--> 



</head>
<body class="application">

<hr>
<div id="container" style="height: 600px; min-width: 500px"></div>
<hr>



</body>
</html>

最佳答案

添加 Jquery 库即可工作

<!DOCTYPE html>
<html>
<head>

<!--highStocks start--> 

    <!--script src="./static/lib/js/highstock.js"></script>
    <script src="./static/lib/js/exporting.js"></script-->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.2/jquery.min.js"></script>
    <script src="http://github.highcharts.com/highstock.js"></script>
    <script src="http://code.highcharts.com/stock/modules/exporting.js"></script>
    <script type="text/javascript">
$(function() {
    $.getJSON('http://www.highcharts.com/samples/data/jsonp.php?filename=aapl-ohlcv.json&callback=?', function(data) {

        // split the data set into ohlc and volume
        var ohlc = [],
            volume = [],
            volume2 = [],
            dataLength = data.length;

        for (i = 0; i < dataLength; i++) {
            ohlc.push([
                data[i][0], // the date
                data[i][1], // open
                data[i][2], // high
                data[i][3], // low
                data[i][4] // close
            ]);

            volume.push([
                data[i][0], // the date
                data[i][5] // the volume
            ]);

            volume2.push([
                data[i][0], // the date
                data[i][5] // the volume
            ]);


        }

        // set the allowed units for data grouping
        var groupingUnits = [[
            'week',                         // unit name
            [1]                             // allowed multiples
        ], [
            'month',
            [1, 2, 3, 4, 6]
        ]];

        // create the chart
        chart = new Highcharts.StockChart({
            chart: {
                renderTo: 'container',
                alignTicks: false
            },

            rangeSelector: {
                selected: 1
            },

            title: {
                text: 'AAPL Historical'
            },

            yAxis: [{
                title: {
                    text: 'OHLC'
                },
                height: 200,
                lineWidth: 2
            }, {
                title: {
                    text: 'Volume'
                },
                top: 300,
                height: 100,
                offset: 0,
                lineWidth: 2
            }, {
                title: {
                    text: 'Volume2'
                },
                top: 400,
                height: 100,
                offset: 0,
                lineWidth: 2
            }],

            series: [{
                type: 'candlestick',
                name: 'AAPL',
                data: ohlc,
                dataGrouping: {
                    units: groupingUnits
                }
            }, {
                type: 'column',
                name: 'Volume',
                data: volume,
                yAxis: 1,
                dataGrouping: {
                    units: groupingUnits
                }
            }, {
                type: 'column',
                name: 'Volume2',
                data: volume,
                yAxis: 2,
                dataGrouping: {
                    units: groupingUnits
                }
            }]
        });
    });
});
    </script>


<!--highStocks end--> 



</head>
<body class="application">

<hr>
<div id="container" style="height: 600px; min-width: 500px"></div>
<hr>



</body>
</html>

关于javascript - Highstocks 图表无法正确显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37583783/

相关文章:

javascript - React如何检测路由变化方法

html - 是否可以通过浏览器开发工具查看浏览器正在使用哪个 srcset 图像

html - 将 css 应用于元素内的所有内容

javascript - 如何隐藏日期时间 x Axis 但仍然能够在 Highcharts 甘特图中显示绘图线?

highcharts - 如何更新新的绘图线值,而不是在 Highcharts 中删除和添加新的绘图线值

javascript - 如何在鼠标悬停时获取 highcharts 图形点的值?

javascript - 未捕获的 TypeError : $(. ..).chosen 不是 Rails 应用程序中的函数

javascript - 使用 JS、jQuery,如何将 AJAX 响应保存到(文本)文件?

javascript - 物体上的声音效果 p5.js

html - 如何在 li 内联中显示 ul?