简体   繁体   English

不使用d3.svg.axis()方法的D3轴

[英]D3 axis without using d3.svg.axis() method

在此处输入图片说明 I am a beginner in D3.js . 我是D3.js的初学者。 I need to draw a graph with axis as per below image.Any working fiddle will help me a lot. 我需要根据下面的图像绘制一个轴图。任何工作的小提琴都会对我有很大帮助。

I have checked the below fiddle but not sure how to tweak it as per my requirement. 我已经检查了以下小提琴,但不确定如何根据我的要求进行调整。

You can create the scale (which is completely independent from the axis) and use its parameters to create the svg elements for your axis; 您可以创建刻度(与轴完全独立),并使用其参数为轴创建svg元素。 in your case the horizontal line and a circle + text as ticks. 在您的情况下,水平线和一个圆圈+文字作为刻度。 Specifically, you could use the scale range() to get the horizontal line length; 具体来说,您可以使用scale range()获得水平线的长度; and the scale ticks(n) functions to get n evenly spaced points in the domain of the scale. 刻度ticks(n)函数在刻度域中获得n均匀间隔的点。 By passing these points to the scale you get the x coordinate to use for your ticks. 通过将这些点传递到比例尺,您将获得用于刻度的x坐标。

xScale = d3.scale.linear().domain([0.0, 1.0]).range([0, width]);
var axis = svg.append('g').attr('class', 'xaxis');

axis.append('line').attr({
    'x1': xScale.range()[0],
    'y1': height,
    'x2': xScale.range()[1],
    'y2': height
});

ticks = axis.selectAll('.ticks')
    .data(xScale.ticks(10))
    .enter()
    .append('g')
    .attr('class', 'tick');

ticks.append('circle')
    .attr({
        'cx': function(d) { return xScale(d); },
        'cy': height,
        'r': 5
    });

ticks.append('text')
    .attr({
        'x': function(d) { return xScale(d); },
        'y': height,
        'dy': 20 // Move the text a little under the line
    })
    .text(function(d) { return d; });

Here is a minimal working JSFiddle: http://jsfiddle.net/LeartS/yGesr/ 这是最小的JSFiddle工作: http : //jsfiddle.net/LeartS/yGesr/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM