1
import { Point, Boundary, Bar } from '../VSparkline'
2

3 1
export function genPoints (
4
  values: number[],
5 1
  boundary: Boundary
6
): Point[] {
7 1
  const { minX, maxX, minY, maxY } = boundary
8 1
  const totalValues = values.length
9 1
  const maxValue = Math.max(...values)
10 1
  const minValue = Math.min(...values)
11

12 1
  const gridX = (maxX - minX) / (totalValues - 1)
13 1
  const gridY = (maxY - minY) / ((maxValue - minValue) || 1)
14

15 1
  return values.map((value, index) => {
16 1
    return {
17
      x: minX + index * gridX,
18
      y: maxY - (value - minValue) * gridY,
19
      value,
20
    }
21
  })
22
}
23

24 1
export function genBars (
25
  values: number[],
26 1
  boundary: Boundary
27
): Bar[] {
28 1
  const { minX, maxX, minY, maxY } = boundary
29 1
  const totalValues = values.length
30 1
  let maxValue = Math.max(...values)
31 1
  let minValue = Math.min(...values)
32

33 1
  if (minValue > 0) minValue = 0
34 1
  if (maxValue < 0) maxValue = 0
35

36 1
  const gridX = maxX / totalValues
37 1
  const gridY = (maxY - minY) / ((maxValue - minValue) || 1)
38 1
  const horizonY = maxY - Math.abs(minValue * gridY)
39

40 1
  return values.map((value, index) => {
41 1
    const height = Math.abs(gridY * value)
42

43 1
    return {
44
      x: minX + index * gridX,
45
      y: horizonY - height +
46
        +(value < 0) * height,
47
      height,
48
      value,
49
    }
50
  })
51
}

Read our documentation on viewing source code .

Loading