javascript - How do I add a legend and a label to my Google Pie Chart? - Stack Overflow

How can I set a legend and labels for my Google pie chart?The deprecated method had this ability: Also,

How can I set a legend and labels for my Google pie chart?

The deprecated method had this ability:

Also, how can I make it so that onclick the selected slice gets exploded from the pie a little?

Here's what I have so far:

  <div id="pie_chart_div" style="width: 940px; height: 500px;"></div>

  <script type="text/javascript" src=""></script>
  <script type="text/javascript">
    google.load("visualization", "1", {packages:["corechart"]});
    google.setOnLoadCallback(render_applicant_sources);

    function render_applicant_sources() {
      var data = new google.visualization.arrayToDataTable([["Source","Number of Applicants"],["Hospitality Online",222],["Indeed",22]]);

      data.addColumn('string', 'Geocoder');
      data.addColumn('number', 'Geocodes');

      var chart = new google.visualization.PieChart(document.getElementById('pie_chart_div'));

      var options = {
          title: 'Scottsdale Resort & Conference Center from December  1, 2012 to December  1, 2013',
          is3D: true,
          colors: [ '#2483b3', '#AAC789', '#1E4147', '#437356', '#F34A53', '#FAE3B4' ],
          titleTextStyle: { bold: false },
          legend: { position: 'labeled' }
      };

      chart.draw(data, options);

      google.visualization.events.addListener(chart, 'click', function(e){
        var data = chart.getSelection();
        if(data.length > 0) {
            chart.setSelection([]);
        }
      });
    }
  </script>

/

How can I set a legend and labels for my Google pie chart?

The deprecated method had this ability: https://developers.google./chart/image/docs/gallery/pie_charts

Also, how can I make it so that onclick the selected slice gets exploded from the pie a little?

Here's what I have so far:

  <div id="pie_chart_div" style="width: 940px; height: 500px;"></div>

  <script type="text/javascript" src="https://www.google./jsapi"></script>
  <script type="text/javascript">
    google.load("visualization", "1", {packages:["corechart"]});
    google.setOnLoadCallback(render_applicant_sources);

    function render_applicant_sources() {
      var data = new google.visualization.arrayToDataTable([["Source","Number of Applicants"],["Hospitality Online",222],["Indeed",22]]);

      data.addColumn('string', 'Geocoder');
      data.addColumn('number', 'Geocodes');

      var chart = new google.visualization.PieChart(document.getElementById('pie_chart_div'));

      var options = {
          title: 'Scottsdale Resort & Conference Center from December  1, 2012 to December  1, 2013',
          is3D: true,
          colors: [ '#2483b3', '#AAC789', '#1E4147', '#437356', '#F34A53', '#FAE3B4' ],
          titleTextStyle: { bold: false },
          legend: { position: 'labeled' }
      };

      chart.draw(data, options);

      google.visualization.events.addListener(chart, 'click', function(e){
        var data = chart.getSelection();
        if(data.length > 0) {
            chart.setSelection([]);
        }
      });
    }
  </script>

http://jsfiddle/xHwZW/

Share asked Dec 11, 2013 at 17:58 Justin HollenderJustin Hollender 3731 gold badge3 silver badges10 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 4

You need to use a "select" event handler, and set the chart's slices.<slice index>.offset option:

google.visualization.events.addListener(chart, 'select', function() {
    var selection = chart.getSelection();
    if (selection.length > 0) {
        if (!options.slices || !options.slices[selection[0].row]) {
            // if this is the first time the user clicked on the pie,
            // or if the user clicks on an unexploded slice,
            // unexplode all slices and explode the selected slice
            options.slices = [];
            options.slices[selection[0].row] = {
                offset: 0.4
            };
        }
        else {
            // otherwise the user clicked on an exploded slice, so unexplode all slices
            options.slices = [];
        }
        chart.draw(data, options);
    }
    else {
        options.slices = [];
        chart.draw(data, options);
    }
});

See working example: http://jsfiddle/asgallant/R8yAK/

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745068439a4609397.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信