javascript - 带 Json 的 NVD3 离散条形图

标签 javascript json d3.js nvd3.js

我正在尝试将 json 文件放入 NVD3 条形图中,但我无法执行我想要的操作。我对 JS 不太了解,但正在为学校的期末项目学习,所以请帮忙 =]

这是我从示例中获得的代码:

<!DOCTYPE html> <meta charset="utf-8">

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

<style>

body {   overflow-y:scroll; }

text {   font: 12px sans-serif; }

svg {   display: block; }

#chart1 svg{   height: 500px;   min-width: 100px;   min-height: 100px; /*   margin: 10px;   Minimum height and width is a good idea to prevent negative SVG dimensions...   For example width should be =< margin.left + margin.right + 1,   of course 1 pixel for the entire chart would not be very useful, BUT should not have errors
*/ }

</style> <body>

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

<script src="../lib/d3.v3.js"></script> <script src="../nv.d3.js"></script> <!-- including all the components so I don't have to minify every time I test in development --> <script src="../src/tooltip.js"></script> <script src="../src/utils.js"></script> <script src="../src/models/axis.js"></script> <script src="../src/models/discreteBar.js"></script> <script src="../src/models/discreteBarChart.js"></script> <script>





historicalBarChart = [    {
    key: "Cumulative Return",
    values: [
      { 
        "label" : "A" ,
        "value" : 29.765957771107
      } , 
      { 
        "label" : "B" , 
        "value" : 0
      } , 
      { 
        "label" : "C" , 
        "value" : 32.807804682612
      } , 
      { 
        "label" : "D" , 
        "value" : 196.45946739256
      } , 
      { 
        "label" : "E" ,
        "value" : 0.19434030906893
      } , 
      { 
        "label" : "F" , 
        "value" : 98.079782601442
      } , 
      { 
        "label" : "G" , 
        "value" : 13.925743130903
      } , 
      { 
        "label" : "H" , 
        "value" : 5.1387322875705
      }
    ]   } ];




nv.addGraph(function() {     var chart = nv.models.discreteBarChart()
      .x(function(d) { return d.label })
      .y(function(d) { return d.value })
      .staggerLabels(true)
      //.staggerLabels(historicalBarChart[0].values.length > 8)
      .tooltips(false)
      .showValues(true)
      .transitionDuration(250)
      ;

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

  nv.utils.windowResize(chart.update);

  return chart; });


</script>

我的问题在这里:

historicalBarChart = [ 
  {
    key: "Cumulative Return",
    values: [
      { 
        "label" : "A" ,
        "value" : 29.765957771107
      } , 
      { 
        "label" : "B" , 
        "value" : 0
      } , 
      { 
        "label" : "C" , 
        "value" : 32.807804682612
      } , 
      { 
        "label" : "D" , 
        "value" : 196.45946739256
      } , 
      { 
        "label" : "E" ,
        "value" : 0.19434030906893
      } , 
      { 
        "label" : "F" , 
        "value" : 98.079782601442
      } , 
      { 
        "label" : "G" , 
        "value" : 13.925743130903
      } , 
      { 
        "label" : "H" , 
        "value" : 5.1387322875705
      }
    ]
  }
];

我想将我的 Json 值放在那里,我该怎么做?

这是我的 Json 文件,一个用于 X 轴,另一个用于 Y 轴:

X 轴:

{"periodos":["112","121","122","131","132","141"]}

Y 轴:

{"reprovados":["247","164","250","147","144","0"]}

如果有人可以尽可能完整地回答我,我将非常感激 =]

最佳答案

我不确定您需要哪种图表,因为您说您想要不同维度的值。我创建了一个图表,其中所有数据都处于同一维度......我想您将能够用它满足您的需求。

DEMO

var json1 = {"periodos":["A","B","C","D","E","F","G","H","I"]};
var json2 = {"reprovados":["20","0","32.81","396.46","0.19","98.08","13.93","5.14","15.14"]};

var data = [{values: []}];
for(var i = 0; i < json1.periodos.length; i++){
  data[0].values.push({
    label: json1.periodos[i],
    value: Number(json2.reprovados[i])
  });
};

...

d3.select('#chart1 svg').datum(data)

...

关于javascript - 带 Json 的 NVD3 离散条形图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23786628/

相关文章:

javascript - 将数据推送到数组填充 jqGrid

JavaScript 在 JSON 对象中递归搜索

javascript - D3 V4 折线图如何根据放大移动点?

javascript - 使用 Meteor-ui-progress-circle(访问在 HTML 中创建的模板变量)

javascript - 仅当更改值的长度等于 x 时,如何在输入文本中使用事件 ng-change 指令?

javascript - 在幻灯片函数中使用 jquery ui 参数的 jquery 自定义 slider (?)

javascript - 来自多语言 API 的 JSON 响应的最佳实践是什么

php - 快速登录 POST 连接 php

d3.js - 如何使用 d3 将文本数组添加到多个 div

javascript - 在 Force Directed Graph d3 中引入 Arrow(directed)