vendredi 2 janvier 2015

Finding and singling out pattern from text jquery


I'm parsing a CSV file into JSON and want to single out some stuff, mainly because I want to use it to draw a graph. The output looks like this


Parser Output:


{ "data": [ { "Output in top 10 percentiles (%)": "CNRS", "Overall": "22,6", "1996": "18,4", "1997": "18,6", "1998": "18,5", "1999": "17,3", "2000": "17,6", "2001": "18,6", "2002": "18,3", "2003": "17,5", "2004": "18,9", "2005": "20,2", "2006": "21,1", "2007": "22,1", "2008": "21,3", "2009": "22,9", "2010": "23,4", "2011": "25,3", "2012": "24,5", "2013": "29,3", "2014": "31,7" }, { "Output in top 10 percentiles (%)": "DACH - Austria, Germany and Switzerland", "Overall": "16,9", "1996": "13,3", "1997": "13,4", "1998": "13,6", "1999": "14,1", "2000": 14, "2001": "14,8", "2002": "15,1", "2003": "14,9", "2004": "15,5", "2005": "15,5", "2006": "15,8", "2007": "16,2", "2008": "16,1", "2009": "16,4", "2010": "17,6", "2011": "18,6", "2012": "18,4", "2013": "21,7", "2014": "25,1" }, { "Output in top 10 percentiles (%)": "Europe", "Overall": "13,5", "1996": "11,2", "1997": "11,2", "1998": "11,3", "1999": "11,5", "2000": "11,5", "2001": "12,2", "2002": "12,3", "2003": "12,2", "2004": "12,5", "2005": "12,6", "2006": "12,8", "2007": 13, "2008": "12,6", "2009": "12,8", "2010": "13,4", "2011": "14,1", "2012": "13,7", "2013": "16,5", "2014": "21,4" }, { "Output in top 10 percentiles (%)": "J. Stefan Institute", "Overall": "13,3", "1996": "6,5", "1997": 8, "1998": "8,9", "1999": "9,7", "2000": 8, "2001": "10,6", "2002": 7, "2003": "8,2", "2004": 12, "2005": "9,5", "2006": "10,6", "2007": "13,4", "2008": "11,9", "2009": "12,8", "2010": "14,9", "2011": "15,5", "2012": "15,2", "2013": "19,7", "2014": "25,6" } ] }


is there a way to search for numbers after a certain year, like 1996: 18,4 (I only want to get 18,4 on a variable)? The numbers are all on a single decimal and never go higher than 100 (they're %). I thought I'd make a for loops that finds the 1996: and then prints out a certain lenght of characters after it. I am now pretty sure this is not the right way to do this :)


So what I want to do is get data for every single year on a separate array, so then I can easily acces it and use it as x for my graph.


The site so far:





function handleFileSelect(evt) {
if ( !(evt.target && evt.target.files && evt.target.files[0]) ) {
return;
}
Papa.parse(evt.target.files[0], {
header: true,
dynamicTyping: true,
complete: function (results) {
debugDataset(results);
renderDataset(results);
}
});
}

function debugDataset(dataset) {
var formatted = JSON.stringify(dataset, null, 2);
$("<div class='parse'></div>").text(formatted).appendTo(".graphcontainer");
}

function renderDataset(dataset) {
// render code here...
}

$(function () {
$("#csv-file").change(handleFileSelect);
});



.graphcontainer {
width:500px;
height:500px;
border:1px solid black;
padding:5px;

margin:auto;
}

.buttoncontainer {
border:1px solid black;
padding:5px;
width:500px;
margin:auto;
}

.parse {
width:500px;
height:480px;

overflow:auto;
}



<script src="http://ift.tt/1oMJErh"></script>
<!DOCTYPE html>

<html>
<head>
<title>csv testing</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<script src="js/libs/jquery/jquery.js"></script>
<script src="js/libs/PapaParse/papaparse.js"></script>
<script src="index.js"></script>
<link type="text/css" rel="stylesheet" href="index.css"/>
</head>

<body>
<div class="graphcontainer">Parser Output:</div>
<div class="buttoncontainer">
<input type="file" id="csv-file" name="files"/>
</div>
</body>

</html>



the CSV file I'm working with:(http://ift.tt/1HkUI6O)


thanks for the help





Aucun commentaire:

Enregistrer un commentaire