Chart.js.json文件中数据的Angular 误差

r8xiu3jd  于 2023-03-18  发布在  Chart.js
关注(0)|答案(2)|浏览(173)

我有一个简单的数据数组,我想使用chart.js库在图表中显示它。唯一的错误似乎是一些DeepPartialObject引用了图表选项中的一些问题(y轴和x轴)。我是Angular和Chart.js的新手,一直在努力解决这个问题。
我当前的代码如下所示:

  1. import { Chart } from 'chart.js/auto';
  2. import { Component, OnInit } from '@angular/core';
  3. import { Fruit } from './fruit.model';
  4. import { FruitService } from './fruit.service';
  5. @Component({
  6. selector: 'app-root',
  7. templateUrl: './app.component.html',
  8. styleUrls: ['./app.component.css']
  9. })
  10. export class AppComponent implements OnInit {
  11. title = 'angular-fetch-json-data';
  12. public fruits: Fruit[];
  13. public chart: any;
  14. public constructor(private fruitService: FruitService) { }
  15. public ngOnInit(): void {
  16. this.fruitService.getFruits().subscribe((response) => {
  17. this.fruits = response.fruits;
  18. this.createChart();
  19. })
  20. }
  21. createChart() {
  22. if (this.chart) {
  23. this.chart.destroy();
  24. }
  25. // Extract the labels and data from the fruits array
  26. const labels = this.fruits.map((fruit: any) => fruit.fruit);
  27. const data = this.fruits.map((fruit: any) => fruit.amountBought);
  28. this.chart = new Chart('canvas', {
  29. type: 'line',
  30. data: {
  31. labels: labels,
  32. datasets: [
  33. {
  34. data: data,
  35. borderColor: '#3cba9f',
  36. fill: false
  37. },
  38. {
  39. data: data,
  40. borderColor: '#ffcc00',
  41. fill: false
  42. },
  43. ]
  44. },
  45. options: {
  46. legend: {
  47. display: false
  48. },
  49. scales: {
  50. xAxes: [{
  51. display: true
  52. }],
  53. yAxes: [{
  54. display: true,
  55. ticks: {
  56. beginAtZero: true
  57. }
  58. }]
  59. }
  60. }
  61. });
  62. }
  63. }
  64. data.json file:
  65. {
  66. "fruits": [
  67. {
  68. "fruit": "Apple",
  69. "size": "Large",
  70. "color": "Red",
  71. "amountBought": 100
  72. },
  73. {
  74. "fruit": "Banana",
  75. "size": "Small",
  76. "color": "Yellow",
  77. "amountBought": 76
  78. },
  79. {
  80. "fruit": "Strawberry",
  81. "size": "Medium",
  82. "color": "Red",
  83. "amountBought": 732
  84. },
  85. {
  86. "fruit": "Blueberry",
  87. "size": "Small",
  88. "color": "Blue",
  89. "amountBought": 27
  90. }
  91. ]
  92. }
  93. The issues are with Yaxes and xAxes, at least according to the compiler. I've tried numerous things with those, and also changing different stuff with the chart options, but can't seem to figure it out.
qeeaahzv

qeeaahzv1#

问题1

正如@Stefan提到的,由于可观察对象是异步的,因此在subscribe()回调中移动this.createChart()。通过移动到回调中,将保证this.createChart()在返回可观察对象时执行。

  1. this.fruitService.getFruits().subscribe((response) => {
  2. this.fruits = response.fruits;
  3. this.createChart();
  4. });

问题2

我认为您安装的 chart.js 版本为3或更高版本,这与您当前的Chart.js配置不兼容。根据文档,不再支持xAxes和yAxes。请按照迁移说明进行操作:

  • 删除了xAxesyAxes阵列,轴选项现在是按比例ID键控的单独比例
  • legendtitletooltip命名空间已从options移动到options.plugins

要从组件呈现图表(回写代码),您应该使用@ViewChild装饰器。
您的代码应该如下所示:
组件

  1. import { ElementRef, ViewChild } from '@angular/core';
  2. import { ChartOptions } from 'chart.js';
  3. @ViewChild('baseChart', { static: true })
  4. canvasElem: ElementRef<HTMLCanvasElement>;
  5. createChart() {
  6. if (this.chart) {
  7. this.chart.destroy();
  8. }
  9. // Extract the labels and data from the fruits array
  10. const labels = this.fruits.map((fruit: any) => fruit.fruit);
  11. const data = this.fruits.map((fruit: any) => fruit.amountBought);
  12. const ctx = this.chart.nativeElement.getContext('2d');
  13. this.chart = new Chart(ctx, {
  14. type: 'line',
  15. data: {
  16. labels: labels,
  17. datasets: [
  18. {
  19. data: data,
  20. borderColor: '#3cba9f',
  21. fill: false,
  22. },
  23. {
  24. data: data,
  25. borderColor: '#ffcc00',
  26. fill: false,
  27. },
  28. ],
  29. },
  30. options: {
  31. plugins: {
  32. legend: {
  33. display: false,
  34. },
  35. },
  36. scales: {
  37. x: {
  38. display: true,
  39. },
  40. y: {
  41. display: true,
  42. ticks: {
  43. beginAtZero: true,
  44. },
  45. },
  46. },
  47. } as ChartOptions<'line'>,
  48. });
  49. }

超文本标记语言

  1. <canvas #baseChart class="chart"></canvas>

Demo @ StackBlitz

展开查看全部
rryofs0p

rryofs0p2#

在你们的帮助下我完成了代码!非常感谢!下面是工作代码:

  1. import { Chart } from 'chart.js/auto';
  2. import { Component, OnInit } from '@angular/core';
  3. import { Fruit } from './fruit.model';
  4. import { FruitService } from './fruit.service';
  5. @Component({
  6. selector: 'app-root',
  7. templateUrl: './app.component.html',
  8. styleUrls: ['./app.component.css']
  9. })
  10. export class AppComponent implements OnInit{
  11. title = 'angular-fetch-json-data';
  12. public fruits: Fruit[];
  13. public chart: any;
  14. public constructor(private fruitService: FruitService){
  15. }
  16. public ngOnInit(): void {
  17. this.fruitService.getFruits().subscribe((response) => {
  18. this.fruits = response.fruits;
  19. this.createChart();
  20. })
  21. }
  22. createChart() {
  23. if (this.chart) {
  24. this.chart.destroy();
  25. }
  26. // Extract the labels and data from the fruits array
  27. const labels = this.fruits.map((fruit: any) => fruit.fruit);
  28. const data = this.fruits.map((fruit: any) => fruit.amountBought);
  29. this.chart = new Chart('canvas', {
  30. type: 'line',
  31. data: {
  32. labels: labels,
  33. datasets: [
  34. {
  35. data: data,
  36. borderColor: '#3cba9f',
  37. fill: false
  38. },
  39. ]
  40. },
  41. options: {
  42. scales: {
  43. xAxes: {
  44. display: true
  45. },
  46. yAxes: {
  47. display: true,
  48. beginAtZero: true
  49. }
  50. }
  51. }
  52. });
  53. }
  54. }
展开查看全部

相关问题