7ad7a165110b33852bd3a9491884df4f78dead0cb58e4536114e4b4d6ddc684c485daf57f31f88bf4a64f7f8687c8c3a582e58f9b293ef50ca761771b6f738 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. import '../extend/index'
  2. import CRender from '@jiaminghi/c-render'
  3. import { deepClone } from '@jiaminghi/c-render/lib/plugin/util'
  4. import { mergeColor, title, grid, axis, radarAxis } from '../core'
  5. import { pie, line, bar, radar, gauge, legend } from '../core'
  6. export default class Charts {
  7. constructor (dom) {
  8. if (!dom) {
  9. console.error('Charts Missing parameters!')
  10. return false
  11. }
  12. const { clientWidth, clientHeight } = dom
  13. const canvas = document.createElement('canvas')
  14. canvas.setAttribute('width', clientWidth)
  15. canvas.setAttribute('height', clientHeight)
  16. dom.appendChild(canvas)
  17. const attribute = {
  18. container: dom,
  19. canvas,
  20. render: new CRender(canvas),
  21. option: null
  22. }
  23. Object.assign(this, attribute)
  24. }
  25. }
  26. /**
  27. * @description Set chart option
  28. * @param {Object} option Chart option
  29. * @param {Boolean} animationEnd Execute animationEnd
  30. * @return {Undefined} No return
  31. */
  32. Charts.prototype.setOption = function (option, animationEnd = false) {
  33. if (!option || typeof option !== 'object') {
  34. console.error('setOption Missing parameters!')
  35. return false
  36. }
  37. if (animationEnd) this.render.graphs.forEach(graph => graph.animationEnd())
  38. const optionCloned = deepClone(option, true)
  39. mergeColor(this, optionCloned)
  40. grid(this, optionCloned)
  41. axis(this, optionCloned)
  42. radarAxis(this, optionCloned)
  43. title(this, optionCloned)
  44. bar(this, optionCloned)
  45. line(this, optionCloned)
  46. pie(this, optionCloned)
  47. radar(this, optionCloned)
  48. gauge(this, optionCloned)
  49. legend(this, optionCloned)
  50. this.option = option
  51. this.render.launchAnimation()
  52. // console.warn(this)
  53. }
  54. /**
  55. * @description Resize chart
  56. * @return {Undefined} No return
  57. */
  58. Charts.prototype.resize = function () {
  59. const { container, canvas, render, option } = this
  60. const { clientWidth, clientHeight } = container
  61. canvas.setAttribute('width', clientWidth)
  62. canvas.setAttribute('height', clientHeight)
  63. render.area = [clientWidth, clientHeight]
  64. this.setOption(option)
  65. }