使用关联数组的 D3 日历视图

时间:2023-03-26
本文介绍了使用关联数组的 D3 日历视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个日历视图,如下例所示:

编辑 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/

I want to create a Calendar View Like this example: http://bl.ocks.org/4063318:

Actually i am trying to modify it.

I have an associative array like this: #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]]

I tried filling the calendar like this:

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]); });
      });

I know i am not looping through the array and i dont know how i tried for each but it seems that i dont get it correctly.

Here are the stuff that i need ur help with :)

  1. Loop through the array.( I know how to loop on it but i dont know if there is a way through the D3 class )
  2. How to Make each cell clickable.
  3. if i can add MultiValues in a cell ( Probably the keys of the array it depends on the date values).
  4. How to make the calendar Dynamic Not Set to a certain Range.

Here is the script code i have:

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");

Thanks In Advance and i would really appreciate the help.

EDIT 1:

I tried to work on something like that:

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); });

Edit 2: i worked around what was above a bit and this is all i can think of would really need Help, no idea why the values aint appended in the calendar.

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");

I think am close. any help would be appreciated.

I made a jsfiddle template: http://jsfiddle.net/ARy8d/1/

EDIT 3:

i got Steps 1 and 2 are solved and here is the jsfiddle link: http://jsfiddle.net/makoto/ARy8d/9/

Now trying to find a workaround to add multivalues in same cell

For Example if i have 2 values in array that has the same date i want to add and view them in the right cell. however what the code does right now that if there are 2 values with the same date value the last one overwrites the first one.

Any help will do, thanks in advance.

解决方案

Here Is The Solution For Number 1 and 2 for those who has the problem similar to the ones i used to have. hope that will helpful.

The array Looks Like That: 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]]};

Save the Value that Your Targeted array Values you want like that: var tahrir = BigWordsDates2['#Tahrir']

and then overwrite the CSV Data with it. You can Find The example with solution in the jsfiddle below.

http://jsfiddle.net/makoto/ARy8d/7/

这篇关于使用关联数组的 D3 日历视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!