Boxplot.vue 8.53 KB
Newer Older
Sascha Herzinger's avatar
Sascha Herzinger committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
<template>
  <div :class="`fjs-boxplot fjs-vm-uid-${this._uid}`">
    <div class="fjs-data-box-container">
      <data-box class="fjs-data-box"
                header="Numerical Variables"
                dataType="numerical"
                v-on:update="update_numData">
      </data-box>
      <data-box class="fjs-data-box"
                header="Categorical Variables"
                dataType="categorical"
                v-on:update="update_catData">
      </data-box>
    </div>

    <div class="fjs-parameter-container">
    </div>

    <div class="fjs-vis-container">
      <svg :width="width"
           :height="height">
        <g :transform="`translate(${margin.left}, ${margin.top})`">
Sascha Herzinger's avatar
Sascha Herzinger committed
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
          <g class="fjs-bplt-axis fjs-x-axis" :transform="`translate(0, ${padded.height})`"></g>
          <g class="fjs-bplt-axis fjs-y-axis"></g>
          <g v-for="label in Object.keys(results.statistics)" :transform="`translate(${scales.x(label)}, 0)`">
            <line class="fjs-whisker"
                  :x1="- boxplotWidth / 6"
                  :y1="scales.y(stat)"
                  :x2="boxplotWidth / 6"
                  :y2="scales.y(stat)"
                  v-for="stat in [results.statistics[label].l_wsk, results.statistics[label].u_wsk]">
            </line>
            <line class="fjs-quartile"
                  :x1="- boxplotWidth / 2"
                  :y1="scales.y(stat)"
                  :x2="boxplotWidth / 2"
                  :y2="scales.y(stat)"
                  v-for="stat in [results.statistics[label].l_qrt, results.statistics[label].u_qrt]">
            </line>
            <line class="fjs-antenna"
                  :x1="0"
                  :y1="scales.y(results.statistics[label].u_wsk)"
                  :x2="0"
                  :y2="scales.y(results.statistics[label].l_wsk)">
            </line>
            <rect class="fjs-above-median-box"
                  :x="- boxplotWidth / 2"
                  :y="scales.y(results.statistics[label].u_qrt) + 1"
                  :width="boxplotWidth"
                  :height="scales.y(results.statistics[label].median) - scales.y(results.statistics[label].u_qrt)">
            </rect>
            <rect class="fjs-below-median-box"
                  :x="- boxplotWidth / 2"
                  :y="scales.y(results.statistics[label].median)"
                  :width="boxplotWidth"
                  :height="scales.y(results.statistics[label].l_qrt) - scales.y(results.statistics[label].median) - 1">
            </rect>
            <!--<circle class="fjs-point"-->
                    <!--:cx="scales.x(label)"-->
                    <!--:cy="scales.y(row[label])"-->
                    <!--r="4"-->
                    <!--v-if="row[label] !== null"-->
                    <!--v-for="row in results.data">-->
            <!--</circle>-->
          </g>
Sascha Herzinger's avatar
Sascha Herzinger committed
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
        </g>
      </svg>
    </div>
  </div>
</template>

<script>
  import DataBox from '../components/DataBox.vue'
  import store from '../../store/store'
  import requestHandling from '../methods/run-analysis'
  import * as d3 from 'd3'
  import { TweenLite } from 'gsap'
  import svgtooltip from '../directives/v-svgtooltip'
  import TaskView from '../components/TaskView.vue'
  import deepFreeze from 'deep-freeze-strict'
81
  import utils from '../../services/utils'
