【发布时间】:2019-10-11 21:14:26
【问题描述】:
我正在尝试创建一个通用的 ng2-charts (Bar Stacked) 组件,我可以在其中传递来自其他组件的数据并且它会得到更新。 实际上,我想在加载页面时在同一页面中显示多个具有不同值的堆叠条形图。
我用 ng-2 图表创建了一个通用组件,还创建了一项服务。现在我正在通过具有不同参数的共享服务从另一个不同的组件调用公共组件,但它始终显示第一个组件值。
我的第一个组件(bar-chart.component.ts),
import { Component, OnInit } from "@angular/core";
import { ChartOptions, ChartType, ChartDataSets } from "chart.js";
import * as pluginDataLabels from "chartjs-plugin-datalabels";
import { Label } from "ng2-charts";
import { BarChartService } from "../service/bar-chart.service";
@Component({
selector: "app-bar-chart",
templateUrl: "./bar-chart.component.html",
styleUrls: ["./bar-chart.component.css"]
})
export class BarChartComponent implements OnInit {
public barChartOptions: ChartOptions = {
responsive: true,
// We use these empty structures as placeholders for dynamic theming.
scales: {
xAxes: [
{
stacked: true
}
],
yAxes: [
{
stacked: true
}
]
},
plugins: {
datalabels: {
anchor: "end",
align: "end"
}
}
};
public barChartLabels: Label[] = [
"2006",
"2007",
"2008",
"2009",
"2010",
"2011",
"2012"
];
public barChartType: ChartType = "bar";
public barChartLegend = true;
public barChartPlugins = [pluginDataLabels];
public barChartData: ChartDataSets[] = [];
constructor(private barchartservice: BarChartService) {}
ngOnInit() {
this.barChartData = this.barchartservice.getBarChartData("one");
}
// events
public chartClicked({
event,
active
}: {
event: MouseEvent;
active: {}[];
}): void {
console.log(event, active);
}
public chartHovered({
event,
active
}: {
event: MouseEvent;
active: {}[];
}): void {
console.log(event, active);
}
public randomize(): void {
// Only Change 3 values
const data = [
Math.round(Math.random() * 100),
59,
80,
Math.random() * 100,
56,
Math.random() * 100,
40
];
this.barChartData[0].data = data;
}
}
在第一个 bar-chart.component.html 中,
<div style="display: block">
<canvas
baseChart
[datasets]="barChartData"
[labels]="barChartLabels"
[options]="barChartOptions"
[plugins]="barChartPlugins"
[legend]="barChartLegend"
[chartType]="barChartType"
>
</canvas>
</div>
在第二个bar-chart.component.ts,
import { Component, OnInit } from "@angular/core";
import { ChartOptions, ChartType, ChartDataSets } from "chart.js";
import * as pluginDataLabels from "chartjs-plugin-datalabels";
import { Label } from "ng2-charts";
import { BarChartService } from "../service/bar-chart.service";
@Component({
selector: "app-second-bar-chart",
templateUrl: "./second-bar-chart.component.html",
styleUrls: ["./second-bar-chart.component.css"]
})
export class SecondBarChartComponent implements OnInit {
public barChartData: ChartDataSets[] = [];
constructor(private barchartservice: BarChartService) {}
ngOnInit() {
this.barChartData = this.barchartservice.getBarChartData("two");
}
}
在第二条形图.component.html中,
<h3>This is 2nd Bar Chart</h3>
<app-bar-chart></app-bar-chart>
在服务(bar-chart.service.ts)中,我是这样写的,
import { Injectable } from "@angular/core";
@Injectable({
providedIn: "root"
})
export class BarChartService {
constructor() {}
getBarChartData(chartType) {
if (chartType == "one") {
return [
{ data: [65, 59, 80, 81, 56, 55, 40], label: "Series A", stack: "1" },
{ data: [28, 48, 40, 19, 86, 27, 90], label: "Series B", stack: "1" }
];
} else {
return [
{ data: [40, 59], label: "Male", stack: "1" },
{ data: [90, 48], label: "Female", stack: "1" }
];
}
}
}
我期待两个不同的图表值,但在这两个组件中,它只显示第一个组件图表值。
请帮帮我。
【问题讨论】:
-
那么第一个组件在第二个组件的html中?
-
包含图表画布的第一个组件 html,我想将此第一个组件 html 画布用作具有不同值的通用 html。我已经更新了帖子。请检查一次。
-
所以条形图中唯一的数据发生了变化,一切都保持不变?
-
是的,你是对的。
-
如果您也发布完整的 first.component.ts 那就太好了
标签: angular ng2-charts