stream_layers.js 922 B

1234567891011121314151617181920212223242526272829303132333435
  1. /* Inspired by Lee Byron's test data generator. */
  2. function stream_layers(n, m, o) {
  3. if (arguments.length < 3) o = 0;
  4. function bump(a) {
  5. var x = 1 / (.1 + Math.random()),
  6. y = 2 * Math.random() - .5,
  7. z = 10 / (.1 + Math.random());
  8. for (var i = 0; i < m; i++) {
  9. var w = (i / m - y) * z;
  10. a[i] += x * Math.exp(-w * w);
  11. }
  12. }
  13. return d3.range(n).map(function() {
  14. var a = [], i;
  15. for (i = 0; i < m; i++) a[i] = o + o * Math.random();
  16. for (i = 0; i < 5; i++) bump(a);
  17. return a.map(stream_index);
  18. });
  19. }
  20. /* Another layer generator using gamma distributions. */
  21. function stream_waves(n, m) {
  22. return d3.range(n).map(function(i) {
  23. return d3.range(m).map(function(j) {
  24. var x = 20 * j / m - i / 3;
  25. return 2 * x * Math.exp(-.5 * x);
  26. }).map(stream_index);
  27. });
  28. }
  29. function stream_index(d, i) {
  30. return {x: i, y: Math.max(0, d)};
  31. }