-
Notifications
You must be signed in to change notification settings - Fork 0
/
InCyan.js
68 lines (61 loc) · 2.13 KB
/
InCyan.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
//function to find the largest value in the items object
function findLargestBar(items) {
let largestValue = 0
let largestKey = ''
let largestBar = {}
for (const [key, value] of Object.entries(items)) {
if (value > largestValue) {
largestValue = value
largestKey = key
}
largestBar = {key: largestKey, value: largestValue}
}
return largestBar
}
// function to create an array of values for each bar
function createArrayOfValues(items) {
let arrayOfValues = []
for (const [key, value] of Object.entries(items)) {
arrayOfValues.push(value)
}
return arrayOfValues
}
// json input
const jsonData = {
"title": "stock count",
"xtitle": "asset",
"ytitle": "count",
"items": [
{"chairs": 20},
{"tables": 5},
{"stands": 7},
{"lamps": 8},
{"cups": 10}
]
}
const title = jsonData.title
const xtitle = jsonData.xtitle
const ytitle = jsonData.ytitle
const items = jsonData.items
const largestBar = findLargestBar(items)
const valueArray = createArrayOfValues(items)
// graph will largestBar + 10 tall and items.length * 5 wide to provide space for labels
const graphHeight = largestBar.value + 10
const graphWidth = items.length * 5
console.log ('----- ' + title + ' -----')
console.log (xtitle)
console.log ('-------')
for (i = 0; i < graphHeight; i++) { // creates a graph of height graphHeight
for (j = 0; j < graphWidth; j++) { // creates a graph of width graphWidth
if (j % 5 === 0) { // every fifth column print a bar to provide space for labels
let currentItemValue = valueArray[j/5] // get the value of the item at the current column
if (i > currentItemValue) {
console.log(' ') // if the current row is greater than the value of the item at the current column, print a space
} else {
console.log('*') // if the current row is less than or equal to the value of the item at the current column, print a star
}
}
}
}
console.log('----------------')
console.log (ytitle)