我有正在保存到内存中的数据。我正在寻找一种方法来绘制保存到内存中的数据。以下是阅读和尝试绘制文件图形的示例脚本。
function Read() {
var myfile=document.getElementById("csvFileInput");
handleFiles(myfile);
}
function handleFiles(myfile) {
// Check for the various File API support.
if (window.FileReader) {
// FileReader are supported.
var files = myfile.files[0];
getAsText(files);
} else {
alert('FileReader are not supported in this browser.');
}
}
function getAsText(files) {
var reader = new FileReader();
// Read file into memory as UTF-8
reader.readAsText(files);
// Handle errors load
reader.onload = loadHandler;
reader.onerror = errorHandler;
}
function loadHandler(event) {
var csv = event.target.result;
processData(csv);
}
processData(csv) {
var allTextLines = csv.split(/\r\n|\n/);
var lines = [];
for (var i=0; i<allTextLines.length; i++) {
var data = allTextLines[i].split(',');
var tarr = [];
for (var j=0; j<data.length; j++) {
tarr.push(data[j]);
}
lines.push(tarr);
}
console.log(lines);
alert("graphing");
Graph(allTextLines);
}
function errorHandler(evt) {
if(evt.target.error.name == "NotReadableError") {
alert("Cannot read file !");
}
}
function Graph(files) {
var x = d3.scale.linear()
.range([0, width]);
var y = d3.scale.linear()
.range([height, 0]);
var xAxis = d3.svg.axis()
.scale(x)
.orient("bottom");
var yAxis = d3.svg.axis()
.scale(y)
.orient("left");
var line1 = d3.svg.line()
.x(function(d) { return x(d.date); })
.y(function(d) { return y(d.A); });
var line2 = d3.svg.line()
.x(function(d) { return x(d.date); })
.y(function(d) { return y(d.B); });
var svg1 = d3.select("#ecgplot").append("svg")
.attr("width", width + margin.left + margin.right)
.attr("height", height + margin.top + margin.bottom)
.append("g")
.attr("transform", "translate(" + margin.left + "," + margin.top + ")");
var data = files;
data.forEach(function(d) {
d.date = +d.tm;
d.A = +d.Adata;
d.B = +d.Bdata;
});
x.domain(d3.extent(data, function(d) { return d.date; }));
y.domain(d3.extent(data, function(d) { return d.A; }));
svg1.append("g")
.attr("class", "x axis")
.attr("transform", "translate(0," + height + ")")
.call(xAxis)
.append("text")
.attr("transform", "rotate(0)")
.attr("y", 28)
.attr("x", 450)
.attr("dx", ".71em")
.style("text-anchor", "end")
.text("Time (in second)");
svg1.append("g")
.attr("class", "y axis")
.call(yAxis)
.append("text")
.attr("transform", "rotate(-90)")
.attr("y", -35)
.attr("x", -35)
.attr("dy", ".71em")
.style("text-anchor", "end")
.text("A");
}
这是我在显示时保存到内存中的数据。
"tm","Adata","Bdata",0,-0.14,4.781,0.003,-0.153,4.781,0.006,-0.151,4.831,0.008,-0.137,4.831
我认为通过将数据保存到内存的方式,我必须将数据解析为5行3列,但我不确定如何执行此操作然后将数据读入d3可以注册。