Pie Chart on Google map

前端 未结 2 758
滥情空心
滥情空心 2021-02-06 11:40

I want to draw a pie charts on several locations in a Google Map. Is there a way to draw a google pie chart in a particular location in a Google Map to represent a data set (lik

2条回答
  •  一整个雨季
    2021-02-06 11:59

    If you're talking about a pie chart drawn with the Visualization API, you could use a custom HTML overlay like the one in this fiddle:

    google.load( 'visualization', '1', { packages:['corechart'] });
    
    function ChartMarker( options ) {
        this.setValues( options );
    
        this.$inner = $('
    ').css({ position: 'relative', left: '-50%', top: '-50%', width: options.width, height: options.height, fontSize: '1px', lineHeight: '1px', border: '1px solid #888', padding: '2px', backgroundColor: 'white', cursor: 'default' }); this.$div = $('
    ') .append( this.$inner ) .css({ position: 'absolute', display: 'none' }); }; ChartMarker.prototype = new google.maps.OverlayView; ChartMarker.prototype.onAdd = function() { $( this.getPanes().overlayMouseTarget ).append( this.$div ); }; ChartMarker.prototype.onRemove = function() { this.$div.remove(); }; ChartMarker.prototype.draw = function() { var marker = this; var projection = this.getProjection(); var position = projection.fromLatLngToDivPixel( this.get('position') ); this.$div.css({ left: position.x, top: position.y, display: 'block' }) this.$inner .html( '' ) .click( function( event ) { var events = marker.get('events'); events && events.click( event ); }); this.chart = new google.visualization.PieChart( this.$inner[0] ); this.chart.draw( this.get('chartData'), this.get('chartOptions') ); }; function initialize() { var latLng = new google.maps.LatLng( 40.708762, -74.006731 ); var map = new google.maps.Map( $('#map_canvas')[0], { zoom: 15, center: latLng, mapTypeId: google.maps.MapTypeId.ROADMAP }); var data = google.visualization.arrayToDataTable([ [ 'Task', 'Hours per Day' ], [ 'Work', 11 ], [ 'Eat', 2 ], [ 'Commute', 2 ], [ 'Watch TV', 2 ], [ 'Sleep', 7 ] ]); var options = { title: 'My Daily Activities', fontSize: 8 }; var marker = new ChartMarker({ map: map, position: latLng, width: '250px', height: '100px', chartData: data, chartOptions: options, events: { click: function( event ) { alert( 'Clicked marker' ); } } }); }; $( initialize );

提交回复
热议问题