1
我正在使用ng2-nvd3,並且我想訪問外部json數據。根據下面的代碼片段,我如何引用.json文件來保存與下面this.data數組中顯示的數據相同的數據?在ng2-nvd3中引用外部數據集的.json文件
import {Component} from 'angular2/core';
declare let d3:any;
import {nvD3} from 'ng2-nvd3';
@Component({
selector: 'bar-chart',
directives: [nvD3],
template: `
<div>
<nvd3 [options]="options" [data]="data"></nvd3>
</div>
`
})
export class BarChartComponent {
options;
data;
ngOnInit() {
this.options = {
chart: {
type: 'discreteBarChart',
height: 450,
margin: {
top: 20,
right: 20,
bottom: 50,
left: 55
},
x: function (d) {
return d.label;
},
y: function (d) {
return d.value;
},
showValues: true,
valueFormat: function(d){
return d3.format(',.4f')(d);
},
duration: 500,
xAxis: {
axisLabel: 'X axis',
},
yAxis: {
axisLabel: 'Y axis',
axisLabelDistance: -10
}
}
}
//Want to replace this with an external json file.
this.data = [
{
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
}
]
}
];
}
}
是否要裝入該文件在運行時使用HTTP,還是始終是靜態的,你只是想將它移動到另一個文件,以保持這個組件小? –
@James Stewart,如何在運行時用http處理加載文件。這就是我想要的。 – kplus