编辑:示例中使用的所有数据/代码的链接:https ://drive.google.com/open?id=16MpDptwV7m4nOkoT3ImlKffl4rYqc5ms
你好朋友和烘焙师,
我几乎是 D3 可视化的新手。我的背景都是 Plotly 和集成的 R 平台地块。我已经为 Shiny 应用程序编写了非常轻量级的 js/css,但我正在尝试扩展到更多自定义和免费的可视化方法。
因此,我一直在研究 r2d3 包以在 R 中进行 d3 集成。我搜索了所有示例,并仔细阅读了我在主存储库和概述页面中可以找到的任何文档:https://rstudio.github。 io/r2d3/文章/图库/日历/
但是,对于我的生活,我根本无法理解 js 是如何实际提取数据的
这里的一个例子:视觉,随后是生成它的脚本,最后是作为数据源提供的 csv 以进行可视化
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
而且我知道这完全是我自己对 js 函数调用和数据处理的理解的来源,但这只是让我无所适从。我可以在其中看到一些 .data 初始化和函数调用,但我在哪里找不到任何关于这个可视化应该捕获什么的迹象。它如何知道哪一列表示日期?指定实际可视化的变量在哪里?
任何帮助在这里的暗示将不胜感激。我已经获得了一些 d3 教程,但任何指针至少可以让我玩那些比我构建的更智能的沙盒 :)
谢谢!

