【问题标题】:Highcharts : using same div to load a chart multiple times with different series dataHighcharts:使用相同的 div 多次加载具有不同系列数据的图表
【发布时间】:2018-02-15 16:04:15
【问题描述】:

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

我的图表功能:

<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) + "');");

【问题讨论】:

  • 在加载新图表之前,您可能应该destroy() 图表。看看这个答案:stackoverflow.com/questions/7880978/… 以及这个文档:api.highcharts.com/class-reference/Highcharts.Chart#destroy
  • 我在 renderGraph() 开头尝试了 destroy() 但无济于事。你能指出一些关于在我的自定义函数中调用 destroy() 的见解吗() @wergeld
  • 如上面在 cmets 中的 API 示例中给出的。您在 renderGraph() 函数中使用了变量var chart = Highcharts.chart('container', {...})
  • @Deep3015 我在我的代码中添加了 var 图表。请告诉我在哪里放置destroy()。请注意,代码中不会有任何按钮来破坏图形。
  • @AbhishekAnand 检查这个jsfiddle.net/g6n0ep44

标签: javascript highcharts appcelerator-titanium


【解决方案1】:

由于您使用的是 jQuery,您是否尝试过在重绘图表之前按如下方式简单地清空容器 div?

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

也许这会更有益,因为chart.destroy() 似乎不适用于您的情况。

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多