Pie Chart on Google map

前端 未结 2 757
滥情空心
滥情空心 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 = $('<div>').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 = $('<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( '<img src="' + this.get('image') + '"/>' )
            .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 );
    
    0 讨论(0)
  • 2021-02-06 12:04

    Recently I had to implement pie-charts as markers for many locations on the map. Having many locations on the map app also required marker clustering... In my particular case displaying pie-chart as overlay didn't fit, that's why I built simple library. Maybe saying that it is library is too much because it's just one function for now that generates pie-char svg node. Github repository with example how to use function along with google-maps below
    Repository

    0 讨论(0)
提交回复
热议问题