gpt4 book ai didi

javascript - Angular2 将值传递给其他类

转载 作者:行者123 更新时间:2023-11-30 12:11:15 26 4
gpt4 key购买 nike

我正在尝试在我的 NG2 应用程序中使用 D3 图表,因为我已经启动并运行了“图表”和硬编码数据。我正在寻找一种能够将数据从另一个组件传递到图表的方法。这是“父”组件,其中包含显示图形的指令:

@Component({
selector: "homeTest",
properties: [ 'data' ]
})
@View({
directives: [CORE_DIRECTIVES, BarGraph],
template: `
<h1 class="title">Angular 2</h1>
<bar-graph>
</bar-graph>
`

})
export class HomeTest {

meter: any;
consumption: any;
elementRef: any;
constructor() {}

我从这个组件中获取我想在图表中使用的数据。假设它看起来像这样:

PassThisArrayToBarChart(){
var dataset = [
{ key: 0, value: 5 },
{ key: 1, value: 10 }
];


//Pass this dataSet to Barchart somehow?
}

这是我们拥有需要数据集的 D3 图表的类:

@Directive({
selector: 'bar-graph',
})
class BarGraph {
data: Array<number>;
divs: any;
dataset: any;
constructor(
@Inject(ElementRef) elementRef: ElementRef,
@Attribute('width') width: string,
@Attribute('height') height: string) {

var w = 600;
var h = 250;

var dataset = [
{ key: 0, value: 5 },
{ key: 1, value: 10 }
];

...

正如您在此处看到的,我有一个可以正常工作的硬编码数据集,但如何将其替换为从 HomeTest 传递的数据集?

如果需要,这里是 BarGraph 的其余部分:

var xScale = d3.scale.ordinal()
.domain(d3.range(dataset.length))
.rangeRoundBands([0, w], 0.05);

var yScale = d3.scale.linear()
.domain([0, d3.max(dataset, function(d) {return d.value;})])
.range([0, h]);

var key = function(d) {
return d.key;
};

//Create SVG element
var svg = d3.select("bar-graph")
.append("svg")
.attr("width", w)
.attr("height", h);

//Create bars
svg.selectAll("rect")
.data(dataset, key)
.enter()
.append("rect")
.attr("x", function(d, i) {
return xScale(i);
})
.attr("y", function(d) {
return h - yScale(d.value);
})
.attr("width", xScale.rangeBand())
.attr("height", function(d) {
return yScale(d.value);
})
.attr("fill", function(d) {
return "rgb(0, 0, " + (d.value * 10) + ")";
})



//Create labels
svg.selectAll("text")
.data(dataset, key)
.enter()
.append("text")
.text(function(d) {
return d.value;
})
.attr("text-anchor", "middle")
.attr("x", function(d, i) {
return xScale(i) + xScale.rangeBand() / 2;
})
.attr("y", function(d) {
return h - yScale(d.value) + 14;
})
.attr("font-family", "sans-serif")
.attr("font-size", "11px")
.attr("fill", "white");

}

更新:

@Component({
selector: "homeTest",
properties: [ 'data' ]
})
@View({
directives: [CORE_DIRECTIVES, BarGraph],

template: `
<h1 class="title">Angular 2 + d3</h1>
<bar-graph [graphdata]="dataset">
</bar-graph>
`

})

export class HomeTest {
dataset: Array<Object> = [
{ key: 0, value: 5 },
{ key: 1, value: 10 }
];

constructor() {}

条形图:

@Directive({
selector: 'bar-graph',

})
export class BarGraph {
@Input() graphdata;
data: Array<number>;
divs: any;
dataset: any;
constructor(
@Inject(ElementRef) elementRef: ElementRef,
@Attribute('width') width: string,
@Attribute('height') height: string) {

var w = 600;
var h = 250;

var dataset = this.graphdata; //graphData undefined

最佳答案

你应该通过元素的属性传递数据

<bar-graph [graphdata]="dataset">

所以你在父类中需要这个

export class HomeTest {
dataset: Array<Object> = [
{ key: 0, value: 5 },
{ key: 1, value: 10 }
];

然后在组件本身中获取它

class BarGraph {
@Input() graphdata;
constructor() {}
afterViewInit() {
var dataset = this.graphdata;
}

关于javascript - Angular2 将值传递给其他类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33736145/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com