gpt4 book ai didi

angular - amcharts 堆叠条形图 - 突出显示值(更改 alpha)以响应事件

转载 作者:搜寻专家 更新时间:2023-10-30 21:21:15 25 4
gpt4 key购买 nike

我有一个用 amcharts 构建的条形图.我按照 this example 构建了堆积条形图.

如果我将鼠标悬停在页面中的某个其他元素(例如表格)上,我正在尝试更改框的 alpha(或颜色)。例如,如果用户将鼠标悬停在我页面某处的相应表格单元格上,我想更改框的 alpha(或颜色)(2003 年,欧洲)。

我已将我的图表放在一个 Angular 为 6 的组件中,但我想这并不重要,只要我正确捕获输入更改事件即可。


编辑 1.

我仍在努力导航这些对象。我构建了一个条形图,它看起来不错

  createSeries(field, name) {
var series = this.chart.series.push(new am4charts.ColumnSeries());
series.name = name;
series.dataFields.valueY = field;
series.dataFields.categoryX = "country";
series.sequencedInterpolation = true;
series.columns.template.fillOpacity = 0.5

// Make it stacked
series.stacked = true;

// Configure columns
series.columns.template.width = am4core.percent(60);
series.columns.template.tooltipText = "[bold]{name}[/]\n[font-size:14px]{categoryX}: {valueY}";
console.log(series)
return series
}

然后我有四个列系列,一个用于我数据中的每个字段。我可以按领域扩展系列。

  grabBar(country, field) {
// If you don't have access to the chart code in your angular scope,
// use am4core global.
// no reason to loop through ALL series/columns, just whipped this up quick
let foundBar: am4charts.XYSeries | null;
this.chart.series.each(series => {
if (series.dataFields.categoryY == field) {
console.log("yay")
series.dataItems.values
}
});
return foundBar;

但我想不出一种方法来访问对应于国家和特定字段的元素。我看到的是四个,每个字段一个,ColumnSeries 没有任何 columns 属性。我的 am4core.registry.baseSprites[0] 里面没有 series

这是我的完整代码:

import { Component, OnInit, NgZone, Input } from '@angular/core';
import * as am4core from "@amcharts/amcharts4/core";
import * as am4charts from "@amcharts/amcharts4/charts";
import am4themes_animated from "@amcharts/amcharts4/themes/animated";
import { CountryItinerary, Team } from "../model/classes"

am4core.useTheme(am4themes_animated);



@Component({
selector: 'barchart-distance',
templateUrl: './barchart-distance.component.html',
styleUrls: ['./barchart-distance.component.scss']
})
export class BarchartDistanceComponent implements OnInit {

constructor(private zone: NgZone) { }
_selectedTeam: Team | null

@Input()
set selectedTeam(team: Team | null) {
this._selectedTeam = team;
if (team) {
this.changeColorToBar(team.isocode, "week_1", 1.0)
}
}

chart: am4charts.XYChart
categoryAxis: any
valueAxis: any

ngOnInit() {

}

grabBar(country, weekId) {
// If you don't have access to the chart code in your angular scope,
// use am4core global.
// no reason to loop through ALL series/columns, just whipped this up quick
let foundBar: am4charts.XYSeries | null;
this.chart.series.each(series => {
if (series.name == "Week_2") {
console.log(series.dataItem)
}

});
return foundBar;
}

changeColorToBar(country, weekId, opacity) {
let bar = this.grabBar(country, weekId)
if (bar) {
bar.background.fillOpacity = opacity
}
}

createSeries(field, name) {
var series = this.chart.series.push(new am4charts.ColumnSeries());
series.name = name;
series.dataFields.valueY = field;
series.dataFields.categoryX = "country";
series.sequencedInterpolation = true;
series.columns.template.fillOpacity = 0.5

// Make it stacked
series.stacked = true;

// Configure columns
series.columns.template.width = am4core.percent(60);
series.columns.template.tooltipText = "[bold]{name}[/]\n[font-size:14px]{categoryX}: {valueY}";
console.log(series)
return series
}

drawWithData() {
this.zone.runOutsideAngular(() => {
this.chart = am4core.create("chartdiv2", am4charts.XYChart);
let data = [
new CountryItinerary("Italy", "IT", [10, 15, 15, 22]),
new CountryItinerary("Germany", "DE", [20, 30, 40, 24]),
new CountryItinerary("Greece", "GR", [13, 10, 20, 15])
]

this.chart.data = data.map ((el) => {
let newEl = { "country": el.isocode }
el.distances.forEach((item, index) => {
newEl["week_" + index] = item
})
return newEl
})
console.log(this.chart.data)

this.categoryAxis = this.chart.xAxes.push(new am4charts.CategoryAxis());
this.categoryAxis.dataFields.category = "country";
this.categoryAxis.renderer.grid.template.location = 0;

this.valueAxis = this.chart.yAxes.push(new am4charts.ValueAxis());
this.valueAxis.renderer.inside = true;
this.valueAxis.renderer.labels.template.disabled = true;
this.valueAxis.min = 0;

let numberOfTrips = data[0].distances.length
for (let i = 0; i < numberOfTrips; i++) {
this.createSeries("week_" + i, "Week_" + i)
}
console.log(this.chart.series)


})
}

ngAfterViewInit() {
this.drawWithData()
}

}

在那里,series.dataItem.dataContext 是未定义的。

最佳答案

(在他提供了实际的 Angular 代码之后添加了一个不同的答案,以及他遇到的问题,我之前的答案似乎不太相关。)

我将自己的方法作为 Input() 传递给了表格组件,但确保使用了箭头函数,因此它保留了该图表组件的范围:

  selectTeam = (team, weekN) => {
this._selectedTeam = team;
if (team) {
this.changeColorToBar(team.isocode, weekN, 1.0);
}
}

至于列本身,由于您要更改的属性的值也可能会更改,因此使用状态并实际直接在对象上调整属性可能意义不大。在这种情况下,您可以直接更改 fillOpacity,而不是在列的 background 上:

  changeColorToBar(country, weekId, opacity) {
let bar = this.grabBar(country, weekId);
if (bar && bar !== this.currentBar) {
if (this.currentBar) this.currentBar.fillOpacity = 0.5; // Default
bar.fillOpacity = opacity;
this.currentBar = bar;
}
}

我很容易找到图表、它的系列和列。时间当然可能是个问题,即图表和系列必须实际加载,但这可能会在用户悬停时发生:

  grabBar(country, weekId = 0) {
const totalSeries = this.chart.series.length;
for (let i = 0; i < totalSeries; ++i) {
const series = this.chart.series.getIndex(i);
if (series.name === "Week_" + weekId) {
const totalColumns = series.columns.length;
for (let c = 0; c < totalColumns; ++c) {
const column = series.columns.getIndex(c);
if (
column.dataItem.dataContext.country === country
) {
return column;
}
}
}
}
}

这是一个演示:

https://stackblitz.com/edit/so-55597531-table-input-chart

如果您仍然无法在系列中找到专栏等等,也许可以在 StackBlitz 上发布您的项目示例,以便我们尝试查看哪里出了问题。

关于angular - amcharts 堆叠条形图 - 突出显示值(更改 alpha)以响应事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55597531/

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