Mercurial > gemma
annotate client/src/components/gauge/Waterlevel.vue @ 2691:023a607c4bcc
client: waterlevel diagram: fixed tooltip display while moving/zooming the chart
and added fade animation
author | Markus Kottlaender <markus@intevation.de> |
---|---|
date | Fri, 15 Mar 2019 16:59:15 +0100 |
parents | 8f919fe629f9 |
children | 04b082a86d50 |
rev | line source |
---|---|
2590
1686ec185155
client: added gauge waterlevel example diagram
Markus Kottlaender <markus@intevation.de>
parents:
diff
changeset
|
1 <template> |
1686ec185155
client: added gauge waterlevel example diagram
Markus Kottlaender <markus@intevation.de>
parents:
diff
changeset
|
2 <div class="flex-fill diagram-container"></div> |
1686ec185155
client: added gauge waterlevel example diagram
Markus Kottlaender <markus@intevation.de>
parents:
diff
changeset
|
3 </template> |
1686ec185155
client: added gauge waterlevel example diagram
Markus Kottlaender <markus@intevation.de>
parents:
diff
changeset
|
4 |
1686ec185155
client: added gauge waterlevel example diagram
Markus Kottlaender <markus@intevation.de>
parents:
diff
changeset
|
5 <style lang="sass" scoped> |
1686ec185155
client: added gauge waterlevel example diagram
Markus Kottlaender <markus@intevation.de>
parents:
diff
changeset
|
6 .diagram-container |
2604
85f9bf4a6eba
client: gauge waterlevel diagram: draw reference waterlevels
Markus Kottlaender <markus@intevation.de>
parents:
2596
diff
changeset
|
7 /deep/ |
2684
c4da269238a4
client: waterlevel diagram: visualize data gaps
Markus Kottlaender <markus@intevation.de>
parents:
2653
diff
changeset
|
8 .main |
c4da269238a4
client: waterlevel diagram: visualize data gaps
Markus Kottlaender <markus@intevation.de>
parents:
2653
diff
changeset
|
9 .line |
c4da269238a4
client: waterlevel diagram: visualize data gaps
Markus Kottlaender <markus@intevation.de>
parents:
2653
diff
changeset
|
10 clip-path: url(#clip) |
c4da269238a4
client: waterlevel diagram: visualize data gaps
Markus Kottlaender <markus@intevation.de>
parents:
2653
diff
changeset
|
11 .nav |
c4da269238a4
client: waterlevel diagram: visualize data gaps
Markus Kottlaender <markus@intevation.de>
parents:
2653
diff
changeset
|
12 .line |
c4da269238a4
client: waterlevel diagram: visualize data gaps
Markus Kottlaender <markus@intevation.de>
parents:
2653
diff
changeset
|
13 stroke: steelblue |
c4da269238a4
client: waterlevel diagram: visualize data gaps
Markus Kottlaender <markus@intevation.de>
parents:
2653
diff
changeset
|
14 stroke-width: 2 |
c4da269238a4
client: waterlevel diagram: visualize data gaps
Markus Kottlaender <markus@intevation.de>
parents:
2653
diff
changeset
|
15 fill: transparent |
c4da269238a4
client: waterlevel diagram: visualize data gaps
Markus Kottlaender <markus@intevation.de>
parents:
2653
diff
changeset
|
16 clip-path: url(#clip) |
2604
85f9bf4a6eba
client: gauge waterlevel diagram: draw reference waterlevels
Markus Kottlaender <markus@intevation.de>
parents:
2596
diff
changeset
|
17 |
85f9bf4a6eba
client: gauge waterlevel diagram: draw reference waterlevels
Markus Kottlaender <markus@intevation.de>
parents:
2596
diff
changeset
|
18 .hdc-line, |
85f9bf4a6eba
client: gauge waterlevel diagram: draw reference waterlevels
Markus Kottlaender <markus@intevation.de>
parents:
2596
diff
changeset
|
19 .ldc-line, |
85f9bf4a6eba
client: gauge waterlevel diagram: draw reference waterlevels
Markus Kottlaender <markus@intevation.de>
parents:
2596
diff
changeset
|
20 .mw-line |
85f9bf4a6eba
client: gauge waterlevel diagram: draw reference waterlevels
Markus Kottlaender <markus@intevation.de>
parents:
2596
diff
changeset
|
21 stroke-width: 1 |
85f9bf4a6eba
client: gauge waterlevel diagram: draw reference waterlevels
Markus Kottlaender <markus@intevation.de>
parents:
2596
diff
changeset
|
22 fill: transparent |
85f9bf4a6eba
client: gauge waterlevel diagram: draw reference waterlevels
Markus Kottlaender <markus@intevation.de>
parents:
2596
diff
changeset
|
23 clip-path: url(#clip) |
85f9bf4a6eba
client: gauge waterlevel diagram: draw reference waterlevels
Markus Kottlaender <markus@intevation.de>
parents:
2596
diff
changeset
|
24 .hdc-line |
85f9bf4a6eba
client: gauge waterlevel diagram: draw reference waterlevels
Markus Kottlaender <markus@intevation.de>
parents:
2596
diff
changeset
|
25 stroke: red |
85f9bf4a6eba
client: gauge waterlevel diagram: draw reference waterlevels
Markus Kottlaender <markus@intevation.de>
parents:
2596
diff
changeset
|
26 .ldc-line |
85f9bf4a6eba
client: gauge waterlevel diagram: draw reference waterlevels
Markus Kottlaender <markus@intevation.de>
parents:
2596
diff
changeset
|
27 stroke: green |
85f9bf4a6eba
client: gauge waterlevel diagram: draw reference waterlevels
Markus Kottlaender <markus@intevation.de>
parents:
2596
diff
changeset
|
28 .mw-line |
85f9bf4a6eba
client: gauge waterlevel diagram: draw reference waterlevels
Markus Kottlaender <markus@intevation.de>
parents:
2596
diff
changeset
|
29 stroke: grey |
2611
e8c97481438f
client: waterlevel diagram: added labels for reference water levels
Markus Kottlaender <markus@intevation.de>
parents:
2610
diff
changeset
|
30 .ref-waterlevel-label |
e8c97481438f
client: waterlevel diagram: added labels for reference water levels
Markus Kottlaender <markus@intevation.de>
parents:
2610
diff
changeset
|
31 font-size: 11px |
e8c97481438f
client: waterlevel diagram: added labels for reference water levels
Markus Kottlaender <markus@intevation.de>
parents:
2610
diff
changeset
|
32 fill: #999 |
2645
6374fc404fdc
client: waterlevel diagram: draw filled area between HDC and LDC
Markus Kottlaender <markus@intevation.de>
parents:
2642
diff
changeset
|
33 .hdc-ldc-area |
6374fc404fdc
client: waterlevel diagram: draw filled area between HDC and LDC
Markus Kottlaender <markus@intevation.de>
parents:
2642
diff
changeset
|
34 fill: rgba(0, 255, 0, 0.15) |
2648
4ffae834276e
client: waterlevel diagram: added background grid to chart
Markus Kottlaender <markus@intevation.de>
parents:
2646
diff
changeset
|
35 |
4ffae834276e
client: waterlevel diagram: added background grid to chart
Markus Kottlaender <markus@intevation.de>
parents:
2646
diff
changeset
|
36 .tick |
4ffae834276e
client: waterlevel diagram: added background grid to chart
Markus Kottlaender <markus@intevation.de>
parents:
2646
diff
changeset
|
37 line |
4ffae834276e
client: waterlevel diagram: added background grid to chart
Markus Kottlaender <markus@intevation.de>
parents:
2646
diff
changeset
|
38 stroke-dasharray: 5 |
4ffae834276e
client: waterlevel diagram: added background grid to chart
Markus Kottlaender <markus@intevation.de>
parents:
2646
diff
changeset
|
39 stroke: #ccc |
4ffae834276e
client: waterlevel diagram: added background grid to chart
Markus Kottlaender <markus@intevation.de>
parents:
2646
diff
changeset
|
40 |
4ffae834276e
client: waterlevel diagram: added background grid to chart
Markus Kottlaender <markus@intevation.de>
parents:
2646
diff
changeset
|
41 .zoom |
4ffae834276e
client: waterlevel diagram: added background grid to chart
Markus Kottlaender <markus@intevation.de>
parents:
2646
diff
changeset
|
42 cursor: move |
4ffae834276e
client: waterlevel diagram: added background grid to chart
Markus Kottlaender <markus@intevation.de>
parents:
2646
diff
changeset
|
43 fill: none |
4ffae834276e
client: waterlevel diagram: added background grid to chart
Markus Kottlaender <markus@intevation.de>
parents:
2646
diff
changeset
|
44 pointer-events: all |
2607
ee60096c7eb6
client: gauge waterlevel diagram: improved style of time selection
Markus Kottlaender <markus@intevation.de>
parents:
2604
diff
changeset
|
45 .brush |
ee60096c7eb6
client: gauge waterlevel diagram: improved style of time selection
Markus Kottlaender <markus@intevation.de>
parents:
2604
diff
changeset
|
46 .selection |
ee60096c7eb6
client: gauge waterlevel diagram: improved style of time selection
Markus Kottlaender <markus@intevation.de>
parents:
2604
diff
changeset
|
47 stroke: transparent |
ee60096c7eb6
client: gauge waterlevel diagram: improved style of time selection
Markus Kottlaender <markus@intevation.de>
parents:
2604
diff
changeset
|
48 fill-opacity: 0.2 |
ee60096c7eb6
client: gauge waterlevel diagram: improved style of time selection
Markus Kottlaender <markus@intevation.de>
parents:
2604
diff
changeset
|
49 .handle |
2646
30697735a7bc
client: waterlevel diagram: fixed handle style in navigation chart
Markus Kottlaender <markus@intevation.de>
parents:
2645
diff
changeset
|
50 stroke: rgba($color-info, 0.5) |
2607
ee60096c7eb6
client: gauge waterlevel diagram: improved style of time selection
Markus Kottlaender <markus@intevation.de>
parents:
2604
diff
changeset
|
51 fill: rgba($color-info, 0.5) |
2689
8f919fe629f9
client: waterlevel diagram: added tooltips showing date and waterlevel on the chart
Markus Kottlaender <markus@intevation.de>
parents:
2684
diff
changeset
|
52 |
8f919fe629f9
client: waterlevel diagram: added tooltips showing date and waterlevel on the chart
Markus Kottlaender <markus@intevation.de>
parents:
2684
diff
changeset
|
53 .chart-dots |
8f919fe629f9
client: waterlevel diagram: added tooltips showing date and waterlevel on the chart
Markus Kottlaender <markus@intevation.de>
parents:
2684
diff
changeset
|
54 clip-path: url(#clip) |
8f919fe629f9
client: waterlevel diagram: added tooltips showing date and waterlevel on the chart
Markus Kottlaender <markus@intevation.de>
parents:
2684
diff
changeset
|
55 .chart-dot |
8f919fe629f9
client: waterlevel diagram: added tooltips showing date and waterlevel on the chart
Markus Kottlaender <markus@intevation.de>
parents:
2684
diff
changeset
|
56 fill: steelblue |
8f919fe629f9
client: waterlevel diagram: added tooltips showing date and waterlevel on the chart
Markus Kottlaender <markus@intevation.de>
parents:
2684
diff
changeset
|
57 stroke: steelblue |
8f919fe629f9
client: waterlevel diagram: added tooltips showing date and waterlevel on the chart
Markus Kottlaender <markus@intevation.de>
parents:
2684
diff
changeset
|
58 pointer-events: none |
2691
023a607c4bcc
client: waterlevel diagram: fixed tooltip display while moving/zooming the chart
Markus Kottlaender <markus@intevation.de>
parents:
2689
diff
changeset
|
59 transition: opacity 0.1s |
2689
8f919fe629f9
client: waterlevel diagram: added tooltips showing date and waterlevel on the chart
Markus Kottlaender <markus@intevation.de>
parents:
2684
diff
changeset
|
60 .chart-tooltip |
2691
023a607c4bcc
client: waterlevel diagram: fixed tooltip display while moving/zooming the chart
Markus Kottlaender <markus@intevation.de>
parents:
2689
diff
changeset
|
61 transition: opacity 0.3s |
2689
8f919fe629f9
client: waterlevel diagram: added tooltips showing date and waterlevel on the chart
Markus Kottlaender <markus@intevation.de>
parents:
2684
diff
changeset
|
62 rect |
8f919fe629f9
client: waterlevel diagram: added tooltips showing date and waterlevel on the chart
Markus Kottlaender <markus@intevation.de>
parents:
2684
diff
changeset
|
63 fill: #fff |
8f919fe629f9
client: waterlevel diagram: added tooltips showing date and waterlevel on the chart
Markus Kottlaender <markus@intevation.de>
parents:
2684
diff
changeset
|
64 stroke: #ccc |
8f919fe629f9
client: waterlevel diagram: added tooltips showing date and waterlevel on the chart
Markus Kottlaender <markus@intevation.de>
parents:
2684
diff
changeset
|
65 text |
8f919fe629f9
client: waterlevel diagram: added tooltips showing date and waterlevel on the chart
Markus Kottlaender <markus@intevation.de>
parents:
2684
diff
changeset
|
66 fill: #666 |
8f919fe629f9
client: waterlevel diagram: added tooltips showing date and waterlevel on the chart
Markus Kottlaender <markus@intevation.de>
parents:
2684
diff
changeset
|
67 font-size: 12px |
2590
1686ec185155
client: added gauge waterlevel example diagram
Markus Kottlaender <markus@intevation.de>
parents:
diff
changeset
|
68 </style> |
1686ec185155
client: added gauge waterlevel example diagram
Markus Kottlaender <markus@intevation.de>
parents:
diff
changeset
|
69 |
1686ec185155
client: added gauge waterlevel example diagram
Markus Kottlaender <markus@intevation.de>
parents:
diff
changeset
|
70 <script> |
1686ec185155
client: added gauge waterlevel example diagram
Markus Kottlaender <markus@intevation.de>
parents:
diff
changeset
|
71 /* This is Free Software under GNU Affero General Public License v >= 3.0 |
1686ec185155
client: added gauge waterlevel example diagram
Markus Kottlaender <markus@intevation.de>
parents:
diff
changeset
|
72 * without warranty, see README.md and license for details. |
1686ec185155
client: added gauge waterlevel example diagram
Markus Kottlaender <markus@intevation.de>
parents:
diff
changeset
|
73 * |
1686ec185155
client: added gauge waterlevel example diagram
Markus Kottlaender <markus@intevation.de>
parents:
diff
changeset
|
74 * SPDX-License-Identifier: AGPL-3.0-or-later |
1686ec185155
client: added gauge waterlevel example diagram
Markus Kottlaender <markus@intevation.de>
parents:
diff
changeset
|
75 * License-Filename: LICENSES/AGPL-3.0.txt |
1686ec185155
client: added gauge waterlevel example diagram
Markus Kottlaender <markus@intevation.de>
parents:
diff
changeset
|
76 * |
1686ec185155
client: added gauge waterlevel example diagram
Markus Kottlaender <markus@intevation.de>
parents:
diff
changeset
|
77 * Copyright (C) 2018 by via donau |
1686ec185155
client: added gauge waterlevel example diagram
Markus Kottlaender <markus@intevation.de>
parents:
diff
changeset
|
78 * – Österreichische Wasserstraßen-Gesellschaft mbH |
1686ec185155
client: added gauge waterlevel example diagram
Markus Kottlaender <markus@intevation.de>
parents:
diff
changeset
|
79 * Software engineering by Intevation GmbH |
1686ec185155
client: added gauge waterlevel example diagram
Markus Kottlaender <markus@intevation.de>
parents:
diff
changeset
|
80 * |
1686ec185155
client: added gauge waterlevel example diagram
Markus Kottlaender <markus@intevation.de>
parents:
diff
changeset
|
81 * Author(s): |
1686ec185155
client: added gauge waterlevel example diagram
Markus Kottlaender <markus@intevation.de>
parents:
diff
changeset
|
82 * Markus Kottländer <markus.kottlaender@intevation.de> |
1686ec185155
client: added gauge waterlevel example diagram
Markus Kottlaender <markus@intevation.de>
parents:
diff
changeset
|
83 */ |
1686ec185155
client: added gauge waterlevel example diagram
Markus Kottlaender <markus@intevation.de>
parents:
diff
changeset
|
84 |
2604
85f9bf4a6eba
client: gauge waterlevel diagram: draw reference waterlevels
Markus Kottlaender <markus@intevation.de>
parents:
2596
diff
changeset
|
85 import { mapState, mapGetters } from "vuex"; |
2684
c4da269238a4
client: waterlevel diagram: visualize data gaps
Markus Kottlaender <markus@intevation.de>
parents:
2653
diff
changeset
|
86 import * as d3Base from "d3"; |
c4da269238a4
client: waterlevel diagram: visualize data gaps
Markus Kottlaender <markus@intevation.de>
parents:
2653
diff
changeset
|
87 import { lineChunked } from "d3-line-chunked"; |
2590
1686ec185155
client: added gauge waterlevel example diagram
Markus Kottlaender <markus@intevation.de>
parents:
diff
changeset
|
88 import debounce from "debounce"; |
1686ec185155
client: added gauge waterlevel example diagram
Markus Kottlaender <markus@intevation.de>
parents:
diff
changeset
|
89 |
2684
c4da269238a4
client: waterlevel diagram: visualize data gaps
Markus Kottlaender <markus@intevation.de>
parents:
2653
diff
changeset
|
90 // we should load only d3 modules we need but for now we'll go with the lazy way |
c4da269238a4
client: waterlevel diagram: visualize data gaps
Markus Kottlaender <markus@intevation.de>
parents:
2653
diff
changeset
|
91 // https://www.giacomodebidda.com/how-to-import-d3-plugins-with-webpack/ |
c4da269238a4
client: waterlevel diagram: visualize data gaps
Markus Kottlaender <markus@intevation.de>
parents:
2653
diff
changeset
|
92 const d3 = Object.assign(d3Base, { lineChunked }); |
c4da269238a4
client: waterlevel diagram: visualize data gaps
Markus Kottlaender <markus@intevation.de>
parents:
2653
diff
changeset
|
93 |
2590
1686ec185155
client: added gauge waterlevel example diagram
Markus Kottlaender <markus@intevation.de>
parents:
diff
changeset
|
94 export default { |
1686ec185155
client: added gauge waterlevel example diagram
Markus Kottlaender <markus@intevation.de>
parents:
diff
changeset
|
95 computed: { |
2604
85f9bf4a6eba
client: gauge waterlevel diagram: draw reference waterlevels
Markus Kottlaender <markus@intevation.de>
parents:
2596
diff
changeset
|
96 ...mapState("gauges", ["waterlevels"]), |
85f9bf4a6eba
client: gauge waterlevel diagram: draw reference waterlevels
Markus Kottlaender <markus@intevation.de>
parents:
2596
diff
changeset
|
97 ...mapGetters("gauges", ["selectedGauge"]) |
2596
8774054959a7
client: added Gauges dialog/tool to show waterlevel diagrams
Markus Kottlaender <markus@intevation.de>
parents:
2590
diff
changeset
|
98 }, |
8774054959a7
client: added Gauges dialog/tool to show waterlevel diagrams
Markus Kottlaender <markus@intevation.de>
parents:
2590
diff
changeset
|
99 watch: { |
2590
1686ec185155
client: added gauge waterlevel example diagram
Markus Kottlaender <markus@intevation.de>
parents:
diff
changeset
|
100 waterlevels() { |
2596
8774054959a7
client: added Gauges dialog/tool to show waterlevel diagrams
Markus Kottlaender <markus@intevation.de>
parents:
2590
diff
changeset
|
101 this.drawDiagram(); |
2590
1686ec185155
client: added gauge waterlevel example diagram
Markus Kottlaender <markus@intevation.de>
parents:
diff
changeset
|
102 } |
1686ec185155
client: added gauge waterlevel example diagram
Markus Kottlaender <markus@intevation.de>
parents:
diff
changeset
|
103 }, |
1686ec185155
client: added gauge waterlevel example diagram
Markus Kottlaender <markus@intevation.de>
parents:
diff
changeset
|
104 methods: { |
1686ec185155
client: added gauge waterlevel example diagram
Markus Kottlaender <markus@intevation.de>
parents:
diff
changeset
|
105 drawDiagram() { |
2604
85f9bf4a6eba
client: gauge waterlevel diagram: draw reference waterlevels
Markus Kottlaender <markus@intevation.de>
parents:
2596
diff
changeset
|
106 if (!this.selectedGauge || !this.waterlevels.length) return; |
85f9bf4a6eba
client: gauge waterlevel diagram: draw reference waterlevels
Markus Kottlaender <markus@intevation.de>
parents:
2596
diff
changeset
|
107 |
2610
5ce1b4b29869
client: waterlevel diagram: refactored code for better readability
Markus Kottlaender <markus@intevation.de>
parents:
2607
diff
changeset
|
108 // remove old diagram |
5ce1b4b29869
client: waterlevel diagram: refactored code for better readability
Markus Kottlaender <markus@intevation.de>
parents:
2607
diff
changeset
|
109 d3.select(".diagram-container svg").remove(); |
5ce1b4b29869
client: waterlevel diagram: refactored code for better readability
Markus Kottlaender <markus@intevation.de>
parents:
2607
diff
changeset
|
110 |
5ce1b4b29869
client: waterlevel diagram: refactored code for better readability
Markus Kottlaender <markus@intevation.de>
parents:
2607
diff
changeset
|
111 // get HDC/LDC/MW of the gauge |
2604
85f9bf4a6eba
client: gauge waterlevel diagram: draw reference waterlevels
Markus Kottlaender <markus@intevation.de>
parents:
2596
diff
changeset
|
112 let refWaterLevels = JSON.parse( |
85f9bf4a6eba
client: gauge waterlevel diagram: draw reference waterlevels
Markus Kottlaender <markus@intevation.de>
parents:
2596
diff
changeset
|
113 this.selectedGauge.properties.reference_water_levels |
85f9bf4a6eba
client: gauge waterlevel diagram: draw reference waterlevels
Markus Kottlaender <markus@intevation.de>
parents:
2596
diff
changeset
|
114 ); |
85f9bf4a6eba
client: gauge waterlevel diagram: draw reference waterlevels
Markus Kottlaender <markus@intevation.de>
parents:
2596
diff
changeset
|
115 |
2610
5ce1b4b29869
client: waterlevel diagram: refactored code for better readability
Markus Kottlaender <markus@intevation.de>
parents:
2607
diff
changeset
|
116 // CREATE SVG AND SET DIMENSIONS/MARGINS |
5ce1b4b29869
client: waterlevel diagram: refactored code for better readability
Markus Kottlaender <markus@intevation.de>
parents:
2607
diff
changeset
|
117 |
2604
85f9bf4a6eba
client: gauge waterlevel diagram: draw reference waterlevels
Markus Kottlaender <markus@intevation.de>
parents:
2596
diff
changeset
|
118 let svgWidth = document.querySelector(".diagram-container").clientWidth; |
85f9bf4a6eba
client: gauge waterlevel diagram: draw reference waterlevels
Markus Kottlaender <markus@intevation.de>
parents:
2596
diff
changeset
|
119 let svgHeight = document.querySelector(".diagram-container").clientHeight; |
2610
5ce1b4b29869
client: waterlevel diagram: refactored code for better readability
Markus Kottlaender <markus@intevation.de>
parents:
2607
diff
changeset
|
120 let svg = d3 |
5ce1b4b29869
client: waterlevel diagram: refactored code for better readability
Markus Kottlaender <markus@intevation.de>
parents:
2607
diff
changeset
|
121 .select(".diagram-container") |
5ce1b4b29869
client: waterlevel diagram: refactored code for better readability
Markus Kottlaender <markus@intevation.de>
parents:
2607
diff
changeset
|
122 .append("svg") |
5ce1b4b29869
client: waterlevel diagram: refactored code for better readability
Markus Kottlaender <markus@intevation.de>
parents:
2607
diff
changeset
|
123 .attr("width", "100%") |
5ce1b4b29869
client: waterlevel diagram: refactored code for better readability
Markus Kottlaender <markus@intevation.de>
parents:
2607
diff
changeset
|
124 .attr("height", "100%"); |
2653
7fd47d9641ac
client: waterlevel diagram: made diagram a bit higher
Markus Kottlaender <markus@intevation.de>
parents:
2652
diff
changeset
|
125 let mainMargin = { top: 20, right: 20, bottom: 110, left: 80 }, |
2610
5ce1b4b29869
client: waterlevel diagram: refactored code for better readability
Markus Kottlaender <markus@intevation.de>
parents:
2607
diff
changeset
|
126 navMargin = { |
2653
7fd47d9641ac
client: waterlevel diagram: made diagram a bit higher
Markus Kottlaender <markus@intevation.de>
parents:
2652
diff
changeset
|
127 top: svgHeight - mainMargin.top - 65, |
2590
1686ec185155
client: added gauge waterlevel example diagram
Markus Kottlaender <markus@intevation.de>
parents:
diff
changeset
|
128 right: 20, |
1686ec185155
client: added gauge waterlevel example diagram
Markus Kottlaender <markus@intevation.de>
parents:
diff
changeset
|
129 bottom: 30, |
2641
a735119e4f5c
client: waterlevel diagram: added y-axis label
Markus Kottlaender <markus@intevation.de>
parents:
2611
diff
changeset
|
130 left: 80 |
2590
1686ec185155
client: added gauge waterlevel example diagram
Markus Kottlaender <markus@intevation.de>
parents:
diff
changeset
|
131 }, |
2610
5ce1b4b29869
client: waterlevel diagram: refactored code for better readability
Markus Kottlaender <markus@intevation.de>
parents:
2607
diff
changeset
|
132 width = +svgWidth - mainMargin.left - mainMargin.right, |
5ce1b4b29869
client: waterlevel diagram: refactored code for better readability
Markus Kottlaender <markus@intevation.de>
parents:
2607
diff
changeset
|
133 mainHeight = +svgHeight - mainMargin.top - mainMargin.bottom, |
5ce1b4b29869
client: waterlevel diagram: refactored code for better readability
Markus Kottlaender <markus@intevation.de>
parents:
2607
diff
changeset
|
134 navHeight = +svgHeight - navMargin.top - navMargin.bottom; |
2590
1686ec185155
client: added gauge waterlevel example diagram
Markus Kottlaender <markus@intevation.de>
parents:
diff
changeset
|
135 |
2610
5ce1b4b29869
client: waterlevel diagram: refactored code for better readability
Markus Kottlaender <markus@intevation.de>
parents:
2607
diff
changeset
|
136 // PREPARING AXES/SCALING |
5ce1b4b29869
client: waterlevel diagram: refactored code for better readability
Markus Kottlaender <markus@intevation.de>
parents:
2607
diff
changeset
|
137 |
5ce1b4b29869
client: waterlevel diagram: refactored code for better readability
Markus Kottlaender <markus@intevation.de>
parents:
2607
diff
changeset
|
138 // scaling helpers to convert real values to pixels |
5ce1b4b29869
client: waterlevel diagram: refactored code for better readability
Markus Kottlaender <markus@intevation.de>
parents:
2607
diff
changeset
|
139 // based on the diagrams dimensions |
2604
85f9bf4a6eba
client: gauge waterlevel diagram: draw reference waterlevels
Markus Kottlaender <markus@intevation.de>
parents:
2596
diff
changeset
|
140 let x = d3.scaleTime().range([0, width]), |
2590
1686ec185155
client: added gauge waterlevel example diagram
Markus Kottlaender <markus@intevation.de>
parents:
diff
changeset
|
141 x2 = d3.scaleTime().range([0, width]), |
2610
5ce1b4b29869
client: waterlevel diagram: refactored code for better readability
Markus Kottlaender <markus@intevation.de>
parents:
2607
diff
changeset
|
142 y = d3.scaleLinear().range([mainHeight, 0]), |
5ce1b4b29869
client: waterlevel diagram: refactored code for better readability
Markus Kottlaender <markus@intevation.de>
parents:
2607
diff
changeset
|
143 y2 = d3.scaleLinear().range([navHeight, 0]); |
2604
85f9bf4a6eba
client: gauge waterlevel diagram: draw reference waterlevels
Markus Kottlaender <markus@intevation.de>
parents:
2596
diff
changeset
|
144 // find min/max values for the waterlevel axis |
2610
5ce1b4b29869
client: waterlevel diagram: refactored code for better readability
Markus Kottlaender <markus@intevation.de>
parents:
2607
diff
changeset
|
145 // including hdc/ldc (+/- 100 cm) |
5ce1b4b29869
client: waterlevel diagram: refactored code for better readability
Markus Kottlaender <markus@intevation.de>
parents:
2607
diff
changeset
|
146 let WaterlevelMinMax = d3.extent( |
2604
85f9bf4a6eba
client: gauge waterlevel diagram: draw reference waterlevels
Markus Kottlaender <markus@intevation.de>
parents:
2596
diff
changeset
|
147 [ |
85f9bf4a6eba
client: gauge waterlevel diagram: draw reference waterlevels
Markus Kottlaender <markus@intevation.de>
parents:
2596
diff
changeset
|
148 ...this.waterlevels, |
85f9bf4a6eba
client: gauge waterlevel diagram: draw reference waterlevels
Markus Kottlaender <markus@intevation.de>
parents:
2596
diff
changeset
|
149 { waterlevel: refWaterLevels.HDC + 100 }, |
85f9bf4a6eba
client: gauge waterlevel diagram: draw reference waterlevels
Markus Kottlaender <markus@intevation.de>
parents:
2596
diff
changeset
|
150 { waterlevel: Math.max(refWaterLevels.LDC - 100, 0) } |
85f9bf4a6eba
client: gauge waterlevel diagram: draw reference waterlevels
Markus Kottlaender <markus@intevation.de>
parents:
2596
diff
changeset
|
151 ], |
2610
5ce1b4b29869
client: waterlevel diagram: refactored code for better readability
Markus Kottlaender <markus@intevation.de>
parents:
2607
diff
changeset
|
152 d => d.waterlevel |
2604
85f9bf4a6eba
client: gauge waterlevel diagram: draw reference waterlevels
Markus Kottlaender <markus@intevation.de>
parents:
2596
diff
changeset
|
153 ); |
2610
5ce1b4b29869
client: waterlevel diagram: refactored code for better readability
Markus Kottlaender <markus@intevation.de>
parents:
2607
diff
changeset
|
154 // setting the min and max values for the diagram axes |
5ce1b4b29869
client: waterlevel diagram: refactored code for better readability
Markus Kottlaender <markus@intevation.de>
parents:
2607
diff
changeset
|
155 x.domain(d3.extent(this.waterlevels, d => d.date)); |
5ce1b4b29869
client: waterlevel diagram: refactored code for better readability
Markus Kottlaender <markus@intevation.de>
parents:
2607
diff
changeset
|
156 y.domain(WaterlevelMinMax); |
2604
85f9bf4a6eba
client: gauge waterlevel diagram: draw reference waterlevels
Markus Kottlaender <markus@intevation.de>
parents:
2596
diff
changeset
|
157 x2.domain(x.domain()); |
85f9bf4a6eba
client: gauge waterlevel diagram: draw reference waterlevels
Markus Kottlaender <markus@intevation.de>
parents:
2596
diff
changeset
|
158 y2.domain(y.domain()); |
2610
5ce1b4b29869
client: waterlevel diagram: refactored code for better readability
Markus Kottlaender <markus@intevation.de>
parents:
2607
diff
changeset
|
159 // creating the axes based on these scales |
2648
4ffae834276e
client: waterlevel diagram: added background grid to chart
Markus Kottlaender <markus@intevation.de>
parents:
2646
diff
changeset
|
160 let xAxis = d3 |
4ffae834276e
client: waterlevel diagram: added background grid to chart
Markus Kottlaender <markus@intevation.de>
parents:
2646
diff
changeset
|
161 .axisTop(x) |
4ffae834276e
client: waterlevel diagram: added background grid to chart
Markus Kottlaender <markus@intevation.de>
parents:
2646
diff
changeset
|
162 .tickSizeInner(mainHeight) |
4ffae834276e
client: waterlevel diagram: added background grid to chart
Markus Kottlaender <markus@intevation.de>
parents:
2646
diff
changeset
|
163 .tickSizeOuter(0); |
4ffae834276e
client: waterlevel diagram: added background grid to chart
Markus Kottlaender <markus@intevation.de>
parents:
2646
diff
changeset
|
164 let xAxis2 = d3.axisBottom(x2); |
4ffae834276e
client: waterlevel diagram: added background grid to chart
Markus Kottlaender <markus@intevation.de>
parents:
2646
diff
changeset
|
165 let yAxis = d3 |
4ffae834276e
client: waterlevel diagram: added background grid to chart
Markus Kottlaender <markus@intevation.de>
parents:
2646
diff
changeset
|
166 .axisRight(y) |
4ffae834276e
client: waterlevel diagram: added background grid to chart
Markus Kottlaender <markus@intevation.de>
parents:
2646
diff
changeset
|
167 .tickSizeInner(width) |
4ffae834276e
client: waterlevel diagram: added background grid to chart
Markus Kottlaender <markus@intevation.de>
parents:
2646
diff
changeset
|
168 .tickSizeOuter(0); |
2590
1686ec185155
client: added gauge waterlevel example diagram
Markus Kottlaender <markus@intevation.de>
parents:
diff
changeset
|
169 |
2610
5ce1b4b29869
client: waterlevel diagram: refactored code for better readability
Markus Kottlaender <markus@intevation.de>
parents:
2607
diff
changeset
|
170 // PREPARING CHART FUNCTIONS |
2590
1686ec185155
client: added gauge waterlevel example diagram
Markus Kottlaender <markus@intevation.de>
parents:
diff
changeset
|
171 |
2604
85f9bf4a6eba
client: gauge waterlevel diagram: draw reference waterlevels
Markus Kottlaender <markus@intevation.de>
parents:
2596
diff
changeset
|
172 // waterlevel line in big chart |
2684
c4da269238a4
client: waterlevel diagram: visualize data gaps
Markus Kottlaender <markus@intevation.de>
parents:
2653
diff
changeset
|
173 // d3-line-chunked plugin: https://github.com/pbeshai/d3-line-chunked |
c4da269238a4
client: waterlevel diagram: visualize data gaps
Markus Kottlaender <markus@intevation.de>
parents:
2653
diff
changeset
|
174 var mainLineChart = d3 |
c4da269238a4
client: waterlevel diagram: visualize data gaps
Markus Kottlaender <markus@intevation.de>
parents:
2653
diff
changeset
|
175 .lineChunked() |
2610
5ce1b4b29869
client: waterlevel diagram: refactored code for better readability
Markus Kottlaender <markus@intevation.de>
parents:
2607
diff
changeset
|
176 .x(d => x(d.date)) |
2684
c4da269238a4
client: waterlevel diagram: visualize data gaps
Markus Kottlaender <markus@intevation.de>
parents:
2653
diff
changeset
|
177 .y(d => y(d.waterlevel)) |
c4da269238a4
client: waterlevel diagram: visualize data gaps
Markus Kottlaender <markus@intevation.de>
parents:
2653
diff
changeset
|
178 .curve(d3.curveLinear) |
c4da269238a4
client: waterlevel diagram: visualize data gaps
Markus Kottlaender <markus@intevation.de>
parents:
2653
diff
changeset
|
179 .isNext((prev, current) => { |
c4da269238a4
client: waterlevel diagram: visualize data gaps
Markus Kottlaender <markus@intevation.de>
parents:
2653
diff
changeset
|
180 // points are "next to each other" when they are exactly 15 minutes apart |
c4da269238a4
client: waterlevel diagram: visualize data gaps
Markus Kottlaender <markus@intevation.de>
parents:
2653
diff
changeset
|
181 return current.date - prev.date === 15 * 60 * 1000; |
c4da269238a4
client: waterlevel diagram: visualize data gaps
Markus Kottlaender <markus@intevation.de>
parents:
2653
diff
changeset
|
182 }) |
c4da269238a4
client: waterlevel diagram: visualize data gaps
Markus Kottlaender <markus@intevation.de>
parents:
2653
diff
changeset
|
183 .lineStyles({ stroke: "steelblue" }) |
c4da269238a4
client: waterlevel diagram: visualize data gaps
Markus Kottlaender <markus@intevation.de>
parents:
2653
diff
changeset
|
184 .gapStyles({ |
c4da269238a4
client: waterlevel diagram: visualize data gaps
Markus Kottlaender <markus@intevation.de>
parents:
2653
diff
changeset
|
185 stroke: "steelblue", |
c4da269238a4
client: waterlevel diagram: visualize data gaps
Markus Kottlaender <markus@intevation.de>
parents:
2653
diff
changeset
|
186 "stroke-opacity": 1, |
c4da269238a4
client: waterlevel diagram: visualize data gaps
Markus Kottlaender <markus@intevation.de>
parents:
2653
diff
changeset
|
187 "stroke-dasharray": "3 3", |
c4da269238a4
client: waterlevel diagram: visualize data gaps
Markus Kottlaender <markus@intevation.de>
parents:
2653
diff
changeset
|
188 "stroke-width": 1 |
c4da269238a4
client: waterlevel diagram: visualize data gaps
Markus Kottlaender <markus@intevation.de>
parents:
2653
diff
changeset
|
189 }); |
2604
85f9bf4a6eba
client: gauge waterlevel diagram: draw reference waterlevels
Markus Kottlaender <markus@intevation.de>
parents:
2596
diff
changeset
|
190 // waterlevel line in small chart |
2610
5ce1b4b29869
client: waterlevel diagram: refactored code for better readability
Markus Kottlaender <markus@intevation.de>
parents:
2607
diff
changeset
|
191 let navLineChart = d3 |
2590
1686ec185155
client: added gauge waterlevel example diagram
Markus Kottlaender <markus@intevation.de>
parents:
diff
changeset
|
192 .line() |
1686ec185155
client: added gauge waterlevel example diagram
Markus Kottlaender <markus@intevation.de>
parents:
diff
changeset
|
193 .curve(d3.curveMonotoneX) |
2610
5ce1b4b29869
client: waterlevel diagram: refactored code for better readability
Markus Kottlaender <markus@intevation.de>
parents:
2607
diff
changeset
|
194 .x(d => x2(d.date)) |
5ce1b4b29869
client: waterlevel diagram: refactored code for better readability
Markus Kottlaender <markus@intevation.de>
parents:
2607
diff
changeset
|
195 .y(d => y2(d.waterlevel)); |
5ce1b4b29869
client: waterlevel diagram: refactored code for better readability
Markus Kottlaender <markus@intevation.de>
parents:
2607
diff
changeset
|
196 // hdc/ldc/mw |
2604
85f9bf4a6eba
client: gauge waterlevel diagram: draw reference waterlevels
Markus Kottlaender <markus@intevation.de>
parents:
2596
diff
changeset
|
197 let refWaterlevelLine = d3 |
85f9bf4a6eba
client: gauge waterlevel diagram: draw reference waterlevels
Markus Kottlaender <markus@intevation.de>
parents:
2596
diff
changeset
|
198 .line() |
85f9bf4a6eba
client: gauge waterlevel diagram: draw reference waterlevels
Markus Kottlaender <markus@intevation.de>
parents:
2596
diff
changeset
|
199 .x(d => x(d.x)) |
85f9bf4a6eba
client: gauge waterlevel diagram: draw reference waterlevels
Markus Kottlaender <markus@intevation.de>
parents:
2596
diff
changeset
|
200 .y(d => y(d.y)); |
85f9bf4a6eba
client: gauge waterlevel diagram: draw reference waterlevels
Markus Kottlaender <markus@intevation.de>
parents:
2596
diff
changeset
|
201 |
2610
5ce1b4b29869
client: waterlevel diagram: refactored code for better readability
Markus Kottlaender <markus@intevation.de>
parents:
2607
diff
changeset
|
202 // DRAWING MAINCHART |
5ce1b4b29869
client: waterlevel diagram: refactored code for better readability
Markus Kottlaender <markus@intevation.de>
parents:
2607
diff
changeset
|
203 |
2641
a735119e4f5c
client: waterlevel diagram: added y-axis label
Markus Kottlaender <markus@intevation.de>
parents:
2611
diff
changeset
|
204 // define visible chart area |
2610
5ce1b4b29869
client: waterlevel diagram: refactored code for better readability
Markus Kottlaender <markus@intevation.de>
parents:
2607
diff
changeset
|
205 // everything outside this area will be hidden (clipped) |
2590
1686ec185155
client: added gauge waterlevel example diagram
Markus Kottlaender <markus@intevation.de>
parents:
diff
changeset
|
206 svg |
1686ec185155
client: added gauge waterlevel example diagram
Markus Kottlaender <markus@intevation.de>
parents:
diff
changeset
|
207 .append("defs") |
1686ec185155
client: added gauge waterlevel example diagram
Markus Kottlaender <markus@intevation.de>
parents:
diff
changeset
|
208 .append("clipPath") |
1686ec185155
client: added gauge waterlevel example diagram
Markus Kottlaender <markus@intevation.de>
parents:
diff
changeset
|
209 .attr("id", "clip") |
1686ec185155
client: added gauge waterlevel example diagram
Markus Kottlaender <markus@intevation.de>
parents:
diff
changeset
|
210 .append("rect") |
1686ec185155
client: added gauge waterlevel example diagram
Markus Kottlaender <markus@intevation.de>
parents:
diff
changeset
|
211 .attr("width", width) |
2610
5ce1b4b29869
client: waterlevel diagram: refactored code for better readability
Markus Kottlaender <markus@intevation.de>
parents:
2607
diff
changeset
|
212 .attr("height", mainHeight); |
2590
1686ec185155
client: added gauge waterlevel example diagram
Markus Kottlaender <markus@intevation.de>
parents:
diff
changeset
|
213 |
2610
5ce1b4b29869
client: waterlevel diagram: refactored code for better readability
Markus Kottlaender <markus@intevation.de>
parents:
2607
diff
changeset
|
214 let mainChart = svg |
2590
1686ec185155
client: added gauge waterlevel example diagram
Markus Kottlaender <markus@intevation.de>
parents:
diff
changeset
|
215 .append("g") |
2684
c4da269238a4
client: waterlevel diagram: visualize data gaps
Markus Kottlaender <markus@intevation.de>
parents:
2653
diff
changeset
|
216 .attr("class", "main") |
2610
5ce1b4b29869
client: waterlevel diagram: refactored code for better readability
Markus Kottlaender <markus@intevation.de>
parents:
2607
diff
changeset
|
217 .attr("transform", `translate(${mainMargin.left}, ${mainMargin.top})`); |
2590
1686ec185155
client: added gauge waterlevel example diagram
Markus Kottlaender <markus@intevation.de>
parents:
diff
changeset
|
218 |
2610
5ce1b4b29869
client: waterlevel diagram: refactored code for better readability
Markus Kottlaender <markus@intevation.de>
parents:
2607
diff
changeset
|
219 // axes |
5ce1b4b29869
client: waterlevel diagram: refactored code for better readability
Markus Kottlaender <markus@intevation.de>
parents:
2607
diff
changeset
|
220 mainChart |
2590
1686ec185155
client: added gauge waterlevel example diagram
Markus Kottlaender <markus@intevation.de>
parents:
diff
changeset
|
221 .append("g") |
2652
a2dd96c305be
client: waterlevel diagram: fixed animated x-axis in mainchart when zooming
Markus Kottlaender <markus@intevation.de>
parents:
2648
diff
changeset
|
222 .attr("class", "axis--x") |
2610
5ce1b4b29869
client: waterlevel diagram: refactored code for better readability
Markus Kottlaender <markus@intevation.de>
parents:
2607
diff
changeset
|
223 .attr("transform", `translate(0, ${mainHeight})`) |
2648
4ffae834276e
client: waterlevel diagram: added background grid to chart
Markus Kottlaender <markus@intevation.de>
parents:
2646
diff
changeset
|
224 .call(xAxis) |
4ffae834276e
client: waterlevel diagram: added background grid to chart
Markus Kottlaender <markus@intevation.de>
parents:
2646
diff
changeset
|
225 .selectAll(".tick text") |
4ffae834276e
client: waterlevel diagram: added background grid to chart
Markus Kottlaender <markus@intevation.de>
parents:
2646
diff
changeset
|
226 .attr("y", 15); |
2641
a735119e4f5c
client: waterlevel diagram: added y-axis label
Markus Kottlaender <markus@intevation.de>
parents:
2611
diff
changeset
|
227 mainChart // label |
a735119e4f5c
client: waterlevel diagram: added y-axis label
Markus Kottlaender <markus@intevation.de>
parents:
2611
diff
changeset
|
228 .append("text") |
2642
ffef5f89b31c
client: waterlevel diagram: changed diagram title and y-axis label
Markus Kottlaender <markus@intevation.de>
parents:
2641
diff
changeset
|
229 .text(this.$gettext("Waterlevel [cm]")) |
2641
a735119e4f5c
client: waterlevel diagram: added y-axis label
Markus Kottlaender <markus@intevation.de>
parents:
2611
diff
changeset
|
230 .attr("text-anchor", "middle") |
a735119e4f5c
client: waterlevel diagram: added y-axis label
Markus Kottlaender <markus@intevation.de>
parents:
2611
diff
changeset
|
231 .attr("transform", `translate(-45, ${mainHeight / 2}) rotate(-90)`); |
2648
4ffae834276e
client: waterlevel diagram: added background grid to chart
Markus Kottlaender <markus@intevation.de>
parents:
2646
diff
changeset
|
232 mainChart |
4ffae834276e
client: waterlevel diagram: added background grid to chart
Markus Kottlaender <markus@intevation.de>
parents:
2646
diff
changeset
|
233 .append("g") |
4ffae834276e
client: waterlevel diagram: added background grid to chart
Markus Kottlaender <markus@intevation.de>
parents:
2646
diff
changeset
|
234 .call(yAxis) |
4ffae834276e
client: waterlevel diagram: added background grid to chart
Markus Kottlaender <markus@intevation.de>
parents:
2646
diff
changeset
|
235 .selectAll(".tick text") |
4ffae834276e
client: waterlevel diagram: added background grid to chart
Markus Kottlaender <markus@intevation.de>
parents:
2646
diff
changeset
|
236 .attr("x", -25); |
2590
1686ec185155
client: added gauge waterlevel example diagram
Markus Kottlaender <markus@intevation.de>
parents:
diff
changeset
|
237 |
2645
6374fc404fdc
client: waterlevel diagram: draw filled area between HDC and LDC
Markus Kottlaender <markus@intevation.de>
parents:
2642
diff
changeset
|
238 // reference waterlevels |
6374fc404fdc
client: waterlevel diagram: draw filled area between HDC and LDC
Markus Kottlaender <markus@intevation.de>
parents:
2642
diff
changeset
|
239 // filling area between HDC and LDC |
2610
5ce1b4b29869
client: waterlevel diagram: refactored code for better readability
Markus Kottlaender <markus@intevation.de>
parents:
2607
diff
changeset
|
240 mainChart |
2645
6374fc404fdc
client: waterlevel diagram: draw filled area between HDC and LDC
Markus Kottlaender <markus@intevation.de>
parents:
2642
diff
changeset
|
241 .append("rect") |
6374fc404fdc
client: waterlevel diagram: draw filled area between HDC and LDC
Markus Kottlaender <markus@intevation.de>
parents:
2642
diff
changeset
|
242 .attr("class", "hdc-ldc-area") |
6374fc404fdc
client: waterlevel diagram: draw filled area between HDC and LDC
Markus Kottlaender <markus@intevation.de>
parents:
2642
diff
changeset
|
243 .attr("x", 0) |
6374fc404fdc
client: waterlevel diagram: draw filled area between HDC and LDC
Markus Kottlaender <markus@intevation.de>
parents:
2642
diff
changeset
|
244 .attr("y", y(refWaterLevels.HDC)) |
6374fc404fdc
client: waterlevel diagram: draw filled area between HDC and LDC
Markus Kottlaender <markus@intevation.de>
parents:
2642
diff
changeset
|
245 .attr("width", width) |
6374fc404fdc
client: waterlevel diagram: draw filled area between HDC and LDC
Markus Kottlaender <markus@intevation.de>
parents:
2642
diff
changeset
|
246 .attr("height", y(refWaterLevels.LDC) - y(refWaterLevels.HDC)); |
2590
1686ec185155
client: added gauge waterlevel example diagram
Markus Kottlaender <markus@intevation.de>
parents:
diff
changeset
|
247 |
2610
5ce1b4b29869
client: waterlevel diagram: refactored code for better readability
Markus Kottlaender <markus@intevation.de>
parents:
2607
diff
changeset
|
248 let lastDate = this.waterlevels[this.waterlevels.length - 1].date; |
2611
e8c97481438f
client: waterlevel diagram: added labels for reference water levels
Markus Kottlaender <markus@intevation.de>
parents:
2610
diff
changeset
|
249 // HDC |
2610
5ce1b4b29869
client: waterlevel diagram: refactored code for better readability
Markus Kottlaender <markus@intevation.de>
parents:
2607
diff
changeset
|
250 mainChart |
2604
85f9bf4a6eba
client: gauge waterlevel diagram: draw reference waterlevels
Markus Kottlaender <markus@intevation.de>
parents:
2596
diff
changeset
|
251 .append("path") |
85f9bf4a6eba
client: gauge waterlevel diagram: draw reference waterlevels
Markus Kottlaender <markus@intevation.de>
parents:
2596
diff
changeset
|
252 .datum([ |
85f9bf4a6eba
client: gauge waterlevel diagram: draw reference waterlevels
Markus Kottlaender <markus@intevation.de>
parents:
2596
diff
changeset
|
253 { x: 0, y: refWaterLevels.HDC }, |
2610
5ce1b4b29869
client: waterlevel diagram: refactored code for better readability
Markus Kottlaender <markus@intevation.de>
parents:
2607
diff
changeset
|
254 { x: lastDate, y: refWaterLevels.HDC } |
2604
85f9bf4a6eba
client: gauge waterlevel diagram: draw reference waterlevels
Markus Kottlaender <markus@intevation.de>
parents:
2596
diff
changeset
|
255 ]) |
85f9bf4a6eba
client: gauge waterlevel diagram: draw reference waterlevels
Markus Kottlaender <markus@intevation.de>
parents:
2596
diff
changeset
|
256 .attr("class", "hdc-line") |
85f9bf4a6eba
client: gauge waterlevel diagram: draw reference waterlevels
Markus Kottlaender <markus@intevation.de>
parents:
2596
diff
changeset
|
257 .attr("d", refWaterlevelLine); |
2611
e8c97481438f
client: waterlevel diagram: added labels for reference water levels
Markus Kottlaender <markus@intevation.de>
parents:
2610
diff
changeset
|
258 mainChart // label |
e8c97481438f
client: waterlevel diagram: added labels for reference water levels
Markus Kottlaender <markus@intevation.de>
parents:
2610
diff
changeset
|
259 .append("text") |
e8c97481438f
client: waterlevel diagram: added labels for reference water levels
Markus Kottlaender <markus@intevation.de>
parents:
2610
diff
changeset
|
260 .text("HDC") |
e8c97481438f
client: waterlevel diagram: added labels for reference water levels
Markus Kottlaender <markus@intevation.de>
parents:
2610
diff
changeset
|
261 .attr("class", "ref-waterlevel-label") |
e8c97481438f
client: waterlevel diagram: added labels for reference water levels
Markus Kottlaender <markus@intevation.de>
parents:
2610
diff
changeset
|
262 .attr("x", x(lastDate) - 20) |
e8c97481438f
client: waterlevel diagram: added labels for reference water levels
Markus Kottlaender <markus@intevation.de>
parents:
2610
diff
changeset
|
263 .attr("y", y(refWaterLevels.HDC) - 3); |
e8c97481438f
client: waterlevel diagram: added labels for reference water levels
Markus Kottlaender <markus@intevation.de>
parents:
2610
diff
changeset
|
264 // LDC |
2610
5ce1b4b29869
client: waterlevel diagram: refactored code for better readability
Markus Kottlaender <markus@intevation.de>
parents:
2607
diff
changeset
|
265 mainChart |
2604
85f9bf4a6eba
client: gauge waterlevel diagram: draw reference waterlevels
Markus Kottlaender <markus@intevation.de>
parents:
2596
diff
changeset
|
266 .append("path") |
85f9bf4a6eba
client: gauge waterlevel diagram: draw reference waterlevels
Markus Kottlaender <markus@intevation.de>
parents:
2596
diff
changeset
|
267 .datum([ |
85f9bf4a6eba
client: gauge waterlevel diagram: draw reference waterlevels
Markus Kottlaender <markus@intevation.de>
parents:
2596
diff
changeset
|
268 { x: 0, y: refWaterLevels.LDC }, |
2610
5ce1b4b29869
client: waterlevel diagram: refactored code for better readability
Markus Kottlaender <markus@intevation.de>
parents:
2607
diff
changeset
|
269 { x: lastDate, y: refWaterLevels.LDC } |
2604
85f9bf4a6eba
client: gauge waterlevel diagram: draw reference waterlevels
Markus Kottlaender <markus@intevation.de>
parents:
2596
diff
changeset
|
270 ]) |
85f9bf4a6eba
client: gauge waterlevel diagram: draw reference waterlevels
Markus Kottlaender <markus@intevation.de>
parents:
2596
diff
changeset
|
271 .attr("class", "ldc-line") |
85f9bf4a6eba
client: gauge waterlevel diagram: draw reference waterlevels
Markus Kottlaender <markus@intevation.de>
parents:
2596
diff
changeset
|
272 .attr("d", refWaterlevelLine); |
2611
e8c97481438f
client: waterlevel diagram: added labels for reference water levels
Markus Kottlaender <markus@intevation.de>
parents:
2610
diff
changeset
|
273 mainChart // label |
e8c97481438f
client: waterlevel diagram: added labels for reference water levels
Markus Kottlaender <markus@intevation.de>
parents:
2610
diff
changeset
|
274 .append("text") |
e8c97481438f
client: waterlevel diagram: added labels for reference water levels
Markus Kottlaender <markus@intevation.de>
parents:
2610
diff
changeset
|
275 .text("LDC") |
e8c97481438f
client: waterlevel diagram: added labels for reference water levels
Markus Kottlaender <markus@intevation.de>
parents:
2610
diff
changeset
|
276 .attr("class", "ref-waterlevel-label") |
e8c97481438f
client: waterlevel diagram: added labels for reference water levels
Markus Kottlaender <markus@intevation.de>
parents:
2610
diff
changeset
|
277 .attr("x", x(lastDate) - 20) |
e8c97481438f
client: waterlevel diagram: added labels for reference water levels
Markus Kottlaender <markus@intevation.de>
parents:
2610
diff
changeset
|
278 .attr("y", y(refWaterLevels.LDC) - 3); |
e8c97481438f
client: waterlevel diagram: added labels for reference water levels
Markus Kottlaender <markus@intevation.de>
parents:
2610
diff
changeset
|
279 // MW |
2610
5ce1b4b29869
client: waterlevel diagram: refactored code for better readability
Markus Kottlaender <markus@intevation.de>
parents:
2607
diff
changeset
|
280 mainChart |
2604
85f9bf4a6eba
client: gauge waterlevel diagram: draw reference waterlevels
Markus Kottlaender <markus@intevation.de>
parents:
2596
diff
changeset
|
281 .append("path") |
85f9bf4a6eba
client: gauge waterlevel diagram: draw reference waterlevels
Markus Kottlaender <markus@intevation.de>
parents:
2596
diff
changeset
|
282 .datum([ |
85f9bf4a6eba
client: gauge waterlevel diagram: draw reference waterlevels
Markus Kottlaender <markus@intevation.de>
parents:
2596
diff
changeset
|
283 { x: 0, y: refWaterLevels.MW }, |
2610
5ce1b4b29869
client: waterlevel diagram: refactored code for better readability
Markus Kottlaender <markus@intevation.de>
parents:
2607
diff
changeset
|
284 { x: lastDate, y: refWaterLevels.MW } |
2604
85f9bf4a6eba
client: gauge waterlevel diagram: draw reference waterlevels
Markus Kottlaender <markus@intevation.de>
parents:
2596
diff
changeset
|
285 ]) |
85f9bf4a6eba
client: gauge waterlevel diagram: draw reference waterlevels
Markus Kottlaender <markus@intevation.de>
parents:
2596
diff
changeset
|
286 .attr("class", "mw-line") |
85f9bf4a6eba
client: gauge waterlevel diagram: draw reference waterlevels
Markus Kottlaender <markus@intevation.de>
parents:
2596
diff
changeset
|
287 .attr("d", refWaterlevelLine); |
2611
e8c97481438f
client: waterlevel diagram: added labels for reference water levels
Markus Kottlaender <markus@intevation.de>
parents:
2610
diff
changeset
|
288 mainChart // label |
e8c97481438f
client: waterlevel diagram: added labels for reference water levels
Markus Kottlaender <markus@intevation.de>
parents:
2610
diff
changeset
|
289 .append("text") |
e8c97481438f
client: waterlevel diagram: added labels for reference water levels
Markus Kottlaender <markus@intevation.de>
parents:
2610
diff
changeset
|
290 .text("MW") |
e8c97481438f
client: waterlevel diagram: added labels for reference water levels
Markus Kottlaender <markus@intevation.de>
parents:
2610
diff
changeset
|
291 .attr("class", "ref-waterlevel-label") |
e8c97481438f
client: waterlevel diagram: added labels for reference water levels
Markus Kottlaender <markus@intevation.de>
parents:
2610
diff
changeset
|
292 .attr("x", x(lastDate) - 20) |
e8c97481438f
client: waterlevel diagram: added labels for reference water levels
Markus Kottlaender <markus@intevation.de>
parents:
2610
diff
changeset
|
293 .attr("y", y(refWaterLevels.MW) - 3); |
2604
85f9bf4a6eba
client: gauge waterlevel diagram: draw reference waterlevels
Markus Kottlaender <markus@intevation.de>
parents:
2596
diff
changeset
|
294 |
2645
6374fc404fdc
client: waterlevel diagram: draw filled area between HDC and LDC
Markus Kottlaender <markus@intevation.de>
parents:
2642
diff
changeset
|
295 // waterlevel chart |
6374fc404fdc
client: waterlevel diagram: draw filled area between HDC and LDC
Markus Kottlaender <markus@intevation.de>
parents:
2642
diff
changeset
|
296 mainChart |
2684
c4da269238a4
client: waterlevel diagram: visualize data gaps
Markus Kottlaender <markus@intevation.de>
parents:
2653
diff
changeset
|
297 .append("g") |
c4da269238a4
client: waterlevel diagram: visualize data gaps
Markus Kottlaender <markus@intevation.de>
parents:
2653
diff
changeset
|
298 .attr("class", "line") |
2645
6374fc404fdc
client: waterlevel diagram: draw filled area between HDC and LDC
Markus Kottlaender <markus@intevation.de>
parents:
2642
diff
changeset
|
299 .datum(this.waterlevels) |
2684
c4da269238a4
client: waterlevel diagram: visualize data gaps
Markus Kottlaender <markus@intevation.de>
parents:
2653
diff
changeset
|
300 .transition() |
c4da269238a4
client: waterlevel diagram: visualize data gaps
Markus Kottlaender <markus@intevation.de>
parents:
2653
diff
changeset
|
301 .duration(1000) |
c4da269238a4
client: waterlevel diagram: visualize data gaps
Markus Kottlaender <markus@intevation.de>
parents:
2653
diff
changeset
|
302 .call(mainLineChart); |
2645
6374fc404fdc
client: waterlevel diagram: draw filled area between HDC and LDC
Markus Kottlaender <markus@intevation.de>
parents:
2642
diff
changeset
|
303 |
2610
5ce1b4b29869
client: waterlevel diagram: refactored code for better readability
Markus Kottlaender <markus@intevation.de>
parents:
2607
diff
changeset
|
304 // DRAWING NAVCHART |
5ce1b4b29869
client: waterlevel diagram: refactored code for better readability
Markus Kottlaender <markus@intevation.de>
parents:
2607
diff
changeset
|
305 |
5ce1b4b29869
client: waterlevel diagram: refactored code for better readability
Markus Kottlaender <markus@intevation.de>
parents:
2607
diff
changeset
|
306 let navChart = svg |
5ce1b4b29869
client: waterlevel diagram: refactored code for better readability
Markus Kottlaender <markus@intevation.de>
parents:
2607
diff
changeset
|
307 .append("g") |
2684
c4da269238a4
client: waterlevel diagram: visualize data gaps
Markus Kottlaender <markus@intevation.de>
parents:
2653
diff
changeset
|
308 .attr("class", "nav") |
2610
5ce1b4b29869
client: waterlevel diagram: refactored code for better readability
Markus Kottlaender <markus@intevation.de>
parents:
2607
diff
changeset
|
309 .attr("transform", `translate(${navMargin.left}, ${navMargin.top})`); |
5ce1b4b29869
client: waterlevel diagram: refactored code for better readability
Markus Kottlaender <markus@intevation.de>
parents:
2607
diff
changeset
|
310 |
5ce1b4b29869
client: waterlevel diagram: refactored code for better readability
Markus Kottlaender <markus@intevation.de>
parents:
2607
diff
changeset
|
311 // axis (nav chart only has y-axis) |
5ce1b4b29869
client: waterlevel diagram: refactored code for better readability
Markus Kottlaender <markus@intevation.de>
parents:
2607
diff
changeset
|
312 navChart |
2590
1686ec185155
client: added gauge waterlevel example diagram
Markus Kottlaender <markus@intevation.de>
parents:
diff
changeset
|
313 .append("g") |
1686ec185155
client: added gauge waterlevel example diagram
Markus Kottlaender <markus@intevation.de>
parents:
diff
changeset
|
314 .attr("class", "axis axis--x") |
2610
5ce1b4b29869
client: waterlevel diagram: refactored code for better readability
Markus Kottlaender <markus@intevation.de>
parents:
2607
diff
changeset
|
315 .attr("transform", `translate(0, ${navHeight})`) |
5ce1b4b29869
client: waterlevel diagram: refactored code for better readability
Markus Kottlaender <markus@intevation.de>
parents:
2607
diff
changeset
|
316 .call(xAxis2); |
2590
1686ec185155
client: added gauge waterlevel example diagram
Markus Kottlaender <markus@intevation.de>
parents:
diff
changeset
|
317 |
2610
5ce1b4b29869
client: waterlevel diagram: refactored code for better readability
Markus Kottlaender <markus@intevation.de>
parents:
2607
diff
changeset
|
318 // waterlevel chart |
5ce1b4b29869
client: waterlevel diagram: refactored code for better readability
Markus Kottlaender <markus@intevation.de>
parents:
2607
diff
changeset
|
319 navChart |
2590
1686ec185155
client: added gauge waterlevel example diagram
Markus Kottlaender <markus@intevation.de>
parents:
diff
changeset
|
320 .append("path") |
1686ec185155
client: added gauge waterlevel example diagram
Markus Kottlaender <markus@intevation.de>
parents:
diff
changeset
|
321 .datum(this.waterlevels) |
2610
5ce1b4b29869
client: waterlevel diagram: refactored code for better readability
Markus Kottlaender <markus@intevation.de>
parents:
2607
diff
changeset
|
322 .attr("class", "line") |
5ce1b4b29869
client: waterlevel diagram: refactored code for better readability
Markus Kottlaender <markus@intevation.de>
parents:
2607
diff
changeset
|
323 .attr("d", navLineChart); |
5ce1b4b29869
client: waterlevel diagram: refactored code for better readability
Markus Kottlaender <markus@intevation.de>
parents:
2607
diff
changeset
|
324 |
5ce1b4b29869
client: waterlevel diagram: refactored code for better readability
Markus Kottlaender <markus@intevation.de>
parents:
2607
diff
changeset
|
325 // INTERACTIVITY |
2590
1686ec185155
client: added gauge waterlevel example diagram
Markus Kottlaender <markus@intevation.de>
parents:
diff
changeset
|
326 |
2610
5ce1b4b29869
client: waterlevel diagram: refactored code for better readability
Markus Kottlaender <markus@intevation.de>
parents:
2607
diff
changeset
|
327 // selecting time period in nav chart |
5ce1b4b29869
client: waterlevel diagram: refactored code for better readability
Markus Kottlaender <markus@intevation.de>
parents:
2607
diff
changeset
|
328 let brush = d3 |
5ce1b4b29869
client: waterlevel diagram: refactored code for better readability
Markus Kottlaender <markus@intevation.de>
parents:
2607
diff
changeset
|
329 .brushX() |
5ce1b4b29869
client: waterlevel diagram: refactored code for better readability
Markus Kottlaender <markus@intevation.de>
parents:
2607
diff
changeset
|
330 .handleSize(4) |
5ce1b4b29869
client: waterlevel diagram: refactored code for better readability
Markus Kottlaender <markus@intevation.de>
parents:
2607
diff
changeset
|
331 .extent([[0, 0], [width, navHeight]]) |
5ce1b4b29869
client: waterlevel diagram: refactored code for better readability
Markus Kottlaender <markus@intevation.de>
parents:
2607
diff
changeset
|
332 .on("brush end", () => { |
5ce1b4b29869
client: waterlevel diagram: refactored code for better readability
Markus Kottlaender <markus@intevation.de>
parents:
2607
diff
changeset
|
333 if (d3.event.sourceEvent && d3.event.sourceEvent.type === "zoom") |
5ce1b4b29869
client: waterlevel diagram: refactored code for better readability
Markus Kottlaender <markus@intevation.de>
parents:
2607
diff
changeset
|
334 return; // ignore brush-by-zoom |
5ce1b4b29869
client: waterlevel diagram: refactored code for better readability
Markus Kottlaender <markus@intevation.de>
parents:
2607
diff
changeset
|
335 let s = d3.event.selection || x2.range(); |
5ce1b4b29869
client: waterlevel diagram: refactored code for better readability
Markus Kottlaender <markus@intevation.de>
parents:
2607
diff
changeset
|
336 x.domain(s.map(x2.invert, x2)); |
2684
c4da269238a4
client: waterlevel diagram: visualize data gaps
Markus Kottlaender <markus@intevation.de>
parents:
2653
diff
changeset
|
337 mainChart.select(".line").call(mainLineChart); |
2652
a2dd96c305be
client: waterlevel diagram: fixed animated x-axis in mainchart when zooming
Markus Kottlaender <markus@intevation.de>
parents:
2648
diff
changeset
|
338 mainChart |
a2dd96c305be
client: waterlevel diagram: fixed animated x-axis in mainchart when zooming
Markus Kottlaender <markus@intevation.de>
parents:
2648
diff
changeset
|
339 .select(".axis--x") |
a2dd96c305be
client: waterlevel diagram: fixed animated x-axis in mainchart when zooming
Markus Kottlaender <markus@intevation.de>
parents:
2648
diff
changeset
|
340 .call(xAxis) |
a2dd96c305be
client: waterlevel diagram: fixed animated x-axis in mainchart when zooming
Markus Kottlaender <markus@intevation.de>
parents:
2648
diff
changeset
|
341 .selectAll(".tick text") |
a2dd96c305be
client: waterlevel diagram: fixed animated x-axis in mainchart when zooming
Markus Kottlaender <markus@intevation.de>
parents:
2648
diff
changeset
|
342 .attr("y", 15); |
2610
5ce1b4b29869
client: waterlevel diagram: refactored code for better readability
Markus Kottlaender <markus@intevation.de>
parents:
2607
diff
changeset
|
343 svg |
5ce1b4b29869
client: waterlevel diagram: refactored code for better readability
Markus Kottlaender <markus@intevation.de>
parents:
2607
diff
changeset
|
344 .select(".zoom") |
5ce1b4b29869
client: waterlevel diagram: refactored code for better readability
Markus Kottlaender <markus@intevation.de>
parents:
2607
diff
changeset
|
345 .call( |
5ce1b4b29869
client: waterlevel diagram: refactored code for better readability
Markus Kottlaender <markus@intevation.de>
parents:
2607
diff
changeset
|
346 zoom.transform, |
5ce1b4b29869
client: waterlevel diagram: refactored code for better readability
Markus Kottlaender <markus@intevation.de>
parents:
2607
diff
changeset
|
347 d3.zoomIdentity.scale(width / (s[1] - s[0])).translate(-s[0], 0) |
5ce1b4b29869
client: waterlevel diagram: refactored code for better readability
Markus Kottlaender <markus@intevation.de>
parents:
2607
diff
changeset
|
348 ); |
5ce1b4b29869
client: waterlevel diagram: refactored code for better readability
Markus Kottlaender <markus@intevation.de>
parents:
2607
diff
changeset
|
349 }); |
2590
1686ec185155
client: added gauge waterlevel example diagram
Markus Kottlaender <markus@intevation.de>
parents:
diff
changeset
|
350 |
2610
5ce1b4b29869
client: waterlevel diagram: refactored code for better readability
Markus Kottlaender <markus@intevation.de>
parents:
2607
diff
changeset
|
351 // zooming with mousewheel in main chart |
5ce1b4b29869
client: waterlevel diagram: refactored code for better readability
Markus Kottlaender <markus@intevation.de>
parents:
2607
diff
changeset
|
352 let zoom = d3 |
5ce1b4b29869
client: waterlevel diagram: refactored code for better readability
Markus Kottlaender <markus@intevation.de>
parents:
2607
diff
changeset
|
353 .zoom() |
5ce1b4b29869
client: waterlevel diagram: refactored code for better readability
Markus Kottlaender <markus@intevation.de>
parents:
2607
diff
changeset
|
354 .scaleExtent([1, Infinity]) |
5ce1b4b29869
client: waterlevel diagram: refactored code for better readability
Markus Kottlaender <markus@intevation.de>
parents:
2607
diff
changeset
|
355 .translateExtent([[0, 0], [width, mainHeight]]) |
5ce1b4b29869
client: waterlevel diagram: refactored code for better readability
Markus Kottlaender <markus@intevation.de>
parents:
2607
diff
changeset
|
356 .extent([[0, 0], [width, mainHeight]]) |
5ce1b4b29869
client: waterlevel diagram: refactored code for better readability
Markus Kottlaender <markus@intevation.de>
parents:
2607
diff
changeset
|
357 .on("zoom", () => { |
5ce1b4b29869
client: waterlevel diagram: refactored code for better readability
Markus Kottlaender <markus@intevation.de>
parents:
2607
diff
changeset
|
358 if (d3.event.sourceEvent && d3.event.sourceEvent.type === "brush") |
5ce1b4b29869
client: waterlevel diagram: refactored code for better readability
Markus Kottlaender <markus@intevation.de>
parents:
2607
diff
changeset
|
359 return; // ignore zoom-by-brush |
5ce1b4b29869
client: waterlevel diagram: refactored code for better readability
Markus Kottlaender <markus@intevation.de>
parents:
2607
diff
changeset
|
360 let t = d3.event.transform; |
5ce1b4b29869
client: waterlevel diagram: refactored code for better readability
Markus Kottlaender <markus@intevation.de>
parents:
2607
diff
changeset
|
361 x.domain(t.rescaleX(x2).domain()); |
2684
c4da269238a4
client: waterlevel diagram: visualize data gaps
Markus Kottlaender <markus@intevation.de>
parents:
2653
diff
changeset
|
362 mainChart.select(".line").call(mainLineChart); |
2652
a2dd96c305be
client: waterlevel diagram: fixed animated x-axis in mainchart when zooming
Markus Kottlaender <markus@intevation.de>
parents:
2648
diff
changeset
|
363 mainChart |
a2dd96c305be
client: waterlevel diagram: fixed animated x-axis in mainchart when zooming
Markus Kottlaender <markus@intevation.de>
parents:
2648
diff
changeset
|
364 .select(".axis--x") |
a2dd96c305be
client: waterlevel diagram: fixed animated x-axis in mainchart when zooming
Markus Kottlaender <markus@intevation.de>
parents:
2648
diff
changeset
|
365 .call(xAxis) |
a2dd96c305be
client: waterlevel diagram: fixed animated x-axis in mainchart when zooming
Markus Kottlaender <markus@intevation.de>
parents:
2648
diff
changeset
|
366 .selectAll(".tick text") |
a2dd96c305be
client: waterlevel diagram: fixed animated x-axis in mainchart when zooming
Markus Kottlaender <markus@intevation.de>
parents:
2648
diff
changeset
|
367 .attr("y", 15); |
2610
5ce1b4b29869
client: waterlevel diagram: refactored code for better readability
Markus Kottlaender <markus@intevation.de>
parents:
2607
diff
changeset
|
368 navChart |
5ce1b4b29869
client: waterlevel diagram: refactored code for better readability
Markus Kottlaender <markus@intevation.de>
parents:
2607
diff
changeset
|
369 .select(".brush") |
5ce1b4b29869
client: waterlevel diagram: refactored code for better readability
Markus Kottlaender <markus@intevation.de>
parents:
2607
diff
changeset
|
370 .call(brush.move, x.range().map(t.invertX, t)); |
2689
8f919fe629f9
client: waterlevel diagram: added tooltips showing date and waterlevel on the chart
Markus Kottlaender <markus@intevation.de>
parents:
2684
diff
changeset
|
371 }) |
8f919fe629f9
client: waterlevel diagram: added tooltips showing date and waterlevel on the chart
Markus Kottlaender <markus@intevation.de>
parents:
2684
diff
changeset
|
372 .on("start", () => { |
2691
023a607c4bcc
client: waterlevel diagram: fixed tooltip display while moving/zooming the chart
Markus Kottlaender <markus@intevation.de>
parents:
2689
diff
changeset
|
373 svg.select(".chart-dot").style("opacity", 0); |
023a607c4bcc
client: waterlevel diagram: fixed tooltip display while moving/zooming the chart
Markus Kottlaender <markus@intevation.de>
parents:
2689
diff
changeset
|
374 svg.select(".chart-tooltip").style("opacity", 0); |
2610
5ce1b4b29869
client: waterlevel diagram: refactored code for better readability
Markus Kottlaender <markus@intevation.de>
parents:
2607
diff
changeset
|
375 }); |
5ce1b4b29869
client: waterlevel diagram: refactored code for better readability
Markus Kottlaender <markus@intevation.de>
parents:
2607
diff
changeset
|
376 |
5ce1b4b29869
client: waterlevel diagram: refactored code for better readability
Markus Kottlaender <markus@intevation.de>
parents:
2607
diff
changeset
|
377 navChart |
2590
1686ec185155
client: added gauge waterlevel example diagram
Markus Kottlaender <markus@intevation.de>
parents:
diff
changeset
|
378 .append("g") |
1686ec185155
client: added gauge waterlevel example diagram
Markus Kottlaender <markus@intevation.de>
parents:
diff
changeset
|
379 .attr("class", "brush") |
1686ec185155
client: added gauge waterlevel example diagram
Markus Kottlaender <markus@intevation.de>
parents:
diff
changeset
|
380 .call(brush) |
1686ec185155
client: added gauge waterlevel example diagram
Markus Kottlaender <markus@intevation.de>
parents:
diff
changeset
|
381 .call(brush.move, x.range()); |
1686ec185155
client: added gauge waterlevel example diagram
Markus Kottlaender <markus@intevation.de>
parents:
diff
changeset
|
382 |
2689
8f919fe629f9
client: waterlevel diagram: added tooltips showing date and waterlevel on the chart
Markus Kottlaender <markus@intevation.de>
parents:
2684
diff
changeset
|
383 let zoomRect = svg |
2590
1686ec185155
client: added gauge waterlevel example diagram
Markus Kottlaender <markus@intevation.de>
parents:
diff
changeset
|
384 .append("rect") |
1686ec185155
client: added gauge waterlevel example diagram
Markus Kottlaender <markus@intevation.de>
parents:
diff
changeset
|
385 .attr("class", "zoom") |
1686ec185155
client: added gauge waterlevel example diagram
Markus Kottlaender <markus@intevation.de>
parents:
diff
changeset
|
386 .attr("width", width) |
2610
5ce1b4b29869
client: waterlevel diagram: refactored code for better readability
Markus Kottlaender <markus@intevation.de>
parents:
2607
diff
changeset
|
387 .attr("height", mainHeight) |
5ce1b4b29869
client: waterlevel diagram: refactored code for better readability
Markus Kottlaender <markus@intevation.de>
parents:
2607
diff
changeset
|
388 .attr("transform", `translate(${mainMargin.left}, ${mainMargin.top})`) |
2590
1686ec185155
client: added gauge waterlevel example diagram
Markus Kottlaender <markus@intevation.de>
parents:
diff
changeset
|
389 .call(zoom); |
2689
8f919fe629f9
client: waterlevel diagram: added tooltips showing date and waterlevel on the chart
Markus Kottlaender <markus@intevation.de>
parents:
2684
diff
changeset
|
390 |
8f919fe629f9
client: waterlevel diagram: added tooltips showing date and waterlevel on the chart
Markus Kottlaender <markus@intevation.de>
parents:
2684
diff
changeset
|
391 // TOOLTIPS |
8f919fe629f9
client: waterlevel diagram: added tooltips showing date and waterlevel on the chart
Markus Kottlaender <markus@intevation.de>
parents:
2684
diff
changeset
|
392 |
8f919fe629f9
client: waterlevel diagram: added tooltips showing date and waterlevel on the chart
Markus Kottlaender <markus@intevation.de>
parents:
2684
diff
changeset
|
393 let dots = mainChart.append("g").attr("class", "chart-dots"); |
8f919fe629f9
client: waterlevel diagram: added tooltips showing date and waterlevel on the chart
Markus Kottlaender <markus@intevation.de>
parents:
2684
diff
changeset
|
394 dots |
8f919fe629f9
client: waterlevel diagram: added tooltips showing date and waterlevel on the chart
Markus Kottlaender <markus@intevation.de>
parents:
2684
diff
changeset
|
395 .append("circle") |
8f919fe629f9
client: waterlevel diagram: added tooltips showing date and waterlevel on the chart
Markus Kottlaender <markus@intevation.de>
parents:
2684
diff
changeset
|
396 .attr("class", "chart-dot") |
8f919fe629f9
client: waterlevel diagram: added tooltips showing date and waterlevel on the chart
Markus Kottlaender <markus@intevation.de>
parents:
2684
diff
changeset
|
397 .attr("r", 4); |
8f919fe629f9
client: waterlevel diagram: added tooltips showing date and waterlevel on the chart
Markus Kottlaender <markus@intevation.de>
parents:
2684
diff
changeset
|
398 let tooltips = mainChart.append("g").attr("class", "chart-tooltip"); |
8f919fe629f9
client: waterlevel diagram: added tooltips showing date and waterlevel on the chart
Markus Kottlaender <markus@intevation.de>
parents:
2684
diff
changeset
|
399 tooltips |
8f919fe629f9
client: waterlevel diagram: added tooltips showing date and waterlevel on the chart
Markus Kottlaender <markus@intevation.de>
parents:
2684
diff
changeset
|
400 .append("rect") |
8f919fe629f9
client: waterlevel diagram: added tooltips showing date and waterlevel on the chart
Markus Kottlaender <markus@intevation.de>
parents:
2684
diff
changeset
|
401 .attr("x", -25) |
8f919fe629f9
client: waterlevel diagram: added tooltips showing date and waterlevel on the chart
Markus Kottlaender <markus@intevation.de>
parents:
2684
diff
changeset
|
402 .attr("y", -25) |
8f919fe629f9
client: waterlevel diagram: added tooltips showing date and waterlevel on the chart
Markus Kottlaender <markus@intevation.de>
parents:
2684
diff
changeset
|
403 .attr("rx", 4) |
8f919fe629f9
client: waterlevel diagram: added tooltips showing date and waterlevel on the chart
Markus Kottlaender <markus@intevation.de>
parents:
2684
diff
changeset
|
404 .attr("ry", 4) |
8f919fe629f9
client: waterlevel diagram: added tooltips showing date and waterlevel on the chart
Markus Kottlaender <markus@intevation.de>
parents:
2684
diff
changeset
|
405 .attr("width", 105) |
8f919fe629f9
client: waterlevel diagram: added tooltips showing date and waterlevel on the chart
Markus Kottlaender <markus@intevation.de>
parents:
2684
diff
changeset
|
406 .attr("height", 40); |
8f919fe629f9
client: waterlevel diagram: added tooltips showing date and waterlevel on the chart
Markus Kottlaender <markus@intevation.de>
parents:
2684
diff
changeset
|
407 let tooltipText = tooltips.append("text"); |
8f919fe629f9
client: waterlevel diagram: added tooltips showing date and waterlevel on the chart
Markus Kottlaender <markus@intevation.de>
parents:
2684
diff
changeset
|
408 tooltipText |
8f919fe629f9
client: waterlevel diagram: added tooltips showing date and waterlevel on the chart
Markus Kottlaender <markus@intevation.de>
parents:
2684
diff
changeset
|
409 .append("tspan") |
8f919fe629f9
client: waterlevel diagram: added tooltips showing date and waterlevel on the chart
Markus Kottlaender <markus@intevation.de>
parents:
2684
diff
changeset
|
410 .attr("x", -15) |
8f919fe629f9
client: waterlevel diagram: added tooltips showing date and waterlevel on the chart
Markus Kottlaender <markus@intevation.de>
parents:
2684
diff
changeset
|
411 .attr("y", -8); |
8f919fe629f9
client: waterlevel diagram: added tooltips showing date and waterlevel on the chart
Markus Kottlaender <markus@intevation.de>
parents:
2684
diff
changeset
|
412 tooltipText |
8f919fe629f9
client: waterlevel diagram: added tooltips showing date and waterlevel on the chart
Markus Kottlaender <markus@intevation.de>
parents:
2684
diff
changeset
|
413 .append("tspan") |
8f919fe629f9
client: waterlevel diagram: added tooltips showing date and waterlevel on the chart
Markus Kottlaender <markus@intevation.de>
parents:
2684
diff
changeset
|
414 .attr("x", 8) |
2691
023a607c4bcc
client: waterlevel diagram: fixed tooltip display while moving/zooming the chart
Markus Kottlaender <markus@intevation.de>
parents:
2689
diff
changeset
|
415 .attr("y", 8) |
2689
8f919fe629f9
client: waterlevel diagram: added tooltips showing date and waterlevel on the chart
Markus Kottlaender <markus@intevation.de>
parents:
2684
diff
changeset
|
416 .style("font-weight", "bold"); |
8f919fe629f9
client: waterlevel diagram: added tooltips showing date and waterlevel on the chart
Markus Kottlaender <markus@intevation.de>
parents:
2684
diff
changeset
|
417 |
8f919fe629f9
client: waterlevel diagram: added tooltips showing date and waterlevel on the chart
Markus Kottlaender <markus@intevation.de>
parents:
2684
diff
changeset
|
418 let bisectDate = d3.bisector(d => d.date).left; |
8f919fe629f9
client: waterlevel diagram: added tooltips showing date and waterlevel on the chart
Markus Kottlaender <markus@intevation.de>
parents:
2684
diff
changeset
|
419 zoomRect |
8f919fe629f9
client: waterlevel diagram: added tooltips showing date and waterlevel on the chart
Markus Kottlaender <markus@intevation.de>
parents:
2684
diff
changeset
|
420 .on("mouseover", () => { |
2691
023a607c4bcc
client: waterlevel diagram: fixed tooltip display while moving/zooming the chart
Markus Kottlaender <markus@intevation.de>
parents:
2689
diff
changeset
|
421 svg.select(".chart-dot").style("opacity", 1); |
023a607c4bcc
client: waterlevel diagram: fixed tooltip display while moving/zooming the chart
Markus Kottlaender <markus@intevation.de>
parents:
2689
diff
changeset
|
422 svg.select(".chart-tooltip").style("opacity", 1); |
2689
8f919fe629f9
client: waterlevel diagram: added tooltips showing date and waterlevel on the chart
Markus Kottlaender <markus@intevation.de>
parents:
2684
diff
changeset
|
423 }) |
8f919fe629f9
client: waterlevel diagram: added tooltips showing date and waterlevel on the chart
Markus Kottlaender <markus@intevation.de>
parents:
2684
diff
changeset
|
424 .on("mouseout", () => { |
2691
023a607c4bcc
client: waterlevel diagram: fixed tooltip display while moving/zooming the chart
Markus Kottlaender <markus@intevation.de>
parents:
2689
diff
changeset
|
425 svg.select(".chart-dot").style("opacity", 0); |
023a607c4bcc
client: waterlevel diagram: fixed tooltip display while moving/zooming the chart
Markus Kottlaender <markus@intevation.de>
parents:
2689
diff
changeset
|
426 svg.select(".chart-tooltip").style("opacity", 0); |
2689
8f919fe629f9
client: waterlevel diagram: added tooltips showing date and waterlevel on the chart
Markus Kottlaender <markus@intevation.de>
parents:
2684
diff
changeset
|
427 }) |
8f919fe629f9
client: waterlevel diagram: added tooltips showing date and waterlevel on the chart
Markus Kottlaender <markus@intevation.de>
parents:
2684
diff
changeset
|
428 .on("mousemove", () => { |
8f919fe629f9
client: waterlevel diagram: added tooltips showing date and waterlevel on the chart
Markus Kottlaender <markus@intevation.de>
parents:
2684
diff
changeset
|
429 let x0 = x.invert(d3.mouse(document.querySelector(".zoom"))[0]), |
8f919fe629f9
client: waterlevel diagram: added tooltips showing date and waterlevel on the chart
Markus Kottlaender <markus@intevation.de>
parents:
2684
diff
changeset
|
430 i = bisectDate(this.waterlevels, x0, 1), |
8f919fe629f9
client: waterlevel diagram: added tooltips showing date and waterlevel on the chart
Markus Kottlaender <markus@intevation.de>
parents:
2684
diff
changeset
|
431 d0 = this.waterlevels[i - 1], |
8f919fe629f9
client: waterlevel diagram: added tooltips showing date and waterlevel on the chart
Markus Kottlaender <markus@intevation.de>
parents:
2684
diff
changeset
|
432 d1 = this.waterlevels[i], |
8f919fe629f9
client: waterlevel diagram: added tooltips showing date and waterlevel on the chart
Markus Kottlaender <markus@intevation.de>
parents:
2684
diff
changeset
|
433 d = x0 - d0.date > d1.date - x0 ? d1 : d0; |
8f919fe629f9
client: waterlevel diagram: added tooltips showing date and waterlevel on the chart
Markus Kottlaender <markus@intevation.de>
parents:
2684
diff
changeset
|
434 |
8f919fe629f9
client: waterlevel diagram: added tooltips showing date and waterlevel on the chart
Markus Kottlaender <markus@intevation.de>
parents:
2684
diff
changeset
|
435 svg |
8f919fe629f9
client: waterlevel diagram: added tooltips showing date and waterlevel on the chart
Markus Kottlaender <markus@intevation.de>
parents:
2684
diff
changeset
|
436 .select(".chart-dot") |
2691
023a607c4bcc
client: waterlevel diagram: fixed tooltip display while moving/zooming the chart
Markus Kottlaender <markus@intevation.de>
parents:
2689
diff
changeset
|
437 .style("opacity", 1) |
2689
8f919fe629f9
client: waterlevel diagram: added tooltips showing date and waterlevel on the chart
Markus Kottlaender <markus@intevation.de>
parents:
2684
diff
changeset
|
438 .attr("transform", `translate(${x(d.date)}, ${y(d.waterlevel)})`); |
8f919fe629f9
client: waterlevel diagram: added tooltips showing date and waterlevel on the chart
Markus Kottlaender <markus@intevation.de>
parents:
2684
diff
changeset
|
439 svg |
8f919fe629f9
client: waterlevel diagram: added tooltips showing date and waterlevel on the chart
Markus Kottlaender <markus@intevation.de>
parents:
2684
diff
changeset
|
440 .select(".chart-tooltip") |
2691
023a607c4bcc
client: waterlevel diagram: fixed tooltip display while moving/zooming the chart
Markus Kottlaender <markus@intevation.de>
parents:
2689
diff
changeset
|
441 .style("opacity", 1) |
2689
8f919fe629f9
client: waterlevel diagram: added tooltips showing date and waterlevel on the chart
Markus Kottlaender <markus@intevation.de>
parents:
2684
diff
changeset
|
442 .attr( |
8f919fe629f9
client: waterlevel diagram: added tooltips showing date and waterlevel on the chart
Markus Kottlaender <markus@intevation.de>
parents:
2684
diff
changeset
|
443 "transform", |
8f919fe629f9
client: waterlevel diagram: added tooltips showing date and waterlevel on the chart
Markus Kottlaender <markus@intevation.de>
parents:
2684
diff
changeset
|
444 `translate(${x(d.date) - 25}, ${y(d.waterlevel) - 25})` |
8f919fe629f9
client: waterlevel diagram: added tooltips showing date and waterlevel on the chart
Markus Kottlaender <markus@intevation.de>
parents:
2684
diff
changeset
|
445 ); |
8f919fe629f9
client: waterlevel diagram: added tooltips showing date and waterlevel on the chart
Markus Kottlaender <markus@intevation.de>
parents:
2684
diff
changeset
|
446 svg.select(".chart-tooltip text tspan:first-child").text( |
8f919fe629f9
client: waterlevel diagram: added tooltips showing date and waterlevel on the chart
Markus Kottlaender <markus@intevation.de>
parents:
2684
diff
changeset
|
447 d.date.toLocaleString([], { |
8f919fe629f9
client: waterlevel diagram: added tooltips showing date and waterlevel on the chart
Markus Kottlaender <markus@intevation.de>
parents:
2684
diff
changeset
|
448 year: "2-digit", |
8f919fe629f9
client: waterlevel diagram: added tooltips showing date and waterlevel on the chart
Markus Kottlaender <markus@intevation.de>
parents:
2684
diff
changeset
|
449 month: "2-digit", |
8f919fe629f9
client: waterlevel diagram: added tooltips showing date and waterlevel on the chart
Markus Kottlaender <markus@intevation.de>
parents:
2684
diff
changeset
|
450 day: "2-digit", |
8f919fe629f9
client: waterlevel diagram: added tooltips showing date and waterlevel on the chart
Markus Kottlaender <markus@intevation.de>
parents:
2684
diff
changeset
|
451 hour: "2-digit", |
8f919fe629f9
client: waterlevel diagram: added tooltips showing date and waterlevel on the chart
Markus Kottlaender <markus@intevation.de>
parents:
2684
diff
changeset
|
452 minute: "2-digit" |
8f919fe629f9
client: waterlevel diagram: added tooltips showing date and waterlevel on the chart
Markus Kottlaender <markus@intevation.de>
parents:
2684
diff
changeset
|
453 }) |
8f919fe629f9
client: waterlevel diagram: added tooltips showing date and waterlevel on the chart
Markus Kottlaender <markus@intevation.de>
parents:
2684
diff
changeset
|
454 ); |
8f919fe629f9
client: waterlevel diagram: added tooltips showing date and waterlevel on the chart
Markus Kottlaender <markus@intevation.de>
parents:
2684
diff
changeset
|
455 svg |
8f919fe629f9
client: waterlevel diagram: added tooltips showing date and waterlevel on the chart
Markus Kottlaender <markus@intevation.de>
parents:
2684
diff
changeset
|
456 .select(".chart-tooltip text tspan:last-child") |
8f919fe629f9
client: waterlevel diagram: added tooltips showing date and waterlevel on the chart
Markus Kottlaender <markus@intevation.de>
parents:
2684
diff
changeset
|
457 .text(d.waterlevel + " cm"); |
8f919fe629f9
client: waterlevel diagram: added tooltips showing date and waterlevel on the chart
Markus Kottlaender <markus@intevation.de>
parents:
2684
diff
changeset
|
458 }); |
2590
1686ec185155
client: added gauge waterlevel example diagram
Markus Kottlaender <markus@intevation.de>
parents:
diff
changeset
|
459 } |
1686ec185155
client: added gauge waterlevel example diagram
Markus Kottlaender <markus@intevation.de>
parents:
diff
changeset
|
460 }, |
1686ec185155
client: added gauge waterlevel example diagram
Markus Kottlaender <markus@intevation.de>
parents:
diff
changeset
|
461 created() { |
1686ec185155
client: added gauge waterlevel example diagram
Markus Kottlaender <markus@intevation.de>
parents:
diff
changeset
|
462 window.addEventListener("resize", debounce(this.drawDiagram), 100); |
1686ec185155
client: added gauge waterlevel example diagram
Markus Kottlaender <markus@intevation.de>
parents:
diff
changeset
|
463 }, |
1686ec185155
client: added gauge waterlevel example diagram
Markus Kottlaender <markus@intevation.de>
parents:
diff
changeset
|
464 mounted() { |
1686ec185155
client: added gauge waterlevel example diagram
Markus Kottlaender <markus@intevation.de>
parents:
diff
changeset
|
465 this.drawDiagram(); |
1686ec185155
client: added gauge waterlevel example diagram
Markus Kottlaender <markus@intevation.de>
parents:
diff
changeset
|
466 }, |
1686ec185155
client: added gauge waterlevel example diagram
Markus Kottlaender <markus@intevation.de>
parents:
diff
changeset
|
467 updated() { |
1686ec185155
client: added gauge waterlevel example diagram
Markus Kottlaender <markus@intevation.de>
parents:
diff
changeset
|
468 this.drawDiagram(); |
1686ec185155
client: added gauge waterlevel example diagram
Markus Kottlaender <markus@intevation.de>
parents:
diff
changeset
|
469 } |
1686ec185155
client: added gauge waterlevel example diagram
Markus Kottlaender <markus@intevation.de>
parents:
diff
changeset
|
470 }; |
1686ec185155
client: added gauge waterlevel example diagram
Markus Kottlaender <markus@intevation.de>
parents:
diff
changeset
|
471 </script> |