Skip to content

Commit

Permalink
📦 NEW: Bar chart (#91)
Browse files Browse the repository at this point in the history
* added bar tag adn basic bar chart

* Max Bars Shown

* getCountries not run on bar flag

* fixed indenting

* further indenting fixes

Co-authored-by: Chris Evans <chrisevans@Chriss-MacBook-Air.local>
  • Loading branch information
ChrisWeldon and Chris Evans authored Apr 23, 2020
1 parent 6be89f5 commit 8de7d68
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 3 deletions.
7 changes: 5 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ const handleError = require('cli-handle-error');
const getStates = require('./utils/getStates.js');
const getCountry = require('./utils/getCountry.js');
const getCountryChart = require('./utils/getCountryChart.js');
const getBar = require('./utils/getBar.js');
const getWorldwide = require('./utils/getWorldwide.js');
const getCountries = require('./utils/getCountries.js');
const {
Expand All @@ -35,8 +36,9 @@ const reverse = cli.flags.reverse;
const limit = Math.abs(cli.flags.limit);
const chart = cli.flags.chart;
const log = cli.flags.log;
const bar = cli.flags.bar;
const minimal = cli.flags.minimal;
const options = { sortBy, limit, reverse, minimal, chart, log };
const options = { sortBy, limit, reverse, minimal, chart, log , bar};

(async () => {
// Init.
Expand All @@ -59,8 +61,9 @@ const options = { sortBy, limit, reverse, minimal, chart, log };
const lastUpdated = await getWorldwide(table, states);
await getCountry(spinner, table, states, country, options);
await getStates(spinner, table, states, options);
await getCountries(spinner, table, states, country, options);
await getCountries(spinner, table, states, country, bar, options);
await getCountryChart(spinner, country, options);
await getBar(spinner, country, states, options);

theEnd(lastUpdated, states, minimal);
})();
6 changes: 6 additions & 0 deletions utils/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ module.exports = meow(
${yellow(`--limit`)}, ${yellow(`-l`)} Print only N entries
${yellow(`--chart`)}, ${yellow(`-c`)} Print chart for a country
${yellow(`--log`)}, ${yellow(`-g`)} Print logarithmic chart
${yellow('--bar')},${yellow('-b')} Print bar chart of cases per country
${yellow(`--xcolor`)}, ${yellow(`-x`)} Single colored output
${yellow(`--minimal`)}, ${yellow(`-m`)} Minimalistic CLI output
Expand Down Expand Up @@ -68,6 +69,11 @@ module.exports = meow(
default: false,
alias: 'g'
},
bar: {
type: 'boolean',
default: false,
alias: 'b'
},
minimal: {
type: 'boolean',
default: false,
Expand Down
77 changes: 77 additions & 0 deletions utils/getBar.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
const comma = require('comma-number');
const handleError = require('cli-handle-error');
const axios = require('axios');
const to = require('await-to-js').default;
const moment = require('moment');
const blessed = require('blessed');
const contrib = require('blessed-contrib');
const { sortingKeys } = require('./table.js');
const orderBy = require('lodash.orderby');

module.exports = async (spinner, countryName, states, { bar, log, sortBy, limit, reverse }) => {
if (!countryName && !states && bar) {
const [err, response] = await to(
axios.get(`https://corona.lmao.ninja/v2/countries`)
);
handleError(`API is down, try again later.`, err, false);
let allCountries = response.data;

// Sort & reverse.
const direction = reverse ? 'asc' : 'desc';
allCountries = orderBy(
allCountries,
[sortingKeys[sortBy]],
[direction]
);
// Limit.
limit = limit>12 ? 12: limit;
allCountries = allCountries.slice(0, limit);


let logScale = x => x;
if (log) {
logScale = x => (x === 0 ? undefined : Math.log(x));
}
// Format Stack Data
barCountries = {};
allCountries.map((country, count)=>{
barCountries[country.country]=[
logScale(country.cases),
logScale(country.deaths),
logScale(country.recovered)
];
});

const names = Object.keys(barCountries);
const data = Object.values(barCountries);

const cumulative = (a, b) => (a = a + b);

const screen = blessed.screen();

const stack = contrib.stackedBar(
{ label: 'Total Cases'
, barWidth: 10
, barSpacing: 10
, xOffset: 0
//, maxValue: 15
, height: "100%"
, width: "100%"
, barBgColor: [ 'cyan', 'red', 'green' ]})

screen.append(stack);
spinner.stop();
stack.setData(
{ barCategory: names
, stackedCategory: ['CASES', 'DEATHS', 'RECOVERED']
, data:
data
});
screen.render();
await new Promise((resolve, _) => {
screen.key(['escape', 'q', 'C-c', 'enter', 'space'], (ch, key) => {
return process.exit(0);
});
});
}
};
3 changes: 2 additions & 1 deletion utils/getCountries.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,10 @@ module.exports = async (
table,
states,
countryName,
bar,
{ sortBy, limit, reverse }
) => {
if (!countryName && !states) {
if (!countryName && !states && !bar) {
const [err, response] = await to(
axios.get(`https://corona.lmao.ninja/v2/countries`)
);
Expand Down

0 comments on commit 8de7d68

Please sign in to comment.