Button Highcharts下载/上下文按钮中添加的选项会重复

Button Highcharts下载/上下文按钮中添加的选项会重复,button,highcharts,download,Button,Highcharts,Download,我在Highcharts的下载/导出/上下文按钮中添加了一些选项。然而,这个过程有点复杂,因为它是一个API。一个文件正在接收调用,在生成图形的所有选项的地方调用另一个文件,然后返回到生成图形的第一个文件。此处,上下文按钮中引入了其他选项: function drawGraph(selectedID, selectedCountries, selectedYears, per_capita, graphBorder, graphSource, graphDefinition, graph

我在Highcharts的下载/导出/上下文按钮中添加了一些选项。然而,这个过程有点复杂,因为它是一个API。一个文件正在接收调用,在生成图形的所有选项的地方调用另一个文件,然后返回到生成图形的第一个文件。此处,上下文按钮中引入了其他选项:

    function drawGraph(selectedID, selectedCountries, selectedYears, per_capita, graphBorder, graphSource, graphDefinition, graphStyle, graphXAxis, renderToGraph, renderToDescription)
    {
        jQuery(document).ready(function() {
            var options = {};               

            url = "xxx.com";

            jQuery.getJSON(url, {selectedCountries: selectedCountries , selectedID: selectedID, selectedYears: selectedYears, per_capita: per_capita, graphBorder: graphBorder, graphSource: graphSource, graphDefinition: graphDefinition, graphStyle: graphStyle, graphXAxis: graphXAxis, renderToGraph: renderToGraph, type: "jsonp"})
            .done(function(data)
            {
                //console.log(data);

                options.chart       = data["chart"];
                options.tooltip     = data["tooltip"];
                options.series      = data["series"];
                options.title       = data["title"];
                options.subtitle    = data["subtitle"];
                options.yAxis       = data["yAxis"];
                options.xAxis       = data["xAxis"];
                options.legend      = data["legend"];
                options.exporting   = data["exporting"];
                options.plotOptions = data["plotOptions"];
                options.credits     = data["credits"];

                if ((graphDefinition == "true") || (graphDefinition == "on"))
                {
                    jQuery('#'+renderToDescription).html(data["description"]);
                }

                var chart = new Highcharts.Chart(options);

            })


            var buttons = Highcharts.getOptions().exporting.buttons.contextButton.menuItems;                

            // add "separator line" 
            buttons.push({
                separator: true,
            });


            // add "Link to metadata"
            buttons.push({
                text: 'Metadata',
                onclick: function () {
                    window.open('http://xxx/metadata.php?selectedID=' + selectedID + '&selectedDatasettype=1', "_blank");
                }
            }); 
            }


            // add "separator line" 
            buttons.push({
                separator: true,
            });


            // add "Link to more data functions"
            buttons.push({
                text: 'Link to more data functions',
                onclick: function () {
                    window.open('http://xxx/options.php?selectedID=' + selectedID + '&selectedDatasettype=1', "_blank");
                }
            }); 
        });
    }
从另一端,我有一个生成JSON代码的文件:

$data_ = array( "chart"         => array("renderTo" => $container, "type" => $graphStyle, "zoomType" => "xy", "marginTop" => $marginTop, "marginRight" => 20, "marginBottom" => 60, "marginLeft" => 80), 

                "title"         => array("useHTML" => true, "text" => "<div style='text-align: center'>".str_replace("CO2", "CO<sub>2</sub>", $selectedDataset -> name)."</div>", "align" => "center", "style" => array("fontFamily" => "Helvetica", "fontSize" => "20px")), 

                "subtitle"      => array("text" => "Original data source: <a href='" . $provider_url . "' style='font-family: Helvetica; color: #428bcc; text-decoration: none' target='_blank'>" . $selectedDataset -> data_provider . "</a>", "useHTML" => true), 

                "xAxis"         => array("tickWidth" => 0, "showFirstLabel" => true, "showLastLabel" => true, "tickInterval" => $step), 

                "yAxis"         => array("min" => $min, "title" => array("useHTML" => true, "text" => str_replace("CO2", "CO<sub>2</sub>", $yTitle), "fontWeight" => "bold", "align" => "high", "textAlign" => "left", "rotation" => 0, "y" => $yAxisY)), 

                "legend"        => array("enabled" => $flagValues, "layout" => "vertical", "align" => "center", "verticalAlign" => "middle", "backgroundColor" => "#efefef", "borderWidth" => 0, "floating" => false, "x" => -185, "y" => 100, "title" => array("text" => ":: Legend ::"), "floating" => true, "draggable" => true, "zIndex" => 20), 

                "plotOptions"   => array("series" => array("connectNulls" => true, "shadow" => false, "lineWidth" => 2, "marker" => array("enabled" => false))), 

                "tooltip"       => array("shared" => true, "crosshairs" => true), 

                "credits"       => array("text" => $copyright, "href" => "http://ede.grid.unep.ch", "position" => array("x" => 10, "y" => -20, "align" => "left"), "style" => array("fontSize" => "9px", "lineHeight" => "9px")), 

                "exporting"     => array("buttons" => array("contextButton" => array("symbol" => "url(http://geodata.grid.unep.ch/images/button_download_4.png)", "x" => -10, "y" => 10))),

                "series"        => $data,

                "protected"     => $selectedDataset -> protected_,

                "description"   => $selectedDataset -> abstract,

                "noData"        => $flagValues);

