cone001 cone001 - 6 months ago 17
HTML Question

Highstocks chart doesn't show up properly

I am trying to replicate a highstocks chart example I find on fiddle( link). I copied and pasted the code to an html file, but the chart doesn't show up. Could anyone help me with this problem? Thanks.

The html file reads as follows:

<!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>

Answer

Add Jquery library it will work

<!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>

Comments