Highcharts:使用相同的 div 多次加载具有不同系列数据的图表 [英] Highcharts : using same div to load a chart multiple times with different series data

查看:23
本文介绍了Highcharts:使用相同的 div 多次加载具有不同系列数据的图表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我编写了一个函数,该函数基于一些图形数据作为参数创建图表并将其呈现为 div .现在我重用这个函数在同一个 div 上生成相同类型的图表来加载不同的系列数据.问题是我可以看到图形渲染显示以前的图表标签一秒钟,然后新图表加载了新标签.当我的新图表加载时,我不想看到旧图表.请帮忙.

I have written a function that creates a chart based on some graphdata as parameter and renders it to a div . Now I am reusing this function to generate same type of chart on same div to load different series data . 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 .

我的图表功能:

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

调用此图表的函数:

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

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

推荐答案

自从您使用 jQuery 以来,您是否尝试过在重新绘制图表之前简单地按如下方式清空容器 div?

Since you're using jQuery, have you tried simply emptying the container div as follows before redrawing the chart?

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

也许这会更有益,因为 chart.destroy() 在您的情况下似乎不起作用.

Perhaps this would be more beneficial since chart.destroy() doesn't seem to be working in your case.

来自 http://api.jquery.com/html/#html2:

当 .html() 用于设置元素的内容时,该元素中的任何内容都将被新内容完全替换.此外,jQuery 在用新内容替换这些元素之前从子元素中删除了其他结构,例如数据和事件处理程序.

When .html() is used to set an element's content, any content that was in that element is completely replaced by the new content. Additionally, jQuery removes other constructs such as data and event handlers from child elements before replacing those elements with the new content.

这篇关于Highcharts:使用相同的 div 多次加载具有不同系列数据的图表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