D3折线图:将Y.domain值设置为以特定数字为中心
D3 Line Chart: Setting Y.domain values to centre on a specific number
我的折线图使用entries.value
来制作y轴,但我想做的是让y轴的绝对中心是goal.value
的数字。
https://jsfiddle.net/everina/hdz2oxhb/1/在这个例子中,你看到了虚线,也就是目标值,我想把它做成虚线在图的中心。(所以现在15的数字在哪里)我不确定该怎么做。
目前,只有当entries.value与虚线足够接近时,虚线才可见。
var entries = [{"date":"2016-01-06","value":15},{"date":"2015-11-17","value":15.4},{"date":"2015-11-11","value":16.5},{"date":"2015-09-24","value":15.1},{"date":"2015-08-22","value":15},{"date":"2015-08-12","value":15},{"date":"2015-07-30","value":14.6},{"date":"2015-07-19","value":14.8},{"date":"2015-07-18","value":14.9},{"date":"2015-07-12","value":14.9},{"date":"2015-07-08","value":14.9},{"date":"2015-06-29","value":14.3},{"date":"2015-06-21","value":14.5},{"date":"2015-06-18","value":14.7},{"date":"2015-06-09","value":15},{"date":"2015-06-08","value":14.1},{"date":"2014-12-06","value":13.4},{"date":"2014-09-10","value":13.1},{"date":"2014-08-01","value":14.2},{"date":"2014-07-07","value":15},{"date":"2014-05-31","value":14},{"date":"2014-05-24","value":15},{"date":"2014-05-14","value":15},{"date":"2014-05-13","value":14},{"date":"2014-05-08","value":14.5},{"date":"2014-05-02","value":15}],
goal = {"value":13.5,"date":"2014-05-02"};
function enter(data, goal) {
data.forEach(function(d) {
d.date = parseDate(d.date);
d.value = +d.value;
});
data.sort(function(a, b) {
return a.date - b.date;
});
x.domain([data[0].date, data[data.length - 1].date]);
// here is where the y axis is made. I need to edit this to somehow force it to use goal.value as the middle number
y.domain(d3.extent(data, function(d) { return d.value; }));
svg.append("path")
.datum(data)
.attr("class", "area")
.attr("d", area);
svg.append("path")
.datum(data)
.attr("class", "line")
.attr("d", line);
svg.append("g")
.attr("class", "x axis")
.attr("transform", "translate(0," + height + ")")
.call(xAxis);
svg.append("g")
.attr("class", "y axis")
.attr("transform", "translate(" + width + ",0)")
.call(yAxis);
// here I make the dashed goal line
svg.append("line")
.attr("class", "goal")
.attr("y1", y(goal.value))
.attr("x1", x(data[0].date))
.attr("y2", y(goal.value))
.attr("x2", x(data[data.length - 1].date));
}
更改轴上的y域,使"target.value"位于域的中心。
//y.domain(d3.extent(data, function(d) { return d.value; }));
var e = d3.extent(data, function(d) { return d.value; });
var e_max_delta = Math.max( Math.abs( e[0] - goal.value ), Math.abs( e[1] - goal.value ) );
y.domain([goal.value - e_max_delta, goal.value + e_max_delta]);
https://jsfiddle.net/hdz2oxhb/2/
相关文章:
- 如何禁用类型为'数字'
- 将数字转换为字符串数字
- 以Jquery为中心的Div中的图像对齐
- 在bootstrap3模式中以谷歌地图为中心发布..不断变化
- 通过jQuery函数将拉丁数字更改为波斯数字并添加逗号
- 谷歌地图API v3:如何以2个动态地址为中心
- Java脚本插件,用于将数字转换为特定于区域性的货币格式,并将具有特定货币格式的非格式化数字转换为普通数字
- 如何将以下函数更改为字母数字输出
- 如何将数字拆分为单个数字
- 以手机为中心的Bootstrap导航栏品牌
- 以谷歌地图为中心,使用标记调整响应大小
- Javascript正则表达式,用于检查字符串是否为有效数字
- 允许HTML5 web应用程序在浏览器关闭时访问位置'以t为中心(iOS)
- 当用户使用JavaScript键入内容时,如何设置以搜索输入为中心
- 用CSS居中内联块DIV“:在“;以垂直和水平为中心的废墟文本
- 用于以用户体验为中心的web应用程序回归测试的工具和技术
- 如何让我的Javascript只显示一个以鼠标为中心的聚光灯
- 立即工作并将所有英文数字替换为阿拉伯数字的函数
- D3折线图:将Y.domain值设置为以特定数字为中心
- 添加带有自定义图像而不是默认标记的标记,然后添加一些以标记为中心的数字