如何从D3.js图表​​中读取(获取)Y轴刻度值?

时间:2017-11-30 23:56:38

标签: javascript angularjs d3.js nvd3.js

有没有办法从D3.js图表​​中获取Y轴刻度值。我有以下图表,我想通过点击按钮获得Y轴刻度。

我想读取红色框内的X轴刻度。我使用下面的代码创建了D3图表。

获得价值的任何想法或输入都将受到高度赞赏。

enter image description here

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

app.controller('MainCtrl', function($scope) {
  $scope.options = {
            chart: {
                type: 'discreteBarChart',
                height: 450,
                margin : {
                    top: 20,
                    right: 20,
                    bottom: 50,
                    left: 55
                },
                x: function(d){return d.label;},
                y: function(d){return d.value + (1e-10);},
                showValues: true,
                valueFormat: function(d){
                    return d3.format(',.4f')(d);
                },
                duration: 500,
                xAxis: {
                    axisLabel: 'X Axis'
                },
                yAxis: {
                    axisLabel: 'Y Axis',
                    axisLabelDistance: -10
                }
            }
        };

        $scope.data = [
            {
                key: "Cumulative Return",
                values: [
                    {
                        "label" : "A" ,
                        "value" : 25
                    } ,
                    {
                        "label" : "B" ,
                        "value" : 17
                    } ,
                    {
                        "label" : "C" ,
                        "value" : 32
                    } ,
                    {
                        "label" : "D" ,
                        "value" : 41
                    } ,
                    {
                        "label" : "E" ,
                        "value" : 52
                    } ,
                    {
                        "label" : "F" ,
                        "value" : 46
                    } ,
                    {
                        "label" : "G" ,
                        "value" : 31
                    } ,
                    {
                        "label" : "H" ,
                        "value" : 36
                    }
                ]
            }
        ]
});
<!DOCTYPE html>
<html ng-app="plunker">

  <head>
    <meta charset="utf-8" />
    <title>Angular-nvD3  Discrete Bar Chart</title>
    <script>document.write('<base href="' + document.location + '" />');</script>
    <link rel="stylesheet" href="style.css" />
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/nvd3/1.8.1/nv.d3.min.css"/>
    <script src="//ajax.googleapis.com/ajax/libs/angularjs/1.3.9/angular.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.6/d3.min.js" charset="utf-8"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/nvd3/1.8.1/nv.d3.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/angular-nvd3/1.0.5/angular-nvd3.min.js"></script>
    <script src="app.js"></script>
  </head>

  <body ng-controller="MainCtrl">
    
    <nvd3 options="options" data="data"></nvd3>
    
    <br><a href="http://krispo.github.io/angular-nvd3/" target="_blank" style="float: right;">See more</a>
  </body>

</html>

1 个答案:

答案 0 :(得分:0)

您可以使用d3.selectAll选择刻度线,并对每个刻度线进行迭代以访问附加的数据值。例如,以下内容将起作用,并且需要更新以确保您在nvd3中选择正确的元素类< / p>

let yValues = []

d3.select(".nv-y").selectAll(".tick")
    .each(function(d){
       yValues.push(d)
    }) 

console.log(yValues)