Skip to content

Commit 9c58f4a

Browse files
authored
Merge pull request #40 from jkga/feature/adaptive-charts
Feature/adaptive charts
2 parents 53d37f5 + 41238ee commit 9c58f4a

File tree

5 files changed

+102
-30
lines changed

5 files changed

+102
-30
lines changed

app.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -129,14 +129,14 @@ app.get('/history/:country/:chartType(cases|deaths)?', async (req, res, next) =>
129129
return next();
130130
});
131131

132-
133132
// historical chart by country
134-
app.get('/history/charts/:country', async (req, res, next) => {
133+
app.get('/history/charts/:country/:chartSize(sm|md|lg)?', async (req, res, next) => {
135134
const userAgent = req.headers['user-agent'],
136135
countryData = req.params.country,
137136
chartType = req.params.chartType || 'cases',
137+
chartSize = req.params.chartSize || 'sm',
138138
summary = await axios.get(`${apiBaseURL}/countries/${countryData}`),
139-
history = await axios.get(`${apiBaseURL}/v2/historical/${summary.data.country}?lastdays=all`),
139+
history = await axios.get(`${apiBaseURL}/historical/${summary.data.country}?lastdays=all`),
140140
s = summary.data,
141141
h = history.data;
142142

@@ -146,7 +146,7 @@ app.get('/history/charts/:country', async (req, res, next) => {
146146
s.country, s.cases, s.todayCases,
147147
s.deaths, s.todayDeaths, s.recovered,
148148
s.active, s.critical, s.casesPerOneMillion,
149-
s.updated, h, chartType, s.countryInfo
149+
s.updated, h, chartType, s.countryInfo, chartSize
150150
)
151151
return null;
152152
}

bin/util/layout.js

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
exports.md = {
2+
grid: {},
3+
header: {
4+
grid: [0, 0, 2, 2]
5+
},
6+
table: {
7+
grid: [0, 2, 2, 7]
8+
},
9+
bar: {
10+
grid: [2, 4, 3, 3]
11+
},
12+
map: {
13+
grid: [2, 0, 3, 4]
14+
},
15+
donut: {
16+
grid: [2, 7, 3, 2]
17+
},
18+
line: {
19+
grid: [5, 0, 4, 9]
20+
},
21+
footer: {
22+
grid: [8, 0, 2, 9]
23+
}
24+
}
25+
26+
exports.lg = {
27+
grid: {},
28+
header: {
29+
grid: [0, 0, 1, 10]
30+
},
31+
table: {
32+
grid: [1, 0, 2, 10]
33+
},
34+
bar: {
35+
grid: [3, 5, 3, 3]
36+
},
37+
map: {
38+
grid: [3, 0, 3, 5]
39+
},
40+
donut: {
41+
grid: [3, 8, 3, 2]
42+
},
43+
line: {
44+
grid: [6, 0, 4, 10]
45+
},
46+
footer: {
47+
grid: [9, 0, 2, 10]
48+
}
49+
}
50+
51+
exports.sm = {
52+
grid: {},
53+
header: {
54+
grid: [0, 0, 2, 2]
55+
},
56+
table: {
57+
grid: [2, 0, 2, 5],
58+
columnSpacing: 3
59+
},
60+
bar: {
61+
grid: [4, 0, 3, 3],
62+
xOffset: 6
63+
},
64+
map: {
65+
grid: [0, 2, 2, 3]
66+
},
67+
donut: {
68+
grid: [4, 3, 3, 2]
69+
},
70+
line: {
71+
grid: [7, 0, 3, 5]
72+
},
73+
footer: {
74+
grid: [9, 0, 2, 5]
75+
}
76+
}

lib/cli/gfx/index.js

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ const
44
say = require('../../sayings/threads.json'),
55
serverUtil = require('./server-util')
66
template = require ('./template.js'),
7+
{sm, md, lg} = require('../../../bin/util/layout'),
78
ts = Date.now(),
89
date_ob = new Date(ts),
910
date = date_ob.getDate(),
@@ -24,8 +25,6 @@ const
2425
ansiTwitter = twitterhandle+space+twitterhashtag,
2526
ansiGCash = '(GCash) '+gcashNum;
2627

27-
28-
// random sayings
2928
const randomSay = () => {
3029
let random = Math.floor(Math.random() * say.length);
3130
return say[random];
@@ -38,7 +37,7 @@ const patchBlessed = () => {
3837

3938
patchBlessed();
4039

41-
exports.historyCountryTracker = (req, res, n, c, tC, d, tD, r, a, cl, cPOM, u, h, chartType, countryInfo) => {
40+
exports.historyCountryTracker = (req, res, n, c, tC, d, tD, r, a, cl, cPOM, u, h, chartType, countryInfo, chartSize = 'sm') => {
4241
const name = n, cases = c, todayCases = tC,
4342
deaths = d, todayDeaths = tD, recovered = r,
4443
active = a, critical = cl, casesPerOneMillion = cPOM,
@@ -51,11 +50,10 @@ exports.historyCountryTracker = (req, res, n, c, tC, d, tD, r, a, cl, cPOM, u, h
5150
defaultfooter = ansiBMC+ansiTwitter+br+br,
5251
specialfooter = ansiGCash+br+ansiBMC+ansiTwitter+br+br,
5352
defaultHeader = header+br+tagline,
54-
footer = (n.toLowerCase() == 'philippines') ? tableFooter+br+specialfooter+br+source+br+repo : tableFooter+br+defaultfooter+br+source+br+repo;
55-
56-
// load template with data
57-
// serverUtil.loadTemplate(template, {jsonData}, callback)
58-
serverUtil.loadTemplate(template, {
53+
footer = (n.toLowerCase() == 'philippines') ? tableFooter+br+specialfooter+br+source+br+repo : tableFooter+br+defaultfooter+br+source+br+repo,
54+
defaultChartStyle = chartSize === 'sm' ? sm : (chartSize === 'md' ? md : lg);
55+
56+
serverUtil.loadTemplate(template, defaultChartStyle, {
5957
name,
6058
cases,
6159
deaths,
@@ -78,6 +76,6 @@ exports.historyCountryTracker = (req, res, n, c, tC, d, tD, r, a, cl, cPOM, u, h
7876
}, (screen) => {
7977
res.send(screen+'\r\n'+'\033[?25h')
8078
})
81-
79+
8280
};
8381

lib/cli/gfx/server-util.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,11 @@ createScreen = (opt = {}) => {
2828
const output = new OutputBuffer({stream: opt.stream, cols: 250, rows: 50});
2929
const input = new InputBuffer(); //required to run under forever since it replaces stdin to non-tty
3030
const program = blessed.program({output: output, input: input});
31-
3231
let screen = blessed.screen({program: program});
3332
return screen
3433
}
3534

36-
loadTemplate = (gridTemplateClass, json, callback) => {
35+
loadTemplate = (gridTemplateClass, gridStyleTemplateClass, json, callback) => {
3736

3837
blessed.Screen.global = null
3938
blessed.Program.global = null
@@ -48,7 +47,7 @@ loadTemplate = (gridTemplateClass, json, callback) => {
4847
if(!gridTemplateClass) throw new Error('No template loaded')
4948

5049
// parse template
51-
gridTemplateClass.load(grid, json)
50+
gridTemplateClass.load(grid, json, gridStyleTemplateClass)
5251

5352
customStream._transform = function (chunk,encoding,done) {
5453
result.push(chunk.toString())

lib/cli/gfx/template.js

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,8 @@ const
22
contrib = require('blessed-contrib'),
33
chalk = require('chalk');
44

5-
exports.load = (grid, data) => {
6-
7-
let markdown = grid.set(0, 0, 2, 2, contrib.markdown,{
5+
exports.load = (grid, data, chartStyle) => {
6+
let markdown = grid.set(chartStyle.header.grid[0], chartStyle.header.grid[1], chartStyle.header.grid[2], chartStyle.header.grid[3], contrib.markdown,{
87
style: {
98
border: {
109
fg: 'black'
@@ -13,7 +12,7 @@ exports.load = (grid, data) => {
1312
})
1413
markdown.setMarkdown(data.defaultHeader)
1514

16-
let table = grid.set(0, 2, 2, 7, contrib.table,
15+
let table = grid.set(chartStyle.table.grid[0], chartStyle.table.grid[1], chartStyle.table.grid[2], chartStyle.table.grid[3], contrib.table,
1716
{ keys: true
1817
, fg: 'white'
1918
, selectedFg: 'white'
@@ -23,8 +22,8 @@ exports.load = (grid, data) => {
2322
, width: '30%'
2423
, height: '30%'
2524
, border: {type: "line", fg: "cyan"}
26-
, columnSpacing: 10 //in chars
27-
, columnWidth: [1,16, 12, 12, 12, 18],
25+
, columnSpacing: chartStyle.table.columnSpacing || 10
26+
, columnWidth: chartStyle.table.columnWidth || [1, 16, 12, 12, 12, 18],
2827
style: {
2928
border: {
3029
fg: 'white'
@@ -41,7 +40,7 @@ exports.load = (grid, data) => {
4140
]
4241
})
4342

44-
let map = grid.set(2, 0, 3, 4, contrib.map,{
43+
let map = grid.set(chartStyle.map.grid[0], chartStyle.map.grid[1], chartStyle.map.grid[2], chartStyle.map.grid[3], contrib.map,{
4544
style: {
4645
border: {
4746
fg: 'black'
@@ -52,11 +51,11 @@ exports.load = (grid, data) => {
5251

5352
map.addMarker({"lon" : data.countryInfo.long, "lat" : data.countryInfo.lat, color: 'magenta', char: '\u24E7'+` ${data.name}`})
5453

55-
let bar = grid.set(2, 4, 3, 3, contrib.bar,{
54+
let bar = grid.set(chartStyle.bar.grid[0], chartStyle.bar.grid[1], chartStyle.bar.grid[2], chartStyle.bar.grid[3], contrib.bar,{
5655
barBgColor: 'red',
57-
barWidth: 7,
58-
barSpacing: 5,
59-
xOffset: 3,
56+
barWidth: chartStyle.bar.barWidth || 7,
57+
barSpacing: chartStyle.bar.barSpacing || 7,
58+
xOffset: chartStyle.bar.xOffset || 3,
6059
maxHeight: 9,
6160
style: {
6261
border: {
@@ -67,7 +66,7 @@ exports.load = (grid, data) => {
6766
screen.append(bar) //must append before setting data
6867
bar.setData({ titles: ['Cases', 'Deaths', 'Recovered', 'Active'], data: [data.cases, data.deaths, data.recovered, data.active]})
6968

70-
let donut = grid.set(2, 7, 3, 2, contrib.donut,{
69+
let donut = grid.set(chartStyle.donut.grid[0], chartStyle.donut.grid[1], chartStyle.donut.grid[2], chartStyle.donut.grid[3], contrib.donut,{
7170
label: 'Mortality Rate',
7271
radius: 8,
7372
arcWidth: 3,
@@ -84,7 +83,7 @@ exports.load = (grid, data) => {
8483
]
8584
});
8685

87-
let line = grid.set(5, 0, 4, 9, contrib.line,
86+
let line = grid.set(chartStyle.line.grid[0], chartStyle.line.grid[1], chartStyle.line.grid[2], chartStyle.line.grid[3], contrib.line,
8887
{ style:
8988
{ line: "yellow"
9089
, text: "green"
@@ -128,7 +127,7 @@ exports.load = (grid, data) => {
128127
screen.append(line) //must append before setting data
129128
line.setData([series1, series2, series3])
130129

131-
let markdownFooter = grid.set(8, 0, 2, 9, contrib.markdown,{
130+
let markdownFooter = grid.set(chartStyle.footer.grid[0], chartStyle.footer.grid[1], chartStyle.footer.grid[2], chartStyle.footer.grid[3], contrib.markdown,{
132131
style: {
133132
border: {
134133
fg: 'black'

0 commit comments

Comments
 (0)