Fix xAxes labels

#788
This commit is contained in:
mauriziovitale84 2016-11-03 17:00:17 +00:00
parent af760996d0
commit f08d5139a9

View File

@ -76,6 +76,8 @@ export class BarChart extends Chart {
labels: string[] = []; labels: string[] = [];
datasets: any[] = []; datasets: any[] = [];
data: any[] = []; data: any[] = [];
xAxisType: string;
yAxisType: string;
options: any = { options: any = {
scales: { scales: {
yAxes: [{ yAxes: [{
@ -83,6 +85,9 @@ export class BarChart extends Chart {
beginAtZero: true, beginAtZero: true,
stepSize: 1 stepSize: 1
} }
}],
xAxes: [{
ticks: {}
}] }]
} }
}; };
@ -91,6 +96,9 @@ export class BarChart extends Chart {
super(obj); super(obj);
this.title = obj && obj.title || null; this.title = obj && obj.title || null;
this.titleKey = obj && obj.titleKey || null; this.titleKey = obj && obj.titleKey || null;
this.xAxisType = obj && obj.xAxisType || null;
this.yAxisType = obj && obj.yAxisType || null;
this.options.scales.xAxes[0].ticks.callback = this.xAxisTickFormatFunction(this.xAxisType);
obj.values.forEach((params: any) => { obj.values.forEach((params: any) => {
let dataValue = []; let dataValue = [];
params.values.forEach((info: any) => { params.values.forEach((info: any) => {
@ -108,6 +116,18 @@ export class BarChart extends Chart {
}); });
} }
xAxisTickFormatFunction = function (xAxisType) {
return function (value) {
if ('date_day' === xAxisType) {
return moment(new Date(value)).format('DD');
} else if ('date_month' === xAxisType) {
return moment(new Date(value)).format('MMMM');
} else if ('date_year' === xAxisType) {
return moment(new Date(value)).format('YYYY');
}
};
};
hasDatasets() { hasDatasets() {
return this.datasets && this.datasets.length > 0 ? true : false; return this.datasets && this.datasets.length > 0 ? true : false;
} }