- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我正在使用 D3 创建日历热图。
这是我当前的代码:
<!DOCTYPE html>
<meta charset="utf-8">
<html>
<head>
<style>
rect.bordered {
stroke: #E6E6E6;
stroke-width:2px;
}
text.mono {
font-size: 10pt;
font-family: Consolas, courier;
fill: #000;
}
text {
font-family: Consolas, courier;
font-size:1.0em;
}
text.axis-workweek {
fill: #000;
}
text.axis-worktime {
fill: #000;
}
</style>
<script src="http://d3js.org/d3.v3.js"></script>
</head>
<body>
<div id="chart"></div>
</div>
<script type="text/javascript">
var title="";
var margin = { top: 90, right: 0, bottom: 80, left: 30 },
width = 960 - margin.left - margin.right,
height = 900 - margin.top - margin.bottom,
gridSize = Math.floor(width / 42),
legendElementWidth = gridSize*2,
buckets = 5,
colors = ["#c7e9b4","#41b6c4","#1d91c0","#225ea8"], // alternatively colorbrewer.YlGnBu[9]
days = ["1", "2", "3", "4", "5", "6", "7","8", "9", "10", "11", "12","13", "14", "15", "16", "17","18", "19", "20",
"21", "22","23", "24", "25", "26", "27","28", "29", "30", "31"],
times = ["7:00", "8:00", "9:00", "10:00", "11:00", "12:00", "13:00", "14:00", "15:00", "16:00", "17:00", "18:00", "19:00", "20:00", "21:00"];
datasets = ["data.tsv"];
var svg = d3.select("#chart").append("svg")
.attr("width", width + margin.left + margin.right)
.attr("height", height + margin.top + margin.bottom)
.append("g")
.attr("transform", "translate(" + margin.left + "," + margin.top + ")");
svg.append("text")
.attr("x",0)
.attr("y",-40)
.text(title);
var dayLabels = svg.selectAll(".dayLabel")
.data(days)
.enter().append("text")
.text(function (d) { return d; })
.attr("x", 0)
.attr("y", function (d, i) { return i * gridSize; })
.style("text-anchor", "end")
.attr("transform", "translate(-6," + gridSize / 1.5 + ")")
.attr("class", function (d, i) { return ((i >= 0 && i <= 4) ? "dayLabel mono axis axis-workweek" : "dayLabel mono axis"); });
var timeLabels = svg.selectAll(".timeLabel")
.data(times)
.enter().append("text")
.text(function(d) { return d; })
.attr("x", function(d, i) { return i * gridSize * 2.1; })
.attr("y", 0)
.style("text-anchor", "middle")
.attr("transform", "translate(" + gridSize / 2 + ", -6)")
.attr("class", function(d, i) { return ((i >= 7 && i <= 16) ? "timeLabel mono axis axis-worktime" : "timeLabel mono axis"); });
var heatmapChart = function(tsvFile) {
d3.tsv(tsvFile,
function(d) {
return {
day: +d.day,
hour: +d.hour,
value: +d.value
};
},
function(error, data) {
var colorScale = d3.scale.quantile()
.domain([0, buckets - 1, d3.max(data, function (d) { return d.value; })])
.range(colors);
var cards = svg.selectAll(".hour")
.data(data, function(d) {return d.day+':'+d.hour;});
cards.append("title");
cards.enter().append("rect")
.attr("x", function(d) { return (d.hour - 7) * gridSize * 2.05; })
.attr("y", function(d) { return (d.day - 1) * gridSize; })
.attr("rx", 4)
.attr("ry", 4)
.attr("class", "hour bordered")
.attr("width", gridSize*2)
.attr("height", gridSize)
.style("fill", colors[0]);
cards.transition().duration(1000)
.style("fill", function(d) { return colorScale(d.value); });
cards.select("title").text(function(d) { return d.value; });
cards.exit().remove();
var legend = svg.selectAll(".legend")
.data([0].concat(colorScale.quantiles()), function(d) { return d; });
legend.enter().append("g")
.attr("class", "legend");
legend.append("rect")
.attr("x", height)
.attr("y", function(d, i) { return legendElementWidth * (i-0.5); })
.attr("width", gridSize / 2 )
.attr("height", legendElementWidth)
.style("fill", function(d, i) { return colors[i]; });
legend.append("text")
.attr("class", "mono")
.text(function(d) { return "≥ " + Math.round(d) + "%"; })
.attr("x", (height) + gridSize)
.attr("y", function(d, i) { return legendElementWidth*i; } );
legend.exit().remove();
});
};
heatmapChart(datasets[0]);
</script>
</body>
</html>
这就是结果:
如何手动定义每种颜色的 % 范围?特别是,由于使用分位数,4% 和 52% 之间存在很大差距 (.data([0].concat(colorScale.quantiles()), function(d) { return d; }
)。我想设置 >0%
, >5%
, >10%
, >15%
,>20%
,>25%
。
示例数据集(data.tsv):
day hour value
1 7 0
1 8 22.6829268293
1 9 24.3243243243
1 10 23.3374766936
1 11 21.8156028369
1 12 21.8499851765
1 13 22.1323529412
1 14 19.8275862069
1 15 20.4545454546
1 16 23.7329042638
1 17 23.1717337716
1 18 20.1895734597
1 19 22.7552275523
1 20 22.6277372263
1 21 19.7278911564
2 7 0
2 8 24.1124260355
2 9 25.0368912936
2 10 23.8484398217
2 11 23.6625514403
2 12 23.5748436925
2 13 23.3272227315
2 14 21.8181818182
2 15 20.3499079189
2 16 20.7635009312
2 17 26.4220183486
2 18 21.8934911243
2 19 19.8433420366
2 20 19.8614318706
2 21 24.6031746031
最佳答案
您可以使用阈值比例:
Threshold scales are similar to quantize scales, except they allow you to map arbitrary subsets of the domain to discrete values in the range. The input domain is still continuous, and divided into slices based on a set of threshold values. The input domain is typically a dimension of the data that you want to visualize, such as the height of students (measured in meters) in a sample population. The output range is typically a dimension of the desired output visualization, such as a set of colors (represented as strings).
但请注意这一点:
If the number of values in the scale's range is N + 1, the number of values in the scale's domain must be N. If there are fewer than N elements in the domain, the additional values in the range are ignored.
这意味着,在您的情况下,由于 colors
数组有 4 个值,因此域只能有三个任意值:
var colorScale = d3.scale.threshold()
.domain([value1, value2, value3])
.range(colors);
使用您想要的百分比设置这些值。
关于javascript - 日历热图 : how to define the range of % for each color instead of using quantiles,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41377199/
所以,假设我们有一个这样的数组:4,6,9,10,2,25,12,6,9然后我尝试使用 numpy.quantile 和 statistics.quantile 计算分位数 import numpy
数据 data = [ {"content": "1", "title": "app sotre", "info": "", "time": 1578877014}, {"conten
在这篇文章中: How does pandas calculate quartiles? 这是@perl 对 quantile() 函数的工作给出的解释: df = pd.DataFrame([5,7
我正在寻找一种基于 quantile() 函数输出创建多列的方法。我实际上想要包含女性和男性值(value)的每一行的分位数。 第 1 行示例:我想要 quantile(c(4.6, 5.8))哪个给
我想使用 Hmisc::wtd.quantile 为具有许多重复日期的数据框创建一个新的 df。我按日期分组,使用 summarize()按日期聚合,并尝试使用 wtd.quantile()在每个日期
两天前的一个问题。 当您拥有大量数据时,PANDAS 可以让您在生成统计数据的同时就地过滤内容,因此您不必为所有内容创建新的数据框。现在,我怀疑这样做有细微差别,因为以两种不同的方式做事 - 一种是“
在某些设置下,np.quantile 在确定正确的分位数时会出错。这是错误吗? x = np.array([374, 358, 341, 355, 342, 334, 353, 346, 355, 3
在处理问题时,我注意到一些有趣的事情。我不知道到底发生了什么,但发生了一些我没想到会发生的事情。我可能犯了一个错误,但让我从一个例子开始: x quantile( x, 0.1 ) ] z qua
所以,我试图从数据框 df 中获取 a 列中的值,它是 b 列的第 90 个百分位。我使用以下代码来获取分位数的值: p = quantile(df$b, c(0.9)) 之后,我想用它来获取该值的行
所以,我试图从数据框 df 中获取 a 列中的值,它是 b 列的第 90 个百分位。我使用以下代码来获取分位数的值: p = quantile(df$b, c(0.9)) 之后,我想用它来获取该值的行
知道为什么会出现这种行为吗? 基础数据: In [1]: tmc_sum.head(6) Out [1]: 1 2 3 8 9 10
根据bigquery查询引用,当前Quantiles不允许通过另一列进行任何分组。我主要感兴趣的是让中位数按特定列分组。我现在看到的唯一解决方法是为每个不同的组成员生成分位数查询,其中组成员是wher
这句话的含义是什么? quantize = d3.scale.quantile().domain([0, 15]).range(d3.range(9)); 我看到域是: 0 - 0 1 - 15 范围
我一整天都对 R 分位数函数感到困惑。 我对分位数的工作原理有一个直观的概念,并且拥有硕士学位。在统计数据中,但是天哪,它的文档让我感到困惑。 来自文档: Q[i](p) = (1 - gamma)
我最近才发现在Julia和MatLab中分位数()的定义不同。我无法将这两个定义统一起来,总是得到不同的结果。。有人知道为什么会发生这种情况吗?知道如何调整定义吗?。我尝试了以下方法:。这导致Q =-
I just recently came accross that there is different defition of quantile() in Julia and Matlab.I
我试图区分 np.quantile() 的场景或 np.percentile()应该使用。 >>> import numpy as np >>> a = np.array([[10, 7, 4], [
我正在运行此代码以在 python 3 中生成分位数,但它会垂直打印输出。是否可以水平打印输出 import numpy as np import pandas as pd r = np.random
我有一个分位数回归模型,其中包含 1 个回归变量和 1 个回归变量。我想假设检验回归量在每个分位数上都相等。我想到的一种方法是在 {0.01,0.02,....,0.99} 上测试所有 tau。但是,
我正在使用 D3 创建日历热图。 这是我当前的代码: rect.bordered { stroke: #E6E6E6; stroke-
我是一名优秀的程序员,十分优秀!