Youen Toupin
3 years ago
4 changed files with 101 additions and 86 deletions
@ -1,36 +0,0 @@
|
||||
div.gauge-linear { |
||||
display: flex; |
||||
flex-direction: column; |
||||
} |
||||
|
||||
div.gauge-linear span.integral-value { |
||||
font-size: 2.3rem; |
||||
font-family: monospace; |
||||
} |
||||
|
||||
div.gauge-linear span.decimal-value { |
||||
font-size: 1.6rem; |
||||
font-family: monospace; |
||||
} |
||||
|
||||
div.gauge-linear span.unit { |
||||
font-size: 1.6rem; |
||||
} |
||||
|
||||
rect.gauge-linear-bg { |
||||
fill: rgb(255,255,255); |
||||
stroke-width: 2; |
||||
stroke: rgb(0,0,0) |
||||
} |
||||
|
||||
rect.gauge-linear-bar { |
||||
fill: rgb(230,230,230); |
||||
stroke-width: 1; |
||||
stroke: rgb(180,180,180) |
||||
} |
||||
|
||||
rect.gauge-linear-bar.lit { |
||||
fill: rgb(180,180,180); |
||||
stroke-width: 1; |
||||
stroke: rgb(0,0,0) |
||||
} |
@ -0,0 +1,36 @@
|
||||
div.gauge { |
||||
display: flex; |
||||
flex-direction: column; |
||||
} |
||||
|
||||
div.gauge span.integral-value { |
||||
font-size: 2.3rem; |
||||
font-family: monospace; |
||||
} |
||||
|
||||
div.gauge span.decimal-value { |
||||
font-size: 1.6rem; |
||||
font-family: monospace; |
||||
} |
||||
|
||||
div.gauge span.unit { |
||||
font-size: 1.6rem; |
||||
} |
||||
|
||||
rect.gauge-bg { |
||||
fill: rgb(255,255,255); |
||||
stroke-width: 2; |
||||
stroke: rgb(0,0,0) |
||||
} |
||||
|
||||
rect.gauge-bar { |
||||
fill: rgb(230,230,230); |
||||
stroke-width: 1; |
||||
stroke: rgb(180,180,180) |
||||
} |
||||
|
||||
rect.gauge-bar.lit { |
||||
fill: rgb(180,180,180); |
||||
stroke-width: 1; |
||||
stroke: rgb(0,0,0) |
||||
} |
@ -0,0 +1,60 @@
|
||||
import m from 'mithril'; |
||||
import { Widget } from 'components/widgets/widget'; |
||||
import { Pipe } from 'utilities/pipe'; |
||||
|
||||
require('./gauge.css'); |
||||
|
||||
export class Gauge extends Widget { |
||||
private gaugeValue: Pipe<number>; |
||||
private gaugeMaxValue: number; |
||||
private unit: string; |
||||
private decimals: number; |
||||
|
||||
protected bars: SVGRectElement[] = []; |
||||
private integralValueElement: HTMLElement; |
||||
private decimalValueElement: HTMLElement; |
||||
|
||||
constructor(vnode: any) { |
||||
super(vnode); |
||||
|
||||
this.gaugeValue = vnode.attrs.gaugeValue || new Pipe(0.0); |
||||
this.gaugeMaxValue = vnode.attrs.gaugeMaxValue || 1.0; |
||||
this.unit = vnode.attrs.unit || ''; |
||||
this.decimals = vnode.attrs.decimals || 0; |
||||
|
||||
this.gaugeValue.onChange(() => this.updateGauge()); |
||||
} |
||||
|
||||
view(vnode: m.Vnode<{}, {}>): m.Children { |
||||
return <div class="widget gauge" style={'flex: ' + this.widgetWidth}> |
||||
<p><span class="integral-value"></span><span class="decimal-value"></span><span class="unit">{this.unit}</span></p> |
||||
<svg> |
||||
</svg> |
||||
</div>; |
||||
} |
||||
|
||||
updateGauge() { |
||||
let value = Math.max(0.0, Math.min(this.gaugeMaxValue, this.gaugeValue.get())); |
||||
|
||||
let integralValue = Math.floor(value); |
||||
let decimalValue = value - integralValue; |
||||
|
||||
this.integralValueElement.innerText = integralValue.toFixed(0); |
||||
this.decimalValueElement.innerText = this.decimals > 0 ? decimalValue.toFixed(this.decimals).substr(1) : ''; |
||||
|
||||
let ratio = value / this.gaugeMaxValue; |
||||
let numBars = this.bars.length; |
||||
let litBars = Math.round(ratio * numBars); |
||||
for(let barIdx = 0; barIdx < numBars; ++barIdx) { |
||||
let bar = this.bars[barIdx]; |
||||
bar.classList.toggle('lit', barIdx < litBars); |
||||
} |
||||
} |
||||
|
||||
oncreate(vnode: any) { |
||||
this.integralValueElement = vnode.dom.querySelector('span.integral-value'); |
||||
this.decimalValueElement = vnode.dom.querySelector('span.decimal-value'); |
||||
|
||||
this.updateGauge(); |
||||
} |
||||
} |
Loading…
Reference in new issue