d5ce0e655d37603cff8159f51d9148b2c10dfbe23a9b959474eb2de5eacef26aa10602ab7f7d80909404e9b3d1ffa66f02c95a8e442930f34644c62a4635cd 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. import { doUpdate } from '../class/updater.class'
  2. import { deepClone } from '@jiaminghi/c-render/lib/plugin/util'
  3. import { gridConfig } from '../config'
  4. import { deepMerge } from '../util'
  5. export function grid (chart, option = {}) {
  6. let { grid } = option
  7. grid = deepMerge(deepClone(gridConfig, true), grid || {})
  8. doUpdate({
  9. chart,
  10. series: [grid],
  11. key: 'grid',
  12. getGraphConfig: getGridConfig
  13. })
  14. }
  15. function getGridConfig (gridItem, updater) {
  16. const { animationCurve, animationFrame, rLevel } = gridItem
  17. const shape = getGridShape(gridItem, updater)
  18. const style = getGridStyle(gridItem)
  19. updater.chart.gridArea = { ...shape }
  20. return [{
  21. name: 'rect',
  22. index: rLevel,
  23. animationCurve,
  24. animationFrame,
  25. shape,
  26. style
  27. }]
  28. }
  29. function getGridShape (gridItem, updater) {
  30. const [w, h] = updater.chart.render.area
  31. const left = getNumberValue(gridItem.left, w)
  32. const right = getNumberValue(gridItem.right, w)
  33. const top = getNumberValue(gridItem.top, h)
  34. const bottom = getNumberValue(gridItem.bottom, h)
  35. const width = w - left - right
  36. const height = h - top - bottom
  37. return { x: left, y: top, w: width, h: height }
  38. }
  39. function getNumberValue (val, all) {
  40. if (typeof val === 'number') return val
  41. if (typeof val !== 'string') return 0
  42. return all * parseInt(val) / 100
  43. }
  44. function getGridStyle (gridItem) {
  45. const { style } = gridItem
  46. return style
  47. }