Skip to content

Commit ec57285

Browse files
implement save CSV and JSON data in perspective.js (#88)
and add sample group by csv example for testing
1 parent 079149b commit ec57285

4 files changed

+239
-22
lines changed

data/CTA-Ridership-by-Station.csv

Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
1+
"stationname (Group by 1)","daytype","rides","date"
2+
,100000,254315980,100000
3+
"Lake/State",745,8410651,745
4+
"Clark/Lake",724,7610009,724
5+
"Chicago/State",712,6877957,712
6+
"Fullerton",734,5656230,734
7+
"95th/Dan Ryan",699,5428804,699
8+
"O'Hare Airport",722,5309782,722
9+
"Belmont-North Main",686,5260206,686
10+
"Roosevelt",705,4817710,705
11+
"Grand/State",686,4760163,686
12+
"Washington/Dearborn",724,4739345,724
13+
"State/Lake",704,4400395,704
14+
"Jackson/State",667,4110664,667
15+
"Midway Airport",707,3834789,707
16+
"Monroe/State",689,3777559,689
17+
"Addison-North Main",734,3738700,734
18+
"Clark/Division",732,3682361,732
19+
"79th",710,3527822,710
20+
"Adams/Wabash",702,3167325,702
21+
"Jackson/Dearborn",681,2958205,681
22+
"Quincy/Wells",681,2909791,681
23+
"Howard",703,2860852,703
24+
"Jefferson Park",680,2839884,680
25+
"Wilson",713,2833069,713
26+
"Washington/Wells",712,2809062,712
27+
"69th",728,2772903,728
28+
"Logan Square",688,2701866,688
29+
"Monroe/Dearborn",708,2695190,708
30+
"Loyola",715,2527112,715
31+
"Merchandise Mart",675,2523233,675
32+
"Rosemont",684,2485163,684
33+
"Chicago/Franklin",691,2429164,691
34+
"Damen/Milwaukee",689,2396590,689
35+
"Division/Milwaukee",703,2331552,703
36+
"UIC-Halsted",705,2325251,705
37+
"Diversey",734,2293996,734
38+
"Belmont-O'Hare",727,2289433,727
39+
"North/Clybourn",670,2281853,670
40+
"87th",727,2246219,727
41+
"Bryn Mawr",722,2198207,722
42+
"Sheridan",687,2193986,687
43+
"Randolph/Wabash",386,2186168,386
44+
"Pulaski-Orange",682,2160806,682
45+
"Morse",699,2060666,699
46+
"Western/Milwaukee",691,2048467,691
47+
"California/Milwaukee",726,2011649,726
48+
"Cumberland",691,2006977,691
49+
"Sox-35th-Dan Ryan",686,1904158,686
50+
"Western-Brown",742,1862070,742
51+
"Irving Park-O'Hare",691,1838009,691
52+
"Harrison",747,1817068,747
53+
"Cermak-Chinatown",667,1780346,667
54+
"Granville",713,1768339,713
55+
"Kimball",693,1754329,693
56+
"Washington/Wabash",370,1710827,370
57+
"Garfield-Dan Ryan",693,1704506,693
58+
"Davis",695,1695672,695
59+
"Chicago/Milwaukee",742,1687059,742
60+
"Harlem-Lake",699,1680530,699
61+
"Armitage",686,1657089,686
62+
"Forest Park",702,1638853,702
63+
"Clinton-Lake",708,1632582,708
64+
"Sedgwick",735,1610186,735
65+
"Berwyn",708,1596433,708
66+
"Western-Orange",698,1592459,698
67+
"Library",707,1564093,707
68+
"47th-Dan Ryan",742,1546217,742
69+
"63rd-Dan Ryan",675,1527885,675
70+
"Kedzie-Midway",727,1493895,727
71+
"Lawrence",703,1472761,703
72+
"Thorndale",735,1408992,735
73+
"Madison/Wabash",374,1367479,374
74+
"Southport",693,1307650,693
75+
"Argyle",678,1305173,678
76+
"Harlem-O'Hare",719,1296768,719
77+
"Irving Park-Brown",768,1290337,768
78+
"Clinton-Forest Park",667,1279905,667
79+
"Addison-O'Hare",730,1272826,730
80+
"Medical Center",718,1259695,718
81+
"35th/Archer",682,1194791,682
82+
"Polk",661,1170972,661
83+
"Montrose-Brown",728,1146115,728
84+
"Wellington",717,1135337,717
85+
"LaSalle",696,1131302,696
86+
"Paulina",700,1093991,700
87+
"LaSalle/Van Buren",652,1070627,652
88+
"Halsted-Orange",673,1060527,673
89+
"Racine",738,995261,738
90+
"Central-Lake",670,994039,670
91+
"Ashland-Lake",678,974531,678
92+
"Addison-Brown",708,962184,708
93+
"Kedzie-Homan-Forest Park",708,955921,708
94+
"Damen-Brown",658,918176,658
95+
"Montrose-O'Hare",679,913225,679
96+
"Pulaski-Forest Park",702,891851,702
97+
"35-Bronzeville-IIT",695,881740,695
98+
"Grand/Milwaukee",674,878173,674
99+
"Austin-Lake",694,876203,694
100+
"Washington/State",181,867253,181
101+
"Kedzie-Brown",708,858920,708
102+
"54th/Cermak",675,853976,675
103+
"Morgan-Lake",484,847782,484
104+
"Pulaski-Lake",720,808250,720
105+
"Austin-Forest Park",659,789387,659
106+
"Ashland-Orange",720,738329,720
107+
"Jarvis",711,737772,711
108+
"Rockwell",717,713331,717
109+
"Cicero-Lake",742,707659,742
110+
"Oak Park-Lake",717,697489,717
111+
"18th",659,680709,659
112+
"Oak Park-Forest Park",684,680499,684
113+
"Ashland/63rd",707,674690,707
114+
"Western-Forest Park",683,666252,683
115+
"Kedzie-Lake",681,638204,681
116+
"Garfield-South Elevated",655,611024,655
117+
"47th-South Elevated",717,593236,717
118+
"Cicero-Forest Park",661,587232,661
119+
"Laramie",671,585635,671
120+
"Damen-Cermak",699,578533,699
121+
"East 63rd-Cottage Grove",696,578351,696
122+
"Francisco",675,555565,675
123+
"California-Cermak",679,548501,679
124+
"Main",703,544456,703
125+
"Ridgeland",672,538857,672
126+
"California-Lake",742,520496,742
127+
"Cicero-Cermak",675,519686,675
128+
"Harlem-Forest Park",718,493452,718
129+
"Western-Cermak",707,478692,707
130+
"51st",679,474702,679
131+
"Central Park",702,473709,702
132+
"Conservatory",739,462180,739
133+
"Pulaski-Cermak",675,458560,675
134+
"Skokie",245,453597,245
135+
"Dempster-Skokie",461,448985,461
136+
"43rd",666,428314,666
137+
"Linden",682,427352,682
138+
"Kedzie-Cermak",703,401977,703
139+
"Dempster",722,395619,722
140+
"Central-Evanston",676,367958,676
141+
"Cermak-McCormick Place",418,361799,418
142+
"Noyes",724,355386,724
143+
"South Boulevard",680,340094,680
144+
"Halsted/63rd",684,338153,684
145+
"Foster",685,335531,685
146+
"Indiana",663,325695,663
147+
"King Drive",705,287531,705
148+
"Oakton-Skokie",485,239810,485
149+
"Kostner",683,187867,683
150+
"Homan",1,0,1
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
{
2+
"fields": [
3+
{
4+
"name": "stationname (Group by 1)",
5+
"type": "string",
6+
"format": "default"
7+
},
8+
{
9+
"name": "daytype",
10+
"type": "integer",
11+
"format": "default"
12+
},
13+
{
14+
"name": "rides",
15+
"type": "integer",
16+
"format": "default"
17+
},
18+
{
19+
"name": "date",
20+
"type": "integer",
21+
"format": "default"
22+
}
23+
],
24+
"missingValues": [
25+
""
26+
]
27+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
{
2+
"columns": [
3+
{
4+
"minWidth": 40,
5+
"download": false,
6+
"formatter": "rowSelection",
7+
"titleFormatter": "rowSelection",
8+
"resizable": true,
9+
"headerSort": false,
10+
"width": 52
11+
},
12+
{
13+
"field": "stationname (Group by 1)",
14+
"title": "stationname (Group by 1)",
15+
"minWidth": 40,
16+
"resizable": true,
17+
"sorter": "string",
18+
"headerSort": true,
19+
"width": 215
20+
},
21+
{
22+
"field": "daytype",
23+
"title": "daytype",
24+
"minWidth": 40,
25+
"resizable": true,
26+
"sorter": "number",
27+
"headerSort": true,
28+
"width": 106
29+
},
30+
{
31+
"field": "rides",
32+
"title": "rides",
33+
"minWidth": 40,
34+
"resizable": true,
35+
"sorter": "number",
36+
"headerSort": true,
37+
"width": 86
38+
},
39+
{
40+
"field": "date",
41+
"title": "date",
42+
"minWidth": 40,
43+
"resizable": true,
44+
"sorter": "number",
45+
"headerSort": true,
46+
"width": 346
47+
}
48+
]
49+
}

web/scripts/perspective.js

Lines changed: 13 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -283,42 +283,33 @@ function scrollToLastRow() {
283283
/**
284284
* Saves table data as CSV, TSV, or JSON data array document.
285285
*/
286-
function saveData() {
286+
async function saveData() {
287287
// get requested data file format
288288
let dataFileType = saveFileTypeSelector.value;
289289

290290
// construct save data file name
291291
const dataFileName = fileName.substring(0, fileName.lastIndexOf('.') + 1);
292292
saveDataFileName = dataFileName + dataFileType;
293-
console.log('tabView:saveData(): saving data:', saveDataFileName);
293+
console.log('perspective.saveData(): saving data:', saveDataFileName);
294294

295-
// adjust text data delimiter and file type
296-
let delimiter = ',';
297-
switch (dataFileType) {
298-
case 'ssv': // semicolon delimited CSV
299-
delimiter = ';';
300-
dataFileType = 'csv';
301-
saveDataFileName = `${dataFileName}.csv`;
302-
break;
303-
case 'tsv':
304-
delimiter = '\t';
305-
dataFileType = 'csv'; // download file type
306-
break;
307-
}
308-
309-
// TODO: use Perspective table API to generate data blob to save
295+
// get current view data
296+
const view = await viewer.getView();
297+
let viewData;
310298
switch (dataFileType) {
311299
case 'csv':
312-
case 'tsv':
313-
// table.download(dataFileType, saveDataFileName, { delimiter: delimiter });
300+
viewData = await view.to_csv();
314301
break;
315302
case 'json':
316-
// table.download(dataFileType, saveDataFileName);
303+
const jsonDataArray = await view.to_json();
304+
viewData = JSON.stringify(jsonDataArray, null, 2);
317305
break;
318-
case 'html':
319-
// table.download(dataFileType, saveDataFileName, { style: true });
306+
case 'arrow':
307+
viewData = await view.to_arrow();
320308
break;
321309
}
310+
311+
// send current view data to webview to save it
312+
downloadData(viewData);
322313
}
323314

324315
/**

0 commit comments

Comments
 (0)