examples/requirejs.html
raw| 1 | <!DOCTYPE html> |
| 2 | <html> |
| 3 | <head> |
| 4 | <title>d3.tip.js - Tooltips for D3</title> |
| 5 | <meta charset="utf-8" /> |
| 6 | <title>require.js</title> |
| 7 | <link rel="stylesheet" href="example-styles.css"> |
| 8 | <style type="text/css"> |
| 9 | body { |
| 10 | padding: 40px; |
| 11 | font-family: "Helvetica Neue", Helvetica, sans-serif; |
| 12 | font-size: 12px; |
| 13 | height: 1000px; |
| 14 | } |
| 15 | |
| 16 | .d3-tip span { |
| 17 | color: #ff00c7; |
| 18 | } |
| 19 | |
| 20 | .domain { |
| 21 | display: none; |
| 22 | } |
| 23 | |
| 24 | .axis line { |
| 25 | stroke-width: 1px; |
| 26 | stroke: #eee; |
| 27 | shape-rendering: crispedges; |
| 28 | } |
| 29 | |
| 30 | .axis text { |
| 31 | fill: #888; |
| 32 | } |
| 33 | |
| 34 | rect { |
| 35 | fill: #339cff; |
| 36 | fill-opacity: 0.7; |
| 37 | } |
| 38 | |
| 39 | rect:hover { |
| 40 | fill-opacity: 1; |
| 41 | } |
| 42 | </style> |
| 43 | <script type="text/javascript"> |
| 44 | data = [{"total":225,"days":[12,51,60,26,38,31,7]},{"total":279,"days":[42,33,34,72,61,12,25]},{"total":212,"days":[12,59,24,70,36,5,6]},{"total":335,"days":[17,43,38,58,67,72,40]},{"total":329,"days":[40,53,62,48,38,36,52]},{"total":234,"days":[15,25,41,66,35,37,15]},{"total":175,"days":[2,40,23,40,23,34,13]},{"total":308,"days":[20,22,63,55,51,66,31]},{"total":401,"days":[20,64,42,62,88,79,46]},{"total":214,"days":[24,27,25,48,38,28,24]},{"total":332,"days":[26,43,20,109,74,29,31]},{"total":333,"days":[65,66,62,60,14,43,23]},{"total":437,"days":[33,74,82,70,85,64,29]},{"total":687,"days":[29,82,87,167,156,126,40]},{"total":243,"days":[11,52,44,50,46,30,10]},{"total":360,"days":[16,41,86,62,83,60,12]},{"total":349,"days":[22,55,48,36,88,57,43]},{"total":368,"days":[29,100,68,54,55,46,16]},{"total":377,"days":[3,71,66,57,93,82,5]},{"total":265,"days":[12,61,61,19,44,46,22]},{"total":396,"days":[20,58,70,52,84,93,19]},{"total":467,"days":[25,47,128,93,70,35,69]},{"total":524,"days":[55,79,94,95,122,70,9]},{"total":535,"days":[21,96,95,115,78,89,41]},{"total":333,"days":[20,50,49,52,58,51,53]},{"total":258,"days":[7,23,50,59,46,50,23]},{"total":242,"days":[12,4,54,53,46,33,40]},{"total":335,"days":[1,34,40,75,97,51,37]},{"total":325,"days":[32,57,65,46,60,58,7]},{"total":224,"days":[23,42,43,28,31,27,30]},{"total":317,"days":[20,46,43,46,83,36,43]},{"total":119,"days":[1,10,9,45,30,17,7]},{"total":368,"days":[13,74,77,85,72,40,7]},{"total":271,"days":[10,50,46,61,68,31,5]},{"total":290,"days":[26,48,39,37,68,43,29]},{"total":393,"days":[27,50,133,47,73,37,26]},{"total":316,"days":[2,63,66,47,63,33,42]},{"total":146,"days":[0,32,38,23,45,7,1]},{"total":255,"days":[23,34,72,46,48,24,8]},{"total":196,"days":[9,32,82,18,16,29,10]},{"total":410,"days":[41,52,67,42,49,103,56]},{"total":280,"days":[13,35,40,60,68,53,11]},{"total":432,"days":[45,65,59,55,71,68,69]},{"total":371,"days":[19,104,95,51,66,20,16]},{"total":285,"days":[2,38,50,72,74,47,2]},{"total":350,"days":[47,16,96,93,32,52,14]},{"total":234,"days":[34,68,66,32,5,10,19]},{"total":279,"days":[27,43,64,48,60,25,12]},{"total":391,"days":[40,54,59,68,80,51,39]},{"total":168,"days":[0,44,42,36,30,16,0]},{"total":0,"days":[0,0,0,0,0,0,0]},{"total":14,"days":[6,0,0,0,0,2,6]},{"total":0,"days":[0,0,0,0,0,0,0]}] |
| 45 | </script> |
| 46 | <script src="https://cdnjs.cloudflare.com/ajax/libs/require.js/2.3.2/require.min.js" charset="utf-8"></script> |
| 47 | <script charset="utf-8"> |
| 48 | console.info("Note that you need 'd3-axis' and 'd3-scale'"); |
| 49 | |
| 50 | // Mostly used for development. Point to proper location of d3 and d3tip. |
| 51 | require.config({ |
| 52 | baseUrl: '../', |
| 53 | paths: { |
| 54 | 'd3-selection': 'node_modules/d3-selection/build/d3-selection', |
| 55 | 'd3-collection': 'node_modules/d3-collection/build/d3-collection', |
| 56 | 'd3-array': 'node_modules/d3-array/build/d3-array', |
| 57 | 'd3-axis': 'node_modules/d3-axis/build/d3-axis', |
| 58 | 'd3-scale': 'node_modules/d3-scale/build/d3-scale', |
| 59 | 'd3-color': 'node_modules/d3-color/build/d3-color', |
| 60 | 'd3-interpolate': 'node_modules/d3-interpolate/build/d3-interpolate', |
| 61 | 'd3-format': 'node_modules/d3-format/build/d3-format', |
| 62 | 'd3-time': 'node_modules/d3-time/build/d3-time', |
| 63 | 'd3-time-format': 'node_modules/d3-time-format/build/d3-time-format', |
| 64 | 'd3-tip': 'index' |
| 65 | }, |
| 66 | shim: { |
| 67 | 'd3-scale': {deps: ['d3-array', 'd3-color', 'd3-format', 'd3-interpolate', 'd3-time', 'd3-time-format']}, |
| 68 | 'd3-tip': {deps: ['d3-selection', 'd3-collection']} |
| 69 | } |
| 70 | }); |
| 71 | |
| 72 | // Require d3 and d3tip |
| 73 | require(['d3-selection', 'd3-array', 'd3-scale', 'd3-axis', 'd3-tip'], function(d3Selection, d3Array, d3Scale, d3Axis, d3tip) { |
| 74 | var tip = d3tip() |
| 75 | .attr('class', 'd3-tip') |
| 76 | .html(function(d) { return '<span>' + d.total + '</span>' + ' entries' }) |
| 77 | .offset([-12, 0]) |
| 78 | |
| 79 | var w = 800, |
| 80 | h = 300, |
| 81 | padt = 20, padr = 20, padb = 60, padl = 30, |
| 82 | x = d3Scale.scaleBand().rangeRound([0, w - padl - padr]).padding(0.1), |
| 83 | y = d3Scale.scaleLinear().range([h, 0]), |
| 84 | yAxis = d3Axis.axisLeft().scale(y).tickSize(-w + padl + padr), |
| 85 | xAxis = d3Axis.axisBottom().scale(x) |
| 86 | |
| 87 | vis = d3Selection.select('#graph') |
| 88 | .append('svg') |
| 89 | .attr('width', w) |
| 90 | .attr('height', h + padt + padb) |
| 91 | .append('g') |
| 92 | .attr('transform', 'translate(' + padl + ',' + padt + ')') |
| 93 | |
| 94 | var max = d3Array.max(data, function(d) { return d.total }) |
| 95 | x.domain(d3Array.range(data.length)) |
| 96 | y.domain([0, max]) |
| 97 | |
| 98 | vis.call(tip) |
| 99 | vis.append("g") |
| 100 | .attr("class", "y axis") |
| 101 | .call(yAxis) |
| 102 | |
| 103 | vis.append("g") |
| 104 | .attr("class", "x axis") |
| 105 | .attr('transform', 'translate(0,' + h + ')') |
| 106 | .call(xAxis) |
| 107 | .selectAll('.x.axis g') |
| 108 | .style('display', function (d, i) { return i % 3 != 0 ? 'none' : 'block' }) |
| 109 | |
| 110 | var bars = vis.selectAll('g.bar') |
| 111 | .data(data) |
| 112 | .enter().append('g') |
| 113 | .attr('class', 'bar') |
| 114 | .attr('transform', function (d, i) { return "translate(" + x(i) + ", 0)" }) |
| 115 | |
| 116 | bars.append('rect') |
| 117 | .attr('width', function() { return x.bandwidth() }) |
| 118 | .attr('height', function(d) { return h - y(d.total) }) |
| 119 | .attr('y', function(d) { return y(d.total) }) |
| 120 | .on('mouseover', tip.show) |
| 121 | .on('mouseout', tip.hide) |
| 122 | }); |
| 123 | </script> |
| 124 | <div id="graph"></div> |
| 125 | </body> |
| 126 | </html> |