简体   繁体   English

Highcharts:使用相同的div使用不同的系列数据多次加载图表

[英]Highcharts : using same div to load a chart multiple times with different series data

I have written a function that creates a chart based on some graphdata as parameter and renders it to a div . 我编写了一个函数,该函数基于一些graphdata作为参数创建图表并将其呈现给div。 Now I am reusing this function to generate same type of chart on same div to load different series data . 现在,我正在重用此函数在同一div上生成相同类型的图表,以加载不同的系列数据。 The problem is I can see the graph rendering shows previous charts labels for a second and then the new graph gets loaded with new labels . 问题是我可以看到图形渲染显示了先前的图表标签一秒钟,然后新图形被加载了新标签。 I dont want to see the old graph when my new graph gets loaded . 新图加载后,我不想看到旧图。 Please help . 请帮忙 。

My chart function : 我的图表功能:

<html>
<head>
    <script src="./jquery.min.jsl"></script>
    <script src="./highcharts.jsl"></script>
    <script src="./exporting.jsl"></script>
    <meta name="viewport" content="user-scalable=no">
    <script>

        function renderGraph(graphdata) {
            var graphObj = JSON.parse(graphdata);
            var chart = null; 
            Highcharts.setOptions({
                lang : {
                    numericSymbols : ["K", "M", "G", "T", "P", "E"]
                }
            });
            var change = {
                0 : '$0K',
                2 : '$2K',
                4 : '$4K',
                6 : '$6K',
                8 : '$8K'
            };
            var xAxisLegends = graphObj.bottomLegends;
            var seriesData = graphObj.seriesData;
            var xAxisLegends = graphObj.bottomLegends;
            //['Q2, 16', 'Q3, 16', 'Q4, 16', 'Q1, 17'];
            var columnColors = ["#69C3DB", "#3a8a9f"];

            var seriesData = graphObj.seriesData;
            /*[{
            name : 'Budget',
            showInLegend : false,
            data : [2, 4, 6, 8]
            }, {
            name : 'Utilisation',
            showInLegend : false,
            data : [1, 2, 3, 4]
            }];*/

            // variables which have diff values according to OS
            var chartProperties = {};
            // properties to assign to Charts's object
            var graphHeight = 0;
            // height of chart
            var graphWidth = 0;
            //Width of the column
            var pointWidth;

            // Separating the graph dimensions & styling properties as per OS name & version
            if (graphObj.osname == "iphone") {
                chartProperties = {
                    type : 'column',
                    renderTo : 'container'
                };
                xAxisProp = {
                    gridLineWidth : 0,
                    categories : xAxisLegends,
                    crosshair : true
                };
                yAxisProp = {
                    min : 0,
                    gridLineWidth : 0,
                    tickAmount : 5,
                    title : {
                        text : ' '
                    },
                    labels : {
                        formatter : function() {
                            var value = this.axis.defaultLabelFormatter.call(this);
                            return '$' + value;
                        }
                    }
                };
                pointWidth = 5;
            } else if (graphObj.osname == "android") {
                chartProperties = {
                    type : 'column',
                    plotBackgroundColor : null,
                    plotBackgroundImage : null,
                    plotBorderWidth : 0,
                    plotShadow : false,
                    height : 450,
                    marginTop : 100,
                    marginLeft : 120

                },
                xAxisProp = {
                    categories : xAxisLegends,
                    width : 800,
                    tickmarkPlacement : 'on',
                    labels : {
                        y : 40,
                        style : {
                            color : '#333333',
                            fontSize : '25',
                            fontFamily : 'Metropolis-Light',
                            opacity : '.6'
                        },

                    }
                };
                yAxisProp = {
                    gridLineWidth : 0,
                    min : 0,
                    tickAmount : 5,
                    offset : 60,
                    title : {
                        text : ''
                    },
                    labels : {
                        align : 'left',
                        style : {
                            color : '#333333',
                            fontSize : '28',
                            fontFamily : 'Metropolis-Light',
                            opacity : '.5'
                        },
                        formatter : function() {
                            var value = this.axis.defaultLabelFormatter.call(this);
                            return '$' + value;
                        }
                    },

                };
                pointWidth = 10;
                if (parseInt(graphObj.osversion) >= 500 && parseInt(graphObj.osversion) <= 600) {
                    graphHeight = 600;
                } else {
                    graphHeight = 630;
                }
            }
            chart = 
                Highcharts.chart('container', {
                    chart : chartProperties,
                    credits : {
                        enabled : false
                    },
                    tooltip : {
                        enabled : false
                    },
                    exporting : {
                        enabled : false
                    },
                    title : {
                        text : ''
                    },
                    xAxis : xAxisProp,
                    yAxis : yAxisProp,
                    plotOptions : {
                        column : {
                            pointPadding : 0.2,
                            borderWidth : 0,
                            groupPadding : 0.38,
                            pointWidth : pointWidth
                        }
                    },
                    colors : columnColors,
                    series : seriesData
                });


        }

    </script>

</head>
<body>
    <div id="container" style="height: 100%; width: 100%; position : center;"></div>
</body>

The function that calls this chart : 调用此图表的函数:

 $.webViewPerformanceGraph.url = "/html/Performance.html";

            $.webViewPerformanceGraph.addEventListener('load', function() {
                $.webViewPerformanceGraph.evalJS("renderGraph('" + JSON.stringify(params) + "');");

Since you're using jQuery, have you tried simply emptying the container div as follows before redrawing the chart? 由于您使用的是jQuery,是否在重绘图表之前尝试按照以下方法清空容器div?

$('#container').html();

Perhaps this would be more beneficial since chart.destroy() doesn't seem to be working in your case. 也许这会更有益,因为chart.destroy()在您的情况下似乎不起作用。

From http://api.jquery.com/html/#html2 : http://api.jquery.com/html/#html2

When .html() is used to set an element's content, any content that was in that element is completely replaced by the new content. 使用.html()设置元素的内容时,该元素中的所有内容都将被新内容完全替换。 Additionally, jQuery removes other constructs such as data and event handlers from child elements before replacing those elements with the new content. 此外,在用新内容替换那些元素之前,jQuery从子元素中删除了其他构造,例如数据和事件处理程序。

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

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