src/app/manage-learn/shared/components/bar-chart/bar-chart.component.ts
OnInit
selector | bar-chart |
styleUrls | ./bar-chart.component.scss |
templateUrl | ./bar-chart.component.html |
Properties |
|
Methods |
Inputs |
constructor()
|
data | |
Type : any
|
|
questionNumber | |
Type : any
|
|
ngOnInit |
ngOnInit()
|
Returns :
void
|
Public barChartData |
Type : ChartDataSets[]
|
Public barChartLabels |
Type : Label[]
|
Public barChartLegend |
Default value : false
|
Public barChartPlugins |
Public barChartType |
Type : ChartType
|
Default value : 'horizontalBar'
|
Public chartColors |
Type : Array<any>
|
import { Component, Input, OnInit } from '@angular/core';
import { ChartOptions, ChartType, ChartDataSets } from 'chart.js';
import { Label } from 'ng2-charts';
@Component({
selector: 'bar-chart',
templateUrl: './bar-chart.component.html',
styleUrls: ['./bar-chart.component.scss'],
})
export class BarChartComponent implements OnInit {
@Input() data;
@Input() questionNumber;
public barChartOptions: ChartOptions = {
responsive: true,
maintainAspectRatio: true,
scales: {
xAxes: [
{
ticks: {
min: 0, // Edit the value according to what you need,
max: 100,
},
scaleLabel: {
display: true,
labelString: 'Response in %',
},
},
],
yAxes: [
{
ticks: {
callback: function (value: any, index, values) {
// // return createSubstrArr(value, 5) || value;
let tempString :any= '';
let result:any = [];
if (value.length >45) {
value = value.substring(0, 45) + '...';
}
let strArr = value.split(' ');
for (let x = 0; x < strArr.length; x++) {
tempString += ' ' + strArr[x];
if ((x % 5 === 0 && x !== 0) || x == strArr.length - 1) {
tempString = tempString.slice(1);
result.push(tempString);
tempString = '';
}
}
return result || value;
},
fontSize: 10,
},
display: true,
scaleLabel: {
display: true,
labelString: 'Response',
},
},
],
},
};
public barChartLabels: Label[];
// = ['Option A', 'Option B'];
public barChartType: ChartType = 'horizontalBar';
public barChartLegend = false;
public barChartPlugins;
public chartColors: Array<any>;
// = [
// {
// // all colors in order
// backgroundColor: ['#D35400', '#D35400', '#D35400'],
// },
// ];
public barChartData: ChartDataSets[];
// =[{ data: [65, 59], label: 'Series A' }];
constructor() {}
ngOnInit() {
this.barChartLabels = this.data.chart.data.labels;
this.barChartData = [{ data: this.data.chart.data.datasets[0].data, label: 'Series A' }];
this.chartColors = [{ backgroundColor: this.data.chart.data.datasets[0].backgroundColor }];
this.barChartPlugins = [
{
beforeUpdate: function (c) {
var chartHeight = c.chart.height;
var size = (chartHeight * 5) / 100;
c.scales['y-axis-0'].options.ticks.minor.fontSize = size;
},
},
];
}
}
function createSubstrArr(str, words): any {
let strArr = str.split(' ');
let tempString = '';
let result = [];
for (let x = 0; x < strArr.length; x++) {
tempString += ' ' + strArr[x];
if ((x % words === 0 && x !== 0) || x == strArr.length - 1) {
tempString = tempString.slice(1);
result.push(tempString);
tempString = '';
}
}
return result;
}
<div *ngIf="barChartData">
<div class="heading">
<h4>{{ questionNumber }}) {{ data?.question }}</h4>
</div>
<div *ngIf="data?.chart?.data">
<div style="display: block">
<canvas
baseChart
[datasets]="barChartData"
[labels]="barChartLabels"
[options]="barChartOptions"
[plugins]="barChartPlugins"
[legend]="barChartLegend"
[chartType]="barChartType"
[colors]="chartColors"
>
</canvas>
</div>
</div>
<div *ngIf="!data?.chart?.data" class="errorMsg">No data found</div>
</div>
./bar-chart.component.scss
@import 'src/assets/styles/base/_variables.scss';
@import 'src/assets/styles/_custom-mixins.scss';
@import 'src/assets/styles/_variables.scss';
.heading {
background: #f4f4f4;
padding: 15px 10px;
}
.errorMsg {
color: $gray-400;
text-align: center;
padding: 20px 0;
}