Google Charts:如何更改饼图切片的顺序?

时间:2016-08-29 07:50:17

标签: charts google-visualization

如何更改饼图切片生成的顺序?这是我的代码。

var DEFAULT_PIE_CHART_OPTIONS = {
        theme: 'material',
        titleTextStyle: { fontSize: 12 },
        is3D: false,
        pieSliceText: 'label',
        colors: ['#8E142E','#C3CA21','#8A972B','#6FF522','#167D13'],
        fontSize: 12,
        pieHole: 0.6,
        pieStartAngle: 180,
        height: 600,
        chartArea: { width: '100%', height: '100%', left: 0, top: 100 },
        legend: { position: 'top', maxLines: 1, textStyle: { fontSize: 12, bold: true, italic: false } }
    };
var chartOptions = DEFAULT_PIE_CHART_OPTIONS;
        chartOptions.colors = null;
        var chartOverallPmmLevelCalculated = new google.visualization.ChartWrapper({
            'chartType': 'PieChart',
            'containerId': 'chartOverallPmmLevelCalculatedHtml',
             options: chartOptions
        });
function drawPieChart() {
            var responseDataTable = levelTableChart.getDataTable();
            var chartDataTable = new google.visualization.DataTable();
            chartDataTable.addColumn('string', 'Fiscal');
            chartDataTable.addColumn('number', 'LEVEL');
            var chartDataTableRow = new Array();
            var rowCounter;
            var levelValue;
            for (rowCounter = 0; rowCounter < responseDataTable.getNumberOfRows() ; rowCounter++) {
                var seek = 0 * 1;
                levelValue = responseDataTable.getValue(rowCounter, 1);
                if (levelValue !== null) {
                    chartDataTableRow[seek++] = "LEVEL " + levelValue.toString();
                    chartDataTableRow[seek++] = responseDataTable.getValue(rowCounter, 2);
                } else {
                    chartDataTableRow[seek++] = null;
                    chartDataTableRow[seek++] = null;
                }
                chartDataTable.addRow(chartDataTableRow);
            }
            chartOverallPmmLevelCalculated.setDataTable(chartDataTable);
            chartOverallPmmLevelCalculated.draw();
        }

我附加了生成的饼图,该饼图使用随机LEVEL信息生成。我只需要在饼图切片中顺时针显示LEVEL 0到LEVEL 3。

enter image description here

1 个答案:

答案 0 :(得分:4)

在绘制图表之前对DataTable进行排序

// sort data - first column
data.sort([{column: 0}]);

请参阅以下工作代码段...

google.charts.load('current', {
  callback: function () {
    var data = google.visualization.arrayToDataTable([
      ['Fiscal', 'LEVEL'],
      ['LEVEL 3',     30],
      ['LEVEL 1',     12],
      ['LEVEL 2',     42],
      ['LEVEL 0',     8],
    ]);

    var chart = new google.visualization.ChartWrapper({
      chartType: 'PieChart',
      containerId: 'chart_div',
      dataTable: data,
      options: {
        theme: 'material',
        titleTextStyle: { fontSize: 12 },
        is3D: false,
        pieSliceText: 'label',
        fontSize: 12,
        pieHole: 0.6,
        pieStartAngle: 180,
        height: 600,
        chartArea: { width: '100%', height: '100%', left: 0, top: 100 },
        legend: { position: 'top', maxLines: 1, textStyle: { fontSize: 12, bold: true, italic: false } }
      }
    });

    // sort data - first column
    data.sort([{column: 0}]);

    chart.draw();
  },
  packages:['corechart']
});
<script src="https://www.gstatic.com/charts/loader.js"></script>
<div id="chart_div"></div>