2017-07-26 19:59:47 +03:00
|
|
|
import { Component } from '@angular/core';
|
2017-07-28 16:09:02 +03:00
|
|
|
import { NgaThemeService } from '@akveo/nga-theme';
|
2017-07-26 19:59:47 +03:00
|
|
|
|
|
|
|
|
@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>
|
|
|
|
|
`,
|
2017-07-26 19:59:47 +03:00
|
|
|
})
|
|
|
|
|
export class ChartjsBarHorizontalComponent {
|
2017-07-28 16:09:02 +03:00
|
|
|
chartData: any[];
|
2017-07-27 12:06:50 +03:00
|
|
|
chartLabels = ['January', 'February', 'March', 'April', 'May', 'June', 'July'];
|
|
|
|
|
chartLegend: boolean = true;
|
|
|
|
|
chartType: string = 'horizontalBar';
|
2017-07-28 16:09:02 +03:00
|
|
|
chartOptions: any;
|
|
|
|
|
|
|
|
|
|
constructor(private theme: NgaThemeService) {
|
|
|
|
|
this.theme.getJsTheme().subscribe(config => {
|
|
|
|
|
this.chartData = [
|
|
|
|
|
{
|
|
|
|
|
label: 'Dataset 1',
|
|
|
|
|
backgroundColor: config.chartjsBarHorizontalColor1,
|
|
|
|
|
borderColor: config.chartjsBarHorizontalColor1,
|
|
|
|
|
borderWidth: 1,
|
|
|
|
|
data: [
|
|
|
|
|
this.randomScalingFactor(),
|
|
|
|
|
this.randomScalingFactor(),
|
|
|
|
|
this.randomScalingFactor(),
|
|
|
|
|
this.randomScalingFactor(),
|
|
|
|
|
this.randomScalingFactor(),
|
|
|
|
|
this.randomScalingFactor(),
|
|
|
|
|
this.randomScalingFactor(),
|
|
|
|
|
],
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
label: 'Dataset 2',
|
|
|
|
|
backgroundColor: config.chartjsBarHorizontalColor2,
|
|
|
|
|
borderColor: config.chartjsBarHorizontalColor2,
|
|
|
|
|
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,
|
|
|
|
|
color: config.chartjsBarHorizontalXAxisColor,
|
|
|
|
|
},
|
|
|
|
|
ticks: {
|
|
|
|
|
fontColor: config.chartjsBarHorizontalTickColor,
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
],
|
|
|
|
|
yAxes: [
|
|
|
|
|
{
|
|
|
|
|
gridLines: {
|
|
|
|
|
display: true,
|
|
|
|
|
color: config.chartjsBarHorizontalYAxisColor,
|
|
|
|
|
},
|
|
|
|
|
ticks: {
|
|
|
|
|
fontColor: config.chartjsBarHorizontalTickColor,
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
],
|
|
|
|
|
},
|
|
|
|
|
legend: {
|
|
|
|
|
position: 'right',
|
|
|
|
|
labels: {
|
|
|
|
|
fontColor: config.chartjsBarHorizontalLegendTextColor,
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
};
|
|
|
|
|
});
|
|
|
|
|
}
|
2017-07-27 12:06:50 +03:00
|
|
|
|
|
|
|
|
private randomScalingFactor() {
|
|
|
|
|
return Math.round(Math.random() * 100);
|
|
|
|
|
}
|
2017-07-26 19:59:47 +03:00
|
|
|
}
|