简体   繁体   English

如何实现Google饼图(带有图例显示和相应的值)

[英]How to implement Google Pie chart(with legend display and corresponding values)

I have created google pie chart using json data from database. 我已经使用数据库中的json数据创建了Google饼图。 The pie chart was created with legend name, but i want also display legend with value,that is the name with value besides the piechart.I am trying out different stuffs but I am completely stumped by this. 饼形图是用图例名称创建的,但是我还想显示带有值的图例,即除了饼图之外还具有值的名称。我正在尝试各种东西,但我对此完全感到困惑。

The google pie chart code which I am implementing is(snippet):- 我正在实现的Google饼图代码是(片段):-

  function drawChart(data)
{
    var hi_cnt = data.length;
    var gdata = new google.visualization.DataTable();
    var total = 0;
    gdata.addColumn('string', 'Task');
    gdata.addColumn('number', 'Hours Per Day');
    gdata.addRows(hi_cnt);
     for  (var i= 0;i < hi_cnt; i++)
     {
        gdata.setCell(i, 0, data[i]['name']);
        gdata.setCell(i, 1, parseInt(data[i]['count']));

     }

     var options = {
        title: 'Number of Issues By Type',
        'width': 750,
        'height': 450,
        backgroundColor: '#EEE', 
        legend: {position: 'right'},
        areaOpacity: 1.0,
        sliceVisibilityThreshold:0,
        //vAxis: {format: '# $'},
        //hAxis: {title: '????', titleTextStyle: {color: 'blue'}, slantedText: true, viewWindow: {min: 39, max: 52}},
        //colors: ['CCFFCC', '66CC66', 'FF9999'],
        //animation: {duration: 1000, easing: 'out'}
     };

    var chart = new google.visualization.PieChart(document.getElementById('outer_tableDiv'));
    chart.draw(gdata, options);
 }  

function getHealthReport() 
{ 
  var dataString = {auth_token: sessionStorage.auth_token,};
  var mh_url = MH_HOST + '/reports/get_health_issues_report.json';

  $.ajax(
  {
    type: "POST",
    url: mh_url,
    data: dataString,
    dataType: "json",
    success: function (data) 
    { 
        drawChart(data);    
    },
    error: function (data) 
    {
        alert("fail");
    }    
  });  
}

Can anyone please help me.. 谁能帮帮我吗..

My guess is you can try the following :- 我的猜测是您可以尝试以下方法:

var total = 0;
for (var i = 0; i < data.getNumberOfRows(); i++) {

    total += data.getValue(i, 1);

    // get the data
    var label = data.getValue(i, 0);
    var value = data.getValue(i, 1);
    var percent = Math.ceil(1000 * value / total) / 10;

    // This will create legend list for the display
    lis[i] = document.createElement('li');
    lis[i].id = 'legend_' + data.getValue(i, 0);
    lis[i].innerHTML = '<div class="legendMarker" style="background-color:' + colors[i] + ';"></div>' + label + ': ' + value + ' (' + percent + '%)</span>';


    legend.appendChild(lis[i]);
}

Please let me know if it is right. 请告诉我是否正确。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM