使用d3 JavaScript读取外部数据-R r2d3用例

时间:2018-11-15 03:59:08

标签: javascript r d3.js r2d3

编辑:示例中使用的所有数据/代码的链接:https://drive.google.com/open?id=16MpDptwV7m4nOkoT3ImlKffl4rYqc5ms

朋友和焙烧炉都好,

我正和D3可视化一样是新手。我的背景是Plotly和集成的R平台图。我为Shiny应用程序编写了非常非常轻量级js / css,但是我试图扩展到更多自定义和免费的可视化方法中。

因此,我一直在研究r2d3软件包以在R中进行d3集成。我搜索了所有示例,并仔细研究了我可以在此处的主存储库和概述页面中找到的所有文档:

但是,对我而言,我简直无法绕过JS如何实际提取数据

这里是一个示例:视觉效果,随后是生成视觉效果的脚本,最后是提供的csv作为用于可视化的数据源

视觉: https://rstudio.github.io/r2d3/articles/gallery/calendar/

calendar.js脚本:

// !preview r2d3 data = read.csv("dji-latest.csv"), d3_version = 4, 

container = "div", options = list(start = 2006, end = 2011)

// Based on https://bl.ocks.org/mbostock/4063318

var height = height / (options.end - options.start),
    cellSize = height / 8;

var formatPercent = d3.format(".1%");

var color = d3.scaleQuantize()
    .domain([-0.05, 0.05])
    .range(["#a50026", "#d73027", "#f46d43", "#fdae61", "#fee08b", "#ffffbf", "#d9ef8b", "#a6d96a", "#66bd63", "#1a9850", "#006837"]);

var svg = div
  .style("line-height", "0")
  .selectAll("svg")
  .data(d3.range(options.start, options.end))
  .enter().append("svg")
    .attr("width", width)
    .attr("height", height)
  .append("g")
    .attr("transform", "translate(" + cellSize * 3.5 + "," + (height - cellSize * 7 - 1) + ")");

svg.append("text")
    .attr("transform", "translate(-" + (6 * height / 60) + "," + cellSize * 3.5 + ")rotate(-90)")
    .attr("font-family", "sans-serif")
    .attr("font-size", 2 + 8 * height / 60)
    .attr("text-anchor", "middle")
    .text(function(d) { return d; });

var rect = svg.append("g")
    .attr("fill", "none")
    .attr("stroke", "#ccc")
    .attr("stroke-width", "0.25")
  .selectAll("rect")
  .data(function(d) { return d3.timeDays(new Date(d, 0, 1), new Date(d + 1, 0, 1)); })
  .enter().append("rect")
    .attr("width", cellSize)
    .attr("height", cellSize)
    .attr("x", function(d) { return d3.timeWeek.count(d3.timeYear(d), d) * cellSize; })
    .attr("y", function(d) { return d.getDay() * cellSize; })
    .datum(d3.timeFormat("%Y-%m-%d"));

svg.append("g")
    .attr("fill", "none")
    .attr("stroke", "#000")
    .attr("stroke-width", "0.25")
  .selectAll("path")
  .data(function(d) { return d3.timeMonths(new Date(d, 0, 1), new Date(d + 1, 0, 1)); })
  .enter().append("path")
    .attr("d", pathMonth);

r2d3.onRender(function(csv, div, width, height, options) {
  var data = d3.nest()
      .key(function(d) { return d.Date; })
      .rollup(function(d) { return (d[0].Close - d[0].Open) / d[0].Open; })
    .object(csv);

  rect.filter(function(d) { return d in data; })
      .attr("fill", function(d) { return color(data[d]); })
    .append("title")
      .text(function(d) { return d + ": " + formatPercent(data[d]); });
});

function pathMonth(t0) {
  var t1 = new Date(t0.getFullYear(), t0.getMonth() + 1, 0),
      d0 = t0.getDay(), w0 = d3.timeWeek.count(d3.timeYear(t0), t0),
      d1 = t1.getDay(), w1 = d3.timeWeek.count(d3.timeYear(t1), 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";
}

这是输入的.csv

Visual Constructed

我知道这完全是我自己对js函数调用和数据处理的理解的来源,但这只是让我无休止。我可以在其中看到一些.data初始化和函数调用,但是在哪里找不到该可视化应该捕获的任何指示。如何知道哪一列表示日期?实际用于可视化的变量在哪里指定?

我们将不胜感激在此提出的任何帮助。我已经有了一些d3教程,但是任何指针至少都可以让我玩那些比我建造的还要聪明的沙箱:)

谢谢!

1 个答案:

答案 0 :(得分:0)

我知道这是一篇老文章...但是我到这里结束了,我认为写点东西供进一步参考可能是个好主意。

它怎么知道哪个列代表日期?该变量实际显示在哪里?

这个例子对于初学者来说有点棘手(或者至少会引起误解),但是指定变量/日期的这段代码是这样的:

var data = d3.nest()
      .key(function(d) { return d.Date; })
      .rollup(function(d) { return (d[0].Close - d[0].Open) / d[0].Open; })
    .object(csv);

您可以看到d3.nest到底here的作用。简而言之,R通过将data中的表转换为一个js友好对象,将csv变量(在js端命名为dji-latest.csv)传递给js,例如(在R语法中) :

data <- list(
  list(Date = "2010-10-01", Open = 10789.72, High = ...),
  list(Date = "2010-09-30", Open = 10789.72, High = ...),
) 

然后通过d.Datesd.Close函数定义中的d.Openkeyrollup选择特定变量。

请注意,以上函数中的csv引用了从R传递的data,因为默认情况下它是r2d3.onRender中函数中的第一个参数,并且可能是混乱。在js端,csv被馈送到nest,以生成此特定可视化所需的嵌套数据对象。

正如其他人所说,要提供比阅读文档更好的解释,这个示例非常简单。