forked from andrewfaria/hGraph
-
Notifications
You must be signed in to change notification settings - Fork 1
/
HealthGraph.js
executable file
·746 lines (626 loc) · 22.9 KB
/
HealthGraph.js
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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
/*global d3: true */
/*
File: HGraph.js
Description:
Class for generating the Health Score Graphs
Requires:
d3.js
Authors:
Michael Bester <[email protected]>
Danny Hadley <[email protected]>
License:
Copyright 2012, Involution Studios <http://goinvo.com>
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.
*/
/**
* Class: HGraph
* Sets up new HGraph instances
*/
var HGraph = function(opts) {
this.container = opts.container || null;
this.context = null;
this.width = opts.width || 0;
this.hheight = opts.height || 0;
this.rotation = opts.rotation || 0;
this.zoomFactor = opts.zoomFactor || 2.2;
this.zoomTime = opts.zoomTime || 800;
this.healthRange = {
lower : 30,
upper : 80
};
this.halfWidth = 0;
this.halfHeight = 0;
this.userdata = opts.userdata || {};
this.primaryIncrement = 1;
this.showLabels = false;
this.isZoomed = false;
this.center = null;
this.originCoords = {
x : 0,
y : 0
};
this.layers = {
ring : null,
web : null,
text : null,
datapoints : null
};
this.colors = {
ring : '#bdcc9e',
text : '#444648',
outOfRange : '#e1604f',
inRange : '#616363',
web : 'rgba(0,0,0,0.1)'
};
};
/**
* Function: HGraph.initialize
* Sets up and draws the graph
*/
HGraph.prototype.initialize = function() {
var layer, i, datapoint, web, touchstart, movedelta, that;
that = this;
this.width = this.width || this.container.offsetWidth;
this.height = this.height || this.container.offsetHeight;
this.halfWidth = this.width / 2;
this.halfHeight = this.height / 2;
console.log(this.width + " " + this.height);
this.showLabels = ((this.width / this.height) > 1.2);
this.scaleRange = this.showLabels ?
[this.halfHeight / 4, this.halfHeight / 1.25] :
[this.halfHeight / 3, this.halfHeight];
this.center = 'translate(' + this.halfWidth + ',' + this.halfHeight + ')';
this.topleft = 'translate(0,0)';
this.scale = d3.scale.linear().domain([0,100]).range(this.scaleRange);
this.context = d3.select(this.container)
.append('svg')
.attr('class','healthscore')
.attr('width',this.width).attr('height',this.height);
// Set up the layers
for ( layer in this.layers ) {
if (this.layers.hasOwnProperty(layer)) {
this.layers[layer] = this.context.append('g')
.attr('class','layer')
.attr('data-layername', layer)
.attr('transform', this.center);
}
}
this.originCoords.x = this.halfWidth;
this.originCoords.y = this.halfHeight;
// Draw the ring around the user's health score
this.ringpath = d3.svg.arc()
.startAngle(0)
.endAngle(360)
.innerRadius(this.scale(this.healthRange.lower))
.outerRadius(this.scale(this.healthRange.upper));
this.ring = this.layers.ring.append('path').attr('d', this.ringpath).attr('fill', this.colors.ring);
// and the score itself
this.overalltxt = this.layers.text.append('text')
.attr('class','overall')
.text(this.calculateHealthScore());
// Center the score
this.overalltxt
.attr('x', 0 - (this.overalltxt[0][0].offsetWidth / 2))
.attr('y', this.overalltxt[0][0].offsetHeight / 3);
// Figure out how many points there should be
this.primaryIncrement = 360 / this.userdata.factors.length || 1;
// Add the datapoints.
for (i = 0; i < this.userdata.factors.length; i++) {
datapoint = this.userdata.factors[i];
this.addPoint(datapoint, i);
}
// Create a web path and hook it up
web = this.layers.web.append('path');
this.updateWeb(false);
web
.attr('data-originalPath', web.attr('d'))
.attr('fill', this.colors.web);
// Set up the dragability of the zoomed in graph
touchstart = {
x : 0,
y : 0
};
moveDelta = {
x : 0,
y : 0
};
this.container.addEventListener('touchstart', function(e){
var touch;
if ( e.touches.length > 1) {
return;
}
touch = e.touches[0];
touchstart.x = touch.pageX;
touchstart.y = touch.pageY;
});
this.container.addEventListener('touchmove', function(e){
var key, layer, delta, touch;
e.preventDefault();
if ( ! that.isZoomedIn() ) {
return;
}
touch = e.touches[0];
delta = {
x : touch.pageX - touchstart.x,
y : touch.pageY - touchstart.y
};
// Get the new origin coordinates, clamping it to the containter size
moveDelta.x = Math.max(0, Math.min(that.width, delta.x + that.originCoords.x));
moveDelta.y = Math.max(0, Math.min(that.height, delta.y + that.originCoords.y));
for ( key in that.layers ) {
if (that.layers.hasOwnProperty(key)) {
that.layers[key].attr('transform', 'translate(' + moveDelta.x + ', ' + moveDelta.y + ')');
}
}
});
this.container.addEventListener('touchend', function(e){
if ( e.touches.length > 1) {
return;
}
that.originCoords.x = moveDelta.x;
that.originCoords.y = moveDelta.y;
});
};
/**
* Function: HGraph.calculateScoreFromValue
* calculates score based on metrics
*
* Arguments:
* features - *(JSON)* Contains the features to be measured against
* myValue - *(number)* Number to compare to features
*/
HGraph.prototype.calculateScoreFromValue = function (features, myValue){
var maxHealthyValue = features.healthyrange[1];
var minHealthyValue = features.healthyrange[0];
var maxAcceptableValue = features.totalrange[1];
var minAcceptableValue = features.totalrange[0];
if(myValue <= maxHealthyValue && myValue >= minHealthyValue){
//calculate if we are in healthy range
var healthyRangeMidPoint = (minHealthyValue + maxHealthyValue)/2;
//This value will have a score of 100.
var healthyHalfRange = (maxHealthyValue - minHealthyValue) / 2;
//This defines the slope of the curve in the healthy range
var score = 100 - (30 / healthyHalfRange) * (abs(myValue - healthyRangeMidPoint));
if(maxHealthyValue == maxAcceptableValue){
//Our graph is clamped on the right side
if(myValue>healthyRangeMidPoint){
score=100;
}
}
if(minHealthyValue == minAcceptableValue){
//our graph is clamped on the left side.
if(myValue> healthyRangeMidPoint){
score=100;
}
}
return score;
}
else{
//We are outside the healthy range.
if(myValue > maxHealthyRange){
//We are on the high side
var highRange = (maxAcceptableValue - maxHealthyValue);
score = 100 + (70/highRange)*(myValue - maxHealthyValue);
//Note that this means we will get a value of up to 170 on the hGraph.
//This is likely to be problematic because it will have a tendancy to make people's hScore super low
//In the case where the healthy region is almost equal to the maxAcceptableValue and the patient
//Is just outside the healthyRange
//For now, maybe we should go with it until we figure out the exact scales we want to use for the hGraph.
//This will show points that are high outside of the green circle. If we just want to get a 0-100 for everything:
//score = 70 - (70/highRange)*(myValue-maxHealthyValue);
return score;
}
else{
//We are on the low side
var lowRange = (minHealthyValue - minAcceptableValue);
score = 70 - (70/lowRange)*(myValue - minAcceptableValue);
//This means we will get down to 0.
return score;
}
}
}
/**
* Function: HGraph.zoomIn
* Zooms the graph.
*
* Arguments:
* zoomFactor - *(Number)* The factor you want to zoom in by. If omitted, the instance's default zoom factor is used.
*/
HGraph.prototype.zoomIn = function(zoomFactor) {
var key, i, j, factor, details, layer, labels, that, zoomedWebFillString, getZoomedRingPath, getZoomedX, getZoomedY, scoreRange;
that = this;
zoomFactor = zoomFactor || this.zoomFactor; // Allow zooming factor override.
zoomedWebFillString = '';
getZoomedRingPath = function() {
var zoomedRingPath, zoomedScale, zoomedScaleRange, i;
zoomedScaleRange = [];
for (i = 0; i < that.scaleRange.length; i++) {
zoomedScaleRange.push(that.scaleRange[i] * zoomFactor);
}
zoomedScale = d3.scale.linear().domain([0,100]).range(zoomedScaleRange);
zoomedRingPath = d3.svg.arc()
.startAngle(0)
.endAngle(360)
.innerRadius(zoomedScale(that.healthRange.lower))
.outerRadius(zoomedScale(that.healthRange.upper));
return zoomedRingPath;
};
getZoomedX = function() {
var coords = JSON.parse(this.getAttribute('data-origCoords'));
return coords.x * zoomFactor;
};
getZoomedY = function() {
var coords = JSON.parse(this.getAttribute('data-origCoords'));
return coords.y * zoomFactor;
};
setDetailText = function() {
return this.textContent + ' (' + this.getAttribute('data-metricValue') + ')';
};
// Do the zooming.
for ( key in this.layers ) {
if (this.layers.hasOwnProperty(key)) {
layer = this.layers[key];
// Zoom the ring in.
if ( key === 'ring' ) {
layer.select('path')
.transition().ease('elastic')
.duration(this.zoomTime)
.attr('d', getZoomedRingPath());
// Do the datapoints
} else if ( key === 'datapoints') {
layer.selectAll('circle')
.transition().ease('elastic')
.duration(this.zoomTime)
.attr('cx', getZoomedX)
.attr('cy', getZoomedY);
// And the labels
if (this.showLabels) {
labels = layer.selectAll('text');
labels
.transition().ease('elastic')
.duration(this.zoomTime)
.attr('x', getZoomedX)
.attr('y', getZoomedY);
window.setTimeout(function(){
labels.text(setDetailText);
}, this.zoomTime);
}
} else if ( key === 'web') {
this.updateWeb(true, true);
}
// Move it to the new origin point.
layer
.transition().ease('quad-out')
.duration(this.zoomTime * 0.8)
.attr('transform', this.topleft);
}
}
window.setTimeout(function(){
var web, secondaryPoints;
for (i = 0; i < that.userdata.factors.length; i++) {
factor = that.userdata.factors[i];
if ( ! factor.details ) {
continue;
}
for (j = 0; j < factor.details.length; j++) {
details = factor.details[j];
// get the score of the current and next parent node
scoreRange = [
factor.score,
(that.userdata.factors[i + 1] ? that.userdata.factors[i + 1] : that.userdata.factors[0]).score
];
that.addPoint(
details,
j + 1,
factor.angle,
(that.primaryIncrement / (factor.details.length + 1) || 1),
factor.details.length + 1,
scoreRange,
i,
true
);
}
}
// Select all the secondary points.
secondaryPoints = that.layers.datapoints.selectAll('.secondary');
// Redraw the path to include the new elements and make a note of the path
web = that.updateWeb(false);
web.attr('data-secondaryStartingPath', web.attr('d'));
// Remove the starting data point attribute
secondaryPoints.attr('data-startingCoords', null);
// Animate them to their proper values
secondaryPoints
.transition().ease('elastic')
.duration(that.zoomTime)
.attr('cx', getZoomedX)
.attr('cy', getZoomedY);
// And make the web follow suit
that.updateWeb();
}, this.zoomTime);
this.originCoords.x = 0;
this.originCoords.y = 0;
this.isZoomed = true;
};
/**
* Function: HGraph.zoomOut
* Zooms the graph back to its original dimensions
*/
HGraph.prototype.zoomOut = function() {
var getOriginalX, getOriginalY, setDetailText, web;
getOriginalX = function() {
var coords = JSON.parse(this.getAttribute('data-origCoords'));
return coords.x;
};
getOriginalY = function() {
var coords = JSON.parse(this.getAttribute('data-origCoords'));
return coords.y;
};
setDetailText = function(d) {
return this.textContent.replace(/\s+\([^\)]*\)$/, '');
};
// Remove secondary points.
this.layers.datapoints.selectAll('.secondary').remove();
// Revert the web back to the previous path
web = this.updateWeb(true, false, true);
// Remove the secondarry starting path reference
web.attr('data-secondaryStartingPath', null);
window.setTimeout(function(that){
var key, layer;
// Revert the web to the primary points.
that.updateWeb(false);
// Loop through the layers and go back to the original state.
for ( key in that.layers ) {
if (that.layers.hasOwnProperty(key)) {
layer = that.layers[key];
// Zoom the ring out
if ( key === 'ring' ) {
layer.select('path')
.transition().ease('quad-in-out')
.duration(that.zoomTime)
.attr('d', that.ringpath);
// And the datapoints
} else if ( key === 'datapoints') {
// remove secondary points
layer.selectAll('.secondary').remove();
layer.selectAll('circle')
.transition().ease('quad-in-out')
.duration(that.zoomTime)
.attr('cx', getOriginalX)
.attr('cy', getOriginalY);
// And the labels
if ( that.showLabels ) {
layer.selectAll('text')
.transition().ease('quad-in-out')
.duration(that.zoomTime)
.text(setDetailText)
.attr('x', getOriginalX)
.attr('y', getOriginalY);
}
} else if ( key === 'web') {
that.updateWeb(true, true, true, 'quad-in-out');
}
// Move the layer back to the center
that.layers[key]
.transition().ease('quad-in')
.duration(that.zoomTime)
.attr('transform', that.center);
}
}
that.isZoomed = false;
}, this.zoomTime * 0.9, this);
};
/**
* Function: HGraph.isZoomedIn
* Tells you whether the graph is currently zoomed in or not.
*
* Returns:
* *(Boolean)* true if the graph is currently zoomed in, otherwise false
*/
HGraph.prototype.isZoomedIn = function() {
return this.isZoomed;
};
/**
* Function: addPoint
* Adds a data point to the graph
*
* Arguments:
* datapont - *(Object)* A datapoint with score and label properties.
* index - *(Number)* An index (i.e. the loop index when looping through point data to add points)
* startingAngle - *(Number)* The angle where you begin calculating the X & Y coordinates to plot. Defaults to 0.
* increment - *(Number)* the number of degrees you want to increment from the previous point. Defaults to this.primaryIncrement
* steps - *(Number)* The number of datapoints in a datapoint array. Used when rendering zoomed in details.
* startingScoreRange - *(Array)* An array of two adjacent datapoint scores. Used to calculate starting coordinates for secondary points.
* klass - *(String)* A class name to add to the new point and point label.
* labelWithDetail - *(Boolean)* If you want the label to include the score or value, pass in true here.
*
* Returns:
* *(Object)* A d3 selection with the point element that was appended to the graph.
*/
HGraph.prototype.addPoint = function(datapoint, index, startingAngle, increment, steps, startingScoreRange, sortIndex, labelWithDetail) {
var point, secondary, angle, coords, radius, getPointColor, getTextAnchor, gotoPage, interstitialScore, startingDataValue, startingCoords, scoreDiff, scaledDataValue, labelPointScale, metricValue;
index = typeof index === 'number' ? index : 1;
secondary = (startingScoreRange && startingScoreRange.constructor === Array);
// Private utility functions to get the point color
getPointColor = function(score) {
return (score > this.scale(this.healthRange.lower) && score < this.scale(this.healthRange.upper) ) ? this.colors.inRange : this.colors.outOfRange;
};
getTextAnchor = function(x) {
x = parseInt(x, 10);
if ( x >= -10 && x <= 10) {
return 'middle';
} else {
return ( x > 0) ? 'start' : 'end';
}
};
gotoPage = function() {
var href = this.getAttribute('href');
if ( ! href ) {
return;
}
window.location.href = href;
};
scaledDataValue = this.scale(datapoint.score);
if ( secondary ) {
scoreDiff = startingScoreRange[1] - startingScoreRange[0];
interstitialScore = startingScoreRange[0] + ((scoreDiff / steps) * index);
startingDataValue = this.scale(interstitialScore);
}
// Figure out where to place the point
angle = ((increment || this.primaryIncrement) * index) + (startingAngle || 0);
//console.log(angle);
radian = angle * (Math.PI / 180);
if ( startingDataValue ) {
startingCoords = {
x : (Math.cos(radian) * startingDataValue).toFixed(1),
y : (Math.sin(radian) * startingDataValue).toFixed(1)
};
}
coords = {
x : (Math.cos(radian) * scaledDataValue).toFixed(1),
y : (Math.sin(radian) * scaledDataValue).toFixed(1)
};
datapoint.angle = angle;
datapoint.coords = coords;
radius = Math.max(1, Math.min(10, Math.round(this.width / 80)));
point = this.layers.datapoints.append('circle')
.data([datapoint.score])
.attr('r', secondary ? radius / 1.5 : radius)
.attr('cx', (startingCoords ? startingCoords.x : coords.x) * (this.isZoomedIn() ? this.zoomFactor : 1))
.attr('cy', (startingCoords ? startingCoords.y : coords.y) * (this.isZoomedIn() ? this.zoomFactor : 1))
.attr('data-origCoords', JSON.stringify(coords))
.attr('data-sortIndex', typeof sortIndex === 'number' ? sortIndex + '.' + index : index)
.attr('fill', getPointColor.call(this, scaledDataValue));
if ( startingCoords ) {
point.attr('data-startingCoords', JSON.stringify(startingCoords));
}
// Note the secondary class as such
if ( secondary ) {
point.classed('secondary', true);
}
if ( this.showLabels ) {
// Calculate the size of the datapoint radius (clamping it between 1 and 10)
labelPointScale = Math.max(scaledDataValue + (radius * 3), this.scale(100));
// Do the labels.
labelCoords = {
x : (Math.cos(radian) * labelPointScale).toFixed(1),
y : (Math.sin(radian) * labelPointScale).toFixed(1)
};
// Are we zoomed in?
if ( this.isZoomedIn() ) {
labelCoords.x = labelCoords.x * this.zoomFactor;
labelCoords.y = labelCoords.y * this.zoomFactor;
}
metricValue = datapoint.value || datapoint.score;
label = this.layers.datapoints.append('text')
.text(datapoint.label + (labelWithDetail ? ' (' + metricValue + ')' : ''))
.data([datapoint.score])
.attr('class', 'label')
.attr('x', labelCoords.x)
.attr('y', labelCoords.y)
.attr('data-origCoords', JSON.stringify(labelCoords))
.attr('data-metricValue', metricValue)
.attr('fill', getPointColor.call(this, scaledDataValue))
.attr('text-anchor', getTextAnchor(coords.x));
if ( secondary ) {
label.classed('secondary', true);
}
// Hook up a link if it's there.
if ( datapoint.link ) {
point
.attr('href', datapoint.link)
.classed('linked', true)
.on('click', gotoPage)
.on('touchend', gotoPage);
label
.attr('href', datapoint.link)
.classed('linked', true)
.on('click', gotoPage)
.on('touchend', gotoPage);
}
// Cascade the display of the labels.
window.setTimeout(function(label){
label.classed('visible', true);
}, 60 * index, label);
}
return point;
};
/**
* Function: HGraph.updateWeb
* Updates the shadow that connects the dots on the web.
*
* Arguments:
* animated - *(Boolean)* if true, the web animates to its new shape, otherwise it assumes the new shape immediately.
* forceZoomedState - *(Boolean)* if true, we assume that calculating the zoomed in state, otherwise it checks for the current zoom status.
* revertToOriginal - *(Boolean)* if true, the web reverts to the path saved in the data-secondaryStartingPath (if it exisits) or data-originalPath attribute. Otherwise, it calculates the path based on the datapoint positions.
*
* Example:
* >
*
* Returns:
* *(Object)* A d3 selection with the web path element.
*/
HGraph.prototype.updateWeb = function(animated, forceZoomedState, revertToOriginal, easing) {
var that, calculateWebPathCoordinate, fillString, points, web;
animated = typeof animated === 'boolean' ? animated : true;
easing = typeof easing === 'string' ? easing : 'elastic';
that = this;
fillString = '';
calculateWebPathCoordinate = function(d, i){
var pointCoords, zoomedCoords;
pointCoords = JSON.parse(this.getAttribute('data-startingCoords') || this.getAttribute('data-origCoords'));
zoomedCoords = {
x : (parseFloat(pointCoords.x) * (forceZoomedState || that.isZoomedIn() ? that.zoomFactor : 1)).toFixed(1),
y : (parseFloat(pointCoords.y) * (forceZoomedState || that.isZoomedIn() ? that.zoomFactor : 1)).toFixed(1)
};
fillString += (i === 0 ? 'M ' : 'L ') + zoomedCoords.x + ' ' + zoomedCoords.y + ' ';
};
// Get the element
web = this.layers.web.select('path');
if ( ! revertToOriginal ) {
// Calculate the new fill string.
points = this.layers.datapoints.selectAll('circle');
// sort the points to make sure the path goes around the ring in one direction.
points[0].sort(function(a, b) {
return parseFloat(a.getAttribute('data-sortIndex'), 10) - parseFloat(b.getAttribute('data-sortIndex'), 10);
});
points.each(calculateWebPathCoordinate);
} else {
// Go back to the original fill string.
fillString = web.attr('data-secondaryStartingPath') || web.attr('data-originalPath');
}
if ( animated ) {
web
.transition().ease(easing)
.duration(this.zoomTime)
.attr('d', fillString);
} else {
web.attr('d', fillString);
}
return web;
};
HGraph.prototype.calculateHealthScore = function(){
//V0.3 of hScore Algorithm.
if(this.userdata && this.userdata.factors){
var numPoints = this.userdata.factors.length;
var idealValue = (this.healthRange.lower + this.healthRange.upper)/2.0;
var widthGood = this.healthRange.upper - this.healthRange.lower;
var factor, sumSquares=0;
for(factor in this.userdata.factors){
var score = this.userdata.factors[factor].score;
sumSquares = sumSquares + Math.pow(idealValue - score,2);
}
// console.log('idealValue='+idealValue);
// console.log('numPoints='+numPoints);
// console.log('sumSquares='+sumSquares);
return parseInt(100-(100/(Math.pow(idealValue,2)*numPoints))*sumSquares);
}
return 50;
};