Survivalplot.vue 9.62 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
<template>
    <chart v-on:resize="resize">
        <control-panel class="fjs-control-panel" name="Survivalplot Panel">
            <data-box class="fjs-data-box"
                      header="Duration [numerical]"
                      :data-types="['numerical']"
                      v-on:update="updateDurationVariable">
            </data-box>
            <data-box class="fjs-data-box"
                      header="Groups (optional) [categorical]"
                      :data-types="['categorical']"
                      v-on:update="updateGroupVariable">
            </data-box>
            <data-box class="fjs-data-box"
                      header="Observed (optional) [categorical]"
                      :data-types="['categorical']"
                      v-on:update="updateObservedVariable">
            </data-box>
        </control-panel>
        <svg :height="height" :width="width">
            <g :transform="`translate(${margin.left}, ${margin.top})`">
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
                <crosshair :width="padded.width" :height="padded.height"/>
                <g class="fjs-axis" ref="yAxis2" :transform="`translate(${padded.width}, 0)`"></g>
                <g class="fjs-axis" ref="xAxis2"></g>
                <g class="fjs-axis" ref="xAxis1" :transform="`translate(0, ${padded.height})`"></g>
                <g class="fjs-axis" ref="yAxis1"></g>
                <g class="fjs-paths">
                    <path class="fjs-estimate-path"
                          :style="{stroke: path.color}"
                          :d="path.d" v-for="path in paths">
                    </path>
                    <path class="fjs-ci-path"
                          :style="{fill: path.color}"
                          :d="path.d" v-for="path in ciPaths">
                    </path>
                </g>
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
            </g>
        </svg>
    </chart>
</template>

