paulo@89: var myChart; paulo@89: var myData; paulo@89: var showCorrBtn = document.getElementById('showcorr'); paulo@89: paulo@89: nv.addGraph(function() { paulo@89: myChart = nv.models.scatterChart() paulo@89: .showLegend(false) paulo@89: .showDistX(true) paulo@89: .showDistY(true) paulo@89: .xDomain([-1, 1]) paulo@89: .yDomain([-1, 1]) paulo@89: .duration(350) paulo@89: .color(d3.scale.category10().range()); paulo@89: myChart.tooltip.contentGenerator(function(data) { paulo@89: return '(' + data.point.x.toFixed(2) + ', ' + data.point.y.toFixed(2) + ')'; paulo@89: }); paulo@89: paulo@89: myChart.xAxis.tickFormat(d3.format('.02f')); paulo@89: myChart.yAxis.tickFormat(d3.format('.02f')); paulo@89: paulo@89: reload(); paulo@89: paulo@89: return myChart; paulo@89: }); paulo@89: paulo@89: paulo@89: function randomData(groups, points) { paulo@89: var data = [], paulo@89: shapes = ['circle', 'cross', 'triangle-up', 'triangle-down', 'diamond', 'square'], paulo@89: random = d3.random.normal(); paulo@89: paulo@89: for (i = 0; i < groups; i++) { paulo@89: data.push({ paulo@89: key: 'Group ' + i, paulo@89: values: [] paulo@89: }); paulo@89: paulo@89: for (j = 0; j < points; j++) { paulo@89: data[i].values.push({ paulo@89: x: random(), paulo@89: y: random(), paulo@89: size: Math.random(), paulo@89: shape: (Math.random() > 0.95) ? shapes[j % 6] : 'circle' paulo@89: }); paulo@89: } paulo@89: } paulo@89: paulo@89: return data; paulo@89: } paulo@89: paulo@89: function myDataGen() { paulo@89: var data = []; paulo@89: var values = []; paulo@89: var n = 25; paulo@89: var m = 2*Math.random() - 1.0; paulo@89: var dv = Math.random(); paulo@89: var normal = d3.random.normal(0.0, dv) paulo@89: paulo@89: for (i = 0; i < n; i++) { paulo@89: var x = i/n + 0.5*normal() - 0.5; paulo@89: var y = m*x + normal(); paulo@89: values.push({ paulo@89: x: x, paulo@89: y: y, paulo@89: size: 2.0, paulo@89: shape: 'circle', paulo@89: }); paulo@89: } paulo@89: paulo@89: data.push({ paulo@89: //key: 'Points', paulo@89: values: values, paulo@89: //slope: m, paulo@89: //intercept: 0.001, paulo@89: }) paulo@89: paulo@89: return data; paulo@89: } paulo@89: paulo@89: function corr(values) { paulo@89: var x_a = []; paulo@89: var y_a = []; paulo@89: paulo@89: for (i = 0; i < values.length; i++) { paulo@89: x_a.push(values[i].x); paulo@89: y_a.push(values[i].y); paulo@89: } paulo@89: paulo@89: return ss.sampleCorrelation(x_a, y_a); paulo@89: } paulo@89: paulo@89: function lr(values) { paulo@89: var x_y = []; paulo@89: paulo@89: for (i = 0; i < values.length; i++) { paulo@89: x_y.push([values[i].x, values[i].y]); paulo@89: } paulo@89: paulo@89: return ss.linearRegression(x_y); paulo@89: } paulo@89: paulo@89: function print_corr() { paulo@89: console.log('corr = ' + corr(myData[0].values)) paulo@89: } paulo@89: paulo@89: function print_lr() { paulo@89: var lro = lr(myData[0].values); paulo@89: console.log('m = ' + lro.m); paulo@89: console.log('b = ' + lro.b); paulo@89: } paulo@89: paulo@89: function add_lr(lro, data) { paulo@89: data.push({ paulo@89: values: [], paulo@89: slope: lro.m, paulo@89: intercept: lro.b, paulo@89: }) paulo@89: } paulo@89: paulo@89: function show_corr() { paulo@89: var ans = corr(myData[0].values); paulo@89: showCorrBtn.textContent = ans.toFixed(2); paulo@89: showCorrBtn.onclick = reload; paulo@89: } paulo@89: paulo@89: function reload() { paulo@89: myData = myDataGen(); paulo@89: add_lr(lr(myData[0].values), myData); paulo@89: showCorrBtn.disabled = false; paulo@89: showCorrBtn.textContent = 'Show Correlation'; paulo@89: showCorrBtn.onclick = show_corr; paulo@89: paulo@89: d3.select('#chart svg') paulo@89: .style('height', '95%') paulo@89: .datum(myData) paulo@89: .call(myChart); paulo@89: paulo@89: nv.utils.windowResize(myChart.update); paulo@89: }