aboutsummaryrefslogtreecommitdiff
path: root/src/client/views/nodes/HistogramBox.tsx
blob: 0fcc25e663eab37b63975d137e007c08b64bf3cf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import React = require("react")
import { observer } from "mobx-react";
import { FieldView, FieldViewProps } from './FieldView';
import "./VideoBox.scss";
import { observable, reaction } from "mobx";
import { HistogramOperation } from "../../northstar/operations/HistogramOperation";
import { Main } from "../Main";
import { ColumnAttributeModel } from "../../northstar/core/attribute/AttributeModel";
import { AttributeTransformationModel } from "../../northstar/core/attribute/AttributeTransformationModel";
import { AggregateFunction, HistogramResult, DoubleValueAggregateResult } from "../../northstar/model/idea/idea";
import { ModelHelpers } from "../../northstar/model/ModelHelpers";

@observer
export class HistogramBox extends React.Component<FieldViewProps> {

    public static LayoutString(fieldStr: string = "DataKey") { return FieldView.LayoutString(HistogramBox, fieldStr) }

    constructor(props: FieldViewProps) {
        super(props);
    }

    @observable _histoResult?: HistogramResult;
    _histoOp?: HistogramOperation;

    componentDidMount() {
        Main.Instance.GetAllAttributes().map(a => {
            if (a.displayName == this.props.doc.Title) {
                var atmod = new ColumnAttributeModel(a);
                this._histoOp = new HistogramOperation(new AttributeTransformationModel(atmod, AggregateFunction.None),
                    new AttributeTransformationModel(atmod, AggregateFunction.Count),
                    new AttributeTransformationModel(atmod, AggregateFunction.Count));
                reaction(() => [this._histoOp && this._histoOp.Result],
                    () => this._histoResult = this._histoOp ? this._histoOp.Result as HistogramResult : undefined
                );
                this._histoOp.Update();
            }
        })
    }

    twoString() {
        let str = "";
        if (this._histoResult && !this._histoResult.isEmpty) {
            for (let key in this._histoResult.bins) {
                if (this._histoResult.bins.hasOwnProperty(key)) {
                    let bin = this._histoResult.bins[key];
                    str += JSON.stringify(bin.binIndex!.toJSON()) + " = ";
                    let valueAggregateKey = ModelHelpers.CreateAggregateKey(this._histoOp!.V, this._histoResult, ModelHelpers.AllBrushIndex(this._histoResult));
                    let value = ModelHelpers.GetAggregateResult(bin, valueAggregateKey) as DoubleValueAggregateResult;
                    if (value && value.hasResult && value.result) {
                        str += value.result;
                    }
                }
            }
        }
        return str;
    }

    render() {
        if (!this._histoResult)
            return (null);
        return (
            <div className="histogrambox-container">
                `HISTOGRAM RESULT : ${this.twoString()}`
            </div>
        )
    }
}