How do I use/retrieve the C3 scale function post generate?

。_饼干妹妹 提交于 2019-12-12 02:57:26

问题


I am using C3 to generate a chart and have found it necessary to dig into the underlying D3 constructs to supplement its functionality. I'm at an impasse and need to draw a region that is both limited by the x values and the y values but the "regions" functionality of C3 only allows one or the other. So, I need to draw on the C3-generated chart a rect at the appropriate location but using the scale functions to determine the X/Y values of the new rect. Is there any way to do this with C3 or underlying D3 library?


回答1:


You can access the scale function from the chart.internal (chart.internal.x and chart.internal.y). You'll probably need the margins too (otherwise your positions will be offset by that much)

Here's how you draw a rectangle from point (0 index, 100) to (2 index, 400)

var rect = chart.internal.svg.append("rect")
    .attr('fill', 'rgba(255, 9, 0, 0.1)')
    .attr("x", chart.internal.x(0) + chart.internal.margin.left)
    .attr("y", chart.internal.y(400) + chart.internal.margin.top)
    .attr("width", chart.internal.x(2) - chart.internal.x(0))
    .attr("height", chart.internal.y(100) - chart.internal.y(400));

Fiddle - http://jsfiddle.net/hehpy91o/



来源:https://stackoverflow.com/questions/31144955/how-do-i-use-retrieve-the-c3-scale-function-post-generate

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