Permalink
Browse files

fixes #83 Added ordinal scale

  • Loading branch information...
1 parent 56e72de commit 9697540e9f427c33d7ac6ca2db8e91149b8455fe @bjornd committed Sep 2, 2012
Showing with 51 additions and 26 deletions.
  1. +35 −21 lib/data-series.js
  2. +7 −0 lib/ordinal-scale.js
  3. +9 −5 tests/markers.html
View
56 lib/data-series.js
@@ -16,16 +16,20 @@ jvm.DataSeries = function(params, elements) {
params.attribute = params.attribute || 'fill';
params.scale = params.scale || ['#C8EEFF', '#0071A4'];
- scaleConstructor = (params.attribute === 'fill' || params.attribute === 'stroke') ? jvm.ColorScale : jvm.NumericScale
-
this.elements = elements;
this.params = params;
if (params.attributes) {
this.setAttributes(params.attributes);
}
- this.scale = new scaleConstructor(params.scale, params.normalizeFunction, params.min, params.max);
+ if (!$.isArray(params.scale)) {
+ this.scale = new jvm.OrdinalScale(params.scale);
+ } else {
+ scaleConstructor = (params.attribute === 'fill' || params.attribute === 'stroke') ? jvm.ColorScale : jvm.NumericScale;
+ this.scale = new scaleConstructor(params.scale, params.normalizeFunction, params.min, params.max);
+ }
+
if (params.values) {
this.values = params.values;
this.setValues(params.values);
@@ -59,30 +63,40 @@ jvm.DataSeries.prototype = {
cc,
attrs = {};
- if (!this.params.min || !this.params.max) {
+ if (!(this.scale instanceof jvm.OrdinalScale)) {
+ if (!this.params.min || !this.params.max) {
+ for (cc in values) {
+ val = parseFloat(values[cc]);
+ if (val > max) max = values[cc];
+ if (val < min) min = val;
+ }
+ if (!this.params.min) {
+ this.scale.setMin(min);
+ }
+ if (!this.params.max) {
+ this.scale.setMax(max);
+ }
+ this.params.min = min;
+ this.params.max = max;
+ }
for (cc in values) {
val = parseFloat(values[cc]);
- if (val > max) max = values[cc];
- if (val < min) min = val;
- }
- if (!this.params.min) {
- this.scale.setMin(min);
+ if (!isNaN(val)) {
+ attrs[cc] = this.scale.getValue(val);
+ } else {
+ attrs[cc] = this.elements[cc].element.style.initial[this.params.attribute];
+ }
}
- if (!this.params.max) {
- this.scale.setMax(max);
+ } else {
+ for (cc in values) {
+ if (values[cc]) {
+ attrs[cc] = this.scale.getValue(values[cc]);
+ } else {
+ attrs[cc] = this.elements[cc].element.style.initial[this.params.attribute];
+ }
}
- this.params.min = min;
- this.params.max = max;
}
- for (cc in values) {
- val = parseFloat(values[cc]);
- if (!isNaN(val)) {
- attrs[cc] = this.scale.getValue(val);
- } else {
- attrs[cc] = this.elements[cc].element.style.initial[this.params.attribute];
- }
- }
this.setAttributes(attrs);
this.values = values;
},
View
7 lib/ordinal-scale.js
@@ -0,0 +1,7 @@
+jvm.OrdinalScale = function(scale){
+ this.scale = scale;
+}
+
+jvm.OrdinalScale.prototype.getValue = function(value){
+ return this.scale[value];
+}
View
14 tests/markers.html
@@ -36,6 +36,7 @@
<script src="../lib/vml-circle-element.js"></script>
<script src="../lib/vector-canvas.js"></script>
+ <script src="../lib/ordinal-scale.js"></script>
<script src="../lib/numeric-scale.js"></script>
<script src="../lib/color-scale.js"></script>
<script src="../lib/data-series.js"></script>
@@ -71,14 +72,17 @@
values: values2
}],
regions: [{
- scale: ['#C8EEFF', '#0071A4'],
+ scale: {
+ red: '#ff0000',
+ blue: '#00ff00'
+ },
attribute: 'fill',
normalizeFunction: 'polynomial',
values: {
- "US-KS": 1,
- "US-MO": 2,
- "US-IA": 3,
- "US-NE": 4
+ "US-KS": 'red',
+ "US-MO": 'red',
+ "US-IA": 'blue',
+ "US-NE": 'blue'
}
}]
},

0 comments on commit 9697540

Please sign in to comment.