传单使用弹出窗口不显示

时间:2020-10-31 15:10:44

标签: highcharts leaflet

我使用highchart图表显示在弹出窗口中。

可以在第一次单击标记时显示。

但是打开另一个弹出窗口无法显示图表。

如果我关闭了原来的一个并打开了一个新的,则可以显示它。

出什么问题了? enter image description here

首次开启

enter image description here

打开另一个时

代码:

 function onEachFeature(feature, layer) {
    var popupContent = '<p style="font-size:130%;">' + feature.properties.id + "<br>PM2.5: " + air + '</p><div class="d-flex"> <button class="day-button">24 hr</button>   <button class="week-button">7 Days</button> <button class="mon-button ">30 Days</button></div>' + '</p><div id="container" style="min-width: 350px; height: 190px; margin: 0 auto"></div> <p> ';
    if (feature.properties && feature.properties.popupContent) {
            popupContent += feature.properties.popupContent;
        }
  layer.bindPopup(popupContent, { minWidth: 370 }).on("popupopen", function(e) {
        
        var id1 = feature.properties.id;
        Highcharts.setOptions({ global: { useUTC: false } });
        var chart = null;
        test(id1);
    }
}

高级图表:

function test (station){
    var ob = [];
    var json_hour ="data.csv";
    var count_nul = 0;
 
    d3.csv(json_hour, function (error, result) {
        function date_to_epoch(key) {
            var epoch_seconds = new Date(key).getTime();
            return Math.floor(epoch_seconds);
        }
        for (var i = 0; i < result.length; i++) {
            var apoche = date_to_epoch(result[i]['date']).toString();
            apoche = parseFloat(apoche);
             
            if (parseFloat(result[i]['pm25']).toString() == 'NaN') {
                count_nul++;
            } else {
                var miles = parseFloat(result[i]['pm25']);

            }
            ob.push([apoche, miles]);
        }

        if (count_nul >= 24) {
            $("#con").text("no data");
        }
        else {
            $("#con").empty();
            console.log((ob));
           
       new Highcharts.Chart({
                chart: {
                    renderTo: 'con',
                    type: 'line'
                },
                title: { text: '' },
                xAxis: {
                    type: 'datetime',
                    dateTimeLabelFormats: {
                        minute: '%H:%M',
                        hour: '%H:%M',
                        day: '%Y<br/>%m%d',
                        week: '%Y<br/>%m-%d',
                        month: '%Y<br/>%m',
                        year: '%Y'
                    }
                    , crosshair: true,
                },
                tooltip: {
                    split: false,
                    shared: true,
                    animation: true,
                    xDateFormat: '%Y-%m-%d %H:%M',
                    valueSuffix: ' µm'
                },
                series: [{
                    name: "PM2.5",
                    data: ob,
                    showInLegend: true
                }
                ],
                credits: {
                    enabled: false 
                },
                exporting: {
                    enabled: false 
                },
                plotOptions: {
                    line: {
                        connectNulls: true,
                    }

                },
                responsive: {
                    rules: [{
                        condition: {
                            maxWidth: 330
                        },

                    }]
                }
            });
          
        }
    });
}

1 个答案:

答案 0 :(得分:0)

将您的代码更改为:

 function onEachFeature(feature, layer) {
    var popupContent = '<p style="font-size:130%;">' + feature.properties.id + "<br>PM2.5: " + air + '</p><div class="d-flex"> <button class="day-button">24 hr</button>   <button class="week-button">7 Days</button> <button class="mon-button ">30 Days</button></div>' + '</p><div id="container" style="min-width: 350px; height: 190px; margin: 0 auto"></div> <p> ';
    if (feature.properties && feature.properties.popupContent) {
            popupContent += feature.properties.popupContent;
        }
  layer.bindPopup(popupContent, { minWidth: 370 }).on("popupopen", function(e) {
        
        var id1 = e.popup._source.feature.properties.id;
        Highcharts.setOptions({ global: { useUTC: false } });
        var chart = null;
        test(id1);
    }
}

使用e.popup._source.feature.properties.id,您可以确保ID来自当前打开的图层