Javascript 如何将字符串作为参数传递给d3.csv()函数,并进一步在d3.js中绘制热图?
我想使用d3.js使用csv数据绘制直方图。我指的是林克。在该教程中,他们使用本地文件csv数据,但在我的应用程序中,数据来自web套接字,因此,我认为我可以收集这些数据并创建一个字符串作为csv数据,并将该字符串传递给d3.csv或d3.csv.parse函数。同时,由于我的数据没有在网页上正确填充,我有一点被卡住了 Web套接字服务器正在逐行发送此文件数据:Javascript 如何将字符串作为参数传递给d3.csv()函数,并进一步在d3.js中绘制热图?,javascript,date,csv,d3.js,Javascript,Date,Csv,D3.js,我想使用d3.js使用csv数据绘制直方图。我指的是林克。在该教程中,他们使用本地文件csv数据,但在我的应用程序中,数据来自web套接字,因此,我认为我可以收集这些数据并创建一个字符串作为csv数据,并将该字符串传递给d3.csv或d3.csv.parse函数。同时,由于我的数据没有在网页上正确填充,我有一点被卡住了 Web套接字服务器正在逐行发送此文件数据: date,bucket,count 1468332421041000,40000,2 1468332421102000,30000,6
date,bucket,count
1468332421041000,40000,2
1468332421102000,30000,6
1468332421103000,30000,8
1468332421116000,30000,10
1468332421117000,30000,2
1468332421139000,30000,2
1468332421155000,50000,2
1468332421158000,40000,2,
1468332421164000,30000,12
这个日期是纪元,因此,在客户端,我需要将其转换为人类可读的格式
客户端html程序-
<!DOCTYPE html>
<html>
<meta charset="utf-8">
<style>
.label {
font-weight: bold;
}
.tile {
shape-rendering: crispEdges;
}
.axis path,
.axis line {
fill: none;
stroke: #000;
shape-rendering: crispEdges;
}
</style>
<head>
<script src="http://d3js.org/d3.v3.min.js" charset="utf-8"></script>
</head>
<body>
<p id="demo"></p>
<script>
var csv_data = '';
var flag = true;
if ("WebSocket" in window)
{
// Let us open a web socket
var ws = new WebSocket("ws://localhost:3333");
ws.onopen = function()
{
};
ws.onmessage = function (evt)
{
var received_msg = evt.data;
if(flag)
{
csv_data = evt.data +"\n"
}
else
{
var b = received_msg.substring(17)
var a = received_msg.substring(0,13)
var dateVal = a;
var date = new Date(parseFloat(dateVal));
csv_data += date.getFullYear() + "-" +
(date.getMonth() + 1) + "-" +
date.getDate() + " " +
date.getHours() + ":" +
date.getMinutes() + ":" +
date.getSeconds() +" "+
date.getMilliseconds() +","+b+"\n"
}
flag = false;
readCsv();
};
ws.onclose = function()
{
alert("Connection is closed...");
};
}
else
{
// The browser doesn't support WebSocket
alert("WebSocket NOT supported by your Browser!");
}
var margin = {top: 20, right: 90, bottom: 30, left: 50},
width = 960 - margin.left - margin.right,
height = 500 - margin.top - margin.bottom;
var parseDate = d3.time.format("%Y-%m-%d").parse,
formatDate = d3.time.format("%b %d");
var x = d3.time.scale().range([0, width]),
y = d3.scale.linear().range([height, 0]),
z = d3.scale.linear().range(["white", "steelblue"]);
// The size of the buckets in the CSV data file.
// This could be inferred from the data if it weren't sparse.
var xStep = 864e5,
yStep = 100;
var svg = d3.select("body").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 + ")");
function readCsv()
{
d3.csv(csv_data, function(buckets) {
// Coerce the CSV data to the appropriate types.
buckets.forEach(function(d) {
d.date = parseDate(d.date);
d.bucket = +d.bucket;
d.count = +d.count;
});
// Compute the scale domains.
x.domain(d3.extent(buckets, function(d) { return d.date; }));
y.domain(d3.extent(buckets, function(d) { return d.bucket; }));
z.domain([0, d3.max(buckets, function(d) { return d.count; })]);
// Extend the x- and y-domain to fit the last bucket.
// For example, the y-bucket 3200 corresponds to values [3200, 3300].
x.domain([x.domain()[0], +x.domain()[1] + xStep]);
y.domain([y.domain()[0], y.domain()[1] + yStep]);
// Display the tiles for each non-zero bucket.
// See http://bl.ocks.org/3074470 for an alternative implementation.
svg.selectAll(".tile")
.data(buckets)
.enter().append("rect")
.attr("class", "tile")
.attr("x", function(d) { return x(d.date); })
.attr("y", function(d) { return y(d.bucket + yStep); })
.attr("width", x(xStep) - x(0))
.attr("height", y(0) - y(yStep))
.style("fill", function(d) { return z(d.count); });
// Add a legend for the color values.
var legend = svg.selectAll(".legend")
.data(z.ticks(6).slice(1).reverse())
.enter().append("g")
.attr("class", "legend")
.attr("transform", function(d, i) { return "translate(" + (width + 20) + "," + (20 + i * 20) + ")"; });
legend.append("rect")
.attr("width", 20)
.attr("height", 20)
.style("fill", z);
legend.append("text")
.attr("x", 26)
.attr("y", 10)
.attr("dy", ".35em")
.text(String);
svg.append("text")
.attr("class", "label")
.attr("x", width + 20)
.attr("y", 10)
.attr("dy", ".35em")
.text("Count");
// Add an x-axis with label.
svg.append("g")
.attr("class", "x axis")
.attr("transform", "translate(0," + height + ")")
.call(d3.svg.axis().scale(x).ticks(d3.time.days).tickFormat(formatDate).orient("bottom"))
.append("text")
.attr("class", "label")
.attr("x", width)
.attr("y", -6)
.attr("text-anchor", "end")
.text("Date");
// Add a y-axis with label.
svg.append("g")
.attr("class", "y axis")
.call(d3.svg.axis().scale(y).orient("left"))
.append("text")
.attr("class", "label")
.attr("y", 6)
.attr("dy", ".71em")
.attr("text-anchor", "end")
.attr("transform", "rotate(-90)")
.text("Latency");
});
}
</script>
</body>
</html>
.标签{
字体大小:粗体;
}
.瓷砖{
形状渲染:边缘清晰;
}
.轴线路径,
.轴线{
填充:无;
行程:#000;
形状渲染:边缘清晰;
}
var csv_数据=“”;
var标志=真;
如果(“窗口中的WebSocket”)
{
//让我们打开一个web套接字
var ws=newwebsocket(“ws://localhost:3333”);
ws.onopen=函数()
{
};
ws.onmessage=函数(evt)
{
收到的var_msg=evt.data;
国际单项体育联合会(旗)
{
csv_data=evt.data+“\n”
}
其他的
{
var b=收到的消息子串(17)
var a=收到的消息子串(0,13)
var-dateVal=a;
变量日期=新日期(parseFloat(dateVal));
csv_data+=date.getFullYear()+“-”+
(date.getMonth()+1)+“-”+
date.getDate()+“”+
date.getHours()+“:”+
date.getMinutes()+“:”+
date.getSeconds()+“”+
date.getmillizes()+“,+b+”\n
}
flag=false;
readCsv();
};
ws.onclose=function()
{
警报(“连接已关闭…”);
};
}
其他的
{
//浏览器不支持WebSocket
警报(“您的浏览器不支持WebSocket!”);
}
var margin={顶部:20,右侧:90,底部:30,左侧:50},
宽度=960-margin.left-margin.right,
高度=500-margin.top-margin.bottom;
var parseDate=d3.time.format(“%Y-%m-%d”).parse,
formatDate=d3.time.format(“%b%d”);
var x=d3.time.scale().range([0,width]),
y=d3.刻度.线性().范围([高度,0]),
z=d3.刻度.线性().范围([“白色”,“钢蓝色]);
//CSV数据文件中存储桶的大小。
//如果数据不是稀疏的,就可以从中推断出来。
var xStep=864e5,
yStep=100;
var svg=d3.选择(“正文”).追加(“svg”)
.attr(“宽度”,宽度+边距。左侧+边距。右侧)
.attr(“高度”,高度+边距。顶部+边距。底部)
.附加(“g”)
.attr(“转换”、“平移”(+margin.left+)、“+margin.top+”);
函数readCsv()
{
d3.csv(csv_数据、函数(桶){
//将CSV数据强制为适当的类型。
bucket.forEach(函数(d){
d、 日期=解析日期(d.date);
d、 铲斗=+d.铲斗;
d、 计数=+d.count;
});
//计算比例域。
x、 域(d3.extent(bucket,函数(d){返回d.date;}));
y、 域(d3.extent(bucket,函数(d){returnd.bucket;}));
z、 域([0,d3.max(bucket,函数(d){返回d.count;})];
//扩展x域和y域以适合最后一个桶。
//例如,y形铲斗3200对应于值[32003300]。
x、 域([x.domain()[0],+x.domain()[1]+xStep]);
y、 域([y.domain()[0],y.domain()[1]+yStep]);
//显示每个非零桶的平铺。
//看http://bl.ocks.org/3074470 用于替代实现。
svg.selectAll(“.tile”)
.数据(桶)
.enter().append(“rect”)
.attr(“类别”、“瓷砖”)
.attr(“x”,函数(d){返回x(d.date);})
.attr(“y”,函数(d){返回y(d.bucket+yStep);})
.attr(“宽度”,x(x步)-x(0))
.attr(“高度”,y(0)-y(yStep))
.style(“fill”,函数(d){返回z(d.count);});
//为颜色值添加图例。
var legend=svg.selectAll(“.legend”)
.data(z.ticks(6.slice(1.reverse())
.enter().append(“g”)
.attr(“类”、“图例”)
.attr(“transform”,函数(d,i){return“translate”(+(width+20)+)”,“+(20+i*20)+”);};
图例。追加(“rect”)
.attr(“宽度”,20)
.attr(“高度”,20)
.样式(“填充”,z);
图例。追加(“文本”)
.attr(“x”,26)
.attr(“y”,10)
.attr(“dy”,“.35em”)
.文本(字符串);
svg.append(“文本”)
.attr(“类别”、“标签”)
.attr(“x”,宽度+20)
.attr(“y”,10)
.attr(“dy”,“.35em”)
.文本(“计数”);
//添加带有标签的x轴。
svg.append(“g”)
.attr(“类”、“x轴”)
.attr(“变换”、“平移(0)”、“高度+”)
.call(d3.svg.axis().scale(x)、ticks(d3.time.days)、tickFormat(formatDate)、orient(“底部”))
.append(“文本”)
.attr(“类别”、“标签”)
.attr(“x”,宽度)
.attr(“y”,-6)
.attr(“文本锚定”、“结束”)
.文本(“日期”);
//添加带有标签的y轴。
svg.append(“g”)
.attr(“类”、“y轴”)
.call(d3.svg.axis().scale(y).orient(“left”))
.append(“文本”)
.attr(“类别”、“标签”)
.attr(“y”,6)
.attr(“dy”,“.71em”)
.attr(“文本锚定”、“结束”)
.attr(“变换”、“旋转(-90)”)
.文本(“延迟”);
});
}
我想知道如何将要解析的字符串作为csv数据提供给d3.c