ng2 charts条形图需要2个连续条之间的间距Angular

bakd9h0s  于 2023-11-18  发布在  Chart.js
关注(0)|答案(2)|浏览(182)

我正在使用ng 2-charts开发Angular应用程序。我试图在一个条形图中显示两个系列数据。我能够成功地做到这一点,但我的条形图彼此粘连。我希望两个条形图之间有空间。
x1c 0d1x的数据
如果我从数据中删除酒吧的厚度,它会自动占用酒吧之间的空间,但酒吧是如此之厚,这不是按照我的设计。我试图实现像https://appstack.bootlab.io/charts-chartjs.html这个网站的东西。
下面是我的代码。

chart.html

  1. <div style="display: block;">
  2. <canvas baseChart
  3. [datasets]="barChartData"
  4. [colors] = "barChartColors"
  5. [labels]="barChartLabels"
  6. [options]="barChartOptions"
  7. [plugins]="barChartPlugins"
  8. [legend]="barChartLegend"
  9. [chartType]="barChartType">
  10. </canvas>
  11. </div>

字符串

Chart.ts

  1. public barChartOptions: ChartOptions = {
  2. scales: {
  3. xAxes: [
  4. {
  5. gridLines: {
  6. display: false
  7. }
  8. }
  9. ],
  10. yAxes: [
  11. {
  12. gridLines: {
  13. display: false
  14. },
  15. ticks: {
  16. beginAtZero: true
  17. }
  18. }
  19. ]
  20. },
  21. responsive: true,
  22. cornerRadius: 100,
  23. plugins: {
  24. labels: {
  25. render: 'value'
  26. }
  27. },
  28. legend: {
  29. position: 'bottom',
  30. labels: {
  31. fontColor: 'black',
  32. boxWidth: 20,
  33. padding: 20,
  34. fontFamily: 'Poppins',
  35. fontSize: 13
  36. }
  37. },
  38. animation: {
  39. animateScale: true,
  40. animateRotate: true
  41. }
  42. };
  43. public barChartLabels: Label[] = ['2006', '2007', '2008', '2009', '2010', '2011', '2012'];
  44. public barChartType: ChartType = 'bar';
  45. public barChartLegend = true;
  46. public barChartPlugins = [];
  47. public barChartColors = [{
  48. backgroundColor: '#3f80ea',
  49. borderColor: '#3f80ea',
  50. pointBackgroundColor: 'rgba(148,159,177,1)',
  51. pointBorderColor: '#fff',
  52. pointHoverBackgroundColor: '#fff',
  53. pointHoverBorderColor: 'rgba(148,159,177,0.8)',
  54. borderWidth: 3
  55. }]
  56. public barChartData: ChartDataSets[] = [
  57. { data: [65, 59, 80, 81, 56, 55, 40], label: 'Series A', barThickness :10 },
  58. { data: [28, 48, 40, 19, 86, 27, 90], label: 'Series B', barThickness :10}
  59. ];


任何帮助将高度赞赏,因为我正在努力我的水平最好作为初学者通过这一点

thtygnil

thtygnil1#

如果我理解你的意思,你希望条形图的每一部分都加倍,也就是说给你两段数据
enter image description here
现在你只需要像这样写音阶部分

  1. scales: {
  2. x: {
  3. stacked: true,
  4. },
  5. y: {
  6. stacked: true,
  7. }
  8. },

字符串

  1. import { Component, ViewChild } from '@angular/core';
  2. import { ChartConfiguration, ChartData, ChartEvent, ChartType } from 'chart.js';
  3. import { BaseChartDirective } from 'ng2-charts';
  4. import DataLabelsPlugin from 'chartjs-plugin-datalabels';
  5. @Component({
  6. selector: 'app-root',
  7. templateUrl: './app.component.html',
  8. styleUrls: ['./app.component.scss']
  9. })
  10. export class AppComponent {
  11. @ViewChild(BaseChartDirective) chart: BaseChartDirective | undefined;
  12. public barChartOptions: ChartConfiguration['options'] = {
  13. responsive: true,
  14. // We use these empty structures as placeholders for dynamic theming.
  15. // scales: {
  16. // x: {},
  17. // y: {
  18. // min: 10
  19. // }
  20. // },
  21. scales: {
  22. x: {
  23. stacked: true,
  24. },
  25. y: {
  26. stacked: true,
  27. }
  28. },
  29. plugins: {
  30. legend: {
  31. display: true,
  32. },
  33. datalabels: {
  34. anchor: 'end',
  35. align: 'end'
  36. }
  37. }
  38. };
  39. public barChartType: ChartType = 'bar';
  40. public barChartPlugins = [
  41. DataLabelsPlugin
  42. ];
  43. public barChartData: ChartData<'bar'> = {
  44. labels: ['2006', '2007', '2008', '2009', '2010', '2011', '2012'],
  45. datasets: [
  46. { data: [65, 59, 80, 81, 56, 55, 40], label: 'Series A' },
  47. { data: [28, 48, 40, 19, 86, 27, 90], label: 'Series B' }
  48. ]
  49. };
  50. // events
  51. public chartClicked({ event, active }: { event?: ChartEvent, active?: {}[] }): void {
  52. console.log(event, active);
  53. }
  54. public chartHovered({ event, active }: { event?: ChartEvent, active?: {}[] }): void {
  55. console.log(event, active);
  56. }
  57. public randomize(): void {
  58. // Only Change 3 values
  59. this.barChartData.datasets[0].data = [
  60. Math.round(Math.random() * 100),
  61. 59,
  62. 80,
  63. Math.round(Math.random() * 100),
  64. 56,
  65. Math.round(Math.random() * 100),
  66. 40];
  67. this.chart?.update();
  68. }
  69. }
  1. <div>
  2. <div>
  3. <div style="display: block">
  4. <canvas baseChart [data]="barChartData" [options]="barChartOptions" [plugins]="barChartPlugins" [type]="barChartType" (chartHover)="chartHovered($event)" (chartClick)="chartClicked($event)">
  5. </canvas>
  6. </div>
  7. <button mat-button mat-raised-button color="primary" (click)="randomize()">Update</button>
  8. </div>
  9. </div>

的数据

这真的不太可能,但我使用的是最新的Angular版本

展开查看全部
wooyq4lh

wooyq4lh2#

如果你想要一个空格-你可以添加另一个数据集,像这样:

  1. public barChartData: ChartData<'bar'> = {
  2. labels: ['2006', '2007', '2008', '2009', '2010', '2011', '2012'],
  3. datasets: [
  4. { data: [65, 59, 80, 81, 56, 55, 40], label: 'Series A' },
  5. { data: [0, 0, 0, 0, 0, 0, 0], label: '' },
  6. { data: [28, 48, 40, 19, 86, 27, 90], label: 'Series B' }
  7. ]
  8. };

字符串

相关问题