forked from predixdesignsystem/px-vis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpx-vis-multi-scale.html
187 lines (154 loc) · 6.08 KB
/
px-vis-multi-scale.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
<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" />
<!--
Interpreter component which creates x & y interpreters.
Interpreter components act as a converter, translating data points into their corresponding pixel coordinate. To do this, the interpreter needs to understand what type of data you have (xScale, yScale), the size of the drawing area you have (width, height, margin), and the extents of your data / domain (chartData, selectedDomain). It then returns the interpreter functions for `x` and `y` and arrays for the current domain those `x` and `y` functions are using.
##### Usage
<px-vis-multi-scale
width="[[width]]"
height="[[height]]"
margin="[[margin]]"
axes="[[axes]]"
chart-data="[[chartData]]"
chart-extents="[[chartExtents]]"
x="{{x}}"
y="{{y}}"
current-domain-x="{{currentDomainX}}"
current-domain-y="{{currentDomainY}}"
domain-changed="{{domainChanged}}">
</px-vis-multi-scale>
##### d3 Reference
https://github.com/d3/d3/wiki/Scales
https://github.com/d3/d3/wiki/API-Reference#d3scale-scales
@element px-vis-scale
@blurb Interpreter component which creates x & y interpreters.
@homepage index.html
@demo demo.html
-->
<dom-module id="px-vis-multi-scale">
<template>
<content></content>
</template>
</dom-module>
<script>
Polymer({
is: 'px-vis-multi-scale',
behaviors: [
PxVisBehaviorD3.svg,
PxVisBehavior.sizing,
PxVisBehaviorD3.axes,
PxVisBehavior.dataset,
PxVisBehavior.commonMethods,
PxVisBehaviorD3.selectedDomain,
PxVisBehavior.axisTypes,
PxVisBehavior.chartExtents,
PxVisBehavior.dimensions,
PxVisBehaviorD3.domainUpdate
],
/**
* Properties block, expose attribute values to the DOM via 'reflect'
*
* @property properties
* @type Object
*/
properties: {
},
observers: [
'_setXScale(width,margin)',
'_setYScale(height,margin,axes)',
'_currentDomain(x,y,chartExtents)'
],
/**
* when attached, re-fire set properties for precipitation pattern
*
* @method attached
*/
attached: function(){
if(this._doesObjHaveValues(this.x)){
this.fire('px-vis-x-updated', { 'dataVar': 'x', 'data': this.x, 'method':'set' });
}
if(this._doesObjHaveValues(this.y)){
this.fire('px-vis-y-updated', { 'dataVar': 'y', 'data': this.y, 'method':'set' });
}
if(this._doesObjHaveValues(this.currentDomainX)){
this.fire('px-vis-current-domain-x-updated', { 'dataVar': 'currentDomainX', 'data': this.currentDomainX, 'method':'set' });
}
if(this._doesObjHaveValues(this.currentDomainY)){
this.fire('px-vis-current-domain-y-updated', { 'dataVar': 'currentDomainY', 'data': this.currentDomainY, 'method':'set' });
}
if(this.domainChanged){
this.fire('px-vis-domain-changed-updated', { 'dataVar': 'domainChanged', 'data': this.domainChanged, 'method':'set' });
}
},
/**
* Sets the x scale to a single ordinal scale
*
* @method _setXScale
*/
_setXScale: function(width,margin) {
var w = Math.max(width - margin.left - margin.right,0),
range = [0, w];
if(this.x) {
var oldDomain = this.x.domain();
this.x = Px.d3.scalePoint().range(range).domain(oldDomain).padding(0.5);
} else {
this.x = Px.d3.scalePoint().range(range).padding(0.5);
}
this.notifyPath('x.range', this.x.range);
this.fire('px-vis-x-updated', { 'dataVar': 'x', 'data': this.x, 'method':'set' });
},
/**
* Sets the y scale to multiple linear scales
*
* @method _setYScale
*/
_setYScale: function(height,margin,axes) {
var h = Math.max(height - margin.top - margin.bottom,0),
range = [h, 0],
newY = {};// y is an object with multiple scales
// iterate through the axes and create a scale for each; set them in the y obj with the axes as keys so we can easily access them again later
for(var i = 0; i < axes.length; i++){
newY[axes[i]] = Px.d3.scaleLinear().nice().range(range);
}
this.set('y', newY);
this.fire('px-vis-y-updated', { 'dataVar': 'y', 'data': this.y, 'method':'set' });
},
/**
* Calculates and Sets the x and y domain after data loads
*
* https://github.com/mbostock/d3/wiki/API-Reference
*
* @method _currentDomain
*/
_currentDomain: function() {
// check to make sure there is data
if(this._doesObjHaveValues(this.x) &&
this._doesObjHaveValues(this.y) && this._doesObjHaveValues(this.chartExtents)){
var dims = this.chartExtents.x
exts = this.chartExtents;
for(var i = 0; i < dims.length; i++){
if(!this.y[dims[i]]) {
return;
}
this.y[dims[i]].domain(exts.y[dims[i]]);
}
this.x.domain(exts.x);
// Set the domains
if(this.x.domain().length > 0 && dims.length > 0){
this.set('currentDomainX', this.x.domain());
this.set('currentDomainY', this.y[dims[0]].domain());
this.fire('px-vis-current-domain-x-updated', { 'dataVar': 'currentDomainX', 'data': this.currentDomainX, 'method':'set' });
this.fire('px-vis-current-domain-y-updated', { 'dataVar': 'currentDomainY', 'data': this.currentDomainY, 'method':'set' });
} else {
this.set('currentDomainX', []);
this.set('currentDomainY', []);
this.fire('px-vis-current-domain-x-updated', { 'dataVar': 'currentDomainX', 'data': this.currentDomainX, 'method':'set' });
this.fire('px-vis-current-domain-y-updated', { 'dataVar': 'currentDomainY', 'data': this.currentDomainY, 'method':'set' });
}
this.set('domainChanged', (this.domainChanged === null) ? true : !this.domainChanged);
this.fire('px-vis-domain-changed-updated', { 'dataVar': 'domainChanged', 'data': this.domainChanged, 'method':'set' });
}
}
});
</script>