gpt4 book ai didi

angular - 未定义列表对象的属性切片

转载 作者:行者123 更新时间:2023-12-04 07:22:09 25 4
gpt4 key购买 nike

我在 csv 中有以下数据我的 Angular 项目中的文件也导入了 D3.js图书馆:

group,Nitrogen,normal,stress
banana,12,1,13
poacee,6,6,33
sorgho,11,28,12
triticum,19,6,1
我创建了一个 stacked-bar在其 html 中包含以下代码的组件脚本:
<h3>Stacked Bar Chart</h3>
<figure id="stacked-bar"></figure>
我在 typescript 文件中还有以下代码来显示堆叠条:
import { Component, OnInit } from '@angular/core';
import * as d3 from 'd3';

@Component({
selector: 'app-stacked-bar',
templateUrl: './stacked-bar.component.html',
styleUrls: ['./stacked-bar.component.css']
})
export class StackedBarComponent implements OnInit {

data = [
{"group": "banana", "Nitrogen": "12", "normal": "1", "stress": "13"},
{"group": "poacee", "Nitrogen": "6", "normal": "6", "stress": "33"},
{"group": "sorgho", "Nitrogen": "11", "normal": "28", "stress": "12"},
{"group": "triticum", "Nitrogen": "19", "normal": "6", "stress": "1"}
];

svg: any;

margin = 50;
width = 750 - (this.margin * 2);
height = 400 - (this.margin * 2);

ngOnInit(): void {

this.createSvg();
this.drawBars(this.data);
}

createSvg(): void {

this.svg = d3.select("figure#stacked-bar")
.append("svg")
.attr("width", this.width + (this.margin * 2))
.attr("height", this.height + (this.margin * 2))
.append("g")
.attr("transform", "translate(" + this.margin + "," + this.margin + ")");
}

drawBars(data): void {

// List of subgroups - Header of the csv file - Here, soil condition.
const subgroups = data.columns.slice(1);

// List of groups - Value of the first column called group - Shown on the X axis - Here, species.
const groups = data.map(d => (d.group));

// Create the X-axis band scale.
const x = d3.scaleBand()
.domain(groups)
.range([0, this.width])
//.domain(data.map(d => d.groups))
.padding(0.2);

// Draw the X-axis on the DOM.
this.svg.append("g")
.attr("transform", "translate(0," + this.height + ")")
.call(d3.axisBottom(x).tickSizeOuter(0));

// Create the Y-axis band scale.
const y = d3.scaleLinear()
.domain([0, 60])
.range([this.height, 0]);

// Draw the Y-axis on the DOM.
this.svg.append("g")
.call(d3.axisLeft(y));

// color palette = one color per subgroup
const color = d3.scaleOrdinal()
.domain(subgroups)
.range(['#e41a1c','#377eb8','#4daf4a']);

// Stack the data per subgroup.
const stackedData = d3.stack()
.keys(subgroups)
(data);

// Create and fill the bars.
this.svg.append("g")
.selectAll("g")
.data(stackedData)
.join("g")
.attr("fill", d => color(d.key))
.selectAll("rect")
.data(d => d)
.join("rect")
.attr("x", d => x(d.data.group))
.attr("y", d => y(d[1]))
.attr("height", d => y(d[0]) - y(d[1]))
.attr("width", x.bandwidth());
}
}
但我只得到一个空的情节!当我检查页面时,出现此控制台错误:
错误类型错误:无法读取未定义的属性“切片”。
使用 console.log(this.data)我意识到我的 data参数有一个 any类型,所以 data.columns.slice(1)未定义。
我还没有完全理解 TypeScript 中这些类型的复杂性,所以我请求有人帮我解决这个问题。
我认为代码应该可以工作并显示一个堆叠条,但我所需要的可能只是解决这个问题并更改代码中的某些数据类型。
有人可以帮我解决这个问题吗?我已经被困了几天了......

最佳答案

该错误与您的类型无关。这是一个运行时错误,因为 data.columnsundefined .您的 data属性不包含 columns属性,因为它是一个数组。只需将其更改为 data.slice(1)它会起作用。

关于angular - 未定义列表对象的属性切片,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68426720/

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