M.N*_*M.N 5 javascript calendar associative-array d3.js
我想创建一个日历视图,如下例所示: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]); });
});
Run Code Online (Sandbox Code Playgroud)
我知道我没有循环遍历数组,我不知道我如何尝试每个数组,但似乎我没有正确理解它。
以下是我需要你帮助的事情:)
这是我的脚本代码:
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");
Run Code Online (Sandbox Code Playgroud)
提前致谢,我非常感谢您的帮助。
编辑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); });
Run Code Online (Sandbox Code Playgroud)
编辑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");
Run Code Online (Sandbox Code Playgroud)
我认为很接近。任何帮助,将不胜感激。
我做了一个jsfiddle模板:http://jsfiddle.net/ARy8d/1/
编辑3:
我的步骤 1 和 2 已解决,这里是 jsfiddle 链接: http://jsfiddle.net/makoto/ARy8d/9/
现在尝试找到一种解决方法来在同一单元格中添加多值
例如,如果我在数组中有 2 个具有相同日期的值,我想添加它们并在右侧单元格中查看它们。但是,代码现在执行的操作是,如果有 2 个值具有相同的日期值,则最后一个值将覆盖第一个值。
任何帮助都可以,提前致谢。
对于那些遇到与我以前遇到的问题类似的问题的人来说,这是第 1 号和第 2 号的解决方案。希望这会有所帮助。
数组看起来像这样:BigWordsDates2 = {"#Tahrir":[["2012-10-12",20],["2012-10-13",1],["2012-10-19",15]],"#Egypt":[["2012-10-01",3],["2012-10-03",2],["2012-10-04",3],["2012-10-07",1],["2012-10-10",1],["2012-10-13",2],["2012-10-14",1],["2012-10-15",1],["2012-10-16",1],["2012-10-17",4],["2012-10-19",5]]};
保存您想要的目标数组值,如下所示:var tahrir = BigWordsDates2['#Tahrir']
然后用它覆盖 CSV 数据。您可以在下面的 jsfiddle 中找到带有解决方案的示例。
http://jsfiddle.net/makoto/ARy8d/7/
归档时间: |
|
查看次数: |
5123 次 |
最近记录: |