|
<html lang="en"> |
|
<head> |
|
<meta charset="utf-8"> |
|
<title>plot data with d3js and html5</title> |
|
<link rel="stylesheet" href="mydiv.css"> |
|
<link rel="stylesheet" href="http://nvd3.org/assets/css/nv.d3.css"> |
|
<link rel="stylesheet" href="http://www.jeasyui.com/easyui/themes/default/easyui.css"> |
|
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script> |
|
<script src="http://d3js.org/d3.v3.min.js" charset="utf-8"></script> |
|
<script src="http://eligrey.com/demos/FileSaver.js/FileSaver.js"></script> |
|
<script src="http://www.jeasyui.com/easyui/jquery.easyui.min.js"></script> |
|
<script src="http://nvd3.org/assets/js/nv.d3.js"></script> |
|
<script src="mathUtils.js"></script> |
|
<link rel="stylesheet" href="http://nvd3.org/assets/css/nv.d3.css"> |
|
<style> |
|
body { |
|
font:10px sans-serif; |
|
} |
|
</style> |
|
<script> |
|
$(function() { |
|
$("#chart").resizable(); |
|
}); |
|
</script> |
|
</head> |
|
|
|
<body> |
|
<div class="easyui-layout" fit="true" style="width:100%;height:100%;min-width:960px;margin:0 auto"> |
|
<div style="width:210px; background-color: aliceblue" region="west" title="DATA SOURCE" > |
|
<div class="easyui-accordion" > |
|
<div id="url" class="myDiv" title="read url"> |
|
<label>google sheet data url: <input type="text" id="data-url" placeholder="https://docs.google.com/spreadsheet/pub?key=0AmT_T9xa0mQ1dFpjdmR1UTdjcmVSb1NvTm51SWZGekE&output=csv" style="width:100%;"/></label> |
|
<button id="url-button" value="read" onclick="readUrl()">read</button> |
|
</div> |
|
<div id="input" class="mydiv" title="read local file"> |
|
<h5>File Format</h5> |
|
<select id="file_sep"> |
|
<option value="tsv">tsv</option> |
|
<option value="csv">csv</option> |
|
</select> |
|
|
|
|
|
<input type="file" id="file" name="file"/> |
|
<a href="newdemo.tsv">Example Data</a> |
|
</div> |
|
<div title="input text" id="text-data" class="myDiv"> |
|
<textArea id="input-text" style="height:50%;width:100%"> |
|
X,Y,group |
|
0.2,0.3,1 |
|
0.3,0.2,2 |
|
0.5,0.7,1 |
|
0.8,0.7,2 |
|
0.7,0.8,2 |
|
</textArea> |
|
<button id="render-input-text-button" onclick="readInputText()">read</button> |
|
<button id="clear-input-text-button" onclick="clearInputText()">clear</button> |
|
</div> |
|
</div> |
|
|
|
</div> |
|
<div id="FIGURE" style=" height:100% ; background-color: white" region="center" class="myDiv" title="FIGURE" align="center"> |
|
<div id="chart" class="easyui-resizable" style="padding:20px height:98%;width:98%;border:1px solid"> |
|
<svg id="scatter"></svg> |
|
</div> |
|
</div> |
|
<div id="right" region="east" style="background-color: aliceblue; width:210px; text-align:center; height:100%" title="CONTROL PANEL"> |
|
<div id="controls" class="myDiv" style="height:30%"> |
|
|
|
<form id="svg-options"> |
|
<label>Filename: <input type="text" class="filename" id="svg-filename" placeholder="demo.scatter"/>.svg</label> |
|
<input type="submit" value="Save"/> |
|
</form> |
|
|
|
Fitting the Canvas<br> |
|
<button id="resize-button" value="resize" onclick="resize()">resize figure</button><br> |
|
Clear the Canvas<br> |
|
<button id="scatter-clear-button" value="clear" onclick="clearScatter()">clear figure</button> |
|
<br> |
|
X: <input type="number" id="X" placeholder="1" step="1"><br> |
|
Y: <input type="number" id="Y" placeholder="2" step="1"><br> |
|
<button id="plot" onclick="plot()">PLOT</button> |
|
<div id="cols"> |
|
</div> |
|
<div id="console"> |
|
</div> |
|
<div id="correlation-output"> |
|
</div> |
|
<button id="correlation" value="correlation" onclick="correlation()">correlation</button> |
|
|
|
</div> |
|
|
|
</div> |
|
<div style="font-size: 90%" align="center" region="south"> |
|
<p>source code are under <a href="http://www.gnu.org/licenses/gpl-3.0.txt">GPL LICENSE</a>.<br /> |
|
Copyright © <a href="https://github.com/nimezhu">zhuxp</a><br /></p> |
|
<p>Please contact |
|
<a href="mailto:[email protected]">zhuxp</a> |
|
with your questions, comments, and suggestions.</p> |
|
</div> |
|
|
|
</div> |
|
|
|
|
|
|
|
<script> |
|
|
|
var svg_options_form = document.getElementById("svg-options"); |
|
var svg_filename = document.getElementById("svg-filename"); |
|
svg_options_form.addEventListener("submit", function(event) { |
|
event.preventDefault(); |
|
var BB = Blob; |
|
var svg = document.getElementById("scatter"); |
|
var svg_xml = (new XMLSerializer).serializeToString(svg); |
|
saveAs( |
|
new BB( |
|
[svg_xml] |
|
, {type: "text/plain;charset=" + document.characterSet} |
|
) |
|
, (svg_filename.value || svg_filename.placeholder) + ".svg" |
|
); |
|
}, false); |
|
</script> |
|
|
|
|
|
|
|
<script> |
|
|
|
var chart = nv.models.scatterChart() |
|
.showDistX(true) //showDist, when true, will display those little distribution lines on the axis. |
|
.showDistY(true) |
|
.transitionDuration(350) |
|
.color(d3.scale.category10().range()); |
|
|
|
//Configure how the tooltip looks. |
|
chart.tooltipContent(function(key) { |
|
return '<h3>' + key + '</h3>'; |
|
}); |
|
|
|
//Axis settings |
|
chart.xAxis.tickFormat(d3.format('.02f')); |
|
chart.yAxis.tickFormat(d3.format('.02f')); |
|
|
|
//We want to show shapes other than circles. |
|
chart.scatter.onlyCircles(false); |
|
nv.utils.windowResize(chart.update); |
|
|
|
var sheet=[]; |
|
var sheet_header=[]; |
|
var colGroup=0; |
|
var colX=0; |
|
var colY=0; |
|
|
|
function renderScatter(myData) |
|
{ |
|
|
|
d3.select('#scatter').text('') |
|
.datum(myData) |
|
.call(chart); |
|
}; |
|
|
|
function resize() |
|
{ |
|
chart.update(); |
|
} |
|
function correlation() |
|
{ |
|
var dots=d3.select("#scatter .nv-point-clips").selectAll("circle").data(); |
|
var x=[]; |
|
var y=[]; |
|
for(var i in dots) |
|
{ |
|
d=dots[i]; |
|
x.push(d[4].x); |
|
y.push(d[4].y); |
|
} |
|
$("#correlation-output").html("<h2> correlation value </h2> <br>" + mathUtils.getPearsonsCorrelation(x,y)); |
|
} |
|
|
|
|
|
|
|
function parseCSV(text, lineTerminator, cellTerminator) { |
|
var lines = text.split(lineTerminator); |
|
sheet_header=lines[0].split(cellTerminator); |
|
colGroup=sheet_header.length-1; |
|
sheet=[] |
|
//TODO change div list header view |
|
var s="<ol>"; |
|
for(i in sheet_header) |
|
{ |
|
s+="<li>"+sheet_header[i]+"</li>"; |
|
} |
|
$("#cols").html(s); |
|
for(var j=1; j<lines.length; j++){ |
|
if(lines[j] != ""){ |
|
var d = lines[j].split(cellTerminator); |
|
sheet.push(d) |
|
} |
|
|
|
} |
|
} |
|
|
|
function plot() |
|
{ |
|
X= document.getElementById("X"); |
|
Y= document.getElementById("Y"); |
|
colX=X.value ||X.placeholder; |
|
colY=Y.value ||Y.placeholder; |
|
colX-=1; |
|
colY-=1; |
|
var s=""; |
|
s+="X = "+sheet_header[colX] +"<br>"; |
|
s+="Y = "+sheet_header[colY] +"<br>"; |
|
$("#console").html(s) |
|
var data=[], |
|
shapes = ['circle', 'cross', 'triangle-up', 'triangle-down', 'diamond', 'square' ], |
|
grp2id = {}; |
|
|
|
for (i0 in sheet) |
|
{ var d=sheet[i0] |
|
t_gName = String(d[colGroup]); //}else{ t_gName = String(0); } //Group IDs from column 3 |
|
if( grp2id[ t_gName ] == undefined ){ |
|
grp2id[ t_gName ] = data.length; |
|
data.push({ |
|
key: 'Group ' + t_gName, |
|
values: [] |
|
}); |
|
} |
|
i = grp2id[ t_gName ]; |
|
data[i].values.push({ |
|
x: parseFloat(d[colX]) |
|
, y: parseFloat(d[colY]) |
|
//, size: parseFloat(d[colSize]) //Math.random() //Configure the size of each scatter point |
|
, shape: shapes[i % 6]//(Math.random() > 0.95) ? shapes[j % 6] : "circle" //Configure the shape of each scatter point. |
|
}); |
|
} |
|
renderScatter(data); |
|
|
|
} |
|
|
|
function clearScatter() |
|
{ |
|
d3.select('#scatter').text('') |
|
} |
|
function handleFileSelect(evt) { |
|
var files = evt.target.files; // FileList object |
|
// Only process image files. |
|
var file=files[0] |
|
var reader = new FileReader(); |
|
s=$("#file_sep").val(); |
|
var sep="\t"; |
|
if (s=="csv") {sep=","}; |
|
reader.onloadend = (function(evt) { |
|
parseCSV(evt.target.result,"\n",sep); |
|
}); |
|
|
|
reader.readAsText(file) |
|
} |
|
function httpGet(theUrl) |
|
{ |
|
var xmlHttp = null; |
|
xmlHttp = new XMLHttpRequest(); |
|
xmlHttp.open( "GET", theUrl, false ); |
|
xmlHttp.send( null ); |
|
return xmlHttp.responseText; |
|
} |
|
|
|
function readUrl() |
|
{ |
|
var url=document.getElementById("data-url").value || document.getElementById("data-url").placeholder;; |
|
parseCSV(httpGet(url),"\n",","); |
|
//render(d3.csv(url)); |
|
|
|
} |
|
function readInputText() |
|
{ |
|
parseCSV(document.getElementById("input-text").value,"\n",","); |
|
} |
|
function clearInputText() |
|
{ |
|
document.getElementById("input-text").value="" |
|
} |
|
document.getElementById('file').addEventListener('change', handleFileSelect, false); |
|
</script> |
|
</body> |
|
</html> |