<script>
  import ControlPanel from '../components/ControlPanel.vue'
  import Chart from '../components/Chart.vue'
  import DataBox from '../components/DataBox.vue'
  import RunAnalysis from '../mixins/run-analysis'
  import store from '../../store/store'
  import deepFreeze from 'deep-freeze-strict'
  import * as d3 from 'd3'
  import Crosshair from '../components/Crosshair.vue'
  export default {
    name: 'survivalplot',
53
    components: {Crosshair, DataBox, Chart, ControlPanel},
54
55
56
57
58
59
60
61
62
    mixins: [RunAnalysis],
    data () {
      return {
        height: 0,
        width: 0,
        durationVariables: [],
        groupVariables: [],
        observedVariables: [],
        estimator: 'KaplanMeier',
63
64
65
66
67
68
        groupColors: d3.schemeCategory10,
        results: {
          subsets: [],
          categories: [],
          stats: {}
        }
69
70
71
72
73
74
75
76
77
      }
    },
    computed: {
      args () {
        return {
          durations: this.durationVariables,
          categories: this.groupVariables,
          event_observed: this.observedVariables,
          estimator: this.estimator,
78
          id_filter: store.getters.filter('ids').value,
79
80
81
82
          subsets: store.getters.subsets
        }
      },
      validArgs () {
83
        return this.durationVariables.length === 1
84
85
86
87
88
89
90
91
92
93
94
95
      },
      margin () {
        const left = this.width / 15
        const top = this.height / 15
        const right = this.width / 15
        const bottom = this.height / 15
        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}
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
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
      },
      dataRanges () {
        let timelineGlobalMin = Number.MAX_SAFE_INTEGER
        let timelineGlobalMax = Number.MIN_SAFE_INTEGER
        let estimateGlobalMin = Number.MAX_SAFE_INTEGER
        let estimateGlobalMax = Number.MIN_SAFE_INTEGER
        this.results.categories.forEach(category => {
          this.results.subsets.forEach(subset => {
            const [localTimelineMin, localTimelineMax] = d3.extent(this.results.stats[category][subset].timeline)
            timelineGlobalMin = localTimelineMin < timelineGlobalMin ? localTimelineMin : timelineGlobalMin
            timelineGlobalMax = localTimelineMax > timelineGlobalMax ? localTimelineMax : timelineGlobalMax
            const [localEstimateMin, localEstimateMax] = d3.extent(this.results.stats[category][subset].estimate)
            estimateGlobalMin = localEstimateMin < estimateGlobalMin ? localEstimateMin : estimateGlobalMin
            estimateGlobalMax = localEstimateMax > estimateGlobalMax ? localEstimateMax : estimateGlobalMax
          })
        })
        return { timelineGlobalMin, timelineGlobalMax, estimateGlobalMin, estimateGlobalMax }
      },
      scales () {
        const x = d3.scaleLinear()
          .domain([this.dataRanges.timelineGlobalMin, this.dataRanges.timelineGlobalMax])
          .range([0, this.padded.width])
        const y = d3.scaleLinear()
          .domain([this.dataRanges.estimateGlobalMin, this.dataRanges.estimateGlobalMax])
          .range([this.padded.height, 0])
        return { x, y }
      },
      axis () {
        const x1 = d3.axisTop(this.scales.x)
        const y1 = d3.axisRight(this.scales.y)
        const x2 = d3.axisBottom(this.scales.x)
          .tickSizeInner(this.padded.height)
          .tickFormat('')
        const y2 = d3.axisLeft(this.scales.y)
          .tickSizeInner(this.padded.width)
          .tickFormat('')
        return { x1, x2, y1, y2 }
      },
      groups () {
        const groups = {}
        this.results.categories.forEach(category => {
          this.results.subsets.forEach(subset => {
            groups[this.getGroupName(category, subset)] = {}
          })
        })
        Object.keys(groups).forEach((key, i) => {
          groups[key].color = this.groupColors[i % this.groupColors.length]
        })
        return groups
      },
      paths () {
        const paths = []
        this.results.categories.forEach(category => {
          this.results.subsets.forEach(subset => {
            let path = ''
            this.results.stats[category][subset].estimate.forEach((d, i, arr) => {
              const stats = this.results.stats[category][subset]
              const x = this.scales.x(stats.timeline[i])
              if (i === 0) {
                path += `M ${x} ${this.scales.y(d)}`
              } else {
                path += `L ${x} ${this.scales.y(arr[i - 1])}`
                path += `L ${x} ${this.scales.y(d)}`
              }
            })
            paths.push({
              d: path,
              color: this.groups[this.getGroupName(category, subset)].color
            })
          })
        })
        return paths
      },
      ciPaths () {
        const paths = []
        this.results.categories.forEach(category => {
          this.results.subsets.forEach(subset => {
            let path = ''
            let backpath = ' Z '
            this.results.stats[category][subset].ci_upper.forEach((d, i, arr) => {
              const stats = this.results.stats[category][subset]
              const x = this.scales.x(stats.timeline[i])
              if (i === 0) {
                return true
              } else if (i === 1) {
                path += `M ${x} ${this.scales.y(d)}`
              } else {
                path += `L ${x} ${this.scales.y(arr[i - 1])}`
                path += `L ${x} ${this.scales.y(d)}`
                backpath = ` L ${x} ${this.scales.y(stats.ci_lower[i - 1])}` + backpath
                backpath = ` L ${x} ${this.scales.y(stats.ci_lower[i])}` + backpath
              }
            })
            paths.push({
              d: path + backpath,
              color: this.groups[this.getGroupName(category, subset)].color
            })
          })
        })
        return paths
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
      }
    },
    methods: {
      updateDurationVariable (ids) {
        this.durationVariables = ids
      },
      updateGroupVariable (ids) {
        this.groupVariables = ids
      },
      updateObservedVariable (ids) {
        this.observedVariables = ids
      },
      resize (width, height) {
        this.width = width
        this.height = height
      },
      runAnalysisWrapper (args) {
        this.runAnalysis('survival-analysis', args)
          .then(response => {
            const results = JSON.parse(response)
            deepFreeze(results) // massively improve performance by telling Vue that the objects properties won't change
            this.results = results
          })
          .catch(error => console.error(error))
220
221
222
      },
      getGroupName (category, subset) {
        return `${category} [s${subset + 1}]`
223
224
225
226
227
228
229
230
231
      }
    },
    watch: {
      'args': {
        handler: function (newArgs) {
          if (this.validArgs) {
            this.runAnalysisWrapper(newArgs)
          }
        }
232
233
234
235
236
237
238
239
240
241
      },
      'axis': {
        handler: function (newAxis) {
          this.$nextTick(() => {
            d3.select(this.$refs.xAxis1).call(newAxis.x1)
            d3.select(this.$refs.yAxis1).call(newAxis.y1)
            d3.select(this.$refs.yAxis2).call(newAxis.y2)
            d3.select(this.$refs.xAxis2).call(newAxis.x2)
          })
        }
242
243
244
245
246
247
248
249
      }
    }
  }
</script>

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

250
251
252
253
254
255
256
257
258
    svg
        .fjs-paths
            path
                shape-rendering: crispEdges
            .fjs-estimate-path
                stroke-width: 2px
                fill: none
            .fjs-ci-path
                opacity: 0.4
259
260
261
262
</style>

<!--CSS for dynamically created components-->
<style lang="sass">
263
    @import '~assets/d3.sass'
264

265
266
267
268
269
270
271
272
273
274
    .fjs-axis
        shape-rendering: crispEdges
        .tick
            shape-rendering: crispEdges
            line
                stroke: #E2E2E2
            text
                font-size: 0.75em
        path
            stroke: none
275
</style>