【发布时间】:2012-12-15 13:56:38
【问题描述】:
我想创建一个日历视图像这个例子:http://bl.ocks.org/4063318:
其实我正在尝试修改它。
我有一个这样的关联数组:#AdminCourt[["2012-10-02", 2], ["2012-10-09", 2], ["2012-10-16", 1]] #ConstituentAssembly[["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]); });
});
我知道我没有循环遍历数组,我不知道我是如何尝试每个数组的,但似乎我没有正确理解它。
这是我需要你帮助的东西:)
- 遍历数组。(我知道如何循环,但我不知道是否有办法通过 D3 类)
- 如何使每个单元格都可点击。
- 如果我可以在单元格中添加 MultiValues(可能数组的键取决于日期值)。
- 如何使日历动态未设置为特定范围。
这是我的脚本代码:
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");
在此先感谢,非常感谢您的帮助。
编辑 1:
我尝试过类似的工作:
var data1 = d3.entries(BigWordsDates2).forEach(function(d) {
for each (var i in BigWordsDates2[d.key]){
return {
Date: BigWordsDates2[d.key][i][0],
Close: BigWordsDates2[d.key][i][1]
};
};
var data = d3.nest()
.key(function(data1) { return d.Date; })
.rollup(function(data1) { return (d.Close - 0); });
编辑 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");
我认为很接近。任何帮助将不胜感激。
我做了一个jsfiddle模板:http://jsfiddle.net/ARy8d/1/
编辑 3:
我已经解决了第 1 步和第 2 步,这里是 jsfiddle 链接: http://jsfiddle.net/makoto/ARy8d/9/
现在试图找到一种解决方法来在同一个单元格中添加多值
例如,如果我在数组中有 2 个值具有相同的日期,我想添加并在右侧单元格中查看它们。但是代码现在的作用是,如果有 2 个值具有相同的日期值,则最后一个会覆盖第一个。
任何帮助都可以,在此先感谢。
【问题讨论】:
-
您有语法错误:
for each应替换为for。您可以将点击处理程序添加到像.on("click",function(){console.log(arguments)})这样的任何元素。现在没时间再深入了,也许我改天再来。 -
好吧,我让它工作了,但是我在向一个单元格添加多值时遇到了问题(可能是追加)。这可能吗?!
标签: javascript d3.js calendar associative-array