我试图重新创建GitHub上呈现的交互式流图,我从Web服务获得的数据。我使用相同的代码从GitHub上的样本来生成图形,但是我得到路径元素的MNaN和NaN坐标(准确地说,它们的d属性)。结果是一个空的图形,其中只绘制了x轴和y轴,而没有其他内容。
<!DOCTYPE html>
<meta charset="utf-8">
<style>
body {
font: 10px sans-serif;
}
.chart {
background: #fff;
}
p {
font: 12px helvetica;
}
.axis path, .axis line {
fill: none;
stroke: #000;
stroke-width: 2px;
shape-rendering: crispEdges;
}
button {
position: absolute;
right: 50px;
top: 10px;
}
</style>
<body>
<script src="http://d3js.org/d3.v3.min.js" charset="utf-8"></script>
<script src="//code.jquery.com/jquery-1.11.0.min.js"></script>
<div class="chart"></div>
</body>
<script>
var dataset;
function getData() {
$.ajax({
type: "POST",
contentType: "application/json; charset=utf-8",
url: "VoxPoliticoService.asmx/ComparePoliticians",
data: {},
dataType: "json",
success: function (res) {
dataset = res.d;
renderChart();
}
});
}
jQuery(document).ready(function ($) {
getData();
});
function renderChart()
{
color = "blue";
var datearray = [];
var colorrange = [];
var data = dataset;
if (color == "blue") {
colorrange = ["#045A8D", "#2B8CBE", "#74A9CF", "#A6BDDB", "#D0D1E6", "#F1EEF6"];
}
else if (color == "pink") {
colorrange = ["#980043", "#DD1C77", "#DF65B0", "#C994C7", "#D4B9DA", "#F1EEF6"];
}
else if (color == "orange") {
colorrange = ["#B30000", "#E34A33", "#FC8D59", "#FDBB84", "#FDD49E", "#FEF0D9"];
}
strokecolor = colorrange[0];
var format = d3.time.format("%m/%d/%y");
var margin = {top: 20, right: 40, bottom: 30, left: 30};
var width = document.body.clientWidth - margin.left - margin.right;
var height = 400 - margin.top - margin.bottom;
var tooltip = d3.select("body")
.append("div")
.attr("class", "remove")
.style("position", "absolute")
.style("z-index", "20")
.style("visibility", "hidden")
.style("top", "30px")
.style("left", "55px");
var x = d3.time.scale()
.range([0, width]);
var y = d3.scale.linear()
.range([height-10, 0]);
var z = d3.scale.ordinal()
.range(colorrange);
var xAxis = d3.svg.axis()
.scale(x)
.orient("bottom")
.ticks(d3.time.weeks);
var yAxis = d3.svg.axis()
.scale(y);
var yAxisr = d3.svg.axis()
.scale(y);
var stack = d3.layout.stack()
.offset("silhouette")
.values(function(d) { return d.values; })
.x(function(d) { return d.date; })
.y(function(d) { return d.value; });
var nest = d3.nest()
.key(function(d) { return d.key; });
var area = d3.svg.area()
.interpolate("cardinal")
.x(function(d) { return x(d.date); })
.y0(function(d) { return y(d.y0); })
.y1(function(d) { return y(d.y0 + d.y); });
var svg = d3.select(".chart").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 + ")");
data.forEach(function(d) {
d.date = format.parse(d.date);
d.value = +d.value;
});
var layers = stack(nest.entries(data));
x.domain(d3.extent(data, function(d) { return d.date; }));
y.domain([0, d3.max(data, function(d) { return d.y0 + d.y; })]);
svg.selectAll(".layer")
.data(layers)
.enter().append("path")
.attr("class", "layer")
.attr("d", function(d) { return area(d.values); })
.style("fill", function(d, i) { return z(i); });
svg.append("g")
.attr("class", "x axis")
.attr("transform", "translate(0," + height + ")")
.call(xAxis);
svg.append("g")
.attr("class", "y axis")
.attr("transform", "translate(" + width + ", 0)")
.call(yAxis.orient("right"));
svg.append("g")
.attr("class", "y axis")
.call(yAxis.orient("left"));
svg.selectAll(".layer")
.attr("opacity", 1)
.on("mouseover", function(d, i) {
svg.selectAll(".layer").transition()
.duration(250)
.attr("opacity", function(d, j) {
return j != i ? 0.6 : 1;
})})
.on("mousemove", function(d, i) {
mousex = d3.mouse(this);
mousex = mousex[0];
var invertedx = x.invert(mousex);
invertedx = invertedx.getMonth() + invertedx.getDate();
var selected = (d.values);
for (var k = 0; k < selected.length; k++) {
datearray[k] = selected[k].date
datearray[k] = datearray[k].getMonth() + datearray[k].getDate();
}
mousedate = datearray.indexOf(invertedx);
pro = d.values[mousedate].value;
d3.select(this)
.classed("hover", true)
.attr("stroke", strokecolor)
.attr("stroke-width", "0.5px"),
tooltip.html( "<p>" + d.key + "<br>" + pro + "</p>" ).style("visibility", "visible");
})
.on("mouseout", function(d, i) {
svg.selectAll(".layer")
.transition()
.duration(250)
.attr("opacity", "1");
d3.select(this)
.classed("hover", false)
.attr("stroke-width", "0px"), tooltip.html( "<p>" + d.key + "<br>" + pro + "</p>" ).style("visibility", "hidden");
})
var vertical = d3.select(".chart")
.append("div")
.attr("class", "remove")
.style("position", "absolute")
.style("z-index", "19")
.style("width", "1px")
.style("height", "380px")
.style("top", "10px")
.style("bottom", "30px")
.style("left", "0px")
.style("background", "#fff");
d3.select(".chart")
.on("mousemove", function(){
mousex = d3.mouse(this);
mousex = mousex[0] + 5;
vertical.style("left", mousex + "px" )})
.on("mouseover", function(){
mousex = d3.mouse(this);
mousex = mousex[0] + 5;
vertical.style("left", mousex + "px")});
}
</script>
为方便起见,下面是生成数据集的web服务代码(c#):
[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public PoliticianWords[] ComparePoliticians()
{
List<PoliticianWords> ret = new List<PoliticianWords>();
Random r = new Random();
string[] politicians = { "A B", "C D", "E F", "G H" };
Random gen = new Random();
List<DateTime> dates = new List<DateTime>();
for (int i = 0; i < 20; i++)
{
DateTime start = new DateTime(1991, 9, 11);
int range = (DateTime.Today - start).Days;
dates.Add(start.AddDays(gen.Next(range)));
}
dates.Sort();
foreach (string politician in politicians)
{
foreach(DateTime d in dates)
{
PoliticianWords pw = new PoliticianWords();
pw.key = politician;
pw.value = gen.Next(0, 100);
pw.date = d.Day.ToString() + "/" + d.Month.ToString() + "/" + d.Year.ToString();
ret.Add(pw);
}
}
return ret.ToArray();
}
编辑1:我忘了把数据集的成员定义为c#类。我知道这个问题可能已经被淹没在一堆其他问题中了,但是任何帮助都将是非常感谢的:
public class PoliticianWords
{
public string key { get; set; }
public int value { get; set; }
public string date { get; set; }
}
编辑2:我想出了一个例子数据集,原来是工作。而不是从一个web服务随机生成它,我将它存储在一个。csv文件中,并获得它类似于GitHub中给出的代码。
键,值、日期100年一个B, 01/08/1301/09/13 B, 15日一个B, 35岁,01/10/13现年38岁的B 01/11/1301/12/13 B, 22日01/13/13 B, 16日35岁的C D 01/08/1336岁的C D 01/09/1337岁的C D 01/10/1322岁的C D 01/11/1324岁的C D 01/12/1301/13/13 C D, 26日01/08/13 F E, 21日01/09/13 F E, 25日01/10/13 F E, 27日01/11/13 F E, 23日01/12/13 F E, 24日01/13/13 F E, 21日G H 10 01/08/1301/09/13 G H, 15日35岁的G H 01/10/13现年38岁的G H 01/11/1301/12/13 G H, 22日G H, 16日01/13/13
这里还有一个不工作的例子,一个从webservice获得的随机生成的数据集:
键,值,日期A B,1,6/5/1992 A B,4,11/29/1992 A B,2,11/3/1993 AB、7,1995年8月1日;B、9,1996年8月1日;B、15,1997年8月1日;d、1,1992年6月5日D、2、1992年11月29日c、5、1993年11月3日c、10、1995年8月1日c、12、1996年8月1日D, 1997,8月1日;e, 1992,6月5日;e, 1992,11月29日;e, 1993,11月3日;eF, 6,1995年8月1日;F, 10,1996年8月1日;F, 5,1997年8月1日;g, 1,1992年6月5日H、17、1992年11月29日g H、16、1993年11月3日g H、6、1995年8月1日g H、10、1996年8月1日g H20岁的H 8/1/1997
我找到了一个解决方案,结果证明我的错误是一个蹩脚的错误:日期的格式。正确的格式应该包含所有三个组成部分(日、月、年),各由两位数字(01/08/13、01/09/13)表示。相反,我的web服务生成的日期格式允许用一个数字表示月份和日期,而年份包含所有数字(1/8/2013,1/9/2013)。我已经修改了将日期转换为字符串的部分代码(webservice),并将其转换为以下形式:
if (d.Day < 10)
pw.date = "0" + d.Day.ToString();
else
pw.date = d.Day.ToString();
if (d.Month < 10)
pw.date += "/0" + d.Month.ToString();
else
pw.date += "/" + d.Month.ToString();
int year = d.Year % 100;
if (year < 10)
pw.date += "/0" + year.ToString();
else
pw.date += "/" + year.ToString();