Browse Source

Merge branch 'ui-update' into gauge-updates

merge-requests/1/merge
rozetko 4 years ago
parent
commit
a8b7e4531f
  1. 8
      dist/index.d.ts
  2. 2
      dist/index.js
  3. 6
      dist/types.d.ts
  4. 38
      src/index.ts
  5. 6
      src/types.ts

8
dist/index.d.ts vendored

@ -1,12 +1,11 @@
import { GaugeTimeSerie, GaugeOptions, Stat } from './types'; import { GaugeTimeSerie, GaugeOptions, Stat } from './types';
import { ChartwerkPod } from '@chartwerk/core'; import { ChartwerkPod } from '@chartwerk/core';
export declare class ChartwerkGaugePod extends ChartwerkPod<GaugeTimeSerie, GaugeOptions> { export declare class ChartwerkGaugePod extends ChartwerkPod<GaugeTimeSerie, GaugeOptions> {
private _gaugeTransform;
private _gaugeCenter;
private _minWH;
constructor(el: HTMLElement, _series?: GaugeTimeSerie[], _options?: GaugeOptions); constructor(el: HTMLElement, _series?: GaugeTimeSerie[], _options?: GaugeOptions);
renderMetrics(): void; renderMetrics(): void;
private _setBoundingBox; get _gaugeTransform(): string;
get _gaugeCenter(): string;
get _minWH(): number;
private _renderValue; private _renderValue;
private _renderValueArc; private _renderValueArc;
private _renderThresholdArc; private _renderThresholdArc;
@ -29,7 +28,6 @@ export declare class ChartwerkGaugePod extends ChartwerkPod<GaugeTimeSerie, Gaug
rescaleSpace(space: number): number; rescaleSpace(space: number): number;
rescaleWith(width: number): number; rescaleWith(width: number): number;
private get _scaleFactor(); private get _scaleFactor();
private get _valueTextDecimals();
private get aggregatedValue(); private get aggregatedValue();
private get _maxValue(); private get _maxValue();
private get _minValue(); private get _minValue();

2
dist/index.js vendored

File diff suppressed because one or more lines are too long

6
dist/types.d.ts vendored

@ -9,9 +9,7 @@ export declare type Stop = {
color: string; color: string;
value: number | null; value: number | null;
}; };
export declare type ValueTextFormat = { export declare type ValueFormatter = (value: number) => string;
decimals: number;
};
export declare type GaugeTimeSerie = TimeSerie; export declare type GaugeTimeSerie = TimeSerie;
export declare type GaugeOptionsParams = { export declare type GaugeOptionsParams = {
innerRadius: number; innerRadius: number;
@ -24,6 +22,6 @@ export declare type GaugeOptionsParams = {
}[]; }[];
defaultColor: string; defaultColor: string;
stat: Stat; stat: Stat;
valueTextFormat: ValueTextFormat; valueFormatter: ValueFormatter;
}; };
export declare type GaugeOptions = Options & Partial<GaugeOptionsParams>; export declare type GaugeOptions = Options & Partial<GaugeOptionsParams>;

38
src/index.ts

@ -45,17 +45,10 @@ const DEFAULT_GAUGE_OPTIONS: GaugeOptions = {
stat: Stat.CURRENT, stat: Stat.CURRENT,
innerRadius: DEFAULT_INNER_RADIUS, innerRadius: DEFAULT_INNER_RADIUS,
outerRadius: DEFAULT_OUTER_RADIUS, outerRadius: DEFAULT_OUTER_RADIUS,
valueTextFormat: { valueFormatter: val => val.toString()
decimals: DEFAULT_VALUE_TEXT_Decimals
}
}; };
export class ChartwerkGaugePod extends ChartwerkPod<GaugeTimeSerie, GaugeOptions> { export class ChartwerkGaugePod extends ChartwerkPod<GaugeTimeSerie, GaugeOptions> {
// TODO: better name
private _gaugeTransform = '';
private _gaugeCenter = '';
private _minWH = 0;
constructor(el: HTMLElement, _series: GaugeTimeSerie[] = [], _options: GaugeOptions = {}) { constructor(el: HTMLElement, _series: GaugeTimeSerie[] = [], _options: GaugeOptions = {}) {
super( super(
d3, el, _series, d3, el, _series,
@ -69,18 +62,21 @@ export class ChartwerkGaugePod extends ChartwerkPod<GaugeTimeSerie, GaugeOptions
return; return;
} }
this._setBoundingBox();
this._renderValueArc(); this._renderValueArc();
this._renderThresholdArc(); this._renderThresholdArc();
this._renderValue(); this._renderValue();
} }
private _setBoundingBox(): void { get _gaugeTransform(): string {
// TODO: refactor return `translate(${this.width / 2},${0.8 * this.height})`;
this._gaugeTransform = `translate(${this.width / 2},${0.8 * this.height})`; }
this._gaugeCenter = `translate(${this.width / 2 + this.margin.left},${0.8 * this.height})`;
this._minWH = _.min([0.6 * this.width, this.height]); get _gaugeCenter(): string {
return `translate(${this.width / 2 + this.margin.left},${0.8 * this.height})`;
}
get _minWH(): number {
return _.min([0.6 * this.width, this.height]);
} }
private _renderValue(): void { private _renderValue(): void {
@ -210,8 +206,11 @@ export class ChartwerkGaugePod extends ChartwerkPod<GaugeTimeSerie, GaugeOptions
} }
private get _valueText(): string { private get _valueText(): string {
const decimalsCount = this._valueTextDecimals; if(this.options.valueFormatter) {
return this.aggregatedValue.toFixed(decimalsCount); console.log('valueFormatter function is not specified, rendering raw value');
return this.aggregatedValue.toString();
}
return this.options.valueFormatter(this.aggregatedValue);
} }
private get _valueTextFontSize(): number { private get _valueTextFontSize(): number {
@ -268,13 +267,6 @@ export class ChartwerkGaugePod extends ChartwerkPod<GaugeTimeSerie, GaugeOptions
return scale; return scale;
} }
private get _valueTextDecimals(): number {
if(this.options.valueTextFormat === undefined) {
throw new Error(`Options has no valueTextFormat`);
}
return this.options.valueTextFormat.decimals;
}
private get aggregatedValue(): number { private get aggregatedValue(): number {
switch(this._stat) { switch(this._stat) {
case Stat.CURRENT: case Stat.CURRENT:

6
src/types.ts

@ -12,9 +12,7 @@ export type Stop = {
value: number | null value: number | null
}; };
export type ValueTextFormat = { export type ValueFormatter = (value: number) => string;
decimals: number
};
export type GaugeTimeSerie = TimeSerie; export type GaugeTimeSerie = TimeSerie;
@ -27,6 +25,6 @@ export type GaugeOptionsParams = {
stops: { color: string , value: number }[]; stops: { color: string , value: number }[];
defaultColor: string; defaultColor: string;
stat: Stat; stat: Stat;
valueTextFormat: ValueTextFormat; valueFormatter: ValueFormatter;
} }
export type GaugeOptions = Options & Partial<GaugeOptionsParams>; export type GaugeOptions = Options & Partial<GaugeOptionsParams>;

Loading…
Cancel
Save