Javascript 带Json的NVD3离散条形图

Javascript 带Json的NVD3离散条形图,javascript,json,d3.js,nvd3.js,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:scrol

我试图将一个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>
我想把我的Json值放在那里,我该怎么做

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

X轴:

{periodos:[112121122131132141]}

Y轴:

{reprovados:[247164250147144,0]}


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

我不确定您需要什么样的图形,因为您说您需要不同维度的值。我创建了一个图表,所有数据都在同一维度上。。。我想你可以用它来满足你的需求


periodos中的值应该是x上的描述,而reprovados中的值应该是条的大小,这很好,只是名称有误。如何将periodos值放置在x轴上?顺便说一句,谢谢你,伙计!你真是个天才哈哈哈我完全明白。。。更新了演示。。。thx对于恭维Thansk,现在已经很完美了哈哈,我怎么能从文件中传递json值,而不是在变量上写入呢?!更新。。。重要行:value:Numberjson1.periodos[i]
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
      }
    ]
  }
];
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)

...