Widget:WJG Sandbox chart: Difference between revisions
Jump to navigation
Jump to search
Undo revision 971611 by William J Gibson (talk) |
No edit summary |
||
Line 9: | Line 9: | ||
<!-- import plugin script --> | <!-- import plugin script --> | ||
<script src='http://dl.dropboxusercontent.com/u/1874620/Chart.js'></script> | <script src='http://dl.dropboxusercontent.com/u/1874620/Chart.js'></script> | ||
<script> | |||
var testScores=[53.4,49.8,61.8,44.5,47.8,62.9,68.7,64.9,65.0,82.7,77.9,78.6,80.1,84.6]; | |||
// I define a function here that takes in an array of values, calculates the exponential moving average based on a certain half-life | |||
// and returns an array with the exponential moving average at each point. | |||
function calc_exp_moving_average(values,half_life){ | |||
var exp_avg_vals=new Array(values.length); //Initialize new array in which we'll put our calculated values | |||
for (var i = values.length; i >(-1); i=i-1) { | |||
exp_weights=[]; | |||
for (var j=0; j<i; j+=1){ | |||
var weight=Math.pow(half_life, i-j-1); | |||
exp_weights[j]=weight; | |||
};//Ends the calculation of the exponential weights for each timepoint. | |||
var score_sum = 0; | |||
var subvalues=values.slice(0,i+1); | |||
var exp_weight_sum=0; | |||
for(var i=0; i< exp_weights.length; i++) { | |||
score_sum += exp_weights[i]*subvalues[i]; | |||
exp_weight_sum+=exp_weights[i]; | |||
}//Ends the multiplication of the weights by the scores. | |||
var average_score=score_sum/exp_weight_sum; | |||
if (i==0){average_score=values[i];} | |||
exp_avg_vals[i]=average_score; | |||
} //Ends the iteration through values.length | |||
return exp_avg_vals.slice(1,exp_avg_vals.length+1); | |||
}//Ends the function | |||
exp_vals=calc_exp_moving_average(testScores,2/3); | |||
</script> | |||
</head> | </head> | ||
<body> | <body> | ||
Line 20: | Line 49: | ||
// line chart data | // line chart data | ||
var buyerData = { | var buyerData = { | ||
labels : ["January","February","March","April","May","June"], | labels : ["January","February","March","April","May","June","July","August","September","October","November","December","Jan2","Feb2"], | ||
datasets : [ | datasets : [ | ||
{ | { | ||
Line 27: | Line 56: | ||
pointColor : "#fff", | pointColor : "#fff", | ||
pointStrokeColor : "#9DB86D", | pointStrokeColor : "#9DB86D", | ||
data : | data : testScores; | ||
} | } | ||
] | ] |