view gtc/gtc.js @ 144:90f3021e3137

myrss2: FEEDS: Remove longform.org; add propublic.org
author paulo
date Tue, 28 May 2024 06:23:58 +0000
parents
children
line source
1 var myChart;
2 var myData;
3 var showCorrBtn = document.getElementById('showcorr');
5 nv.addGraph(function() {
6 myChart = nv.models.scatterChart()
7 .showLegend(false)
8 .showDistX(true)
9 .showDistY(true)
10 .xDomain([-1, 1])
11 .yDomain([-1, 1])
12 .duration(350)
13 .color(d3.scale.category10().range());
14 myChart.tooltip.contentGenerator(function(data) {
15 return '(' + data.point.x.toFixed(2) + ', ' + data.point.y.toFixed(2) + ')';
16 });
18 myChart.xAxis.tickFormat(d3.format('.02f'));
19 myChart.yAxis.tickFormat(d3.format('.02f'));
21 reload();
23 return myChart;
24 });
27 function randomData(groups, points) {
28 var data = [],
29 shapes = ['circle', 'cross', 'triangle-up', 'triangle-down', 'diamond', 'square'],
30 random = d3.random.normal();
32 for (i = 0; i < groups; i++) {
33 data.push({
34 key: 'Group ' + i,
35 values: []
36 });
38 for (j = 0; j < points; j++) {
39 data[i].values.push({
40 x: random(),
41 y: random(),
42 size: Math.random(),
43 shape: (Math.random() > 0.95) ? shapes[j % 6] : 'circle'
44 });
45 }
46 }
48 return data;
49 }
51 function myDataGen() {
52 var data = [];
53 var values = [];
54 var n = 25;
55 var m = 2*Math.random() - 1.0;
56 var dv = Math.random();
57 var normal = d3.random.normal(0.0, dv)
59 for (i = 0; i < n; i++) {
60 var x = i/n + 0.5*normal() - 0.5;
61 var y = m*x + normal();
62 values.push({
63 x: x,
64 y: y,
65 size: 2.0,
66 shape: 'circle',
67 });
68 }
70 data.push({
71 //key: 'Points',
72 values: values,
73 //slope: m,
74 //intercept: 0.001,
75 })
77 return data;
78 }
80 function corr(values) {
81 var x_a = [];
82 var y_a = [];
84 for (i = 0; i < values.length; i++) {
85 x_a.push(values[i].x);
86 y_a.push(values[i].y);
87 }
89 return ss.sampleCorrelation(x_a, y_a);
90 }
92 function lr(values) {
93 var x_y = [];
95 for (i = 0; i < values.length; i++) {
96 x_y.push([values[i].x, values[i].y]);
97 }
99 return ss.linearRegression(x_y);
100 }
102 function print_corr() {
103 console.log('corr = ' + corr(myData[0].values))
104 }
106 function print_lr() {
107 var lro = lr(myData[0].values);
108 console.log('m = ' + lro.m);
109 console.log('b = ' + lro.b);
110 }
112 function add_lr(lro, data) {
113 data.push({
114 values: [],
115 slope: lro.m,
116 intercept: lro.b,
117 })
118 }
120 function show_corr() {
121 var ans = corr(myData[0].values);
122 showCorrBtn.textContent = ans.toFixed(2);
123 showCorrBtn.onclick = reload;
124 }
126 function reload() {
127 myData = myDataGen();
128 add_lr(lr(myData[0].values), myData);
129 showCorrBtn.disabled = false;
130 showCorrBtn.textContent = 'Show Correlation';
131 showCorrBtn.onclick = show_corr;
133 d3.select('#chart svg')
134 .style('height', '95%')
135 .datum(myData)
136 .call(myChart);
138 nv.utils.windowResize(myChart.update);
139 }