1 | const utils = require('./utils')
|
2 | const {
|
3 | EOL,
|
4 | PAD,
|
5 | verifyData,
|
6 | curForward,
|
7 | curUp,
|
8 | curDown,
|
9 | curBack,
|
10 | getOriginLen
|
11 | } = utils
|
12 |
|
13 | const printBox = (width, height, style = '# ', left = 0, top = 0, type = 'coordinate') => {
|
14 | let result = curForward(left) + curUp(top)
|
15 | const hasSide = width > 1 || height > 1
|
16 |
|
17 | for (let i = 0; i < height; i++) {
|
18 | for (let j = 0; j < width; j++) {
|
19 | result += style
|
20 | }
|
21 |
|
22 | if (hasSide) {
|
23 | if (i !== height - 1) {
|
24 | result += EOL + curForward(left)
|
25 | } else {
|
26 | result += EOL
|
27 | }
|
28 | }
|
29 | }
|
30 |
|
31 | if (type === 'data') {
|
32 | result += curDown(hasSide ? (top - height) : top) + curBack(left + getOriginLen(style))
|
33 | }
|
34 | return result
|
35 | }
|
36 |
|
37 | module.exports = (data, opts) => {
|
38 | verifyData(data)
|
39 |
|
40 | const newOpts = Object.assign({
|
41 | width: 10,
|
42 | left: 2,
|
43 | height: 10,
|
44 | style: '# ',
|
45 | sides: [1, 1],
|
46 | hAxis: ['+', '-', '>'],
|
47 | vAxis: ['|', 'A'],
|
48 | hName: 'X Axis',
|
49 | vName: 'Y Axis',
|
50 | zero: '+',
|
51 | ratio: [1, 1],
|
52 | hGap: 2,
|
53 | vGap: 2,
|
54 | legendGap: 0
|
55 | }, opts)
|
56 |
|
57 | const {
|
58 | left, height, style, sides, width, zero, hAxis, vAxis, ratio,
|
59 | hName, vName, hGap, vGap, legendGap
|
60 | } = newOpts
|
61 |
|
62 | let tmp; let result = ''
|
63 | const styles = data.map(item => item.style || style)
|
64 | const allSides = data.map(item => item.sides || sides)
|
65 | const keys = new Set(data.map(item => item.key))
|
66 |
|
67 | result += PAD.repeat(left) + vName
|
68 | result += PAD.repeat(legendGap)
|
69 | result += Array.from(keys)
|
70 | .map(
|
71 | key => key + ': ' + (data.find(
|
72 | item => item.key === key
|
73 | ).style || style)
|
74 | ).join(' | ') +
|
75 | EOL.repeat(2)
|
76 |
|
77 | result += printBox(width + left, height + 1, PAD.repeat(2))
|
78 |
|
79 | data.map((item, index) => {
|
80 | result += printBox(
|
81 | allSides[index][0],
|
82 | allSides[index][1],
|
83 | styles[index],
|
84 | item.value[0] * 2 + left + 1,
|
85 | item.value[1],
|
86 | 'data'
|
87 | )
|
88 | })
|
89 |
|
90 | result += curBack(width * 2) + curUp(height + 1) +
|
91 | PAD.repeat(left + 1) + vAxis[1]
|
92 |
|
93 | for (let i = 0; i < height + 1; i++) {
|
94 | tmp = ((height - i) % vGap === 0 && i !== height)
|
95 | ? ((height - i) * ratio[1]).toString() : ''
|
96 | result += EOL + tmp.padStart(left + 1) + vAxis[0]
|
97 | }
|
98 |
|
99 | result += curBack() + zero + curDown(1) + curBack(1) + '0' + curUp(1)
|
100 |
|
101 | for (let i = 1; i < (width * 2) + hGap; i++) {
|
102 | if (!(i % (hGap * 2))) {
|
103 | result += hAxis[0]
|
104 |
|
105 |
|
106 | const item = (i / 2) * ratio[0]
|
107 | const len = item.toString().length
|
108 |
|
109 | result += curDown(1) + curBack(1) + item + curUp(1)
|
110 | if (len > 1) {
|
111 | result += curBack(len - 1)
|
112 | }
|
113 |
|
114 | continue
|
115 | }
|
116 |
|
117 | result += hAxis[1]
|
118 | }
|
119 |
|
120 | result += hAxis[2] + PAD + hName + EOL
|
121 |
|
122 | return result
|
123 | }
|