如何在 chartjs 中显示 y 轴上的条形图

how to show bar graph on y axis in charjs

我正在使用使用 chartjs 的 ng2-charts。 Angular 版本为 8.2.14。我必须显示条形图,但它们必须从 Y 轴而不是 X 轴开始。意味着图表应该从 y 轴增长。有什么方法可以实现?

您必须在配置选项中将图表的 type 定义为 horizontalBar

var myBarChart = new Chart(ctx, {
    type: 'horizontalBar',
    data: data,
    options: options
});

或 ng2-charts 的 TypeScript:

public barChartType: ChartType = 'horizontalBar';

谨记

The configuration options for the horizontal bar chart are the same as for the bar chart.
However, any options specified on the x axis in a bar chart, are applied to the y axis in a horizontal bar chart.

这是 Stackblitz 上的一个例子: https://stackblitz.com/edit/ng2-charts-bar-template-vqunbc

export class AppComponent  {
  public barChartOptions: ChartOptions = {
    responsive: true,
  };
  public barChartLabels: Label[] = ['2006', '2007', '2008', '2009', '2010', '2011', '2012'];
  public barChartType: ChartType = 'horizontalBar';
  public barChartLegend = true;
  public barChartPlugins = [];

  public barChartData: ChartDataSets[] = [
    { data: [65, 59, 80, 81, 56, 55, 40], label: 'Series A' },
    { data: [28, 48, 40, 19, 86, 27, 90], label: 'Series B' }
  ];

  constructor() { }

  ngOnInit() {
  }
}

参考Chart.js文档:https://www.chartjs.org/docs/latest/charts/bar.html#horizontal-bar-chart