multiBarHorizontalWithLegend.html 6.18 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 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258
<!DOCTYPE html>
<meta charset="utf-8">

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

<style>

body {
  overflow-y:scroll;
}

text {
  font: 12px sans-serif;
}

#chart1 {
  height: 500px;
  margin: 10px;
  min-width: 100px;
  min-height: 100px;
/*
  Minimum height and width is a good idea to prevent negative SVG dimensions...
  For example width should be =< margin.left + margin.right + 1,
  of course 1 pixel for the entire chart would not be very useful, BUT should not have errors
*/
}

</style>
<body>

  <div id="chart1">
  </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/multiBarHorizontal.js"></script>
<script src="../src/models/multiBarHorizontalWithLegend.js"></script>
<script src="stream_layers.js"></script>
<script>

var test_data = stream_layers(3,12,.1).map(function(data, i) {
  return {
    key: 'Stream' + i,
    values: data
  };
});



long_short_data = [ 
  {
    key: 'Short',
    color: '#d62728',
    values: [
      { 
        "label" : "Information Technology" ,
        "y" : -1.8746444827653
      } , 
      { 
        "label" : "Consumer Discretionary" ,
        "y" : -8.0961543492239
      } , 
      { 
        "label" : "Health Care" ,
        "y" : -0.57072943117674
      } , 
      { 
        "label" : "Consumer Staples" ,
        "y" : -2.4174010336624
      } , 
      {
        "label" : "Financials" ,
        "y" : -0.72009071426284
      } , 
      { 
        "label" : "Industrials" ,
        "y" : -0.77154485523777
      } , 
      { 
        "label" : "Energy" ,
        "y" : -0.90152097798131
      } , 
      {
        "label" : "Materials" , 
        "y" : -0.91445417330854
      } , 
      { 
        "label" : "Telecommunication Services" , 
        "y" : -0.055746319141851
      }
    ]
  },
  {
    key: 'Long',
    color: '#1f77b4',
    values: [
      { 
        "label" : "Information Technology" ,
        "y" : 25.307646510375
      } , 
      { 
        "label" : "Consumer Discretionary" ,
        "y" : 16.756779544553
      } , 
      { 
        "label" : "Health Care" ,
        "y" : 18.451534877007
      } , 
      { 
        "label" : "Consumer Staples" ,
        "y" : 8.6142352811805
      } , 
      {
        "label" : "Financials" ,
        "y" : 7.8082472075876
      } , 
      { 
        "label" : "Industrials" ,
        "y" : 5.259101026956
      } , 
      { 
        "label" : "Energy" ,
        "y" : 0.30947953487127
      } , 
      { 
        "label" : "Materials" ,
        "y" : 0
      } , 
      { 
        "label" : "Telecommunication Services" ,
        "y" : 0 
      }
    ]
  }
];



  var selector = '#chart1',
  chart = nv.models.multiBarHorizontalWithLegend()
            .x(function(d) { return d.label })
            .margin({top: 30, right: 20, bottom: 50, left: 160})
            .showControls(false),
      data = long_short_data,
      //data = test_data,
      xTickFormat = function(d) { return d },
      //xTickFormat = d3.format(',r'),
      yTickFormat = d3.format(',.2f'),
      xAxisLabel = null,
      yAxisLabel = null,
    duration = 500;

nv.addGraph({
  generate: function() {
    var container = d3.select(selector),
        width = function() { return parseInt(container.style('width')) },
        height = function() { return parseInt(container.style('height')) },
        svg = container.append('svg');

    chart
        .width(width)
        .height(height);

    chart.xAxis
        .tickFormat(xTickFormat);

    chart.yAxis
        .tickFormat(yTickFormat)
        .axisLabel(yAxisLabel);

    svg
        .attr('width', width())
        .attr('height', height())
        .datum(data)
      .transition().duration(duration).call(chart);

    return chart;
  },
  callback: function(chart) {
    var showTooltip = function showTooltip(e) {
      var offsetElement = document.getElementById(selector.substr(1)),
          left = e.pos[0] + offsetElement.offsetLeft,
          top = e.pos[1] + offsetElement.offsetTop,
          formatY = chart.yAxis.tickFormat(), //Assumes using same format as axis, can customize to show higher precision, etc.
          formatX = chart.xAxis.tickFormat();

      // uses the chart's getX and getY, you may customize if x position is not the same as the value you want
      //   ex. daily data without weekends, x is the index, while you want the date
      var content = '<h3>' + e.series.key + '</h3>' +
                    '<p>' +
                    formatX(chart.x()(e.point)) + ': ' + formatY(chart.y()(e.point)) +
                    //formatY(chart.y()(e.point)) + ' at ' + formatX(chart.x()(e.point)) +
                    '</p>';

      var tooltip = nv.tooltip.show([left, top], content, e.value < 0 ? 'e' : 'w');

      /*
      if (!arguments[1]) {
        tooltip.onmouseenter = function() { showTooltip(e, 1) }; //working on geting tooltips to stay if mouse hovers over them
        //tooltip.onmouseout = function() { console.log('mouseout'); };
        tooltip.onmouseout = nv.tooltip.cleanup;
      }
      */
    };

    chart.dispatch.on('tooltipShow', showTooltip);
    chart.dispatch.on('tooltipHide', nv.tooltip.cleanup);


    window.onresize= function() {
      // now that width and height are functions, should be automatic..of course you can always override them

      d3.select('#chart1 svg')
          .attr('width', chart.width()()) //need to set SVG dimensions, chart is not aware of the SVG component
          .attr('height', chart.height()())
        .transition().duration(duration)
          .call(chart);

/*
      d3.select('#chart1 svg')
          .attr('width', chart.width()()) //need to set SVG dimensions, chart is not aware of the SVG component
          .attr('height', chart.height()())
          .call(chart);
*/
    };
  }
});



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"
    }
  ];
}


</script>