3923f286a5e25e8ddd3685980bdce4c21663b08c6937cfaa6ba89a7cc89a9e2a1ae8569e721fe2fd4589d6b46f0a87b17a6ca4655ca214e951d08728e09cc0 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. import { deepClone } from '@jiaminghi/c-render/lib/plugin/util'
  2. export function filterNonNumber (array) {
  3. return array.filter(n => typeof n === 'number')
  4. }
  5. export function deepMerge (target, merged) {
  6. for (var key in merged) {
  7. if (target[key] && typeof target[key] === 'object') {
  8. deepMerge(target[key], merged[key])
  9. continue
  10. }
  11. if (typeof merged[key] === 'object') {
  12. target[key] = deepClone(merged[key], true)
  13. continue
  14. }
  15. target[key] = merged[key]
  16. }
  17. return target
  18. }
  19. export function mulAdd (nums) {
  20. nums = filterNonNumber(nums)
  21. return nums.reduce((all, num) => all + num, 0)
  22. }
  23. export function mergeSameStackData (item, series) {
  24. const stack = item.stack
  25. if (!stack) return [...item.data]
  26. const stacks = series.filter(({ stack: s }) => s === stack)
  27. const index = stacks.findIndex(({ data: d }) => d === item.data)
  28. const datas = stacks.splice(0, index + 1).map(({ data }) => data)
  29. const dataLength = datas[0].length
  30. return new Array(dataLength)
  31. .fill(0)
  32. .map((foo, i) => mulAdd(datas.map(d => d[i])))
  33. }
  34. export function getTwoPointDistance (pointOne, pointTwo) {
  35. const minusX = Math.abs(pointOne[0] - pointTwo[0])
  36. const minusY = Math.abs(pointOne[1] - pointTwo[1])
  37. return Math.sqrt(minusX * minusX + minusY * minusY)
  38. }
  39. export function getLinearGradientColor (ctx, begin, end, color) {
  40. if (!ctx || !begin || !end || !color.length) return
  41. let colors = color
  42. typeof colors === 'string' && (colors = [color, color])
  43. const linearGradientColor = ctx.createLinearGradient(...begin, ...end)
  44. const colorGap = 1 / (colors.length - 1)
  45. colors.forEach((c, i) => linearGradientColor.addColorStop(colorGap * i, c))
  46. return linearGradientColor
  47. }
  48. export function getPolylineLength (points) {
  49. const lineSegments = new Array(points.length - 1)
  50. .fill(0)
  51. .map((foo, i) => [points[i], points[i + 1]])
  52. const lengths = lineSegments.map(item => getTwoPointDistance(...item))
  53. return mulAdd(lengths)
  54. }
  55. export function getPointToLineDistance (point, linePointOne, linePointTwo) {
  56. const a = getTwoPointDistance(point, linePointOne)
  57. const b = getTwoPointDistance(point, linePointTwo)
  58. const c = getTwoPointDistance(linePointOne, linePointTwo)
  59. return 0.5 * Math.sqrt((a + b + c) * (a + b - c) * (a + c - b) * (b + c - a)) / c
  60. }
  61. export function initNeedSeries (series, config, type) {
  62. series = series.filter(({ type: st }) => st === type)
  63. series = series.map(item => deepMerge(deepClone(config, true), item))
  64. return series.filter(({ show }) => show)
  65. }
  66. export function radianToAngle (radian) {
  67. return radian / Math.PI * 180
  68. }