xcharts-demo.js 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. $(function() {
  2. var data = {
  3. "xScale": "ordinal",
  4. "yScale": "linear",
  5. "main": [{
  6. "className": ".pizza",
  7. "data": [{
  8. "x": "Pepperoni",
  9. "y": 4
  10. }, {
  11. "x": "Cheese",
  12. "y": 8
  13. }]
  14. }]
  15. };
  16. var myChart = new xChart('bar', data, '#example1');
  17. });
  18. $(function() {
  19. var tt = document.createElement('div'),
  20. leftOffset = -(~~$('html').css('padding-left').replace('px', '') + ~~$('body').css('margin-left').replace('px', '')),
  21. topOffset = -32;
  22. tt.className = 'ex-tooltip';
  23. document.body.appendChild(tt);
  24. var data = {
  25. "xScale": "time",
  26. "yScale": "linear",
  27. "main": [{
  28. "className": ".pizza",
  29. "data": [{
  30. "x": "2012-11-05",
  31. "y": 6
  32. }, {
  33. "x": "2012-11-06",
  34. "y": 6
  35. }, {
  36. "x": "2012-11-07",
  37. "y": 8
  38. }, {
  39. "x": "2012-11-08",
  40. "y": 3
  41. }, {
  42. "x": "2012-11-09",
  43. "y": 4
  44. }, {
  45. "x": "2012-11-10",
  46. "y": 9
  47. }, {
  48. "x": "2012-11-11",
  49. "y": 6
  50. }]
  51. }]
  52. };
  53. var opts = {
  54. "dataFormatX": function(x) {
  55. return d3.time.format('%Y-%m-%d').parse(x);
  56. },
  57. "tickFormatX": function(x) {
  58. return d3.time.format('%A')(x);
  59. },
  60. "mouseover": function(d, i) {
  61. var pos = $(this).offset();
  62. $(tt).text(d3.time.format('%A')(d.x) + ': ' + d.y)
  63. .css({
  64. top: topOffset + pos.top,
  65. left: pos.left + leftOffset
  66. })
  67. .show();
  68. },
  69. "mouseout": function(x) {
  70. $(tt).hide();
  71. }
  72. };
  73. var myChart = new xChart('line-dotted', data, '#example4', opts);
  74. });