【问题标题】:In nvd3 line chart, showMaxMin:true is not working在 nvd3 折线图中,showMaxMin:true 不起作用
【发布时间】:2017-01-04 14:57:21
【问题描述】:

大家好,我已经在我的应用程序中实现了 NVD3 折线图,我在 X 轴上有“一月,二月..直到..十二月”之类的刻度,图表绘制得很完美,但是 x 轴刻度没有被渲染照原样。请帮我解决问题,这是我所做的以及我可以绘制的折线图的快照,

JSP 代码:

<nvd3 options="options" data="dataForLineChart"></nvd3>

控制器:

$scope.options = {
        chart: {
            type: 'lineChart',
            showXAxis:true,
            height: 300,
            margin : {
                top: 20,
                right: 20,
                bottom: 40,
                left: 55
            },
            x: function(d){ return d.x; },
            y: function(d){ return d.y; },
            useInteractiveGuideline: false,
            dispatch: {
                stateChange: function(e){ console.log("stateChange"); },
                changeState: function(e){ console.log("changeState"); },
                tooltipShow: function(e){ console.log("tooltipShow"); },
                tooltipHide: function(e){ console.log("tooltipHide"); }
            },
            xAxis: {
                axisLabel: 'Timeline(months)',
                showMaxMin: false
            },
            yAxis: {
                axisLabel: 'Rate of aquisition',
                tickFormat: function(d){
                    return d3.format('')(d);
                },
                axisLabelDistance: -8
            },
            callback: function(chart){
                console.log("!!! lineChart callback !!!");
            },
            showLegend : false

        }
    }

我提供的数据是:

$scope.dataForLineChart =  
    [{
      "key": "3",
       "values":[{
        "x": "0",
        "y": "57.0"
       }, {
        "x": "1",
        "y": "67.0"
       }, {
        "x": "2",
        "y": "40.0"
       }, {
        "x": "3",
        "y": "20.0"
       }, {
        "x": "4",
        "y": "10.0",
       }, {
        "x": "5",
        "y": "40.0"
       }, {
        "x": "6",
        "y": "57.0",
       }, {
        "x": "7",
        "y": "44.0"
       }, {
        "x": "8",
        "y": "23.0"
       }, {
        "x": "9",
        "y": "75.0"
       }, {
        "x": "10",
        "y": "22.0"
       }, {
        "x": "11",
        "y": "12.0"
       }]
      }];

最后是我的图表的样子 为什么所有 12 个月都没有得到渲染

将宽度设置为 800 后:

【问题讨论】:

  • 剩下的数字去哪儿了,???这可能是一个html问题吗?
  • 您找到解决方案了吗?

标签: angularjs charts nvd3.js


【解决方案1】:

其实它是有效的,它显示了x轴上的所有值,只需调整图表大小即可查看,

发生这种情况是因为它会在您调整图表大小时自动调整轴的大小。把宽度改成800就可以看到了。

演示

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

app.controller('MainCtrl', function($scope) {
  $scope.options = {
    chart: {
      type: 'lineChart',
      showXAxis: true,
      height: 300,
      width:800,
      margin: {
        top: 20,
        right: 20,
        bottom: 40,
        left: 55
      },
      x: function(d) {
        return d.x;
      },
      y: function(d) {
        return d.y;
      },
      useInteractiveGuideline: false,
      dispatch: {
        stateChange: function(e) {
          console.log("stateChange");
        },
        changeState: function(e) {
          console.log("changeState");
        },
        tooltipShow: function(e) {
          console.log("tooltipShow");
        },
        tooltipHide: function(e) {
          console.log("tooltipHide");
        }
      },
      
      xAxis: {
        axisLabel: 'Timeline(months)',
        showMaxMin: false,
           },
      yAxis: {
        axisLabel: 'Rate of aquisition',
        tickFormat: function(d) {
          return d3.format('')(d);
        },
        axisLabelDistance: -8
      },
      callback: function(chart) {
        console.log("!!! lineChart callback !!!");
      },
      showLegend: false

    }
  }

  $scope.dataForLineChart = [{
    "key": "3",
    "values": [{
      "x": "0",
      "y": "57.0"
    }, {
      "x": "1",
      "y": "67.0"
    }, {
      "x": "2",
      "y": "40.0"
    }, {
      "x": "3",
      "y": "20.0"
    }, {
      "x": "4",
      "y": "10.0",
    }, {
      "x": "5",
      "y": "40.0"
    }, {
      "x": "6",
      "y": "57.0",
    }, {
      "x": "7",
      "y": "44.0"
    }, {
      "x": "8",
      "y": "23.0"
    }, {
      "x": "9",
      "y": "75.0"
    }, {
      "x": "10",
      "y": "22.0"
    }, {
      "x": "11",
      "y": "12.0"
    }]
  }];
});
<!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="dataForLineChart"></nvd3>    
    <br><a href="http://krispo.github.io/angular-nvd3/" target="_blank" style="float: right;">See more</a>
  </body>

</html>

【讨论】:

  • 请看上图,这是我将宽度设置为 800 后得到的,所有的 X 刻度都显示出来了,但是这些都在 div 之外了...... :(
  • @Vish 检查我的样本
【解决方案2】:

为了显示标签,虽然没有海的宽度是渲染所必需的,但要如下函数:

 var chart = nv.models.multiBarChart()
  .reduceXTicks(false)   //If 'false', every single x-axis tick label will be rendered.

【讨论】:

  • 抱歉,您的答案是针对多条形图,我需要它用于折线图。reduceXTicks(false) 不适用于折线图。
猜你喜欢
  • 1970-01-01
  • 2015-12-05
  • 1970-01-01
  • 1970-01-01
  • 2018-09-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-11-22
相关资源
最近更新 更多