首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >100%堆叠条形图

100%堆叠条形图
EN

Stack Overflow用户
提问于 2014-12-09 18:24:29
回答 2查看 5K关注 0票数 2

我使用ng-google图表,angularjs绘制堆叠条形图。我需要画100%的堆叠条形图。下面的示例代码是堆叠条形图,但不是100%堆叠。

代码语言:javascript
运行
复制
'use strict';

angular.module('google-chart-example', ['googlechart']).controller("MainCtrl", function ($scope) {

    var chart1 = {};
    chart1.type = "BarChart";
    chart1.cssStyle = "height:200px; width:550px;";
    chart1.data = {"cols": [
        {id: "month", label: "Month", type: "string"},
        {id: "laptop-id", label: "Laptop", type: "number"},
        {id: "desktop-id", label: "Desktop", type: "number"},
        {id: "server-id", label: "Server", type: "number"},
        {id: "cost-id", label: "Shipping", type: "number"}
    ], "rows": [
        {c: [
            {v: "January"},
            {v: (19), f: "42 items"},
            {v: (12), f: "Ony 12 items"},
            {v: (7), f: "7 servers"},
            {v: (4)}
        ]},
        {c: [
            {v: "March"},
            {v: (24)},
            {v: (0)},
            {v: (11)},
            {v: (6)}

        ]}
    ]};

    chart1.options = {
        "title": "Sales per month",
        "isStacked": "true",
        "fill": 20,
        "displayExactValues": true,
        "hAxis": {
                  viewWindowMode:'explicit',
                  "gridlines": {"count": 11},
                  viewWindow:{
                    max:100,
                    min:0
                  },
                  format: '#\'%\''
                },  
    };

    chart1.formatters = {};

    $scope.chart = chart1;

});
代码语言:javascript
运行
复制
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.0.5/angular.min.js"></script>
<html xmlns="http://www.w3.org/1999/html">
    <head>
        <title>
            Google Chart Tools AngularJS Directive Example
        </title>
    </head>
    <body ng-app="google-chart-example" ng-controller="MainCtrl">

    <div>
    <div google-chart chart="chart" style="{{chart.cssStyle}}"/>
    </div>


    <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.0.5/angular.min.js"></script>
    <script src="http://bouil.github.io/angular-google-chart/ng-google-chart.js"></script>
    <script src="example.js"></script>
    </body>
</html>

我的问题是,我需要绘制像http://jsfiddle.net/qy6cn/这样的堆叠图,它是100%堆叠的。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2015-02-11 10:17:07

将计算100 %的叠加条形图-(数组中的第一个单元/第一个数组元素的总数)*100

(阵列第一单元=19/阵列第一单元总数= 56)*100 (阵列第一单元=24/阵列第一单元总数= 56)*100

代码语言:javascript
运行
复制
    'use strict';

    angular.module('google-chart-example', ['googlechart']).controller("MainCtrl", function ($scope) {

        var chart1 = {};
        chart1.type = "BarChart";
        chart1.cssStyle = "height:200px; width:550px;";
        chart1.data = {"cols": [
            {id: "month", label: "Month", type: "string"},
            {id: "laptop-id", label: "Laptop", type: "number"},
            {id: "desktop-id", label: "Desktop", type: "number"},
            {id: "server-id", label: "Server", type: "number"},
            {id: "cost-id", label: "Shipping", type: "number"}
        ], "rows": [
            {c: [
                {v: "January"},
                {v: (19/56)*100, f: "42 items"},
                {v: (12/13)*100, f: "Ony 12 items"},
                {v: (7/30)*100, f: "7 servers"},
                {v: (4/14)*100}
            ]},
            {c: [
                {v: "March"},
                {v: (24/56)*100},
                {v: (0/13)*100},
                {v: (11/30)*100},
                {v: (6/14)*100}

            ]}
        ]};

        chart1.options = {
            "title": "Sales per month",
            "isStacked": "true",
            "fill": 20,
            "displayExactValues": true,
            "hAxis": {
                      viewWindowMode:'explicit',
                      "gridlines": {"count": 11},
                      viewWindow:{
                        max:100,
                        min:0
                      },
                      format: '#\'%\''
                    },  
        };

        chart1.formatters = {};

        $scope.chart = chart1;

    });
代码语言:javascript
运行
复制
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.0.5/angular.min.js"></script>
    <html xmlns="http://www.w3.org/1999/html">
        <head>
            <title>
                Google Chart Tools AngularJS Directive Example
            </title>
        </head>
        <body ng-app="google-chart-example" ng-controller="MainCtrl">

        <div>
        <div google-chart chart="chart" style="{{chart.cssStyle}}"/>
        </div>


        <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.0.5/angular.min.js"></script>
        <script src="http://bouil.github.io/angular-google-chart/ng-google-chart.js"></script>
        <script src="example.js"></script>
        </body>
    </html>

票数 3
EN

Stack Overflow用户

发布于 2014-12-14 08:35:32

您需要将chart1.type = "BarChart";更改为chart1.type = "ColumnChart";,以便它是垂直的。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/27386099

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档