diff gtc/gtc.js @ 89:18f8c214169f

add gtc
author paulo
date Sun, 19 Feb 2017 19:45:31 -0800
parents
children
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/gtc/gtc.js	Sun Feb 19 19:45:31 2017 -0800
     1.3 @@ -0,0 +1,139 @@
     1.4 +var myChart;
     1.5 +var myData;
     1.6 +var showCorrBtn = document.getElementById('showcorr');
     1.7 +
     1.8 +nv.addGraph(function() {
     1.9 +  myChart = nv.models.scatterChart()
    1.10 +                .showLegend(false)
    1.11 +                .showDistX(true)
    1.12 +                .showDistY(true)
    1.13 +                .xDomain([-1, 1])
    1.14 +                .yDomain([-1, 1])
    1.15 +                .duration(350)
    1.16 +                .color(d3.scale.category10().range());
    1.17 +  myChart.tooltip.contentGenerator(function(data) {
    1.18 +    return '(' + data.point.x.toFixed(2) + ', ' + data.point.y.toFixed(2) + ')';
    1.19 +  });
    1.20 +
    1.21 +  myChart.xAxis.tickFormat(d3.format('.02f'));
    1.22 +  myChart.yAxis.tickFormat(d3.format('.02f'));
    1.23 +
    1.24 +  reload();
    1.25 +
    1.26 +  return myChart;
    1.27 +});
    1.28 +
    1.29 +
    1.30 +function randomData(groups, points) {
    1.31 +  var data = [],
    1.32 +      shapes = ['circle', 'cross', 'triangle-up', 'triangle-down', 'diamond', 'square'],
    1.33 +      random = d3.random.normal();
    1.34 +
    1.35 +  for (i = 0; i < groups; i++) {
    1.36 +    data.push({
    1.37 +      key: 'Group ' + i,
    1.38 +      values: []
    1.39 +    });
    1.40 +
    1.41 +    for (j = 0; j < points; j++) {
    1.42 +      data[i].values.push({
    1.43 +        x: random(),
    1.44 +        y: random(),
    1.45 +        size: Math.random(),
    1.46 +        shape: (Math.random() > 0.95) ? shapes[j % 6] : 'circle'
    1.47 +      });
    1.48 +    }
    1.49 +  }
    1.50 +
    1.51 +  return data;
    1.52 +}
    1.53 +
    1.54 +function myDataGen() {
    1.55 +  var data = [];
    1.56 +  var values = [];
    1.57 +  var n = 25;
    1.58 +  var m = 2*Math.random() - 1.0;
    1.59 +  var dv = Math.random();
    1.60 +  var normal = d3.random.normal(0.0, dv)
    1.61 +
    1.62 +  for (i = 0; i < n; i++) {
    1.63 +    var x = i/n + 0.5*normal() - 0.5;
    1.64 +    var y = m*x + normal();
    1.65 +    values.push({
    1.66 +      x: x,
    1.67 +      y: y,
    1.68 +      size: 2.0,
    1.69 +      shape: 'circle',
    1.70 +    });
    1.71 +  }
    1.72 +
    1.73 +  data.push({
    1.74 +    //key: 'Points',
    1.75 +    values: values,
    1.76 +    //slope: m,
    1.77 +    //intercept: 0.001,
    1.78 +  })
    1.79 +
    1.80 +  return data;
    1.81 +}
    1.82 +
    1.83 +function corr(values) {
    1.84 +  var x_a = [];
    1.85 +  var y_a = [];
    1.86 +
    1.87 +  for (i = 0; i < values.length; i++) {
    1.88 +    x_a.push(values[i].x);
    1.89 +    y_a.push(values[i].y);
    1.90 +  }
    1.91 +
    1.92 +  return ss.sampleCorrelation(x_a, y_a);
    1.93 +}
    1.94 +
    1.95 +function lr(values) {
    1.96 +  var x_y = [];
    1.97 +
    1.98 +  for (i = 0; i < values.length; i++) {
    1.99 +    x_y.push([values[i].x, values[i].y]);
   1.100 +  }
   1.101 +
   1.102 +  return ss.linearRegression(x_y);
   1.103 +}
   1.104 +
   1.105 +function print_corr() {
   1.106 +  console.log('corr = ' + corr(myData[0].values))
   1.107 +}
   1.108 +
   1.109 +function print_lr() {
   1.110 +  var lro = lr(myData[0].values);
   1.111 +  console.log('m = ' + lro.m);
   1.112 +  console.log('b = ' + lro.b);
   1.113 +}
   1.114 +
   1.115 +function add_lr(lro, data) {
   1.116 +  data.push({
   1.117 +    values: [],
   1.118 +    slope: lro.m,
   1.119 +    intercept: lro.b,
   1.120 +  })
   1.121 +}
   1.122 +
   1.123 +function show_corr() {
   1.124 +  var ans = corr(myData[0].values);
   1.125 +  showCorrBtn.textContent = ans.toFixed(2);
   1.126 +  showCorrBtn.onclick = reload;
   1.127 +}  
   1.128 +
   1.129 +function reload() {
   1.130 +  myData = myDataGen();
   1.131 +  add_lr(lr(myData[0].values), myData);
   1.132 +  showCorrBtn.disabled = false;
   1.133 +  showCorrBtn.textContent = 'Show Correlation';
   1.134 +  showCorrBtn.onclick = show_corr;
   1.135 +
   1.136 +  d3.select('#chart svg')
   1.137 +    .style('height', '95%')
   1.138 +    .datum(myData)
   1.139 +    .call(myChart);  
   1.140 +
   1.141 +  nv.utils.windowResize(myChart.update);
   1.142 +}