|
|
@ -1,18 +1,25 @@ |
|
|
|
import m from 'mithril'; |
|
|
|
import m from 'mithril'; |
|
|
|
import { Widget } from 'components/widgets/widget'; |
|
|
|
import { Widget } from 'components/widgets/widget'; |
|
|
|
|
|
|
|
import { Pipe } from 'utilities/pipe'; |
|
|
|
|
|
|
|
|
|
|
|
require('./gauge-linear.css'); |
|
|
|
require('./gauge-linear.css'); |
|
|
|
|
|
|
|
|
|
|
|
export class GaugeLinear extends Widget { |
|
|
|
export class GaugeLinear extends Widget { |
|
|
|
|
|
|
|
private gaugeValue: Pipe<number>; |
|
|
|
|
|
|
|
private gaugeMaxValue: number; |
|
|
|
private bars: SVGRectElement[] = []; |
|
|
|
private bars: SVGRectElement[] = []; |
|
|
|
|
|
|
|
private valueElement: HTMLElement; |
|
|
|
|
|
|
|
|
|
|
|
constructor(vnode: any) { |
|
|
|
constructor(vnode: any) { |
|
|
|
super(vnode); |
|
|
|
super(vnode); |
|
|
|
|
|
|
|
this.gaugeValue = vnode.attrs.gaugeValue || new Pipe(0.0); |
|
|
|
|
|
|
|
this.gaugeMaxValue = vnode.attrs.gaugeMaxValue || 1.0; |
|
|
|
|
|
|
|
this.gaugeValue.onChange(() => this.updateGauge()); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
view(vnode: m.Vnode<{}, {}>): m.Children { |
|
|
|
view(vnode: m.Vnode<{}, {}>): m.Children { |
|
|
|
return <div class="widget gauge-linear" style={'flex: ' + this.widgetWidth}> |
|
|
|
return <div class="widget gauge-linear" style={'flex: ' + this.widgetWidth}> |
|
|
|
<p class="value">643<span class="unit">W</span></p> |
|
|
|
<p><span class="value"></span><span class="unit">W</span></p> |
|
|
|
<svg> |
|
|
|
<svg> |
|
|
|
</svg> |
|
|
|
</svg> |
|
|
|
</div>; |
|
|
|
</div>; |
|
|
@ -53,10 +60,13 @@ export class GaugeLinear extends Widget { |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
renderGaugeRatio(ratio: number) { |
|
|
|
updateGauge() { |
|
|
|
|
|
|
|
let value = Math.max(0.0, Math.min(this.gaugeMaxValue, this.gaugeValue.get())); |
|
|
|
|
|
|
|
this.valueElement.innerText = value.toFixed(0); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
let ratio = value / this.gaugeMaxValue; |
|
|
|
let numBars = this.bars.length; |
|
|
|
let numBars = this.bars.length; |
|
|
|
let litBars = Math.round(ratio * numBars); |
|
|
|
let litBars = Math.round(ratio * numBars); |
|
|
|
console.log(litBars); |
|
|
|
|
|
|
|
for(let barIdx = 0; barIdx < numBars; ++barIdx) { |
|
|
|
for(let barIdx = 0; barIdx < numBars; ++barIdx) { |
|
|
|
let bar = this.bars[barIdx]; |
|
|
|
let bar = this.bars[barIdx]; |
|
|
|
bar.classList.toggle('lit', barIdx < litBars); |
|
|
|
bar.classList.toggle('lit', barIdx < litBars); |
|
|
@ -66,6 +76,9 @@ export class GaugeLinear extends Widget { |
|
|
|
oncreate(vnode: any) { |
|
|
|
oncreate(vnode: any) { |
|
|
|
let svgElement: SVGElement = vnode.dom.querySelector('svg'); |
|
|
|
let svgElement: SVGElement = vnode.dom.querySelector('svg'); |
|
|
|
this.createSvg(svgElement); |
|
|
|
this.createSvg(svgElement); |
|
|
|
this.renderGaugeRatio(0.2); |
|
|
|
|
|
|
|
|
|
|
|
this.valueElement = vnode.dom.querySelector('span.value'); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
this.updateGauge(); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|