c3-helper.ts 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. import c3 from '../src/index'
  2. ;(window as any).c3 = c3
  3. const d3 = (window.d3 = require('d3'))
  4. const initDom = ((window as any).initDom = function() {
  5. var div = document.createElement('div')
  6. div.id = 'chart'
  7. div.style.width = '640px'
  8. div.style.height = '480px'
  9. document.body.appendChild(div)
  10. document.body.style.margin = '0px'
  11. })
  12. const setMouseEvent = ((window as any).setMouseEvent = function(
  13. chart,
  14. name,
  15. x,
  16. y,
  17. element
  18. ) {
  19. var paddingLeft = chart.internal.main.node().transform.baseVal.getItem(0)
  20. .matrix.e,
  21. event = document.createEvent('MouseEvents')
  22. event.initMouseEvent(
  23. name,
  24. true,
  25. true,
  26. window,
  27. 0,
  28. 0,
  29. 0,
  30. x + paddingLeft,
  31. y + 5,
  32. false,
  33. false,
  34. false,
  35. false,
  36. 0,
  37. null
  38. )
  39. if (element) {
  40. element.dispatchEvent(event)
  41. }
  42. })
  43. const initChart = ((window as any).initChart = function(chart, args, done) {
  44. if (typeof chart === 'undefined') {
  45. initDom()
  46. }
  47. if (args) {
  48. chart = c3.generate(args)
  49. window.d3 = chart.internal.d3
  50. window.d3
  51. .select('.jasmine_html-reporter')
  52. .style('position', 'absolute')
  53. .style('width', '640px')
  54. .style('right', 0)
  55. // when using Karma debug in browser the `window.chart` reference the DOM element
  56. // instead of the actual chart instance here so let's keep it here
  57. ;(window as any).chartInstance = chart
  58. }
  59. window.setTimeout(function() {
  60. done()
  61. }, 10)
  62. return chart
  63. })
  64. export { d3, c3, initDom, setMouseEvent, initChart }