import { Component, ViewEncapsulation, ViewChild, OnInit, OnChanges, DoCheck, Input, Output, EventEmitter, ElementRef, KeyValueDiffers } from '@angular/core';
import { CfCoreComponent } from '../core/core.component';
import { GaugeModel } from '../../models/gauge/gauge.model';
import { GaugeStylingModel } from '../../models/gauge/gauge-styling.model';
/**
*
Gauge component is a modification of Angular gauge with added label, prefix and suffix elements. All elements can be customized and stylized.
* Example of using:
*
* <cf-gauge [properties]="myGauge" [styling]="myGaugeStyles"></cf-gauge>
*
*/
@Component({
selector: 'cf-gauge',
encapsulation: ViewEncapsulation.None,
template: " ",
styles: [".cf-gauge-container { display: inline-flex; position: relative; z-index: 0; } .cf-gauge-container > div.core-gauge-platform { position: relative; z-index: 0; } .cf-gauge-container mwl-gauge { display: block; cursor: pointer; } .cf-gauge-container mwl-gauge .cf-gauge .cf-gauge-value-hidden { display: none !important; } .core-gauge-value-platform { position: absolute; z-index: 1; top: 50% !important; left: 50% !important; width: 0px !important; height: 0px !important; display: flex; justify-content: center; align-items: center; color: #3f51b5; } .cf-gauge-value-container { position: relative; z-index: 0; display: flex; flex-direction: row; justify-content: center; align-items: baseline; } .cf-gauge-bar { stroke: #e0e0e0; stroke-width: 50; fill: transparent; } .cf-gauge-track { stroke: #3f51b5; stroke-width: 50; fill: transparent; } .cf-gauge-prefix, .cf-gauge-suffix { font-size: 14px; } .cf-gauge-value { display: inline-flex; align-items: baseline; font-size: 26px; } .cf-gauge-value md-input-container { overflow: hidden; margin-bottom: -13px !important; } .cf-gauge-value md-input-container .mat-input-wrapper { margin: 0 !important; } .cf-gauge-value md-input-container input { padding-right: 30px !important; width: 150% !important; } .cf-gauge-value md-input-container .mat-input-underline { margin-top: -2px !important; } .cf-gauge-value .editing { position: absolute; z-index: -1; opacity: 0 !important; } .core-gauge-label-platform { position: absolute; z-index: 1; bottom: -20px !important; left: 50% !important; width: 0px !important; height: 0px !important; display: flex; justify-content: center; align-items: baseline; } .cf-gauge-label { white-space: nowrap; font-size: 10px; } [hidden] { display: none !important; } "]
})
export class CfGaugeComponent extends CfCoreComponent implements OnInit, OnChanges, DoCheck {
/**
* It is an object with GaugeModel type to be rendered inside cf-gauge template.
*/
cfGauge: GaugeModel;
/**
* Object to register element state and compute differences.
*/
differ: any;
/**
* It is a clone of gauge value to work with calculated effect during gauge animation time. It is using only inside component functionality.
*/
_animatedValue: number;
/**
* It is time of one point change of _animatedValue during animation time.
*/
_tickInterval: number;
/**
* It is increase or decrease of gauge value
*/
_animationValueDirection: string;
/**
* It is number of ticks between previous and current gauge value.
*/
_ticks: number;
/**
* It is an attribute [properties] of directive cf-gauge which take GaugeModel object.
* Example:
* {
* max: 100,
* value: 35,
* type: 'arch',
* size: 150,
* label: 'Gauge label',
* prefix: 'prefix',
* suffix: 'suffix',
* animationDuration: 1
* }
* max: number
* It is maximum integer value of gauge
* value: number
* Current integer value of gauge
* type: string
* Type value of gauge. Can be: full, arch, semi. Default is 'full'.
* label: string
* Label of gauge
* prefix: string
* Prefix of gauge
* suffix: string
* Suffix of gauge
* size: number
* It is size of gauge. Means number of pixels.
* animationDuration: number
* Aanimation duration of gauge in seconds
*/
@Input() properties: GaugeModel;
/**
* It is an object with styling for two component elements
*/
@Input() styling: GaugeStylingModel;
/**
* It is [display] attribute of cf-gauge component
*/
@Input() display: boolean;
/**
* It is [disable] attribute of cf-gauge component
*/
@Input() disable: boolean;
/**
* It is selected type of gauge. It consists of start/end coordinates for svg elements.
*/
_type = { start: -90, end: -90.001 };
/**
* It is an object with all gauges types and their start/end coordinates.
*/
_types = {
"full": { start: -90, end: -90.001 },
"arch": { start: 135, end: 45 },
"semi": { start: 0, end: 180 },
};
@ViewChild('valueToShow') valueToShow;
@ViewChild('valueToEditContainer') valueToEditContainer;
@ViewChild('valueToEdit') valueToEdit;
_setInputWidth() {
setTimeout(() => {
this.valueToEditContainer.nativeElement.style.width = (this.valueToShow.nativeElement.clientWidth + 2) + 'px';
}, 0);
}
_mode: string = 'displaying';
_changeGaugeValue($event) {
if( this.cfGauge.value <= 0 ) this.cfGauge.value = 0;
if( this.cfGauge.value >= this.cfGauge.max ) this.cfGauge.value = this.cfGauge.max;
this._animatedValue = this.cfGauge.value;
this._setInputWidth();
}
_showInput($event) {
if(!this.cfGauge.disable) {
setTimeout(() => {
this.valueToEdit.nativeElement.focus();
}, 0);
this.valueToEditContainer.nativeElement.style.width = (this.valueToShow.nativeElement.clientWidth + 2) + 'px';
}
}
/**
* It is function to increase / decrease gauge value from previous to current during animation time. Used only inside component logic.
*/
private _animatingValue() {
if(this._ticks > 0) {
setTimeout(() => {
if(this._animationValueDirection === 'increase') {
++this._animatedValue;
} else {
--this._animatedValue;
}
--this._ticks;
this._animatingValue();
}, this._tickInterval);
}
}
/**
* It is function to recalculate value if it was changed during animation time. Used only inside component logic.
* @param prev previous value
* @param curr current value
*/
private _animateValueChange(prev, curr) {
this._ticks = Math.abs(curr - prev);
this._tickInterval = Math.ceil(this.cfGauge.animationDuration*1000 / this._ticks);
this._animationValueDirection = curr > prev ? 'increase' : 'decrease';
this._animatingValue();
}
_centerX: number;
_centerY: number;
_clickedRatio: number;
private _pickFullTypeValue(eX, eY) {
var eXCentered = eX > this._centerX ? eX - this._centerX : this._centerX - eX,
eYCentered = eY > this._centerY ? eY - this._centerY : this._centerY - eY,
clickedRarius = Math.sqrt(Math.pow(eXCentered, 2) + Math.pow(eYCentered, 2)),
circuit = 2 * Math.PI * clickedRarius,
clickedAngle;
if(eX <= this._centerX && eY <= this._centerY) {
clickedAngle = 270 + Math.acos(eXCentered/clickedRarius) / (Math.PI / 180);
} else if(eX > this._centerX && eY <= this._centerY) {
clickedAngle = Math.acos(eYCentered/clickedRarius) / (Math.PI / 180);
} else if(eX >= this._centerX && eY >= this._centerY) {
clickedAngle = 90 + Math.acos(eXCentered/clickedRarius) / (Math.PI / 180);
} else {
clickedAngle = 180 + Math.acos(eYCentered/clickedRarius) / (Math.PI / 180);
}
this._clickedRatio = ( (Math.PI * clickedRarius * clickedAngle) / 180 ) / circuit;
this.cfGauge.value = Number((this._clickedRatio * this.cfGauge.max).toFixed());
this._animatedValue = this.cfGauge.value;
}
private _pickArchTypeValue(eX, eY) {
if(eY this._centerX ? eX - this._centerX : this._centerX - eX,
eYCentered = eY > this._centerY ? eY - this._centerY : this._centerY - eY,
clickedRarius = Math.sqrt(Math.pow(eXCentered, 2) + Math.pow(eYCentered, 2)),
circuit = 1.5 * Math.PI * clickedRarius,
clickedAngle;
if(eX <= this._centerX && eY <= this._centerY) {
clickedAngle = 45 + Math.acos(eXCentered/clickedRarius) / (Math.PI / 180);
} else if(eX > this._centerX && eY <= this._centerY) {
clickedAngle = 135 + Math.acos(eYCentered/clickedRarius) / (Math.PI / 180);
} else if(eX >= this._centerX && eY >= this._centerY) {
clickedAngle = 225 + Math.acos(eXCentered/clickedRarius) / (Math.PI / 180);
} else {
clickedAngle = Math.acos(eYCentered/clickedRarius) / (Math.PI / 180) - 45;
}
if(clickedAngle >= 0 && clickedAngle <= 270) {
this._clickedRatio = ( (Math.PI * clickedRarius * clickedAngle) / 180 ) / circuit;
this.cfGauge.value = Number((this._clickedRatio * this.cfGauge.max).toFixed());
this._animatedValue = this.cfGauge.value;
}
}
}
private _pickSemiTypeValue(eX, eY) {
if(eY<=this._centerY) {
var eXCentered = eX > this._centerX ? eX - this._centerX : this._centerX - eX,
eYCentered = this._centerY - eY,
clickedRarius = Math.sqrt(Math.pow(eXCentered, 2) + Math.pow(eYCentered, 2)),
circuit = Math.PI * clickedRarius,
clickedAngle;
if(eX <= this._centerX) {
clickedAngle = Math.acos(eXCentered/clickedRarius) / (Math.PI / 180);
} else {
clickedAngle = 90 + Math.acos(eYCentered/clickedRarius) / (Math.PI / 180);
}
if(clickedAngle >= 0 && clickedAngle <= 180) {
this._clickedRatio = ( (Math.PI * clickedRarius * clickedAngle) / 180 ) / circuit;
this.cfGauge.value = Number((this._clickedRatio * this.cfGauge.max).toFixed());
this._animatedValue = this.cfGauge.value;
}
}
}
private _pickValue(eX, eY) {
if(!this.cfGauge.disable) {
this._mode = 'clicks';
if(this.cfGauge.type === 'full') this._pickFullTypeValue(eX, eY);
if(this.cfGauge.type === 'arch') this._pickArchTypeValue(eX, eY);
if(this.cfGauge.type === 'semi') this._pickSemiTypeValue(eX, eY);
}
}
constructor(public elementRef: ElementRef, differs: KeyValueDiffers) {
super(elementRef);
this.differ = differs.find({}).create(null);
}
ngOnInit() {
this.cfGauge = this.properties ? this.properties : new GaugeModel({
max: 100,
value: 0,
size: 150,
type: 'full',
animationDuration: 1
});
if(this.disable!=null) this.properties.disable = this.disable;
if(this.display!=null) {
if(this.properties) {
this.properties.display = this.display;
} else {
this.cfGauge.display = this.display;
}
}
this._centerX = this.cfGauge.size / 2;
this._centerY = this.cfGauge.size / 2;
this._animatedValue = this.cfGauge.value;
if (this.cfGauge.type) {
switch (this.cfGauge.type) {
case "arch": this._type = this._types.arch; break;
case "semi": this._type = this._types.semi; break;
}
}
if(this.styling==null) this.styling = new GaugeStylingModel();
// console.log('cf-gauge init ', this);
}
ngOnChanges(changes: any) {
console.log('changes', changes);
}
ngDoCheck() {
if (this.differ) {
const changes = this.differ.diff(this.cfGauge);
if (changes) {
changes.forEachChangedItem((record) => {
if(record.key === 'value') {
if (this._mode === 'displaying') {
this._animatedValue = record.previousValue;
this._animateValueChange(record.previousValue, record.currentValue);
}
}
});
}
}
}
}