header("content-type: application/json"); 

echo $_GET['callback']. '('. json_encode($data_) . ')';    

谢谢你的提示。

我终于找到了一种似乎很有效的方法。这是通过contextButton的菜单项数组进行循环。检查项目是否已存在,替换其功能。否则,请使用标准推送选项

希望这能帮助其他类似情况的人

    var buttons = [];
    buttons = Highcharts.getOptions().exporting.buttons.contextButton.menuItems;

    flag_metadata = false;
    flag_link_functions = false;

    for (var x in Highcharts.getOptions().exporting.buttons.contextButton.menuItems) 
    {
        if (buttons[x].id == "Metadata")
        {
            buttons[x].onclick = function () {
                window.open('http://ede.grid.unep.ch/mod_metadata/metadata.php?selectedID=' + selectedID + '&selectedDatasettype=1', "_blank");}

            flag_metadata = true;
        }

        if (buttons[x].id == "Link")
        {
            buttons[x].onclick = function () {
                window.open('http://ede.grid.unep.ch/options.php?selectedID=' + selectedID + '&selectedDatasettype=1', "_blank");}

            flag_metadata = true;
        }
    }


    if (flag_metadata == false)
    {                                       
        // add "separator line" 
        Highcharts.getOptions().exporting.buttons.contextButton.menuItems.push({
            separator: true,
            id: 'sep1',
        });


        // add "Link to metadata"
        Highcharts.getOptions().exporting.buttons.contextButton.menuItems.push({
            id: 'Metadata',
            text: 'Metadata',
            onclick: function () {
                window.open('http://ede.grid.unep.ch/mod_metadata/metadata.php?selectedID=' + selectedID + '&selectedDatasettype=1', "_blank");
            }
        }); 


        // add "separator line" 
        Highcharts.getOptions().exporting.buttons.contextButton.menuItems.push({
            separator: true,
            id: 'sep2',
        });


        // add "Link to more data functions"
        Highcharts.getOptions().exporting.buttons.contextButton.menuItems.push({
            id: 'Link',
            text: 'Link to more data functions',
            onclick: function () {
                window.open('http://ede.grid.unep.ch/options.php?selectedID=' + selectedID + '&selectedDatasettype=1', "_blank");
            }
        }); 
    }

“选择一个接一个的图形”?这意味着什么?@Strand:这意味着我调用显示图形的网站,然后选择另一个变量,然后图形显示该变量。第二次“加载”或“调用”显示图表,然后生成一个包含两个“元数据”条目的下拉列表。查看屏幕截图。这意味着每次创建新图表时,都要添加
//添加“分隔线”
//添加“链接到元数据”
。这应该只发生一次。也许问答会对你有所帮助?@PawełFus:谢谢。是的,确实如此。不幸的是,问答并没有真正起到帮助作用。然而,它给了我一些思考和尝试。但仍然没有找到解决办法。最好是能够删除两个“推送”,并在每次加载时重新创建它们。有可能吗?
    var buttons = [];
    buttons = Highcharts.getOptions().exporting.buttons.contextButton.menuItems;

    flag_metadata = false;
    flag_link_functions = false;

    for (var x in Highcharts.getOptions().exporting.buttons.contextButton.menuItems) 
    {
        if (buttons[x].id == "Metadata")
        {
            buttons[x].onclick = function () {
                window.open('http://ede.grid.unep.ch/mod_metadata/metadata.php?selectedID=' + selectedID + '&selectedDatasettype=1', "_blank");}

            flag_metadata = true;
        }

        if (buttons[x].id == "Link")
        {
            buttons[x].onclick = function () {
                window.open('http://ede.grid.unep.ch/options.php?selectedID=' + selectedID + '&selectedDatasettype=1', "_blank");}

            flag_metadata = true;
        }
    }


    if (flag_metadata == false)
    {                                       
        // add "separator line" 
        Highcharts.getOptions().exporting.buttons.contextButton.menuItems.push({
            separator: true,
            id: 'sep1',
        });


        // add "Link to metadata"
        Highcharts.getOptions().exporting.buttons.contextButton.menuItems.push({
            id: 'Metadata',
            text: 'Metadata',
            onclick: function () {
                window.open('http://ede.grid.unep.ch/mod_metadata/metadata.php?selectedID=' + selectedID + '&selectedDatasettype=1', "_blank");
            }
        }); 


        // add "separator line" 
        Highcharts.getOptions().exporting.buttons.contextButton.menuItems.push({
            separator: true,
            id: 'sep2',
        });


        // add "Link to more data functions"
        Highcharts.getOptions().exporting.buttons.contextButton.menuItems.push({
            id: 'Link',
            text: 'Link to more data functions',
            onclick: function () {
                window.open('http://ede.grid.unep.ch/options.php?selectedID=' + selectedID + '&selectedDatasettype=1', "_blank");
            }
        }); 
    }