typescript - 如何阻止 ngx-chart 将小数添加到 x 轴
问题描述
我使用 ngx-chart 创建了一个图表。x 轴应填充年份。我的年份数组目前有值:2020、2021、2022、2023。但是,当它显示在图表中时,它会自动添加 2020.5、2021.5、...
该值必须是数字才能按升序对年份进行排序。有没有办法防止小数自动生成?
打字稿:
setChartValue(items: any[]): void {
let chartValues = [];
items.forEach((item) => {
chartValues.push({
'name': moment(item.purchaseDate, "DD/MM/YYYY").year(),
'value': item.purchasePrice
});
})
this.multi = [
{
'name': 'Purchase Summary',
'series': chartValues
}
];
}
html:
<ngx-charts-line-chart [view]="view"
[scheme]="colorScheme"
[results]="multi"
[gradient]="gradient"
[xAxis]="showXAxis"
[yAxis]="showYAxis"
[legend]="showLegend"
[showXAxisLabel]="showXAxisLabel"
[showYAxisLabel]="showYAxisLabel"
[xAxisLabel]="xAxisLabel"
[yAxisLabel]="yAxisLabel"
[autoScale]="autoScale"
[timeline]="timeline">
</ngx-charts-line-chart>
解决方案
我在 y 轴上有类似的情况,可能会有所帮助。在图表的 html 中,我添加了 [yAxisTickFormatting]="yAxisTickFormattingFn"
在我添加的类定义中: public yAxisTickFormattingFn = this.yAxisTickFormatting.bind(this);
yAxisTickFormatting(value) {
return value;
}
我不知道为什么这会改变任何东西,但它删除了 y 轴值的“.0”。
推荐阅读
- python - 使用漂亮的汤进行网络抓取会给出不准确的结果
- entity-framework - 实体框架 6:“清除”旧迁移的资源以减小程序集大小是否安全?
- git - 是否有一个 gitkraken 功能可以显示背后有多少提交?前面一个分支与另一个分支进行比较?
- laravel - “错误”:“invalid_grant” Laravel 7 护照
- flutter - 模型类中的属性更改后更新 UI
- amazon-web-services - 数据管道(DynamoDB 到 S3) - 如何格式化 S3 文件?
- c# - 使用嵌套对象键生成 JSON
- excel - 在 Excel 中的 2 个团队之间平均分配球员
- azure - 无法使用“客户”托管加密密钥选项在 Azure 认知搜索中创建索引/同义词映射
- xml - 使用 xslt1.0 基于输入 xml 动态构建节点集