如何写精华回答,获更多曝光?
发布
我最开始没想定义class来做假数据,我想用Record来直接造假数据,但是好像是方式不对,没造出来~
然后就是更改数组中某个字段的值,原API9中是正常执行的,但是API11中原来的方法就会报错,大家有空帮我看看应该怎么修改~感谢
class testModel {
time: number
value: number
constructor(time: number, value: number) {
this.time = time
this.value = value
}
}
const testData: testModel[] = [
new testModel(1010, 1),
new testModel(1012, 5),
new testModel(1013, 9),
new testModel(1014, 6),
new testModel(1015, 2)
]
@Entry
@Component
struct CanvasIndex {
@State LineArray: testModel[] = testData
@State yScale: number = 1
aboutToAppear(): void {
let maxValue = Math.max(...this.LineArray.map(item => item.value));
let minValue = Math.min(...this.LineArray.map(item => item.value));
this.yScale = 300 / (maxValue - minValue) //y轴值换算的比例
//按照比例,更新数据中的value值
this.LineArray = this.LineArray.map(item => ({ ...item, value: (item.value - minValue) * this.yScale }));
}
build() {
Column() {
ForEach(this.LineArray, (item: testModel) => {
Text(item.value.toString())
}, (item: testModel) => JSON.stringify(item))
}
}
}