ngx-admin/src/app/pages/charts/chartjs/chartjs-bar-horizontal.component.ts

113 lines
2.9 KiB
TypeScript
Raw Normal View History

import { Component } from '@angular/core';
import { NbThemeService } from '@nebular/theme';
@Component({
selector: 'ngx-chartjs-bar-horizontal',
2017-07-27 12:06:50 +03:00
styles: [
`
:host {
display: block;
}
`,
],
template: `
<canvas baseChart
[datasets]="chartData"
[labels]="chartLabels"
[options]="chartOptions"
[legend]="chartLegend"
[chartType]="chartType"></canvas>
`,
})
export class ChartjsBarHorizontalComponent {
chartData: any[];
2017-07-27 12:06:50 +03:00
chartLabels = ['January', 'February', 'March', 'April', 'May', 'June', 'July'];
chartLegend: boolean = true;
chartType: string = 'horizontalBar';
chartOptions: any;
constructor(private theme: NbThemeService) {
this.theme.getJsTheme().subscribe(config => {
2017-08-01 15:42:06 +03:00
const chartjs: any = config.variables.chartjs;
this.chartData = [
{
label: 'Dataset 1',
2017-08-01 15:42:06 +03:00
backgroundColor: chartjs.barHorizontal.colors[0],
borderColor: chartjs.barHorizontal.colors[0],
borderWidth: 1,
data: [
this.randomScalingFactor(),
this.randomScalingFactor(),
this.randomScalingFactor(),
this.randomScalingFactor(),
this.randomScalingFactor(),
this.randomScalingFactor(),
this.randomScalingFactor(),
],
},
{
label: 'Dataset 2',
2017-08-01 15:42:06 +03:00
backgroundColor: chartjs.barHorizontal.colors[1],
borderColor: chartjs.barHorizontal.colors[1],
data: [
this.randomScalingFactor(),
this.randomScalingFactor(),
this.randomScalingFactor(),
this.randomScalingFactor(),
this.randomScalingFactor(),
this.randomScalingFactor(),
this.randomScalingFactor(),
],
},
];
this.chartOptions = {
// Elements options apply to all of the options unless overridden in a dataset
// In this case, we are setting the border of each horizontal bar to be 2px wide
elements: {
rectangle: {
borderWidth: 2,
},
},
responsive: true,
scales: {
xAxes: [
{
gridLines: {
display: true,
2017-08-01 15:42:06 +03:00
color: chartjs.xAxisColor,
},
ticks: {
2017-08-01 15:42:06 +03:00
fontColor: chartjs.tickColor,
},
},
],
yAxes: [
{
gridLines: {
display: true,
2017-08-01 15:42:06 +03:00
color: chartjs.yAxisColor,
},
ticks: {
2017-08-01 15:42:06 +03:00
fontColor: chartjs.tickColor,
},
},
],
},
legend: {
position: 'right',
labels: {
2017-08-01 15:42:06 +03:00
fontColor: chartjs.legendTextColor,
},
},
};
});
}
2017-07-27 12:06:50 +03:00
private randomScalingFactor() {
return Math.round(Math.random() * 100);
}
}