【问题标题】:angular-dc Bar chart not getting renderedangular-dc条形图未呈现
【发布时间】:2015-05-21 11:06:38
【问题描述】:

我正在尝试使用 crossfilter、dc.js 和 angular-dc 绘制一个简单的条形图。行图工作正常。条形图不呈现条形。在 Chrome 中,如果我检查,我会看到值在那里。如果我强制专注,我会看到图表。我已经尝试了所有建议,但似乎有 3 个问题:

所有条形图都从 x 轴上的 0 开始。 每个条的宽度为 1 px。 条形图未在屏幕上呈现。

您能否为我提供上述问题的解决方案。 我使用以下链接创建我的条形图:https://github.com/TomNeyland/angular-dc/blob/master/example/stocks/nasdaq.html

还有其他与使用 angular-dc 的条形图相关的链接吗?

这里是示例代码:

<!DOCTYPE html>
<html lang="en">
<head>
    <title>dc.js - Pie Chart Example</title>
    <meta charset="UTF-8">
    <link rel="stylesheet" type="text/css" href="public/lib/dcjs/web/css/dc.css"/>
    <script type="text/javascript" src="//cdnjs.cloudflare.com/ajax/libs/lodash.js/2.4.1/lodash.min.js"></script>
    <script type="text/javascript" src="public/lib/d3/d3.js"></script>
    <script type="text/javascript" src="public/lib/crossfilter/crossfilter.js"></script>
    <script type="text/javascript" src="public/lib/dcjs/dc.js"></script>
    <script type="text/javascript" src="public/lib/angular/angular.js"></script>
    <script type="text/javascript" src="public/lib/angular-dc/dist/angular-dc.js"></script>
</head>
<body ng-app="app">
<!-- we nicely separate the view and the data. Here, all information concerning the way to display the data
is in the template -->
<div ng-controller="myController">
    <div dc-chart="pieChart"
         dc-chart-group="1"
         dc-width="780"
         dc-height="480"
         dc-inner-radius="100"
         dc-dimension="fruit"
         dc-group="fruitSales"
         dc-legend="dc.legend()">
    </div>
    <div dc-chart="barChart"
         dc-width="780"
         dc-height="480"
         dc-dimension="fruit"
         dc-group="fruitSales"
         dc-x="d3.scale.ordinal().domain(['','Apple','Banana'])"
         dc-xUnits ="dc-units-ordinal"
         dc-elastic-y="true"
         dc-center-bar="true"
         dc-gap="1"
         dc-bar-padding="0.5"
         dc-xAxisPadding="50"
         dc-legend="dc.legend()">
    </div>
    </div>
</div>
<script type="text/javascript">
    angular.module("app", ["angularDc"]);
    myController = function($scope) {
            var fruitCf =  crossfilter([
                {Name: 'Apple', Sales: 40},
                {Name: 'Apple', Sales: 40},
                {Name: 'Banana', Sales: 20}]);
            $scope.fruit  = fruitCf.dimension(function(d) {return d.Name;});
            $scope.fruitSales = $scope.fruit.group().reduceSum(function(d) {return d.Sales;});
            //$scope.$apply()
        };
</script>
</body>
</html>

【问题讨论】:

  • 在 html 中硬编码图表的属性不是很好的风格。你的问题解决了吗?如果是这样,您应该发布答案或评论

标签: javascript css angularjs dc.js


【解决方案1】:

就在几天前,我尝试做同样的事情。我什至尝试了以下链接提供的解决方案:

Angular dc.js filter issue

dc.js x axis ordinal chart issue

但这对我不起作用。然后就在今天,我尝试在我的控制器中使用 dc.js Plain Javascript,它运行良好。您甚至不必在您的应用程序中注入“angularDc”依赖项。

这是我建议你应该尝试的:

<body ng-app="app">
<div id="England_batting" ng-controller="myController"></div>
<script>
    var app = angular.module("app", []);
    app.controller('myController', function($scope) {

        d3.json('New.json', function(error, data) {
            var data = data.Cricket;

            var ndx = crossfilter(data);
            dateDim = ndx.dimension(function(d) {
                return d.Date;
            });
            var a = dateDim.filter("abdul");
            var b = a.top(Infinity);
            var c = crossfilter(b);

            inningDim = c.dimension(function(d) {

                return d.Inning;

            });
            var d = inningDim.filter("Australia bowling");
            var e = d.top(Infinity);
            var f = crossfilter(e);

            nameDim = f.dimension(function(d) {

                return d.Name;


            });

            runName = nameDim.group().reduceSum(function(d) {

                return d.Runs;


            });


            var England_batting = dc.barChart("#England_batting");
            England_batting.width(700)
                .height(480)
                .gap(30)
                .yAxisLabel(".", 50)
                .xAxisLabel("U", 50)
                .dimension(nameDim)
                .group(runName)
                .x(d3.scale.ordinal().domain(nameDim))
                .xUnits(dc.units.ordinal)
                .y(d3.scale.linear().domain([0, 200]))
                .elasticY(true)
                .renderHorizontalGridLines(true)
                .renderTitle(true)
                .brushOn(true);
            dc.renderAll();

        });

    });
</script>

希望这对您有所帮助。干杯! :)

【讨论】:

    猜你喜欢
    • 2021-10-28
    • 1970-01-01
    • 2014-09-10
    • 1970-01-01
    • 2016-08-29
    • 1970-01-01
    • 2022-09-23
    • 1970-01-01
    • 2020-08-26
    相关资源
    最近更新 更多