scatterWithLegend.html 3.6 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 159 160 161 162 163 164 165 166 167
<!DOCTYPE html>
<meta charset="utf-8">

<link href="../src/d3.css" rel="stylesheet" type="text/css">

<style>

body {
  overflow-y:scroll;
  margin: 0;
  padding: 0;
}

svg {
  overflow: hidden;
}

div {
  border: 0;
  margin: 0;
}


#offsetDiv {
}


#test1 {
  margin: 0;
}


</style>

<body>

<div id="offsetDiv">
  <div id="test1" class="chartWrap">
    <svg></svg>
  </div>
</div>

<script src="../lib/d3.v2.js"></script>
<script src="../nv.d3.js"></script>
<script src="../src/tooltip.js"></script>
<script src="../src/models/legend.js"></script>
<script src="../src/models/axis.js"></script>
<script src="../src/models/scatter.js"></script>
<script src="../src/models/scatterWithLegend.js"></script>
<script>



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

      var chart = nv.models.scatterWithLegend()
                .width(width)
                .height(height)
                .showDistX(true)
                .showDistY(true)
                .color(d3.scale.category10().range());
                //.width(width)
                //.height(height)
                //.forceX([-4,4])
                //.forceY([-4,4])
                //.margin({top: 20, right: 10, bottom: 50, left: 80})


    //chart.xaxis.tickFormat(d3.format(".02f"))

    var svg =d3.select('#test1 svg')
      .datum(randomData());


    svg.transition().duration(500)
      .attr('width', width)
      .attr('height', height)
      .call(chart)
      //.attr('width', wid0th)
      //.attr('height', height)

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

    graph.dispatch.on('tooltipShow', function(e) {
        var offsetElement = document.getElementById("test1"),
                left = e.pos[0] + offsetElement.offsetLeft,
                top = e.pos[1] + offsetElement.offsetTop,
                formatter = d3.format(".2f");


      /*
      var content = '<h3>' + e.series.key + '</h3>' +
                    '<p>' +
                    '<span class="value"><span class="label">x:</span><span class="val">' + formatter(e.point.x) + '</span></span><br>' +
                    '<s0pan class="value"><span class="label">y:</span><span class="val">' + formatter(e.point.y) + '</span></span>' +
                    '</p>';

      nvtooltip.show([left, top], content);
      */

      var contentX = '<strong>' + formatter(e.point.x) + '</strong>';
      var contentY = '<strong>' + formatter(e.point.y) + '</strong>';
      var height = nv.utils.windowSize().height - 40;

      nv.tooltip.show([left, height - 50], contentX, 'n', 1);
      nv.tooltip.show([5, top], contentY, 'w', 1);
    });


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

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


      if (width < margin.left + margin.right + 20)
        width = margin.left + margin.right + 20;

      if (height < margin.top + margin.bottom + 20)
        height = margin.top + margin.bottom + 20;


      graph
        .width(width)
        .height(height);

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

      };
  }
});


function randomData() {
  var data = [],
      random = d3.random.normal();

  for (i = 0; i < 2; i++) {
    data.push({
      key: 'Group ' + i,
      values: []
    });

    for (j = 0; j < 100; j++) {
      data[i].values.push({x: random(), y: random(), size: Math.random()});
    }
  }

  return data;
}



</script>