Skip to content

Instantly share code, notes, and snippets.

@CBasis
Last active January 10, 2018 15:13
Show Gist options
  • Save CBasis/97fa786ba7f8ab0d9fda0d2660570ca1 to your computer and use it in GitHub Desktop.
Save CBasis/97fa786ba7f8ab0d9fda0d2660570ca1 to your computer and use it in GitHub Desktop.
Basic CSV Parsing
license: mit
//Simple d3.js barchart example to illustrate d3 selections
//other good related tutorials
//http://www.recursion.org/d3-for-mere-mortals/
//http://mbostock.github.com/d3/tutorial/bar-1.html
var w = 850
var h = 400
var bars = function(data)
{
max = d3.max(data, function(d)
{
return d.value
})
//nice breakdown of d3 scales
//http://www.jeromecukier.net/blog/2011/08/11/d3-scales-and-color/
y = d3.scale.linear()
.domain([0, max])
.range([0, h])
x = d3.scale.ordinal()
.domain(d3.range(data.length))
.rangeBands([0, w], .2)
var vis = d3.select("#barchart")
console.log("vis", vis)
//a good written tutorial of d3 selections coming from protovis
//http://www.jeromecukier.net/blog/2011/08/09/d3-adding-stuff-and-oh-understanding-selections/
var bars = vis.selectAll("rect.bar")
.data(data)
//update
bars
.attr("fill", "#0a0")
.attr("stroke", "#050")
//enter
bars.enter()
.append("svg:rect")
.attr("class", "bar")
.attr("fill", "#800")
.attr("stroke", "#800")
//exit
bars.exit()
.remove()
//apply to everything (enter and update)
bars
.attr("stroke-width", 4)
.attr("height", function(d,i)
{
return y(d.value);
})
.attr("width", x.rangeBand())
.attr("transform", function(d,i) {
return "translate(" + [x(i), h - y(d.value)] + ")"
})
}
function init()
{
//setup the svg
var svg = d3.select("#svg")
.attr("width", w+100)
.attr("height", h+100)
console.log("svg", svg)
svg.append("svg:rect")
.attr("width", "100%")
.attr("height", "100%")
.attr("stroke", "#000")
.attr("fill", "none")
svg.append("svg:g")
.attr("id", "barchart")
.attr("transform", "translate(50,50)")
}
var prices_csv = function()
{
var parse = d3.time.format("%m/%d/%Y").parse;
d3.csv("prices.csv", function(prices)
{
//prices is an array of json objects containing the data in from the csv
console.log("prices:", prices)
data = prices.map(function(d)
{
//each d is one line of the csv file represented as a json object
console.log("d", d)
month = parse(d.month).getMonth();
console.log("month:", d.month, month)
//we slice the dollar sign off then convert to a number with the + sign
//slicing works like "$216".slice(1) gives you 216,
//you can also give it a range like "$216 asdf".slice(1,4) gives you 216
p = d.price
price = +p.slice(1)
console.log("price:", p, price);
return {"month": month, "value":price} ;
})
console.log("data", data)
bars(data);
})
}
var trans_csv = function()
{
var parse = d3.time.format("%m/%d/%Y").parse;
d3.csv("trans.csv", function(trans)
{
//prices is an array of json objects containing the data in from the csv
console.log("trans:", trans)
data = trans.map(function(d)
{
//each d is one line of the csv file represented as a json object
console.log("d", d)
summer = +d.Summer
winter = +d.Winter
return {
//"winter": winter,
//"summer": summer
"value":winter
}
})
console.log("data", data)
bars(data);
})
var list_csv = function()
{
console.log("--/////----");
d3.csv("list.csv", function(data) {
for (var i = 0; i < data.length; i++) {
console.log(data[i].id);
console.log(data[i].value);
}
});
var parse = d3.time.format("%m/%d/%Y").parse;
d3.csv("list.csv", function(trans)
{
//prices is an array of json objects containing the data in from the csv
console.log("trans:", trans)
data = trans.map(function(d)
{
//each d is one line of the csv file represented as a json object
console.log("d", d)
summer = +d.Summer
winter = +d.Winter
return {
//"winter": winter,
//"summer": summer
"value":winter
}
})
console.log("data", data)
bars(data);
})
}
id value
/DC_EX 3
/DC_EX/in 3
/DC_EX/out 365
/DC_EX/status 300
/DC_EX/A/in 3
/DC_EX/A/out 12
/DC_EX/A/status 32
/DC_EX/B/in 1
/DC_EX/B/out 3
/DC_EX/B/status 34
<!DOCTYPE html>
<html>
<head>
<title>Simple Template Example</title>
<script type="text/javascript" src="https://d3js.org/d3.v4.min.js"></script>
<script>
d3.csv("fs.csv",
function(error, data) {
var select = d3.select("body")
.append("div")
// select
// .on("change", function(d) {
// var value = d3.select(this).property("value");
// alert(value);
// });
//
select.selectAll("a")
.data(data)
.enter()
.append("font")
.attr("size", function (d) { return d.value+"px"; })
.append("a")
.text(function (d)
{ return d.id; })
.append("br")
});
//
// for (var i = 0; i < data.length; i++) {
// console.log(data[i].id);
// console.log(data[i].value);
// }
//});
//
//var p = d3.select("body").selectAll("p")
//.data(d3)
//.enter()
//.append("p")
//.text("hello ");
//--/
</script>
</head>
<body>
<font family="Arial">
<!--
<button id="prices" value="Prices">Prices</button><button id="trans">Transmission Limits</button>
<button id="list">List</button><br>
<svg id="svg"></svg>
<script type="text/javascript">
d3.select("#prices").on("click", function(d,i)
{
prices_csv();
})
d3.select("#trans").on("click", function(d,i)
{
trans_csv();
})
d3.select("#list").on("click", function(d,i)
{
list_csv();
})
init();
</script>
-->
</body>
</html>
We can make this file beautiful and searchable if this error is corrected: It looks like row 2 should actually have 2 columns, instead of 1. in line 1.
id,value
flare
flare.$VTHOSTNAME
flare.$VTHOSTNAME.VS0
flare.$VTHOSTNAME.VS0.DATA
flare.$VTHOSTNAME.VS0.DATA.SYSTEMDB,4096
flare.$VTHOSTNAME.VS0.LOG,4096
flare.$VTHOSTNAME.VS0.LOG.DB_ES1,4096
flare.$VTHOSTNAME.VS0.LOG.SYSTEMDB,4096
flare.VS0
flare.VS0.config_files
flare.VS0.config_files.COUNT_0,4096
flare.VS0.config_files.COUNT_0.dfvvesvs0hd00,5646
flare.VS0.config_files.COUNT_0.global,5515
flare.VS0.config_files.COUNT_1,4096
flare.VS0.config_files.COUNT_1.dfvvesvs0hd00,5646
flare.VS0.config_files.COUNT_1.global,5515
flare.VS0.config_files.COUNT_2,4096
flare.VS0.config_files.COUNT_2.dfvvesvs0hd00,5646
flare.VS0.config_files.COUNT_2.global,5515
flare.VS0.config_files.COUNT_3,4096
flare.VS0.config_files.COUNT_3.dfvvesvs0hd00,5646
flare.VS0.config_files.COUNT_3.global,5515
flare.VS0.config_files.COUNT_4,4096
flare.VS0.config_files.COUNT_4.dfvvesvs0hd00,5646
flare.VS0.config_files.COUNT_4.global,5515
flare.VS0.config_files.COUNT_5,4096
flare.VS0.config_files.COUNT_5.dfvvesvs0hd00,5646
flare.VS0.config_files.COUNT_5.global,5515
flare.VS0.config_files.COUNT_6,4096
flare.VS0.config_files.COUNT_6.dfvvesvs0hd00,5646
flare.VS0.config_files.COUNT_6.global,5515
flare.VS0.config_files.COUNT_7,4096
flare.VS0.config_files.COUNT_7.dfvvesvs0hd00,5646
flare.VS0.config_files.COUNT_7.global,5515
flare.VS0.config_files.COUNT_9,4096
flare.VS0.config_files.COUNT_9.dfvvesvs0hd00,5329
flare.VS0.config_files.COUNT_9.global,5515
flare.VS0.config_files.Fri
flare.VS0.config_files.Fri.dfvvesvs0hd00,5329
flare.VS0.config_files.Fri.global,5515
flare.VS0.config_files.Tue
flare.VS0.config_files.Tue.dfvvesvs0hd00,5329
flare.VS0.config_files.Tue.global,5515
flare.VS0.data
flare.VS0.data.DB_EN0,4096
flare.VS0.data.DB_ES0,934097145856
flare.VS0.data.DB_ES1,113590620160
flare.VS0.data.DB_ES1.DB_ES1,4096
flare.VS0.data.SYSTEMDB,66648190976
flare.VS0.log
flare.VS0.log.DB_ES0,127421808640
flare.VS0.log.DB_ES0,127421808640
flare.VS0.log.DB_ES1,41320476672
flare.VS0.log.DB_ES1,41320476672
flare.VS0.log.SYSTEMDB,29750636544
flare.VS0.log.SYSTEMDB.newer,94208
flare.VS0.log.SYSTEMDB.newer,94208
flare.WS0
flare.WS0.config_files,4096
flare.WS0.data
flare.WS0.data.DB_FN0,4096
flare.WS0.data.DB_FS0,49291956224
flare.WS0.data.DB_FS1,14395346944
flare.WS0.data.DB_WS0,4096
flare.WS0.data.SYSTEMDB,5369049088
flare.WS0.log
flare.WS0.log.DB_FS0,399214690304
flare.WS0.log.DB_FS1,292114722816
flare.WS0.log.SYSTEMDB,1052390526976
flare.XS0
flare.XS0.config_files,4096
flare.XS0.data
flare.XS0.data.DB_KN0,19059085312
flare.XS0.data.DB_KS0,36457058304
flare.XS0.data.DB_KS1,6056742912
flare.XS0.data.SYSTEMDB,6157410304
flare.XS0.log
flare.XS0.log.DB_KN0,22690299904
flare.XS0.log.DB_KS0,26873028608
flare.XS0.log.DB_KS1,17937993728
flare.XS0.log.SYSTEMDB,32914739200
flare.ZS0
flare.ZS0.config_files,4096
flare.ZS0.data
flare.ZS0.data.DB_PS0,51271340032
flare.ZS0.data.DB_PS1,6392287232
flare.ZS0.data.DB_ZS0,4096
flare.ZS0.data.SYSTEMDB,6241296384
flare.ZS0.log
flare.ZS0.log.DB_PS0,655249408
flare.ZS0.log.DB_PS1,834719744
flare.ZS0.log.SYSTEMDB,667246592
//Simple d3.js barchart example to illustrate d3 selections
//other good related tutorials
//http://www.recursion.org/d3-for-mere-mortals/
//http://mbostock.github.com/d3/tutorial/bar-1.html
var w = 850
var h = 400
var bars = function(data)
{
max = d3.max(data, function(d)
{
return d.value
})
//nice breakdown of d3 scales
//http://www.jeromecukier.net/blog/2011/08/11/d3-scales-and-color/
y = d3.scale.linear()
.domain([0, max])
.range([0, h])
x = d3.scale.ordinal()
.domain(d3.range(data.length))
.rangeBands([0, w], .2)
var vis = d3.select("#barchart")
console.log("vis", vis)
//a good written tutorial of d3 selections coming from protovis
//http://www.jeromecukier.net/blog/2011/08/09/d3-adding-stuff-and-oh-understanding-selections/
var bars = vis.selectAll("rect.bar")
.data(data)
//update
bars
.attr("fill", "#0a0")
.attr("stroke", "#050")
//enter
bars.enter()
.append("svg:rect")
.attr("class", "bar")
.attr("fill", "#800")
.attr("stroke", "#800")
//exit
bars.exit()
.remove()
//apply to everything (enter and update)
bars
.attr("stroke-width", 4)
.attr("height", function(d,i)
{
return y(d.value);
})
.attr("width", x.rangeBand())
.attr("transform", function(d,i) {
return "translate(" + [x(i), h - y(d.value)] + ")"
})
}
function init()
{
//setup the svg
var svg = d3.select("#svg")
.attr("width", w+100)
.attr("height", h+100)
console.log("svg", svg)
svg.append("svg:rect")
.attr("width", "100%")
.attr("height", "100%")
.attr("stroke", "#000")
.attr("fill", "none")
svg.append("svg:g")
.attr("id", "barchart")
.attr("transform", "translate(50,50)")
}
month price
2/1/1990 $67
3/1/1990 $75
4/1/1990 $75
5/1/1990 $73
6/1/1990 $70
7/1/1990 $68
8/1/1990 $74
9/1/1990 $76
10/1/1990 $74
11/1/1990 $70
12/1/1990 $73
1/1/1991 $69
2/1/1991 $71
3/1/1991 $72
4/1/1991 $71
5/1/1991 $67
6/1/1991 $66
7/1/1991 $64
8/1/1991 $63
9/1/1991 $67
10/1/1991 $63
11/1/1991 $64
12/1/1991 $63
1/1/1992 $61
2/1/1992 $56
3/1/1992 $56
4/1/1992 $54
5/1/1992 $49
6/1/1992 $48
7/1/1992 $49
8/1/1992 $46
9/1/1992 $47
10/1/1992 $53
11/1/1992 $57
12/1/1992 $64
1/1/1993 $58
2/1/1993 $57
3/1/1993 $55
4/1/1993 $51
5/1/1993 $54
6/1/1993 $55
7/1/1993 $61
8/1/1993 $68
9/1/1993 $72
10/1/1993 $68
11/1/1993 $70
12/1/1993 $72
1/1/1994 $69
2/1/1994 $72
3/1/1994 $76
4/1/1994 $81
5/1/1994 $108
6/1/1994 $128
7/1/1994 $191
8/1/1994 $182
9/1/1994 $202
10/1/1994 $186
11/1/1994 $168
12/1/1994 $149
1/1/1995 $152
2/1/1995 $152
3/1/1995 $163
4/1/1995 $160
5/1/1995 $156
6/1/1995 $142
7/1/1995 $133
8/1/1995 $142
9/1/1995 $125
10/1/1995 $120
11/1/1995 $118
12/1/1995 $100
1/1/1996 $100
2/1/1996 $111
3/1/1996 $106
4/1/1996 $107
5/1/1996 $110
6/1/1996 $106
7/1/1996 $100
8/1/1996 $103
9/1/1996 $97
10/1/1996 $99
11/1/1996 $97
12/1/1996 $90
1/1/1997 $100
2/1/1997 $122
3/1/1997 $137
4/1/1997 $142
5/1/1997 $180
6/1/1997 $155
7/1/1997 $135
8/1/1997 $133
9/1/1997 $133
10/1/1997 $121
11/1/1997 $118
12/1/1997 $130
1/1/1998 $131
2/1/1998 $131
3/1/1998 $120
4/1/1998 $120
5/1/1998 $114
6/1/1998 $104
7/1/1998 $97
8/1/1998 $101
9/1/1998 $96
10/1/1998 $95
11/1/1998 $98
12/1/1998 $101
1/1/1999 $98
2/1/1999 $92
3/1/1999 $89
4/1/1999 $86
5/1/1999 $90
6/1/1999 $86
7/1/1999 $78
8/1/1999 $77
9/1/1999 $72
10/1/1999 $76
11/1/1999 $88
12/1/1999 $96
1/1/2000 $82
2/1/2000 $76
3/1/2000 $73
4/1/2000 $70
5/1/2000 $69
6/1/2000 $65
7/1/2000 $64
8/1/2000 $58
9/1/2000 $57
10/1/2000 $56
11/1/2000 $52
12/1/2000 $48
1/1/2001 $49
2/1/2001 $49
3/1/2001 $49
4/1/2001 $47
5/1/2001 $49
6/1/2001 $47
7/1/2001 $43
8/1/2001 $43
9/1/2001 $41
10/1/2001 $42
11/1/2001 $44
12/1/2001 $43
1/1/2002 $43
2/1/2002 $44
3/1/2002 $49
4/1/2002 $50
5/1/2002 $47
6/1/2002 $46
7/1/2002 $45
8/1/2002 $43
9/1/2002 $48
10/1/2002 $51
11/1/2002 $55
12/1/2002 $52
1/1/2003 $54
2/1/2003 $54
3/1/2003 $50
4/1/2003 $52
5/1/2003 $53
6/1/2003 $49
7/1/2003 $51
8/1/2003 $52
9/1/2003 $54
10/1/2003 $52
11/1/2003 $50
12/1/2003 $52
1/1/2004 $59
2/1/2004 $60
3/1/2004 $61
4/1/2004 $59
5/1/2004 $60
6/1/2004 $64
7/1/2004 $58
8/1/2004 $57
9/1/2004 $61
10/1/2004 $61
11/1/2004 $68
12/1/2004 $78
1/1/2005 $79
2/1/2005 $89
3/1/2005 $101
4/1/2005 $98
5/1/2005 $100
6/1/2005 $96
7/1/2005 $88
8/1/2005 $85
9/1/2005 $79
10/1/2005 $83
11/1/2005 $86
12/1/2005 $87
1/1/2006 $101
2/1/2006 $97
3/1/2006 $93
4/1/2006 $94
5/1/2006 $90
6/1/2006 $86
7/1/2006 $89
8/1/2006 $96
9/1/2006 $96
10/1/2006 $96
11/1/2006 $103
12/1/2006 $108
1/1/2007 $106
2/1/2007 $104
3/1/2007 $100
4/1/2007 $99
5/1/2007 $100
6/1/2007 $107
7/1/2007 $106
8/1/2007 $108
9/1/2007 $113
10/1/2007 $116
11/1/2007 $114
12/1/2007 $118
1/1/2008 $122
2/1/2008 $139
3/1/2008 $136
4/1/2008 $127
5/1/2008 $127
6/1/2008 $131
7/1/2008 $133
8/1/2008 $131
9/1/2008 $127
10/1/2008 $108
11/1/2008 $108
12/1/2008 $103
1/1/2009 $108
2/1/2009 $108
3/1/2009 $106
4/1/2009 $112
5/1/2009 $123
6/1/2009 $119
7/1/2009 $113
8/1/2009 $117
9/1/2009 $116
10/1/2009 $121
11/1/2009 $120
12/1/2009 $125
1/1/2010 $127
2/1/2010 $123
3/1/2010 $125
4/1/2010 $127
5/1/2010 $128
6/1/2010 $142
7/1/2010 $153
8/1/2010 $157
9/1/2010 $164
10/1/2010 $162
11/1/2010 $174
12/1/2010 $184
1/1/2011 $197
2/1/2011 $216
3/1/2011 $224
We can make this file beautiful and searchable if this error is corrected: It looks like row 19 should actually have 2 columns, instead of 1. in line 18.
id,value
/DC_DI/TMS/QPMS/QM1/IN,5
/DC_DI/TMS/QPMS/QM1/IN/ARCHIVE,5
/DC_DI/TMS/QPMS/QM1/IN/AVISE,5
/DC_DI/TMS/QPMS/QM1/OUT,5
/DC_DI/TMS/QPMS/QM1/OUT_ARCHIV,5
/DC_DI/TMS/QPMS/Q01,5
/DC_DI/TMS/QPMS/Q01/IN/AVISE,5
/DC_DI/TMS/QPMS/bank,5
/DC_DI/TMS/QPMS/bank/in/alt,5
/DC_DI/TMS/QPMS/bank/in/Test,5
/DC_DI/TMS/QPMS/bank/mt100,5
/DC_DI/TMS/QPMS/bank/out,5
/DC_DI/TMS/QPMS/bank/out/Ausland,5
/DC_DI/TMS/QPMS/bank/out/Inland,5
/DC_DI/TMS/QPMS/bank/out/save,5
/DC_DI/TMS/QPMS/bank/save,5
/DC_DI/TMS/QPMS/Q04,5
/DC_DI/TMS/QPMS/Q04/IN
/DC_DI/TMS/QPMS/Q04/IN/AVISE
/DC_DI/TMS/QPMS/Q04/IN/ARCHIVE
/DC_DI/TMS/QPMS/Q04/OUT
/DC_DI/TMS/QPMS/Q04/OUT_ARCHIV
/DC_DI/TMS/QPMS/Q04/002
/DC_DI/TMS/QPMS/Q04/002/OUT_ARCHIV
/DC_DI/TMS/QPMS/R14
/DC_DI/TMS/QPMS/R14/IN/AVISE
/DC_DI/TMS/QPMS/Reports/COPS/Archiv
/DC_DI/TMS/QPMS/Reports/COPS/Archiv/201105
/DC_DI/TMS/QPMS/Reports/COPS/Archiv/201101
/DC_DI/TMS/QPMS/Reports/COPS/Archiv/201103
/DC_DI/TMS/QPMS/Reports/COPS/Archiv/2009
/DC_DI/TMS/QPMS/Reports/COPS/Archiv/201104
/DC_DI/TMS/QPMS/Reports/COPS/Archiv/201109
/DC_DI/TMS/QPMS/Reports/COPS/Archiv/201003
/DC_DI/TMS/QPMS/Reports/COPS/Archiv/201110
/DC_DI/TMS/QPMS/Reports/COPS/Archiv/201012
/DC_DI/TMS/QPMS/Reports/COPS/Archiv/201010
/DC_DI/TMS/QPMS/Reports/COPS/Archiv/201007
/DC_DI/TMS/QPMS/Reports/COPS/Archiv/201111
/DC_DI/TMS/QPMS/Reports/COPS/Archiv/201009
/DC_DI/TMS/QPMS/Reports/COPS/Archiv/201112
/DC_DI/TMS/QPMS/Reports/COPS/Archiv/201207
/DC_DI/TMS/QPMS/Reports/COPS/Archiv/201008
/DC_DI/TMS/QPMS/Reports/COPS/Archiv/2008
/DC_DI/TMS/QPMS/Reports/COPS/Archiv/201102
/DC_DI/TMS/QPMS/Reports/COPS/Archiv/20150602
/DC_DI/TMS/QPMS/Reports/PG
/DC_DI/TMS/QPMS/Reports/PG/Schulung
/DC_DI/TMS/QPMS/Reports/PG/Finanzstatus
/DC_DI/TMS/QPMS/Reports/PG/Developer
/DC_DI/TMS/QPMS/Reports/COPS_bis_090512
/DC_DI/TMS/QPMS/Reports/COPS_bis_090623
/DC_DI/TMS/QPMS/Reports/COPS_bis_090623/Archiv
/DC_DI/TMS/QPMS/Reports/Brassat
/DC_DI/TMS/QPMS/Reports/Schulung
/DC_DI/TMS/QPMS/Reports/TEC
/DC_DI/TMS/QPMS/Reports/Wi
/DC_DI/TMS/QPMS/Reports/Bratzo
/DC_DI/TMS/QPMS/Reports/Frind
/DC_DI/TMS/QPMS/Reports/bultmann
/DC_DI/TMS/QPMS/Reports/Hantke
/DC_DI/TMS/QPMS/Reports/Perisic
/DC_DI/TMS/QPMS/Reports/Juckenhöfel
/DC_DI/TMS/QPMS/Reports/Hey
/DC_DI/TMS/QPMS/Reports/LIP
/DC_DI/TMS/QPMS/Reports/Schlabbers
/DC_DI/TMS/QPMS/Reports/Wicke
/DC_DI/TMS/QPMS/Reports/KHE
/DC_DI/TMS/QPMS/Reports/Erweiterungen
/DC_DI/TMS/QPMS/Reports/Monitoring
/DC_DI/TMS/QPMS/Reports/Sicherung_QPMS_Stand_070214
/DC_DI/TMS/QPMS/Reports/Sicherung_QPMS_Stand_070214/COPS
/DC_DI/TMS/QPMS/Reports/Sicherung_QPMS_Stand_070214/COPS/Archiv
/DC_DI/TMS/QPMS/Reports/Sicherung_QPMS_Stand_070214/PG
/DC_DI/TMS/QPMS/Reports/Sicherung_QPMS_Stand_070214/PG/Schulung
/DC_DI/TMS/QPMS/Reports/Sicherung_QPMS_Stand_070214/PG/Finanzstatus
/DC_DI/TMS/QPMS/Reports/Sicherung_QPMS_Stand_070214/PG/Developer
/DC_DI/TMS/QPMS/Reports/C-FN-FC
/DC_DI/TMS/QPMS/Reports/Commodity
IRGEX IRGIM Winter Summer
1 18 0.82 0.82
1 33 0.28 0.28
2 14 1.9 1
3 4 0.27 0.27
3 11 5.2 1.1
4 3 0.8 0.8
4 13 0.32 0.32
4 17 2.08 2.04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment