在 raphael js 中访问集合内的集合
我正在使用 raphael js 创建一个小“信息图”。信息图将呈现许多圆圈,其中包含一些文本。圆圈的数量是未知的,将取决于它所输入的数据。
我以为我会将拉斐尔对象组织成集合,每个圆一个,然后将这些集合移动到一个“容器”集合中,但我在使用类似以下内容以编程方式访问它们时遇到了困难:
console.log(ss[0].circle);
这是我用来绘制的代码片段我的圈子/将它们添加到集合中:
var r = Raphael('raph', '500px', '500px');
var coord = {
'0': {x: 177, y: 75},
'1': {x: 420, y: 173},
'2': {x: 177, y: 415}
};
var ss = r.set();
for(var i=0; i < data.values.length; i++){
var s = r.set();
s.push(r.path("M "+ coord[i].x +" "+ coord[i].y +" L 247 247 z"));
s.push(r.circle(coord[i].x, coord[i].y, 50).attr({fill: '#fff', stroke: '#00adef', 'stroke-width': 2}));
s.push(r.text(coord[i].x, coord[i].y-41).attr({'font': '12px Arial', 'font-weight': 'bold', fill: '#474747', text: data.values[i].name}));
s.push(r.text(coord[i].x, coord[i].y-19).attr({'font': '28px Arial', 'font-weight': 'bold', fill: '#00adef', text: data.values[i].grade}));
ss.push(s);
}
有人能给我指出正确的方向吗?
I'm in the process of creating a little 'infographic' using raphael js. The infographic will render a number of circles with some text inside. The number of circles isn't know and will depend on the data it gets fed.
I thought that I would organise the raphael objects into sets, one for each circle and then move those sets into one 'container' set but I am having trouble accessing them programatically using something like:
console.log(ss[0].circle);
Here is a snippet of the code im using to draw my circles/add them to a set:
var r = Raphael('raph', '500px', '500px');
var coord = {
'0': {x: 177, y: 75},
'1': {x: 420, y: 173},
'2': {x: 177, y: 415}
};
var ss = r.set();
for(var i=0; i < data.values.length; i++){
var s = r.set();
s.push(r.path("M "+ coord[i].x +" "+ coord[i].y +" L 247 247 z"));
s.push(r.circle(coord[i].x, coord[i].y, 50).attr({fill: '#fff', stroke: '#00adef', 'stroke-width': 2}));
s.push(r.text(coord[i].x, coord[i].y-41).attr({'font': '12px Arial', 'font-weight': 'bold', fill: '#474747', text: data.values[i].name}));
s.push(r.text(coord[i].x, coord[i].y-19).attr({'font': '28px Arial', 'font-weight': 'bold', fill: '#00adef', text: data.values[i].grade}));
ss.push(s);
}
Can someone point me in the right direction?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
因此,让我确保我正确理解您的意思:
您希望能够:
(1) 通过更新一组对象来同时更改所有圆的属性。例如,
将所有圆圈向右移动 10 像素,向下移动 10 像素。
(2) 更改各个圆圈的属性以移动圆圈(及其关联的路径和文本元素)。
只移动第一个圆。
下面的内容能达到你想要的效果吗?
然后,您可以一次移动所有圆圈:
并移动单个圆圈:
我是否理解您想要正确完成的任务?
So let me make sure I understand you correctly:
You want to be able to:
(1) change properties on all of the circles at the same time by updating one set object. For example
moves all of the circles 10px right and 10px down.
(2) change properties on individual circles to move the circle (and it's associated path and text elements).
moves the first circle only.
Does the following accomplish what you want?
You can then move all of the circles at once by:
and move an individual circle by:
Am I understanding what you're trying to accomplish correctly?
我拿了 bbrame 的代码并玩了一下。我学到了两件事:
即,
这是我测试的方法:
I took bbrame's code and played with it. Two things I learned:
i.e.,
Here's how I tested:
也许将外部集合设置为平面旧的 JavaScript 数组,并在循环中渲染每个集合。
Perhaps make the outer set a plane old javascript array, and render each set in a loop.