Sascha Herzinger's avatar
Sascha Herzinger committed
82
83
84
85
86
87
88
89
  export default {
    name: 'boxplot',
    data () {
      return {
        width: 0,
        height: 0,
        numData: [],
        catData: [],
Sascha Herzinger's avatar
Sascha Herzinger committed
90
91
92
93
        results: {
          data: [],
          statistics: {}
        }
Sascha Herzinger's avatar
Sascha Herzinger committed
94
95
96
97
98
99
100
      }
    },
    computed: {
      args () {
        return {
          variables: this.numData.map(d => `$${d}$`),
          categories: this.catData.map(d => `$${d}$`),
101
          id_filter: [],
Sascha Herzinger's avatar
Sascha Herzinger committed
102
103
104
105
106
107
108
          subsets: store.getters.subsets
        }
      },
      validArgs () {
        return this.numData.length > 0
      },
      margin () {
109
        const left = 60
Sascha Herzinger's avatar
Sascha Herzinger committed
110
111
        const top = 10
        const right = 10
112
        const bottom = 100
Sascha Herzinger's avatar
Sascha Herzinger committed
113
114
115
116
117
118
119
        return { left, top, right, bottom }
      },
      padded () {
        const width = this.width - this.margin.left - this.margin.right
        const height = this.height - this.margin.top - this.margin.bottom
        return { width, height }
      },
120
121
122
      numOfBoxplots () {
        return Object.keys(this.results.statistics).length
      },
Sascha Herzinger's avatar
Sascha Herzinger committed
123
      boxplotWidth () {
124
125
126
127
128
129
130
131
132
        const maxBoxplotWidth = this.padded.width / 4
        let boxplotWidth = this.padded.width / this.numOfBoxplots - this.padded.width * 0.05
        boxplotWidth = boxplotWidth > maxBoxplotWidth ? maxBoxplotWidth : boxplotWidth
        return boxplotWidth
      },
      scales () {
        const values = this.results.data.map(entry => this.results.variables.map(v => entry[v]))
        const flattened = [].concat.apply([], values)
        const extent = d3.extent(flattened)
Sascha Herzinger's avatar
Sascha Herzinger committed
133
        const padding = (extent[1] - extent[0]) / 10
Sascha Herzinger's avatar
Sascha Herzinger committed
134
135
        const x = d3.scalePoint()
          .domain(Object.keys(this.results.statistics))
136
          .range([0, this.padded.height])
Sascha Herzinger's avatar
Sascha Herzinger committed
137
          .padding(1)
138
        const y = d3.scaleLinear()
Sascha Herzinger's avatar
Sascha Herzinger committed
139
          .domain([extent[0] - padding, extent[1] + padding])
Sascha Herzinger's avatar
Sascha Herzinger committed
140
141
142
143
          .range([this.padded.width, 0])
        return { x, y }
      },
      axis () {
144
        const x = d3.axisBottom(this.scales.x).tickFormat(d => utils.truncate({fullStr: d, strLen: 20}))
Sascha Herzinger's avatar
Sascha Herzinger committed
145
        const y = d3.axisLeft(this.scales.y)
146
        return { x, y }
Sascha Herzinger's avatar
Sascha Herzinger committed
147
148
149
150
151
152
153
154
155
156
157
158
      }
    },
    watch: {
      'args': {
        handler: function (newArgs, oldArgs) {
          // if our data selection change we will want to re-initialize the current view
          const init = JSON.stringify(newArgs.variables) !== JSON.stringify(oldArgs.variables) ||
            JSON.stringify(newArgs.categories) !== JSON.stringify(oldArgs.categories)
          if (this.validArgs) {
            this.runAnalysisWrapper({init, args: this.args})
          }
        }
Sascha Herzinger's avatar
Sascha Herzinger committed
159
160
161
      },
      'axis': {
        handler: function (newAxis) {
Sascha Herzinger's avatar
Sascha Herzinger committed
162
163
164
165
166
167
          d3.select(`.fjs-vm-uid-${this._uid} .fjs-x-axis`)
            .call(newAxis.x)
            .selectAll('text')
            .attr('transform', 'rotate(20)')
          d3.select(`.fjs-vm-uid-${this._uid} .fjs-y-axis`)
            .call(newAxis.y)
Sascha Herzinger's avatar
Sascha Herzinger committed
168
        }
Sascha Herzinger's avatar
Sascha Herzinger committed
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
      }
    },
    methods: {
      update_numData (ids) {
        this.numData = ids
      },
      update_catData (ids) {
        this.catData = ids
      },
      handleResize () {
        const container = this.$el.querySelector(`.fjs-vm-uid-${this._uid} .fjs-vis-container svg`)
        // noinspection JSSuspiciousNameCombination
        this.height = container.getBoundingClientRect().width
        this.width = container.getBoundingClientRect().width
      },
      runAnalysisWrapper ({args}) {
        // function made available via requestHandling mixin
        this.runAnalysis({task_name: 'compute-boxplot', args})
          .then(response => {
            const results = JSON.parse(response)
            const data = JSON.parse(results.data)
            results.data = Object.keys(data).map(key => data[key])
            deepFreeze(results) // massively improve performance by telling Vue that the objects properties won't change
            this.results = results
          })
          .catch(error => console.error(error))
      }
    },
    components: {
      DataBox,
      TaskView
    },
    mixins: [
      requestHandling,
      svgtooltip
    ],
    mounted () {
      window.addEventListener('resize', this.handleResize)
      this.handleResize()
    },
    beforeDestroy () {
      window.removeEventListener('resize', this.handleResize)
    }
  }
</script>

<style lang="sass" scoped>
  @import './src/assets/base.sass'

  *
    font-family: Roboto, sans-serif

  .fjs-boxplot
    height: 100%
    width: 100%
    display: flex
    flex-direction: column

    .fjs-data-box-container
      height: 160px
      display: flex
      justify-content: space-around

    .fjs-vis-container
      flex: 1
      display: flex
      svg
        flex: 1
Sascha Herzinger's avatar
Sascha Herzinger committed
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
      .fjs-whisker, .fjs-quartile, .fjs-antenna
        shape-rendering: crispEdges
        stroke: black
        stroke-width: 2px
      .fjs-below-median-box
        stroke: none
        fill: rgb(205, 232, 254)
        shape-rendering: crispEdges
      .fjs-above-median-box
        stroke: none
        fill: rgb(180, 221, 253)
        shape-rendering: crispEdges
</style>


<!--CSS for dynamically created components-->
<style lang="sass">
  @import './src/assets/svgtooltip.sass'

  .fjs-bplt-axis
    shape-rendering: crispEdges
    stroke-width: 2px
    .tick
      shape-rendering: crispEdges
Sascha Herzinger's avatar
Sascha Herzinger committed
261
262
      text
        font-size: 18px
Sascha Herzinger's avatar
Sascha Herzinger committed
263
264
    line
      stroke: #999
Sascha Herzinger's avatar
Sascha Herzinger committed
265
266
267
268
  .fjs-x-axis
    .tick
      text
        text-anchor: start
Sascha Herzinger's avatar
Sascha Herzinger committed
269
</style>