Javascript 使用关联数组的D3日历视图
我想创建一个日历视图,如以下示例所示: 实际上,我正在尝试修改它 我有一个这样的关联数组:Javascript 使用关联数组的D3日历视图,javascript,d3.js,calendar,associative-array,Javascript,D3.js,Calendar,Associative Array,我想创建一个日历视图,如以下示例所示: 实际上,我正在尝试修改它 我有一个这样的关联数组:#AdminCourt[[“2012-10-02”,2],“2012-10-09”,2],“2012-10-16”,1]。[2012-10-02”,2],“2012-10-09”,2],“2012-10-12”,2],“2012-10-16”,2]。 我试着这样填写日历: BigWordsDates2.map(function(d) { return {
#AdminCourt[[“2012-10-02”,2],“2012-10-09”,2],“2012-10-16”,1]。[2012-10-02”,2],“2012-10-09”,2],“2012-10-12”,2],“2012-10-16”,2]。
我试着这样填写日历:
BigWordsDates2.map(function(d) {
return {
date: d[0],
close: d[1]
};
var data = d3.nest()
.key(function(d) { return d.Date; })
.rollup(function(d) { return (d.Close - 0); });
rect.filter(function(d) { return d in data; })
.attr("class", function(d) { return "day " + color(data[d]); })
.select("title")
.text(function(d) { return d + ": " + percent(data[d]); });
});
我知道我没有在数组中循环,我也不知道我是如何尝试每个数组的,但似乎我没有正确地得到它
以下是我需要你帮助的东西:)
var w = 760,
h = 530;
var cloudwidth = 700, cloudheight=500;
var FunctionCount=0;
var BigWord;
var SmallWord;
var tweets = <?php echo json_encode($Row_Words_Repeated_Relation); ?>;
//var tweets = JSON.parse(TweetsAnalized);
var tweetscounts = JSON.parse('<?php echo $Array_OfCounters_to_json ?>');
var BigWordsDates2 = <?php echo json_encode($Array_OfDates); ?>;
//var BigWordsDates2 = JSON.parse(BigWordsDates);
var OriginalTweets = JSON.parse('<?php echo mysql_real_escape_string($OriginalTweets_to_json) ?>');
var width = 960,
height = 136,
cellSize = 17; // cell size
var day = d3.time.format("%w"),
week = d3.time.format("%U"),
percent = d3.format(".1%"),
format = d3.time.format("%Y-%m-%d");
var color = d3.scale.quantize()
.domain([-.05, .05])
.range(d3.range(11).map(function(d) { return "q" + d + "-11"; }));
var svg = d3.select("body").selectAll("svg")
.data(d3.range(2005, 2015))
.enter().append("svg")
.attr("width", width)
.attr("height", height)
.attr("class", "RdYlGn")
.append("g")
.attr("transform", "translate(" + ((width - cellSize * 53) / 2) + "," + (height - cellSize * 7 - 1) + ")");
svg.append("text")
.attr("transform", "translate(-6," + cellSize * 3.5 + ")rotate(-90)")
.style("text-anchor", "middle")
.text(function(d) { return d; });
var rect = svg.selectAll(".day")
.data(function(d) { return d3.time.days(new Date(d, 0, 1), new Date(d + 1, 0, 1)); })
.enter().append("rect")
.attr("class", "day")
.attr("width", cellSize)
.attr("height", cellSize)
.attr("x", function(d) { return week(d) * cellSize; })
.attr("y", function(d) { return day(d) * cellSize; })
.datum(format);
rect.append("title")
.text(function(d) { return d; });
svg.selectAll(".month")
.data(function(d) { return d3.time.months(new Date(d, 0, 1), new Date(d + 1, 0, 1)); })
.enter().append("path")
.attr("class", "month")
.attr("d", monthPath);
/*d3.csv("dji.csv", function(error, csv) {
var data = d3.nest()
.key(function(d) { return d.Date; })
.rollup(function(d) { return (d[0].Close - d[0].Open) / d[0].Open; })
.map(csv);
rect.filter(function(d) { return d in data; })
.attr("class", function(d) { return "day " + color(data[d]); })
.select("title")
.text(function(d) { return d + ": " + percent(data[d]); });
});*/
BigWordsDates2["#Tahrir"].map(function(d) {
return {
date: d[0],
close: d[1]
};
var data = d3.nest()
.key(function(d) { return d.Date; })
.rollup(function(d) { return (d.Close - 0); });
rect.filter(function(d) { return d in data; })
.attr("class", function(d) { return "day " + color(data[d]); })
.select("title")
.text(function(d) { return d + ": " + percent(data[d]); });
});
function monthPath(t0) {
var t1 = new Date(t0.getFullYear(), t0.getMonth() + 1, 0),
d0 = +day(t0), w0 = +week(t0),
d1 = +day(t1), w1 = +week(t1);
return "M" + (w0 + 1) * cellSize + "," + d0 * cellSize
+ "H" + w0 * cellSize + "V" + 7 * cellSize
+ "H" + w1 * cellSize + "V" + (d1 + 1) * cellSize
+ "H" + (w1 + 1) * cellSize + "V" + 0
+ "H" + (w0 + 1) * cellSize + "Z";
}
d3.select(self.frameElement).style("height", "2910px");
编辑2:我在上面做了一些工作,这就是我所能想到的,我真的需要帮助,不知道为什么这些值没有附加在日历上
var width = 960,
height = 136,
cellSize = 17; // cell size
var day = d3.time.format("%w"),
week = d3.time.format("%U"),
percent = d3.format(".1%"),
format = d3.time.format("%Y-%m-%d");
var color = d3.scale.quantize()
.domain([-.05, .05])
.range(d3.range(11).map(function(d) { return "q" + d + "-11"; }));
var svg = d3.select("body").selectAll("svg")
.data(d3.range(2005, 2015))
.enter().append("svg")
.attr("width", width)
.attr("height", height)
.attr("class", "RdYlGn")
.append("g")
.attr("transform", "translate(" + ((width - cellSize * 53) / 2) + "," + (height - cellSize * 7 - 1) + ")");
svg.append("text")
.attr("transform", "translate(-6," + cellSize * 3.5 + ")rotate(-90)")
.style("text-anchor", "middle")
.text(function(d) { return d; });
var rect = svg.selectAll(".day")
.data(function(d) { return d3.time.days(new Date(d, 0, 1), new Date(d + 1, 0, 1)); })
.enter().append("rect")
.attr("class", "day")
.attr("width", cellSize)
.attr("height", cellSize)
.attr("x", function(d) { return week(d) * cellSize; })
.attr("y", function(d) { return day(d) * cellSize; })
.datum(format);
rect.append("title")
.text(function(d) { return d; });
svg.selectAll(".month")
.data(function(d) { return d3.time.months(new Date(d, 0, 1), new Date(d + 1, 0, 1)); })
.enter().append("path")
.attr("class", "month")
.attr("d", monthPath);
/*d3.csv("dji.csv", function(error, csv) {
var data = d3.nest()
.key(function(d) { return d.Date; })
.rollup(function(d) { return (d[0].Close - d[0].Open) / d[0].Open; })
.map(csv);
rect.filter(function(d) { return d in data; })
.attr("class", function(d) { return "day " + color(data[d]); })
.select("title")
.text(function(d) { return d + ": " + percent(data[d]); });
});*/
d3.entries(BigWordsDates2).map(function(d) {
for each (var i in BigWordsDates2[d.key]){
/*var count =i;
return {
Date: i[0],
Close: i[1]
};*/
rect.filter(function(i) { return i in BigWordsDates2; })
.attr("class", function(i) { return "day " + color(i[0]); })
.select("title")
.text(function(i) { return d.key + ": " + percent(i[1]); });
};
});
function monthPath(t0) {
var t1 = new Date(t0.getFullYear(), t0.getMonth() + 1, 0),
d0 = +day(t0), w0 = +week(t0),
d1 = +day(t1), w1 = +week(t1);
return "M" + (w0 + 1) * cellSize + "," + d0 * cellSize
+ "H" + w0 * cellSize + "V" + 7 * cellSize
+ "H" + w1 * cellSize + "V" + (d1 + 1) * cellSize
+ "H" + (w1 + 1) * cellSize + "V" + 0
+ "H" + (w0 + 1) * cellSize + "Z";
}
d3.select(self.frameElement).style("height", "2910px");
我想我很接近。任何帮助都将不胜感激
我制作了一个JSFIDLE模板:
编辑3:
我已经解决了步骤1和步骤2,下面是JSFIDLE链接:
现在正试图找到一种在同一单元格中添加多值的解决方法
例如,如果我在数组中有两个日期相同的值,我想在右边的单元格中添加并查看它们。但是,代码现在所做的是,如果有两个值具有相同的日期值,则最后一个值将覆盖第一个值
任何帮助都可以,提前谢谢。这是解决1号和2号问题的方法,适用于那些与我以前遇到的问题类似的人。希望这会有帮助 这个数组看起来像:
BigWordsDates2={“#塔利尔”:[“2012-10-12”,20],“2012-10-13”,1],“2012-10-19”,15],“#埃及]:[[“2012-10-01”,3],“2012-10-03”,2],“2012-10-04”,3],“2012-10-07”,1],“2012-10-10-10”,1],“2012-10-13”,2],“2012-10-14”,1],“2012-10-15”,1],“2012-10-16”,1],“2012-10-10-10],“4],“2012-10-10”,5]代码>
按如下方式保存目标数组值:var tahrir=BigWordsDates2['#tahrir']
然后用它覆盖CSV数据。您可以在下面的JSFIDLE中找到带有解决方案的示例
您有一个语法错误:for each
应替换为for
。您可以将一个单击处理程序添加到任何元素中,如.on(“click”,function(){console.log(arguments)})
。现在我没有时间进一步讨论,也许我改天再来。好吧,我让它工作了,但我在向一个单元格添加多值(可能追加)时遇到了问题。这可能吗?!
var width = 960,
height = 136,
cellSize = 17; // cell size
var day = d3.time.format("%w"),
week = d3.time.format("%U"),
percent = d3.format(".1%"),
format = d3.time.format("%Y-%m-%d");
var color = d3.scale.quantize()
.domain([-.05, .05])
.range(d3.range(11).map(function(d) { return "q" + d + "-11"; }));
var svg = d3.select("body").selectAll("svg")
.data(d3.range(2005, 2015))
.enter().append("svg")
.attr("width", width)
.attr("height", height)
.attr("class", "RdYlGn")
.append("g")
.attr("transform", "translate(" + ((width - cellSize * 53) / 2) + "," + (height - cellSize * 7 - 1) + ")");
svg.append("text")
.attr("transform", "translate(-6," + cellSize * 3.5 + ")rotate(-90)")
.style("text-anchor", "middle")
.text(function(d) { return d; });
var rect = svg.selectAll(".day")
.data(function(d) { return d3.time.days(new Date(d, 0, 1), new Date(d + 1, 0, 1)); })
.enter().append("rect")
.attr("class", "day")
.attr("width", cellSize)
.attr("height", cellSize)
.attr("x", function(d) { return week(d) * cellSize; })
.attr("y", function(d) { return day(d) * cellSize; })
.datum(format);
rect.append("title")
.text(function(d) { return d; });
svg.selectAll(".month")
.data(function(d) { return d3.time.months(new Date(d, 0, 1), new Date(d + 1, 0, 1)); })
.enter().append("path")
.attr("class", "month")
.attr("d", monthPath);
/*d3.csv("dji.csv", function(error, csv) {
var data = d3.nest()
.key(function(d) { return d.Date; })
.rollup(function(d) { return (d[0].Close - d[0].Open) / d[0].Open; })
.map(csv);
rect.filter(function(d) { return d in data; })
.attr("class", function(d) { return "day " + color(data[d]); })
.select("title")
.text(function(d) { return d + ": " + percent(data[d]); });
});*/
d3.entries(BigWordsDates2).map(function(d) {
for each (var i in BigWordsDates2[d.key]){
/*var count =i;
return {
Date: i[0],
Close: i[1]
};*/
rect.filter(function(i) { return i in BigWordsDates2; })
.attr("class", function(i) { return "day " + color(i[0]); })
.select("title")
.text(function(i) { return d.key + ": " + percent(i[1]); });
};
});
function monthPath(t0) {
var t1 = new Date(t0.getFullYear(), t0.getMonth() + 1, 0),
d0 = +day(t0), w0 = +week(t0),
d1 = +day(t1), w1 = +week(t1);
return "M" + (w0 + 1) * cellSize + "," + d0 * cellSize
+ "H" + w0 * cellSize + "V" + 7 * cellSize
+ "H" + w1 * cellSize + "V" + (d1 + 1) * cellSize
+ "H" + (w1 + 1) * cellSize + "V" + 0
+ "H" + (w0 + 1) * cellSize + "Z";
}
d3.select(self.frameElement).style("height", "2910px");