【问题标题】:how to display top 5 values in google pie chart如何在谷歌饼图中显示前 5 个值
【发布时间】:2023-03-20 10:22:01
【问题描述】:

// Load google charts
google.charts.load('current', {
  'packages': ['corechart']
});
google.charts.setOnLoadCallback(drawChart);

// Draw the chart and set the chart values
function drawChart() {
  var data = google.visualization.arrayToDataTable([
    ['Task', 'Hours per Day'],
    ['Work', 8],
    ['Eat', 2],
    ['TV', 4],
    ['Gym', 2],
    ['Sleep', 8],
    ['walk', 2],
    ['games', 2],
    ['chess', 2],
    ['drink', 4],
    ['dance', 6]
  ]);

  // Optional; add a title and set the width and height of the chart
  var options = {
    'title': 'My Average Day',
    'width': 550,
    'height': 400
  };

  // Display the chart inside the <div> element with id="piechart"
  var chart = new google.visualization.PieChart(document.getElementById('piechart'));
  chart.draw(data, options);
}
<div id="piechart"></div>

<script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script>
  1. 我在谷歌图表中展示了 10 个不同的作品
  2. 其中一些百分比较低,而其中一些百分比较高
  3. 谁能帮我在谷歌饼图中只显示前 5 个高百分比值

【问题讨论】:

    标签: javascript jquery for-loop pie-chart google-pie-chart


    【解决方案1】:

    我的解决方案是将值保存到一个数组中,对数组进行排序,然后遍历前五个。

    排序:

    var a = [['Work', 8],['Eat', 2],['TV', 4],['Gym', 2],['Sleep', 8],['walk', 2],['games', 2],['chess', 2],['drink', 4],['dance', 6]];
    a.sort(compareSecondColumn);
    
    function compareSecondColumn(a, b) {
        if (a[1] === b[1]) {
            return 0;
        }
        else {
            return (a[1] > b[1]) ? -1 : 1;
        }
    }
    

    对于图表:

    var data = google.visualization.arrayToDataTable([
        ['Task', 'Hours per Day'],a[0],a[1],a[2],a[3],a[4]
    ]);
    

    大家一起:

    var a = [['Work', 8], ['Eat', 2], ['TV', 4], ['Gym', 2], ['Sleep', 8], ['walk', 2], ['games', 2], ['chess', 2], ['drink', 4], ['dance', 6]];
    a.sort(compareSecondColumn);
    
    function compareSecondColumn(a, b) {
        if (a[1] === b[1]) {
            return 0;
        }
        else {
            return (a[1] > b[1]) ? -1 : 1;
        }
    }
    
    
    
    // Draw the chart and set the chart values
    function drawChart() {
        var data = google.visualization.arrayToDataTable([
            ['Task', 'Hours per Day'],a[0],a[1],a[2],a[3],a[4]
            
        ]);
        // Optional; add a title and set the width and height of the chart
        var options = { 'title': 'the title', 'width': 550, 'height': 400 };
        // Display the chart inside the <div> element with id="piechart"
        var chart = new google.visualization.PieChart(document.getElementById('piechart'));
        chart.draw(data, options);
    }
    

    查看这篇文章了解更多关于排序的信息:

    https://stackoverflow.com/a/16097058/10958914

    【讨论】:

    • 请在我的 sn-p 代码中编辑,我试过它不起作用
    • 嗨,队长,我使用的是条形图而不是饼图,我们如何为每个条形图添加颜色和注释
    • 我在我的问题中添加了图片
    猜你喜欢
    • 1970-01-01
    • 2023-04-04
    • 1970-01-01
    • 2019-01-26
    • 1970-01-01
    • 1970-01-01
    • 2011-10-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多