d3 arc diagram - add top semi circles

你说的曾经没有我的故事 提交于 2019-12-24 15:27:49

问题


http://jsfiddle.net/pRkpL/ and http://fiddle.jshell.net/pRkpL/show/

I am making an arc diagram which is based heavily on this demo: http://bl.ocks.org/sjengle/5431779. It's taken me a while to get to this stage, and the graph now adapts to the data in all the ways I need it to.

However, I have 2 questions:

1) I need to create 2 arc diagrams - one above the black rectangles (named buckets in the code) and one below. As a temporary fix, I have used css3 3D transforms to flip one of the arc diagrams over on the x-axis, but the browser support is flaky at best. It does work, only in Chrome, and sometimes you have to refresh, open dev tools, or resize the fiddle frame for it to kick in. I'd like to do it properly with d3.

I think the code which creates the red arcs (the lower half of a circle) is this:

var radians = d3.scale.linear()
                .range([Math.PI / 2, 3 * Math.PI / 2]);

I found if I remove the last / 2 it displays a full circle, however I can't get it to display only the top half in my drawTop function.

2) I currently duplicate 2 large functions for the 2 arc diagrams, drawTop and drawBtm. The only differences between the two are the data arrays (good_jumps and bad_jumps), the container id, and the radians logic above, if that can get solved. Is there a way I can combine these into one so I'm not duplicating the logic?

I'm fairly new to JS so please let me know if there are any obvious blunders in the code :) Also, there's no possibility to alter the json as it comes from an external api.


回答1:


To make the arcs appear on top of the boxes, you just need to change the output range of your radians scale to cover the top half:

radians.range([-Math.PI / 2, Math.PI / 2]);

Regarding the second question, one way of doing this is to add another attribute to your data that allows to distinguish between the two types of arcs, e.g. .good. Once you've done that, you can switch on the value of that attribute to decide the class of the added path and the range of radians, which are the only differences between the arcs.

Complete jsfiddle here.



来源:https://stackoverflow.com/questions/20400184/d3-arc-diagram-add-top-semi-circles

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!