【问题标题】:nvd3 positive and negative value for same timestampnvd3 相同时间戳的正负值
【发布时间】:2018-09-10 13:24:17
【问题描述】:

使用nvd3 库我试图在条形图中显示相同“x”值的正和负“y”值(如两个倒置条)。值正在显示,但负值也绘制在 y 轴的正刻度上。

如果我只显示负值,它可以正常工作,但在两种情况下,它都不能按预期工作。

以下是我目前在我的代码中的内容:

var chart;
nv.addGraph(function() {
  chart = nv.models.historicalBarChart();
  chart.useInteractiveGuideline(true);
  chart.xAxis.axisLabel("Time (s)");
  chart.yAxis.axisLabel('Voltage (v)');

  d3.select('#chart svg')
    .datum(sinAndCos())
    .call(chart);

  nv.utils.windowResize(chart.update);
  return chart;
});

function sinAndCos() {
  var sin = [], cos = [];
    
  for (var i = 0; i < 100; i++) {
    sin.push({
      x: i,
      y: i
    });

    cos.push({
      x: i,
      y: -(i)
    });
  }

  return [
  {
      values: sin,
      key: "Sine Wave",
      color: "#ff7f0e"
    },
    {
      values: cos,
      key: "Cosine Wave",
      color: "#2ca02c"
    }
  ];
}
 text {
   font: 12px sans-serif;
 }

 svg {
   display: block;
 }

 html, body, #chart, svg {
   margin: 0px;
   padding: 0px;
   height: 100%;
   width: 100%;
 }
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.4.11/d3.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/nvd3/1.8.6/nv.d3.min.js"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/nvd3/1.8.6/nv.d3.min.css" rel="stylesheet"/>

<div id="chart">
  <svg></svg>
</div>

这里是重现问题的 JSFiddle 的链接:https://jsfiddle.net/prerak6962/a1zh9o4e/14/

我有什么遗漏或需要补充的吗?

【问题讨论】:

    标签: javascript d3.js nvd3.js


    【解决方案1】:

    使用不同的条形图可以得到更好的结果

    • chart = nv.models.discreteBarChart();
    • chart = nv.models.multiBarChart();

    var chart;
    nv.addGraph(function() {
      //chart = nv.models.historicalBarChart();
      chart = nv.models.multiBarChart();
      //chart.useInteractiveGuideline(true);
      chart.xAxis.axisLabel("Time (s)");
      chart.yAxis.axisLabel('Voltage (v)');
    
      d3.select('#chart svg')
        .datum(sinAndCos())
        .call(chart);
    
      nv.utils.windowResize(chart.update);
      return chart;
    });
    
    function sinAndCos() {
      var sin = [], cos = [];
        
      for (var i = 0; i < 100; i++) {
        sin.push({
          x: i,
          y: i
        });
    
        cos.push({
          x: i,
          y: -(i)
        });
      }
    
      return [
      {
          values: sin,
          key: "Sine Wave",
          color: "#ff7f0e"
        },
        {
          values: cos,
          key: "Cosine Wave",
          color: "#2ca02c"
        }
      ];
    }
     text {
       font: 12px sans-serif;
     }
    
     svg {
       display: block;
     }
    
     html, body, #chart, svg {
       margin: 0px;
       padding: 0px;
       height: 100%;
       width: 100%;
     }
    <script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.4.11/d3.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/nvd3/1.8.6/nv.d3.min.js"></script>
    <link href="https://cdnjs.cloudflare.com/ajax/libs/nvd3/1.8.6/nv.d3.min.css" rel="stylesheet"/>
    
    <div id="chart">
      <svg></svg>
    </div>

    【讨论】:

    • 我想使用相同的图表类型,但它现在可以工作。
    • @Prera​​kSola:找出这些条形图之间的区别。 nvd3 文档是……(我在主站点上找不到)
    猜你喜欢
    • 2015-04-02
    • 1970-01-01
    • 2022-01-24
    • 1970-01-01
    • 2012-02-04
    • 2017-09-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多