-
Notifications
You must be signed in to change notification settings - Fork 46
/
px-vis-reference-curve.html
262 lines (219 loc) · 7.55 KB
/
px-vis-reference-curve.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
<!--
Copyright (c) 2018, General Electric
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<link rel="import" href="../polymer/polymer.html"/>
<link rel="import" href="px-vis-behavior-common.html" />
<link rel="import" href="px-vis-behavior-d3.html" />
<link rel="import" href="px-vis-line-svg.html"/>
<link rel="import" href="css/px-vis-styles.html">
<!--
### Usage
<px-vis-reference-curve
svg="[[svg]]"
x="[[x]]"
y="[[y]]"
axis-color="[[axisColor]]"
tick-values="[[drawnTickValues]]"
dimensions="[[dimensions]]"
margin="[[margin]]"
domain-changed="[[domainChanged]]">
</px-vis-reference-curve>
### Styling
The following custom properties are available for styling:
Custom property | Description
:----------------|:-------------
`--px-vis-reference-curve-default-color` | default color for the reference curve
@element px-vis-reference-curve
An element which draws polygonal grid lines.
@homepage index.html
@demo demo.html
-->
<dom-module id="px-vis-reference-curve">
<template>
<style include="px-vis-styles"></style>
<template is="dom-repeat" items="[[_seriesKeys]]">
<px-vis-line-svg
svg="[[_refCurveG]]"
id-prefix="[[_idPrefix]]"
series-id="[[item]]"
chart-data="[[_referenceData]]"
complete-series-config="[[_referenceConfig]]"
x="[[x]]"
y="[[_returnYScale(item, _referenceConfig, domainChanged)]]"
domain-changed="[[domainChanged]]"
stroke-width="[[_returnStrokeWidth(item, _referenceConfig)]]"
clip-path="[[clipPath]]"
interpolation-function="[[_returnInterpolation(item, _referenceConfig)]]">
</px-vis-line-svg>
</template>
</template>
</dom-module>
<script>
Polymer({
is: 'px-vis-reference-curve',
behaviors: [
PxVisBehavior.observerCheck,
PxVisBehaviorD3.svg,
PxVisBehaviorD3.axes,
PxVisBehavior.commonMethods,
PxVisBehaviorD3.clipPath,
PxVisBehavior.updateStylesOverride,
PxVisBehavior.referenceCurveProps,
PxVisBehavior.scaleTypeCheck
],
/**
* Properties block, expose attribute values to the DOM via 'reflect'
*
* @property properties
* @type Object
*/
properties: {
_referenceConfig: {
type: Object,
notify: true
},
_seriesKeys: Array,
_refCurveG: Object,
_textBuilder: Object,
_textNodes: Object,
_referenceData: {
type: Array,
computed: '_createReferenceData(referenceData.*, reverseData)'
},
reverseData: {
type: Boolean,
value: false
},
chartId: {
type: String,
value: ''
},
_idPrefix: {
type: String,
computed: '_computeIdPrefix(chartId)'
}
},
observers: [
'_createRefCurveG(svg, domainChanged)',
'_buildReferenceConfig(referenceConfig.*)',
'_drawText(_referenceConfig.*, _refCurveG)'
],
_createReferenceData: function() {
if(!this.referenceData) {
return;
}
var d = JSON.parse(JSON.stringify(this.referenceData));
if(this.reverseData) {
d.reverse();
}
return d;
},
_createRefCurveG: function() {
if(this._isD3Empty(this.svg) || !this.domainChanged) {
return;
}
if(this._isD3Empty(this._refCurveG)) {
const g = this.svg.append('g')
.attr('class', 'reference-curves');
this.set('_refCurveG', g);
}
},
_buildReferenceConfig: function() {
if(!this.referenceConfig) {
return;
}
var keys = Object.keys(this.referenceConfig),
config = {};
keys.forEach(function(k) {
if(!this.referenceConfig[k]) {
return;
}
config[k] = {};
config[k]['x'] = this.referenceConfig[k]['x'] ? this.referenceConfig[k]['x'] : 'x';
config[k]['y'] = this.referenceConfig[k]['y'] ? this.referenceConfig[k]['y'] : 'y';
config[k]['interpolationFunction'] = this.referenceConfig[k]['interpolationFunction'] ? this.referenceConfig[k]['interpolationFunction'] : Px.d3.curveCardinal;
config[k]['color'] = this.referenceConfig[k]['color'] ? this.referenceConfig[k]['color'] : this._checkThemeVariable("--px-vis-reference-curve-default-color", 'rgb(0,0,0)');
config[k]['strokeWidth'] = this.referenceConfig[k]['strokeWidth'] ? this.referenceConfig[k]['strokeWidth'] : 1;
config[k]['axis'] = this.referenceConfig[k]['axis'] ? this.referenceConfig[k]['axis'] : { "id": "defaultAxis" };
config[k]['name'] = this.referenceConfig[k]['name'] ? this.referenceConfig[k]['name'] : null;
}.bind(this));
this.set('_seriesKeys', keys);
this.set('_referenceConfig', config);
},
_drawText: function() {
if(this._isD3Empty(this._refCurveG) || this._isObjEmpty(this._referenceConfig)) {
return;
}
var _this = this;
this._textBuilder = this._refCurveG.selectAll('text.reference-curve-name')
.data(this._seriesKeys);
this._textBuilder.enter()
.append('text')
.style('font-family',this._checkThemeVariable("--px-vis-font-family", 'Comic Sans MS'))
.attr('font-style', this._checkThemeVariable("--px-vis-font-family", 'Comic Sans MS'))
.attr('font-size', '12px')
.each(function(d,i) {
_this._enterText.bind(_this)(d, i, this);
})
.merge(this._textBuilder)
.attr('fill', function(d) { return this.referenceConfig[d]['color']; }.bind(this))
.each(function(d,i) {
_this._mergeText.bind(_this)(d, i, this);
});
this._textNodes = this._refCurveG.selectAll('text.reference-curve-name');
},
_enterText: function(d, i, elem) {
Px.d3.select(elem).append('textPath')
.attr('class', 'reference-curve')
.attr('startOffset', 5)
.attr('alignment-baseline', 'after-edge');
},
_mergeText: function(d, i, elem) {
Px.d3.select(elem).select('textPath.reference-curve')
.attr("xlink:href", function(d) { return '#refCurve_' + this.chartId + d; }.bind(this))
.text(function(d) { return this.referenceConfig[d]['name']; }.bind(this));
},
/**
* Helper function to return a d3 interpolation function.
*
* @method _returnInterpolation
*/
_returnInterpolation: function(item) {
if(this.hasUndefinedArguments(arguments)) {
return;
}
return this._referenceConfig[item]['interpolationFunction'];
},
_returnStrokeWidth: function(item) {
if(this.hasUndefinedArguments(arguments)) {
return;
}
return this._referenceConfig[item]['strokeWidth'];
},
_returnYScale: function(seriesId, config) {
if(this.domainChanged) {
if(typeof this.y === 'object') {
var d = config[seriesId]['axis']['id'];
if(this.y && this.y[d]) {
return this.y[d];
}
} else if(typeof this.y === 'function') {
return this.y;
}
}
return;
},
_computeIdPrefix: function() {
return 'refCurve_' + this.chartId;
}
});
</script>