Skip to content

Commit 807cd1e

Browse files
committed
Renaming Chart types. Adding plot charts
1 parent f63c37a commit 807cd1e

File tree

3 files changed

+84
-12
lines changed

3 files changed

+84
-12
lines changed

README.md

Lines changed: 39 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,10 @@ class `ConsoleCharts` (data `array`)
2929
* `pattern` `default false` Every second bar gets a different pattern.
3030
* `spaced` `default false` Space between bars.
3131
* `hideLabels` `default false` Hide / Do not display any labels at the axis.
32-
* `type` `default "sideways-barchart"` Defines the barchart type. Possible Values are:
33-
* `sideways-barchart` (horizontal bars) ![Alt text](./images/bar-chart-horizontal.svg)
34-
* `barchart` (vertical bars). ![Alt text](./images/bar-chart.svg)
32+
* `type` `default "sideways-bar"` Defines the barchart type. Possible Values are:
33+
* `sideways-bar` (horizontal bars) ![Alt text](./images/bar-chart-horizontal.svg)
34+
* `bar` (vertical bars). ![Alt text](./images/bar-chart.svg)
35+
* `dot` (vertical dot graph) -> no `pattern` option
3536
* return `string`
3637

3738
### Helper Methods
@@ -168,7 +169,7 @@ Strawberries │▓▓▓▓▓▓▓▓▓▓▓▓ 12
168169

169170
```js
170171
let obj = new ConsoleCharts([{label: "Apples", value: 5}, {label: "Oranges", value: 7}, {label: "Bananas", value: 8}, {label: "Pears", value: 10}, {label: "Grapes", value: 4}, {label: "Pineapples", value: 5}, {label: "Strawberries", value: 12}, {label: "Watermelons", value: 3}, {label: "Mangos", value: 3}, {label: "Lemons", value: 6}, {label: "Limes", value: 4}, {label: "Peaches", value: 10}]);
171-
console.log(obj.chart({ type: "barchart", spaced: true, pattern: true}));
172+
console.log(obj.chart({ type: "bar", spaced: true, pattern: true}));
172173
```
173174

174175
```
@@ -202,7 +203,7 @@ console.log(obj.chart({ type: "barchart", spaced: true, pattern: true}));
202203

203204
```js
204205
let objV = new ConsoleCharts([{label: "Apples", value: 5}, {label: "Oranges", value: 7}, {label: "Bananas", value: 8}, {label: "Pears", value: 10}, {label: "Grapes", value: 4}, {label: "Pineapples", value: 5}, {label: "Strawberries", value: 12}, {label: "Watermelons", value: 11}, {label: "Mangos", value: 10}, {label: "Lemons", value: 6}, {label: "Limes", value: 4}, {label: "Peaches", value: 10}]);
205-
console.log(objV.chart({ type: "barchart", spaced: true, pattern: true, hideLabels: true, minHeight: true}));
206+
console.log(objV.chart({ type: "bar", spaced: true, pattern: true, hideLabels: true, minHeight: true}));
206207
```
207208

208209
```
@@ -218,3 +219,36 @@ console.log(objV.chart({ type: "barchart", spaced: true, pattern: true, hideLabe
218219
│═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═
219220
└────────────────────────
220221
```
222+
223+
```js
224+
let arr = new ConsoleCharts([{label: "Apples", value: 5}, {label: "Oranges", value: 7}, {label: "Bananas", value: 8}, {label: "Pears", value: 10}, {label: "Grapes", value: 4}, {label: "Pineapples", value: 5}, {label: "Strawberries", value: 12}, {label: "Watermelons", value: 11}, {label: "Mangos", value: 10}, {label: "Lemons", value: 6}, {label: "Limes", value: 4}, {label: "Peaches", value: 10}]);
225+
console.log(arr.chart({ type: "dot", spaced: false, hideLabels: false, minHeight: false}));
226+
```
227+
228+
```
229+
│ ·
230+
│ · · ·
231+
232+
│ ·
233+
│ ·
234+
│ ·
235+
│· ·
236+
│ · ·
237+
238+
239+
240+
241+
└────────────
242+
Apples (5)
243+
Oranges (7)
244+
Bananas (8)
245+
Pears (10)
246+
Grapes (4)
247+
Pineapples (5)
248+
Strawberries (12)
249+
Watermelons (11)
250+
Mangos (10)
251+
Lemons (6)
252+
Limes (4)
253+
Peaches (10)
254+
```

src/ConsoleCharts.js

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,12 @@ class ConsoleCharts {
2828
chart(options = {}){
2929
options.minHeight = options.minHeight || false;
3030
options.pattern = options.pattern || false;
31-
options.type = options.type || "sideways-barchart";
31+
options.type = options.type || "sideways-bar";
3232
options.spaced = options.spaced || false;
3333
options.showLabels = options.hideLabels || false;
3434
let chart = "\n";
3535
/* Option variants */
36-
if(options.type === "sideways-barchart"){
36+
if(options.type === "sideways-bar"){
3737
// SIDEWAYS BARCHART
3838
let lines = [];
3939
let descY = 0; /* It's a variable that will be used to store the length of the longest label. */
@@ -99,7 +99,7 @@ class ConsoleCharts {
9999
chart += this.#repeatCharacter(" ", descY) + " └" + this.#repeatCharacter("─", (options.minHeight == true ? (max - min + 1) : max));
100100
return chart;
101101
}
102-
else if(options.type === "barchart"){
102+
else if(options.type === "bar"){
103103
// BARCHART
104104
let spaced = (options.spaced == true ? " " : "");
105105
let chart = "", descX = "";
@@ -135,6 +135,42 @@ class ConsoleCharts {
135135
chart += " └" + this.#repeatCharacter((options.spaced ? "──" : "─"), this.data.length) + " " + "\n" + descX;
136136
return chart;
137137
}
138+
else if(options.type === "dot"){
139+
// DOTCHART
140+
let spaced = (options.spaced ? " " : "");
141+
let chart = "", descX = "";
142+
let maxHeight = Math.max.apply(Math, this.data.map(function(o) { return o.value; }));
143+
let min = Math.min(...this.data.map(function(o){ return o.value; }));
144+
for(let i = 0; i < maxHeight; i++){
145+
let ln = "";
146+
if(options.minHeight && i == 0){
147+
let lnt = "";
148+
for(let j = 0; j < this.data.length; j++){
149+
lnt += "═" + spaced;
150+
}
151+
chart = " │" + lnt + "\n" + chart;
152+
}
153+
for(let j = 0; j < this.data.length; j++){
154+
if(this.data[j].value == i){
155+
if(options.minHeight != true || i >= min - 1){
156+
ln += "·" + spaced;
157+
}
158+
}
159+
else{
160+
ln += " " + spaced;
161+
}
162+
}
163+
if(options.minHeight != true || i >= min - 1){
164+
chart = " │" + ln + "\n" + chart;
165+
}
166+
167+
if(options.hideLabels != true){
168+
descX += this.#repeatCharacter((options.spaced ? " " : " "), i + (options.spaced ? 1 : 2)) + this.data[i].label + " (" + this.data[i].value + ")" + "\n";
169+
}
170+
}
171+
chart += " └" + this.#repeatCharacter((options.spaced ? "──" : "─"), this.data.length) + " " + "\n" + descX;
172+
return chart;
173+
}
138174
}
139175

140176
/**

test/test.js

Lines changed: 6 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)