我正在使用使用chartjs的ng2-charts。角版本为8.2.14。我必须显示条形图,但它们必须从Y轴而不是X轴开始。表示图应从y轴开始增长。实现此目的的方法是什么?
答案 0 :(得分:1)
您必须在配置选项中将图表的type
定义为horizontalBar
:
var myBarChart = new Chart(ctx, {
type: 'horizontalBar',
data: data,
options: options
});
或用于ng2-charts的TypeScript:
public barChartType: ChartType = 'horizontalBar';
记住
水平条形图的配置选项与条形图相同。
但是,在条形图的x轴上指定的所有选项都将应用于水平条形图的y轴。
这是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