How to create a div container for a highcharts graphic by means of code?

自闭症网瘾萝莉.ら 提交于 2019-12-12 04:47:48

问题


I need to create a div container for a highcharts graphic. This should be done by means of code, but I can not make it work.

The reason to create divs by code is because I have to show many graphics.

Currently I first create the div, then the id and finally the properties.

Error on graphic:

Example :

http://jsfiddle.net/povyq7em/1/

My code is:

var nombre="container-speed";

var div = document.createElement('div');
div.setAttribute("style", "width: 580px; height: 400px; float: left");
div.setAttribute("id", nombre);


var gaugeOptions = {

    chart: {
        type: 'solidgauge'
    },

    title: null,

    pane: {
        center: ['50%', '85%'],
        size: '140%',
        startAngle: -90,
        endAngle: 90,
        background: {
            backgroundColor: (Highcharts.theme && 
            Highcharts.theme.background2) || '#EEE',
            innerRadius: '60%',
            outerRadius: '100%',
            shape: 'arc'
        }
    },

    tooltip: {
        enabled: false
    },


    yAxis: {
        stops: [
            [0.1, '#55BF3B'], // green
            [0.5, '#DDDF0D'], // yellow
            [0.9, '#DF5353'] // red
        ],
        lineWidth: 0,
        minorTickInterval: null,
        tickAmount: 2,
        title: {
            y: -70
        },
        labels: {
            y: 16
        }
    },

    plotOptions: {
        solidgauge: {
            dataLabels: {
                y: 5,
                borderWidth: 0,
                useHTML: true
            }
        }
    }
};


var chartSpeed = Highcharts.chart(nombre, Highcharts.merge(gaugeOptions, {
    yAxis: {
        min: 0,
        max: 200,
        title: {
            text: 'Speed'
        }
    },

    credits: {
        enabled: false
    },

    series: [{
        name: 'Speed',
        data: [80],
        dataLabels: {
            format: '<div style="text-align:center"><span style="font-size:25px;color:' +
                ((Highcharts.theme && Highcharts.theme.contrastTextColor) || 'black') + '">{y}</span><br/>' +
                   '<span style="font-size:12px;color:silver">km/h</span></div>'
        },
        tooltip: {
            valueSuffix: ' km/h'
        }
    }]

}));
.highcharts-yaxis-grid .highcharts-grid-line {
	display: none;
}
<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/solid-gauge.js"></script>

<!--<div id="container-speed" style="width: 300px; height: 200px; float: left"></div>-->

Thank you very much for your help.


回答1:


You're just missing one step. You need to append the element after you create it. Here's the beginning of your code:

var nombre="container-speed";
var div = document.createElement('div');
div.setAttribute("style", "width: 580px; height: 400px; float: left");
div.setAttribute("id", nombre);
// APPEND ELEMENT TO document.body
document.body.appendChild(div);

Also, I updated your fiddle

Per your request, I updated the fiddle once more. I made 3 changes. Only one of which were really important.

  1. declare and value name to be used as element id and as argument for grafica() var name = "chart-" + i;
  2. alter setAttribute to div.setAttribute("id", name);
  3. ***MOST IMPORTANTLY, you changed the variable nombre to name in every place but here var chartSpeed = Highcharts.chart(name, Highcharts.merge(gaugeOptions, {... which you can tell, I've updated.


来源:https://stackoverflow.com/questions/46270858/how-to-create-a-div-container-for-a-highcharts-graphic-by-means-of-code

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!