使用nvd3进行缩放

时间:2015-06-16 16:14:14

标签: angularjs nvd3.js angularjs-nvd3-directives

我需要放大图形。我正在使用角度js和nvd3。 这是我的代码,html和角度模块的一部分:



var app = angular.module('DL', ['nvd3']);

app.controller('daCtrl', function($scope) {
  
$scope.graphic = [{key: "a", values: [[243434, 1],[363352, 2],...]}, [[243434, 1],[363352, 2],...]},... ];

  $scope.options = {
                    chart: {
                        type: "lineChart",
                        height: 450,
                        margin : {
                            top: 20,
                            right: 20,
                            bottom: 60,
                            left: 65
                        },
                        x: function(d){ return d[0]; },
                        y: function(d){ return d[1]/100; },
                        average: function(d) { return d.mean/100; },

                        color: d3.scale.category10().range(),
                        transitionDuration: 300,
                        useInteractiveGuideline: true,
                        clipVoronoi: false,

                        xAxis: {
                            axisLabel: 'Date',
                            tickFormat: function(d) {
                                return d3.time.format('%m/%d/%y')(new Date(d));
                            }
                        },

                        yAxis: {
                            axisLabel: 'Measure',
                            tickFormat: function(d){
                                return d3.format('.02f')(d);
                            },
                            axisLabelDistance: 20,
                            showMaxMin: true,
                            staggerLabels: true
                        }

                    }
                };
  });

<link rel="stylesheet" type="text/css" href="../bower_components/nvd3/nv.d3.css">

<script src="../bower_components/d3/d3.js"></script>
<script src="../bower_components/nvd3/nv.d3.js"></script>
<script src="../bower_components/angular-nvd3/dist/angular-nvd3.js"></script>

<nvd3 options="options" data="graphic"></nvd3>
&#13;
&#13;
&#13;

图形没问题,但我需要在图形上进行缩放。 我已经看到D3是可能的,但如果你可以同样使用nvd3

3 个答案:

答案 0 :(得分:1)

我知道我迟到了两年,但放大angularjs完全是一件事,只需添加类似的内容:

zoom: {
    enabled: true,
    scaleExtent: [1, 10000],
    useFixedDomain: false,
    useNiceScale: false,
    horizontalOff: false,
    verticalOff: true,
    unzoomEventType: "dblclick.zoom"
}

到图表选项

答案 1 :(得分:1)

尝试使用lineWithFocusChart而不是lineChart,然后将此选项添加到图表中:

                    x2Axis: {
                        tickFormat: function (d) {
                             return d3.time.format('%m/%d/%y')(new Date(d));
                        }
                    },
                    y2Axis: {
                        tickFormat: function(d){
                                return d3.format('.02f')(d);
                            }
                    }

答案 2 :(得分:-1)

我使用Dygraphs angularjs指令(https://twittstrap.com/dygraphs-angularjs-directive/)将nvd3更改为dygraph

现在(网站的同一个例子)

HTML:

&#13;
&#13;
<head>
    <script src="bower_components/jquery/dist/jquery.min.js"></script>
    <script src="bower_components/dygraphs/dygraph-combined.js"></script>
    <script src="bower_components/angular/angular.min.js"></script>
    <script src="bower_components/angular-sanitize/angular-sanitize.js"></script>
    <script src="bower_components/moment/moment.js"></script>
    <script src="bower_components/bootstrap/dist/js/bootstrap.min.js"></script>
    <script src="bower_components/angular-dygraphs/src/angular-dygraphs.js"></script>

    <!--<link rel="stylesheet" type="text/css" href="bower_components/angular-dygraphs/demo/demo.css"/>-->
    <link rel="stylesheet" href="bower_components/bootstrap/dist/css/bootstrap.min.css">
</head>
<body ng-app="demoApp">
<div ng-controller="DemoCtrl">
    <ng-dygraphs data="graph.data" options="graph.options"  legend="graph.legend"></ng-dygraphs>
<div>
</body>
&#13;
&#13;
&#13;

js:

&#13;
&#13;
var example = angular.module('demoApp', [
        "angular-dygraphs"
    ]);

    demoApp.controller('DemoCtrl', function ($scope) {
        $scope.graph = {
            data: [
            ],
            options: {
                labels: ["x", "A", "B"]
            },
            legend: {
                series: {
                    A: {
                        label: "Series A"
                    },
                    B: {
                        label: "Series B",
                        format: 3
                    }
                }
            }
        };

        var base_time = Date.parse("2008/07/01");
        var num = 24 * 0.25 * 365;
        for (var i = 0; i < num; i++) {
            $scope.graph.data.push([ new Date(base_time + i * 3600 * 1000),
                i + 50 * (i % 60),        // line
                i * (num - i) * 4.0 / num  // parabola
            ]);
        }
       
    });
&#13;
&#13;
&#13;