NVD3离散柱状图与Json
NVD3 Discrete BarChart With Json
我试图将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"]}
如果有人能尽可能完整地回答我,我将非常感激=]
我不确定你需要什么样的图形,因为你说你想要不同维度的值。我创建了一个图表,所有的数据都在同一个维度上。我想它一定能满足你的需要。
演示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)
...
相关文章:
- jQuery匹配JSON对象的部分文本
- 在循环中分配json值时,值被覆盖
- 需要帮助设置json数组
- 如何在Javascript中将JSon对象转换为数组
- 我可以在json对象中添加一个函数吗
- 如何使用d3js创建多行图表,json格式为nvd3
- 将JSON数据加载到Angular-nvD3 Graph(AngularJS)中
- Working with JSON and Angular NVD3
- 将 Json 数据加载到 nvd3 图中
- 饼图不适用于使用AngularJS和nvd3的Json Data
- 如何将JSON数据添加到nvd3图表中
- 本地折线图不同&使用NVD3创建远程json文件
- 无法获得Nvd3图表的json提要
- 在nvd3结构中使用d3和json的多折线图
- 用d3.csv加载csv数据在nvd3多条形图示例(JSON格式)
- nvd3或d3不是默认的json格式
- Angular NVD3多条形图,双y轴,只显示使用json数据的线
- NVD3离散柱状图与Json
- 如何在不使用“标签”的情况下制作NVD3饼状图?和“;value"JSON对象中
- NVD3图表中的JSON数据格式