在Google Piechart图例上显示值

时间:2018-10-12 15:29:22

标签: asp.net json ajax google-pie-chart

我有这个Google饼图,它工作正常,除了带有图例的文字外,我想显示百分比和数字。下面是代码

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script type="text/javascript" src="https://www.google.com/jsapi"></script>
<script type="text/javascript">
    google.load("visualization", "1", { packages: ["corechart"] });
    google.setOnLoadCallback(drawChart);
    function drawChart() {
        var options = {
            title: 'Registration',
            legend: { position: 'right', textStyle: { color: 'blue', fontSize: 16 } }
        };
        $.ajax({
            type: "POST",
            url: "adminrep.aspx/GetChartData",
            data: '{}',
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function (r) {
                var data = google.visualization.arrayToDataTable(r.d);
                var chart = new google.visualization.PieChart($("#chart")[0]);
                chart.draw(data, options);
            },
            failure: function (r) {
                alert(r.d);
            },
            error: function (r) {
                alert(r.d);
            }
        });
    }
</script>

<div id="chart" style="width: 900px; height: 500px; margin-top:60px;"></div>

我该如何完成?提前致谢。

2 个答案:

答案 0 :(得分:2)

尝试使用.setFormattedValue来格式化DataTable中的标签。
仍然需要您进行一些手动计算才能获得总值,但是应该可以:

<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script type="text/javascript" src="https://www.google.com/jsapi"></script>
<script type="text/javascript">
  google.load("visualization", "1", { packages: ["corechart"] });
  google.setOnLoadCallback(drawChart);
  function drawChart() {
    var options = {
      title: 'Registration',
      legend: { 
        position: 'right', 
        textStyle: { color: 'blue', fontSize: 16 } 
      }
    };
    $.ajax({
        type: "POST",
        url: "/echo/json/",
        data: {
        json: JSON.stringify({ 
          d: [
            ['Task', 'Hours per Day'],
            ['Work',     11],
            ['Eat',      2],
            ['Commute',  2],
            ['Watch TV', 2],
            ['Sleep',    7]
          ]}) 
        },
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (r) {
            var data   = google.visualization.arrayToDataTable(r.d);                
            var count  = data.getNumberOfRows();
            var values = Array(count).fill().map(function(v, i) {
              return data.getValue(i, 1);
            });
            var total =  google.visualization.data.sum(values);
            values.forEach(function(v, i) {                                         
              var key = data.getValue(i, 0);
              var val = data.getValue(i, 1);
              data.setFormattedValue(i, 0, key + ' (' + (val/total * 100).toFixed(1) + '%)');                  
            });

            var chart = new google.visualization.PieChart($("#chart")[0]);
            chart.draw(data, options);
        },
        failure: function (r) {
            alert(r.d);
        },
        error: function (r) {
            alert(r.d);
        }
    });
  }
</script>

<div id="chart" style="width: 900px; height: 500px; margin-top:60px;"></div>

示例:https://jsfiddle.net/cn74tvmL/show

答案 1 :(得分:0)

您需要使用number format

var formatter = new google.visualization.NumberFormat({pattern: '#,### MW'});
formatter.format(data, 1);

或:

chart1.options = {
    .
    .
    .
    pieSliceText: 'value',
    legend: {
        position: 'labeled',
        labeledValueText: 'both',
        textStyle: {
            color: 'blue', 
            fontSize: 14
        }
    }
};