弹出窗口中的图表和单独的Div中的Div [英] Graph in popup and separate Div in Leaflet

查看:101
本文介绍了弹出窗口中的图表和单独的Div中的Div的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道有没有人能告诉我我的代码有什么问题?我希望能够选择一个多边形并使用传单和高图在弹出窗口中显示图形。我已经设法在弹出框中创建图表,但是缺少该行,并且我还得到了一个单独的div,在我不想要的网页底部显示相同的图表(和行)。任何人都可以告诉我如何在弹出窗口的图表上显示该行,并删除单独的图表?这里是我的code.enter代码在这里

I wonder if anyone can tell me what is wrong in my code please? I want to be able to select a polygon and show a graph in a popup using leaflet and highchart. I have managed to create the graph in the popup, but the line is missing on it, and I also get a separate div showing the same chart (and the line) at the bottom of my web page which I don't want. Can anyone tell me how to get the line to show on the chart in the popup and to remove the separate chart? Here is my code.enter code here

<!DOCTYPE html>
<html>
<head>

    <title>Quick Start - Leaflet</title>

    <meta charset="utf-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <!--include leaflet CSS file-->

    <link rel="stylesheet" href="css/leaflet.css" />
    <link rel="markers" type="images/marker-icon" href="images/marker-icon.png" />

<!--include Leaflet Javascript file-->
<!-- Make sure you put this AFTER Leaflet's CSS -->

    <script src="js/leaflet.js"></script>
    <script src="https://code.jquery.com/jquery-1.11.0.min.js"></script>
    <script src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.11.1.min.js"></script>
    <script src='http://unpkg.com/leaflet@1.0.2/dist/leaflet.js'></script>
    <script src="js/esri-leaflet.js"></script>
    <script src="https://d3js.org/d3.v4.min.js"></script>
    <script src="https://code.highcharts.com/highcharts.src.js"></script>

 </head>
 <body>

 <!--Put a div element with a certain id where you want your map to be:  -->

   <div id="map" style="width: 1000px; height: 800px;"></div>
   <div id="chartcontainer" class="highchart" style="width: 500px; height: 200px;"></div>

  <!-- First we’ll initialize the map and set its view to our chosen geographical coordinates and a zoom level:-->
  <script>

    var mymap = L.map('map', {
            zoomControl:true, maxZoom:28, minZoom:1
        }).fitBounds([[51.0269253989,-1.34762355597],[51.1990603009,-0.951310026203]]);

    L.esri.basemapLayer('Imagery').addTo(mymap);        

//loads geoserver layer as WMS
 var field_boundaries = L.tileLayer.wms("http://localhost:1997/geoserver/RSAC/wms", {
    layers: 'RSAC:results_clipped_with_growth_small_new',
    format: 'image/png',
    transparent: true,
    version: '1.1.0',
    attribution: "myattribution"
    });


//loads the geojson layer
    var owsrootUrl = 'http://localhost:1997/geoserver/RSAC/wms';
    var defaultParameters = {
        service : 'WFS',
        version : '2.0',
        request : 'GetFeature',
        typeName : 'RSAC:results_clipped_with_growth_small_new',
        outputFormat : 'json',
        format_options : 'callback:getJson',
        SrsName : 'EPSG:4326'
    };
var parameters = L.Util.extend(defaultParameters);
var URL = owsrootUrl + L.Util.getParamString(parameters);
var ajax = $.ajax({
    url : URL,
    dataType : 'json',
    jsonpCallback : 'getJson',
    success : function (response) {
    L.geoJson(response, {
        onEachFeature: function (feature, url) {
        url.on('click', function(e){

                    var chartplotoptions ={
                        chart: {
                            type: 'line'
                        },
                        title: {
                            text: 'Growth'
                        },

                        xAxis: {
                            allowDecimals: true,
                            categories: ['20151114', '20151126', '20151208', '20151220', '20160113', '20160125', '20160206', '20160218', '20160301', '20160313', '20160325', '20160406', '20160418', '20160430', '20160512', '20160524', '20160605', '20160629', '20160723', '20160804', '20160816'],
                            labels: {
                                formatter: function () {
                                    return this.value; 
                                }
                            }
                        },
                        yAxis: {
                                startOnTick: false,
                                minPadding: 0.05,
                            title: {
                                text: 'Crop Growth',

                            },
                            labels: {
                                formatter: function () {
                                    return this.value;
                                }
                            }
                        },
                        tooltip: {
                            pointFormat: '{series.name}{point.y}'
                        },
                        plotOptions: {

                                area: {
                                pointStart: -20,
                                threshold: 10,
                                marker: {
                                    enabled: false,
                                    symbol: 'circle',
                                    radius: 2,
                                    states: {
                                        hover: {
                                            enabled: false
                                        }
                                    }
                                }
                            }
                        },
                        series: [{
                            name: 'Growth',
                            data: [parseFloat(feature.properties.Date_20151114),parseFloat(feature.properties.Date_20151126),parseFloat(feature.properties.Date_20151208), parseFloat(feature.properties.Date_20151220), parseFloat(feature.properties.Date_20160113), parseFloat(feature.properties.Date_20150125), parseFloat(feature.properties.Date_20160206), parseFloat(feature.properties.Date_20160218), parseFloat(feature.properties.Date_20160301), parseFloat(feature.properties.Date_20160313), parseFloat(feature.properties.Date_20160325), parseFloat(feature.properties.Date_20160406), parseFloat(feature.properties.Date_20160418), parseFloat(feature.properties.Date_20160430), parseFloat(feature.properties.Date_20160512), parseFloat(feature.properties.Date_20160524), parseFloat(feature.properties.Date_20160605), parseFloat(feature.properties.Date_20160629), parseFloat(feature.properties.Date_20160723), parseFloat(feature.properties.Date_20160804), parseFloat(feature.properties.Date_20160816)]
                        }, 

                        ]
                    };


                    $('#chartcontainer').highcharts(chartplotoptions);
                    url.bindPopup($('#chartcontainer').html());
                    url.openPopup();  
                    });
                }

             }).addTo(mymap);
         }
     });     

  </script>



</body>
</html> 


推荐答案

在HTML中不需要div元素标记。您可以在 onEachFeature 函数中实时创建一个,并将其添加到弹出窗口中。此外,您需要在弹出窗口打开之后初始化您的Highchart,而不是之前。在带有注释的代码中:

You don't need the div element in your HTML markup. You can create one on the fly in your onEachFeature function and add it to the popup. Also, you need to initialize your highchart after the popup has opened, not before. In code with comments:

new L.GeoJSON(feature, {
    onEachFeature: function (feature, layer) {

        // Create div with class name highchart
        var div = L.DomUtil.create('div', 'highchart');

        // Bind popup to layer with div as content
        layer.bindPopup(div);

        // Handle event when popup opens
        layer.on('popupopen', function (e) {

            console.log(e.target);  // layer object
            console.log(e.target.feature); // layer's feature object
            console.log(e.popup); // popup object
            console.log(e.popup.getContent()); // the div

            // Now do the highcharts stuff
            Highcharts.chart(e.popup.getContent(), { /**/ });
        });
    }
});

不要忘记用CSS设置div的尺寸:

And don't forget to set the div's dimensions with CSS:

.highchart {
    width: 500px;
    height: 200px;
}

这篇关于弹出窗口中的图表和单独的Div中的Div的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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