score:7

Accepted answer

If your goal is to keep this dynamic, by being able to build multiple charts with multiple renderTo's but using the same options, you can do it like this:

change

function createChart(questiontitle, answers){
    chart = new Highcharts.Chart(options); 
    chart.renderTo(container);

to:

function createChart(questiontitle, answers){
    chart = $('#container').highcharts(options); 

or, if not using jQuery,

function createChart(questiontitle, answers){
    options.chart.renderTo = 'container';
    chart = new Highcharts.Chart(options); 

score:1

You may be facing the same problem that i was. Try to put your highchart script right after your div's declaration just like in the example bellow so it can recognize your div's id:

<head>
    <title>HighCharts :D</title>
    <script src="http://code.highcharts.com/highcharts.js"></script>
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script>
</head>
<body>
<div id="container" style="width: 600px; height: 400px; margin: 0 auto">
</div>
<script type="text/javascript">
    var myChart = Highcharts.chart('container', {
        chart: {
            type: 'bar'
        },
        title: {
            text: 'Fruit Consumption'
        },
        xAxis: {
            categories: ['Apples', 'Bananas', 'Oranges']
        },
        yAxis: {
            title: {
                text: 'Fruit eaten'
            }
        },
        series: [{
            name: 'Jane',
            data: [1, 0, 4]
        }, {
            name: 'John',
            data: [5, 7, 3]
        }]
    });
    myChart.renderTo('container');

</script>
</body>

score:4

Something like this: chart.renderTo(container); doesn't exists in Highcharts.

To set renderTo use options:

var options = {
    chart: {
        type: 'bar',
        renderTo: 'container'
    },

score:5

In case someone stumbles on this while Googling, when you specific the element in renderTo, you shouldn't include the # if that element is an ID.

If you have this element <div id="graph-container"></div>

This fails: renderTo: '#graph-container'

This works: renderTo: 'graph-container'

Reference to the Highcharts docs


Related Query

More Query from same tag