tyl*_*ler 2 javascript charts chart.js ng2-charts angular
我正在使用 ng2-charts 开发 Angular 应用程序。我试图在一个条形图中显示两个系列数据。我能够成功地做到这一点,但我的酒吧将两个彼此粘在一起。我想要两个酒吧之间有空间。
如果我从数据中删除条形厚度,它会自动占用条形之间的空间,但条形太厚,不符合我的设计。我正在尝试实现类似https://appstack.bootlab.io/charts-chartjs.html这个网站的目标。
下面是我的代码。
图表.html
<div style="display: block;">
<canvas baseChart
[datasets]="barChartData"
[colors] = "barChartColors"
[labels]="barChartLabels"
[options]="barChartOptions"
[plugins]="barChartPlugins"
[legend]="barChartLegend"
[chartType]="barChartType">
</canvas>
</div>
Run Code Online (Sandbox Code Playgroud)
图表.ts
public barChartOptions: ChartOptions = {
scales: {
xAxes: [
{
gridLines: {
display: false
}
}
],
yAxes: [
{
gridLines: {
display: false
},
ticks: {
beginAtZero: true
}
}
]
},
responsive: true,
cornerRadius: 100,
plugins: {
labels: {
render: 'value'
}
},
legend: {
position: 'bottom',
labels: {
fontColor: 'black',
boxWidth: 20,
padding: 20,
fontFamily: 'Poppins',
fontSize: 13
}
},
animation: {
animateScale: true,
animateRotate: true
}
};
public barChartLabels: Label[] = ['2006', '2007', '2008', '2009', '2010', '2011', '2012'];
public barChartType: ChartType = 'bar';
public barChartLegend = true;
public barChartPlugins = [];
public barChartColors = [{
backgroundColor: '#3f80ea',
borderColor: '#3f80ea',
pointBackgroundColor: 'rgba(148,159,177,1)',
pointBorderColor: '#fff',
pointHoverBackgroundColor: '#fff',
pointHoverBorderColor: 'rgba(148,159,177,0.8)',
borderWidth: 3
}]
public barChartData: ChartDataSets[] = [
{ data: [65, 59, 80, 81, 56, 55, 40], label: 'Series A', barThickness :10 },
{ data: [28, 48, 40, 19, 86, 27, 90], label: 'Series B', barThickness :10}
];
Run Code Online (Sandbox Code Playgroud)
任何帮助将不胜感激,因为我正在尽最大努力作为初学者来完成这个任务
如果我明白你的意思,你希望条形图的每个部分都加倍 这意味着向你显示两条数据
现在你只需要像这样编写尺度部分
scales: {
x: {
stacked: true,
},
y: {
stacked: true,
}
},
Run Code Online (Sandbox Code Playgroud)
scales: {
x: {
stacked: true,
},
y: {
stacked: true,
}
},
Run Code Online (Sandbox Code Playgroud)
import { Component, ViewChild } from '@angular/core';
import { ChartConfiguration, ChartData, ChartEvent, ChartType } from 'chart.js';
import { BaseChartDirective } from 'ng2-charts';
import DataLabelsPlugin from 'chartjs-plugin-datalabels';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.scss']
})
export class AppComponent {
@ViewChild(BaseChartDirective) chart: BaseChartDirective | undefined;
public barChartOptions: ChartConfiguration['options'] = {
responsive: true,
// We use these empty structures as placeholders for dynamic theming.
// scales: {
// x: {},
// y: {
// min: 10
// }
// },
scales: {
x: {
stacked: true,
},
y: {
stacked: true,
}
},
plugins: {
legend: {
display: true,
},
datalabels: {
anchor: 'end',
align: 'end'
}
}
};
public barChartType: ChartType = 'bar';
public barChartPlugins = [
DataLabelsPlugin
];
public barChartData: ChartData<'bar'> = {
labels: ['2006', '2007', '2008', '2009', '2010', '2011', '2012'],
datasets: [
{ data: [65, 59, 80, 81, 56, 55, 40], label: 'Series A' },
{ data: [28, 48, 40, 19, 86, 27, 90], label: 'Series B' }
]
};
// events
public chartClicked({ event, active }: { event?: ChartEvent, active?: {}[] }): void {
console.log(event, active);
}
public chartHovered({ event, active }: { event?: ChartEvent, active?: {}[] }): void {
console.log(event, active);
}
public randomize(): void {
// Only Change 3 values
this.barChartData.datasets[0].data = [
Math.round(Math.random() * 100),
59,
80,
Math.round(Math.random() * 100),
56,
Math.round(Math.random() * 100),
40];
this.chart?.update();
}
}Run Code Online (Sandbox Code Playgroud)
这确实不太可能,但我正在使用最新的 Angular 版本