historicalBar.html 2.93 KB
Newer Older
afe's avatar
afe committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158
<!DOCTYPE html>
<meta charset="utf-8">
<link href="../src/nv.d3.css" rel="stylesheet" type="text/css">
<style>

body {
  overflow-y:scroll;
}

text {
  font: 12px sans-serif;
}

</style>
<body>

<svg id="test1"></svg>

<script src="../lib/d3.v3.js"></script>
<script src="../nv.d3.js"></script>
<script src="../src/models/historicalBar.js"></script>
<script src="../src/utils.js"></script>

<script> 


nv.addGraph({
  generate: function() {
    var width = nv.utils.windowSize().width - 40,
        height = nv.utils.windowSize().height - 40;

    var chart = nv.models.historicalBar()
        .padData(true)
        .width(width)
        .height(height);

    d3.select("#test1")
      .attr('width', width)
      .attr('height', height)
      .datum(sinData())
      .transition()
      .call(chart);

    return chart;
  },
  callback: function(graph) {

    graph.dispatch.on('elementMouseover', function(e) {
        var offsetElement = document.getElementById("chart"),
                left = e.pos[0],
                top = e.pos[1];

                /*
        var content = '<h3>' + e.label + '</h3>' +
                '<p>' +
                e.value +
                '</p>';
                */

        var content = '<p>' + e.point.y + '</p>';

        nv.tooltip.show([left, top], content, e.value < 0 ? 'n' : 's');
    });

    graph.dispatch.on('elementMouseout', function(e) {
        nv.tooltip.cleanup();
    });

    /*
    graph.dispatch.on('elementClick', function(e) {
        console.log("Bar Click",e);
    });

    graph.dispatch.on('chartClick', function(e) {
        console.log("Chart Click",e);
    });

    graph.dispatch.on('chartClick', function(e) {
        console.log('Click Switching to');
        if (td === 0) {
            d3.select("#test1")
                    .datum(testdata2)
                    .call(graph);
            td = 1;

        } else {
            d3.select("#test1")
                    .datum(testdata)
                    .call(graph);
            td = 0;
        }
      });
    */


    window.onresize = function() {
      var width = nv.utils.windowSize().width - 40,
          height = nv.utils.windowSize().height - 40;

      graph
        .width(width)
        .height(height)

      d3.select("#test1")
          .attr('width', width)
          .attr('height', height)
        .call(graph);
    };
  }
});





//Simple test data generators

function sinAndCos() {
  var sin = [],
      cos = [];

  for (var i = 0; i < 100; i++) {
    sin.push({x: i, y: Math.sin(i/10)});
    cos.push({x: i, y: .5 * Math.cos(i/10)});
  }

  return [
    {
      values: sin,
      key: "Sine Wave",
      color: "#ff7f0e"
    },
    {
      values: cos,
      key: "Cosine Wave",
      color: "#2ca02c"
    }
  ];
}


function sinData() {
  var sin = [];

  for (var i = 0; i < 100; i++) {
    sin.push({x: i, y: Math.sin(i/10)});
  }

  return [
    {
      values: sin,
      key: "Sine Wave",
      color: "#ff7f0e"
    }
  ];
}

</script>