Skip to content

Commit 123ba01

Browse files
committed
feat: different chart sizes - sm, md, lg
1 parent edef8f9 commit 123ba01

File tree

7 files changed

+93
-20
lines changed

7 files changed

+93
-20
lines changed

app.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -129,12 +129,12 @@ 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 || 'md',
138138
summary = await axios.get(`${apiBaseURL}/countries/${countryData}`),
139139
history = await axios.get(`${apiBaseURL}/v2/historical/${summary.data.country}?lastdays=all`),
140140
s = summary.data,
@@ -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
}

lib/cli/gfx/index.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ const patchBlessed = () => {
3838

3939
patchBlessed();
4040

41-
exports.historyCountryTracker = (req, res, n, c, tC, d, tD, r, a, cl, cPOM, u, h, chartType, countryInfo) => {
41+
exports.historyCountryTracker = (req, res, n, c, tC, d, tD, r, a, cl, cPOM, u, h, chartType, countryInfo, chartSize = 'md') => {
4242
const name = n, cases = c, todayCases = tC,
4343
deaths = d, todayDeaths = tD, recovered = r,
4444
active = a, critical = cl, casesPerOneMillion = cPOM,
@@ -51,11 +51,13 @@ exports.historyCountryTracker = (req, res, n, c, tC, d, tD, r, a, cl, cPOM, u, h
5151
defaultfooter = ansiBMC+ansiTwitter+br+br,
5252
specialfooter = ansiGCash+br+ansiBMC+ansiTwitter+br+br,
5353
defaultHeader = header+br+tagline,
54-
footer = (n.toLowerCase() == 'philippines') ? tableFooter+br+specialfooter+br+source+br+repo : tableFooter+br+defaultfooter+br+source+br+repo;
55-
54+
footer = (n.toLowerCase() == 'philippines') ? tableFooter+br+specialfooter+br+source+br+repo : tableFooter+br+defaultfooter+br+source+br+repo,
55+
allowedDefaultChartSize = ['sm', 'md', 'lg'],
56+
defaultChartStyle = require(`../gfx/layout/${allowedDefaultChartSize.indexOf(chartSize) != -1 ? chartSize: allowedDefaultChartSize[1] }.json`);
57+
5658
// load template with data
5759
// serverUtil.loadTemplate(template, {jsonData}, callback)
58-
serverUtil.loadTemplate(template, {
60+
serverUtil.loadTemplate(template, defaultChartStyle, {
5961
name,
6062
cases,
6163
deaths,

lib/cli/gfx/layout/lg.json

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
{
2+
"grid": {},
3+
"header": {
4+
"grid": [0, 0, 1, 10]
5+
},
6+
"table": {
7+
"grid": [1, 0, 2, 10]
8+
},
9+
"bar": {
10+
"grid": [3, 5, 3, 3]
11+
},
12+
"map": {
13+
"grid": [3, 0, 3, 5]
14+
},
15+
"donut": {
16+
"grid": [3, 8, 3, 2]
17+
},
18+
"line": {
19+
"grid": [6, 0, 4, 10]
20+
},
21+
"footer": {
22+
"grid": [9, 0, 2, 10]
23+
}
24+
}

lib/cli/gfx/layout/md.json

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
{
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+
}

lib/cli/gfx/layout/sm.json

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
{
2+
"grid": {},
3+
"header": {
4+
"grid": [0, 0, 2, 2]
5+
},
6+
"table": {
7+
"grid": [2, 0, 2, 5],
8+
"columnSpacing": 5
9+
},
10+
"bar": {
11+
"grid": [4, 0, 3, 3]
12+
},
13+
"map": {
14+
"grid": [0, 2, 2, 3]
15+
},
16+
"donut": {
17+
"grid": [4, 3, 3, 2]
18+
},
19+
"line": {
20+
"grid": [7, 0, 3, 5]
21+
},
22+
"footer": {
23+
"grid": [9, 0, 2, 5]
24+
}
25+
}

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: 9 additions & 10 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,7 +22,7 @@ exports.load = (grid, data) => {
2322
, width: '30%'
2423
, height: '30%'
2524
, border: {type: "line", fg: "cyan"}
26-
, columnSpacing: 10 //in chars
25+
, columnSpacing: chartStyle.table.columnSpacing || 10 //in chars
2726
, columnWidth: [1,16, 12, 12, 12, 18],
2827
style: {
2928
border: {
@@ -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,7 +51,7 @@ 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',
5756
barWidth: 7,
5857
barSpacing: 5,
@@ -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)