- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在创建一个界面,用户可以通过该界面生成多系列折线图,并控制绘制哪些数据集。
要绘制哪些数据集由复选框控制。页面加载时,默认数据集以图表形式呈现,并且
$('input:checkbox.data-set-control').on('change')
图表已更新以添加或删除请求的数据集。
fiddle :Multi-series line chart with enter/exit transitions
我重构了代码以排除 d3.json() 请求,但一般功能保持不变。
我的问题是让代表一系列数据点的圆圈正确进入/退出。在我的服务器上,圆圈正确更新,但不退出。 (由于某种原因,更新和退出都无法在 Fiddle 中工作...)
我已经阅读并重读了 Mike 的所有有关 D3 更新/输入/退出功能的教程,此外还查看了我能找到的每个示例,甚至远程利用了折线图的更新/输入/退出。我相信我的问题在于我随后对圆圈进行操作的选择,尽管经过几个小时的修补后我似乎无法弄清楚正确的范围或结构或其他什么。
图表数据结构的简要总结:
data = [Object, Object, ...]
data[Object] = {
name: dataSetName,
data: [Object, Object, ...]
}
data[Object].data = [
{
x: val1,
y: val2,
}, ...
]
非常感谢在这种情况下理解更新/进入/退出的任何指导和指示。谢谢。
--尼尔·B.
最佳答案
这里有几个问题:
1.) 您没有处理 nodeGroup g
元素的进入/更新/退出模式。特别是现有的 g 存在问题。
2.) 当您选择圈子时,您正在使用 selectAll('.circle')
:选择 class 圈子的所有内容(不存在)。你的意思是 selectAll('circle')
:选择 element 圆的所有内容。此外,我建议您在 nodeGroup
和 dataNode
上使用类。当您继续构建图表时,它会导致更少的问题。
这里一切都已修复:
<!DOCTYPE html>
<html>
<head>
<script data-require="d3@3.5.3" data-semver="3.5.3" src="//cdnjs.cloudflare.com/ajax/libs/d3/3.5.3/d3.js"></script>
<script data-require="jquery@2.1.4" data-semver="2.1.4" src="http://code.jquery.com/jquery-2.1.4.min.js"></script>
<style>
svg {
font: 10px sans-serif;
}
svg .text {
font-size: 1.2em;
shape-rendering: crispEdges;
}
.controls {
display: block;
margin-left: 20px;
margin-top: 20px;
}
.axis path,
.axis line {
fill: none;
stroke: #000;
shape-rendering: crispEdges;
}
.line {
fill: none;
}
.grid {
stroke: #eee;
}
.bar {
fill: #40779C;
}
.bar:hover {
fill: #AD4444;
}
.barChart .x.axis path {
stroke: none;
}
.barChart .text,
.lineChart .text {
fill: #fff;
}
.pieChart .text {
fill: #666;
}
.arc path {
stroke: #fff;
}
#getData {
margin-left: 10px;
}
#weeks {
margin-left: 10px;
}
.accordion-title {
margin: 3px 0;
}
.overlay {
fill: none;
stroke: none;
}
.dataNode {
fill: #eee;
stroke: #666;
}
</style>
</head>
<body>
<div class="row">
<div class="col-lg-12">
<div class="panel panel-default">
<div class="panel-heading">
Data sets
</div>
<div class="panel-body">
<div class="checkbox">
<label>
<input type="checkbox" class="data-set-control" checked="" />
<span>dataSet1</span>
</label>
</div>
<div class="checkbox">
<label>
<input type="checkbox" class="data-set-control" checked="" />
<span>dataSet2</span>
</label>
</div>
<div class="checkbox">
<label>
<input type="checkbox" class="data-set-control" checked="" />
<span>dataSet3</span>
</label>
</div>
<div class="checkbox">
<label>
<input type="checkbox" class="data-set-control" />
<span>dataSet4</span>
</label>
</div>
</div>
</div>
</div>
</div>
<br />
<div class="row">
<div id="chartContainer"></div>
</div>
<script>
dataSet1 = {
name: 'dataSet1',
data: [{
x: 0,
y: 19
}, {
x: 1,
y: 16
}, {
x: 2,
y: 21
}, {
x: 3,
y: 24
}, {
x: 4,
y: 19
}, {
x: 5,
y: 18
}, {
x: 6,
y: 22
}]
}
dataSet2 = {
name: 'dataSet2',
data: [{
x: 0,
y: 26
}, {
x: 1,
y: 23
}, {
x: 2,
y: 29
}, {
x: 3,
y: 34
}, {
x: 4,
y: 27
}, {
x: 5,
y: 28
}, {
x: 6,
y: 33
}]
};
dataSet3 = {
name: 'dataSet3',
data: [{
x: 0,
y: 45
}, {
x: 1,
y: 51
}, {
x: 2,
y: 42
}, {
x: 3,
y: 47
}, {
x: 4,
y: 54
}, {
x: 5,
y: 57
}, {
x: 6,
y: 49
}]
};
dataSet4 = {
name: 'dataSet4',
data: [{
x: 0,
y: 80
}, {
x: 1,
y: 69
}, {
x: 2,
y: 81
}, {
x: 3,
y: 84
}, {
x: 4,
y: 78
}, {
x: 5,
y: 83
}, {
x: 6,
y: 85
}]
};
// coerce json strings to integers
function mapData(data) {
data.forEach(function(d) {
d.data.forEach(function(v) {
v.y = (v.y) ? +v.y : null;
});
});
return data;
}
// get requested data sets for json request
function getDataSets() {
// loop through checked boxes for requested data sets
dataSets = [];
var c = 0;
$('input:checkbox.data-set-control').each(function(i) {
if ($(this).prop('checked')) {
var name = $(this).siblings('span').html();
var val = $(this).val();
if (name == 'dataSet1') {
dataSets[c] = dataSet1;
c++;
}
if (name == 'dataSet2') {
dataSets[c] = dataSet2;
c++
}
if (name == 'dataSet3') {
dataSets[c] = dataSet3;
c++;
}
if (name == 'dataSet4') {
dataSets[c] = dataSet4;
c++;
}
}
});
return dataSets;
}
// svg margins
var margin = {
top: 20,
right: 30,
bottom: 30,
left: 40
};
// svg dimensions
totalW = 900;
totalH = 400;
var width = totalW - margin.left - margin.right;
var height = totalH - margin.top - margin.bottom;
var radius = 3;
var x = d3.scale.linear()
.range([0, width], .1);
var y = d3.scale.linear()
.range([height, 0]);
var svg = d3.select('#chartContainer').append('svg')
.attr('class', 'lineChart')
.attr('width', width + margin.left + margin.right)
.attr('height', height + margin.top + margin.bottom)
.append('g')
.attr('transform', 'translate(' + margin.left + ',' + margin.top + ')');
var color = d3.scale.category10();
var line = d3.svg.line()
.defined(function(d) {
return d.y != null;
})
.interpolate('cardinal')
.x(function(d) {
return x(d.x);
})
.y(function(d) {
return y(d.y);
});
function render() {
/*
var ajax = JSON.stringify( getDataSets() );
var url = '../scripts/d3-data.php?ajax='+ajax;
d3.json(url, function (data) { */
// json normally returns an array of objects commensurate
// to the number of checked boxes representing data sets
// manually set data for fiddle
data = getDataSets();
// global width/height variables are lost through json request
totalW = 900;
totalH = 400;
var width = totalW - margin.left - margin.right;
var height = totalH - margin.top - margin.bottom;
// coerce json to integers
d3Data = mapData(data);
color.domain(d3Data.map(function(d) {
return d.name;
}));
x.domain([
0,
d3.max(d3Data, function(d) {
return d3.max(d.data, function(v) {
return v.x;
});
})
]);
y.domain([
0,
d3.max(d3Data, function(d) {
return d3.max(d.data, function(v) {
return v.y + 10;
});
})
]);
var xAxis = d3.svg.axis()
.scale(x)
.orient('bottom');
var yAxis = d3.svg.axis()
.scale(y)
.orient('left');
// check if y axis exists
if (svg.selectAll(".y.axis")[0].length < 1) {
// does not, append to svg
svg.append("g")
.attr("class", "y axis")
.call(yAxis);
} else {
// does, so transition any domain change(s)
var updateY = svg.selectAll(".y.axis");
updateY.transition().duration(500).call(yAxis);
}
// x axis never changes so only append if doesn't exist
if (svg.selectAll('.x.axis')[0].length < 1) {
svg.append('g')
.attr('class', 'x axis')
.call(xAxis)
.attr('transform', 'translate(0,' + height + ')');
}
// only add horizontal grid if doesn't exist
if (svg.selectAll('.grid')[0].length < 1) {
// add horizontal lines for readability
svg.append('g')
.attr('class', 'grid')
.call(d3.svg.axis().scale(y)
.orient('left')
.tickSize(-(width), 0, 0)
.tickValues(function(d, i) {
var tickVals = [];
var max = d3.max(d3Data, function(d) {
return d3.max(d.data, function(v) {
return +v.y;
});
});
for (i = 10; i < (max + 10); i += 10) {
tickVals.push(i);
}
return tickVals;
})
.tickFormat('')
);
}
var path = svg.selectAll('.line')
.data(d3Data)
.attr('class', 'line');
path.transition()
.duration(500)
.attr('d', function(d) {
return line(d.data);
})
.style('stroke', function(d) {
return color(d.name);
});
path.enter()
.append('path')
.attr('class', 'line')
.attr('d', function(d) {
return line(d.data);
})
.style('stroke', function(d) {
return color(d.name);
});
path.exit().remove();
var nodeGroup = svg.selectAll('.nodeGroup')
.data(d3Data, function(d){
return d.name;
});
nodeGroup.exit().remove();
nodeGroup
.enter()
.append('g')
.attr('class', 'nodeGroup');
var circles = nodeGroup.selectAll('.dataNode')
.data(function(d) {
return d.data;
});
circles.enter()
.append('circle')
.style('opacity', 0)
.attr('class', 'dataNode')
.attr('r', function(d) {
return d.y == null ? 0 : radius;
});
circles.exit().remove();
circles
.attr('cx', function(d) {
return x(d.x);
})
.attr('cy', function(d) {
return y(d.y);
})
.transition()
.duration(500)
.delay(300)
.style('opacity', 1);
//});
}
render();
$(function() {
$('input:checkbox.data-set-control').on('change', function() {
var type;
$('.btn-group button.btn').each(function() {
if ($(this).hasClass('active')) type = $(this).html().toLowerCase();
});
render();
});
});
</script>
</body>
</html>
关于jquery - D3 进入/退出路径圆组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33711044/
以下是我所拥有的。 我唯一需要的是让顶部填充。所以 12 点钟它应该是一个填充,广告 6 点钟它应该以渐变结束。 实现这一目标的最佳方法是什么? (这个想法是让它在下一步中旋转。) Codepen
我用 Canvas 绘制了倒计时 工作代码:http://jsfiddle.net/ajFsx/ window.onload = function() { canvas = document
我是stagexl的新手,我知道这是非常基本的问题,但是我找不到真正快速的答案,因此我认为将这个答案提供给与我处于同一职位的任何人都很好。 如何在stagexl中创建从x到y的线? 以及如何创建一个以
我想知道以编程方式为图像制作圆 Angular 的最佳方法是什么。这可以使用 PHP 或 javascript。一个算法也可以做同样的事情,我可以用 Image::Magick 或 GD 对其进行编码
关闭。这个问题需要更多focused .它目前不接受答案。 想改进这个问题吗? 更新问题,使其只关注一个问题 editing this post . 关闭 8 年前。 Improve this q
我有一组二维点。我想找到: 包含所有点的最小三角形 包含所有点的最小圆。 是否有任何算法可以做到这一点?我遇到了 Convex Hull 来为一组点拟合凸多边形。但我想要一个圆形和三角形。 提前致谢
如何计算两个圆的交点。我希望在所有情况下都有两个、一个或没有交点。 我有中心点的 x 和 y 坐标,以及每个圆的半径。 Python 中的答案是首选,但任何可用的算法都是可以接受的。 最佳答案 Int
我需要用 QPainter 画一个圆。当我像这样使用 drawEllipse 函数时: void UserClass::Draw(QPainter &painter) { painter.sa
计算几何问题: 在多边形(例如BCDE)的边(例如EB)上随机选择点P0,以找到可能的点(即, P1,P2,P3,...) 基于给定距离(即 r)在其他边上。下面的演示展示了一个解决方案,它通过找到以
这个问题在这里已经有了答案: 关闭 13 年前。 重复: What is the best way to create rounded corners How to make a cross bro
我有一个 ionic4 应用程序,我需要在其中实现类似于下面卡片中的 img 效果。在边缘模糊到中心,然后在中心用另一个白色边框清除 我怎样才能做到这一点?请忽略编辑图标 最佳答案 .card
我想旋转一个 SVG 圆圈,同时保持其他元素不旋转 当我尝试使用 rotateZ(15deg) 旋转圆(白色)时,这就是我得到的: 这是我目前的进展: https://jsfiddle.net/41h
我正在尝试根据时间戳实现 LineString 挤压。正如 github 中提到的,它应该被实现,但事实并非如此。它应该类似于下面的屏幕截图。 到目前为止,我发现可以对多边形使用挤压,但随后我必须以某
我用了this question我创建了像this这样的形状但现在我不知道如何在第一次单击时为每个圆圈设置文本? (如井字棋) 最佳答案 给你! - 为了方便起见,我合并了它。只需单击圆圈即可查看其上
如何判断圆和矩形在二维欧几里得空间中是否相交? (即经典的二维几何) 最佳答案 这是我的做法: bool intersects(CircleType circle, RectType rect) {
圆 A 沿 x 轴向右移动。圆 B 沿 y 轴向上移动。我想知道他们是否会发生碰撞。 (不是何时,只是如果。) 半径相同,恒速度不同。 This answer似乎解决了这个问题,我的问题最好应该是这个
Relevant Codesandbox 我一直在我的应用程序中看到一种模式,当我创建圆形的div时,当它们的尺寸较小时,它们有时似乎具有边缘。请参见下面突出显示的代码的图像。为什么会发生这种情况,有
目前,我在 c3.js 中生成的图表图例是颜色矩形,我想将其更改为圆形。我该怎么做? var chart = c3.generate({ data: { columns: [
我需要显示带有圆 Angular 的图像。很久以前,我看到一个网站使用 javascript 库执行此操作,该库将圆 Angular 图像覆盖在普通图像上。 我们是否有任何 javascript 库(
在我的程序中,我使用 css 设计了我的按钮样式。我正在使用“-fx-background-radius”来圆 Angular ,并注意到当我将鼠标悬停在原来的 Angular 上时,它允许我单击按钮
我是一名优秀的程序员,十分优秀!