简体   繁体   English

d3js时间轴刻度持续时间动态设置

[英]d3js time axis ticks duration set dynamically

I am trying to create a bar graph with time x-axis. 我正在尝试创建带有时间x轴的条形图。 I am having trouble in dynamically setting ticks values. 我在动态设置刻度值时遇到麻烦。

.ticks(d3.time.minutes, 10)

above code works for me, but when data is more i want the duration to be increased to 30 minutes ie, 上面的代码对我有用,但是当数据更多时,我希望持续时间增加到30分钟,即

.ticks(d3.time.minutes, 30)

I have tried, 我努力了,

.ticks(function () { return (dataPassed.length > 5) ? ((d3.time.minutes, 30)) : ((d3.time.minutes, 10)); })
.tickValues(xScale.domain())

above ways and none worked. 以上方法,没有任何工作。 JSfiddle link for my code: http://jsfiddle.net/hpjqt1dL/20/ . 我的代码的JSfiddle链接: http : //jsfiddle.net/hpjqt1dL/20/ I have put both datasets in the fiddle there(with less and more values). 我将两个数据集都放在了小提琴中(值越来越小)。 Can someone please help me with this. 有人可以帮我吗

Thanks in advance.... 提前致谢....

[UPDATE:] Actually, its not just 2 conditions I want. [更新:]实际上,它不仅是我想要的2个条件。 My updated code is: 我更新的代码是:

if (dataPassed.length < 4)
        {
            var xAxis = d3.svg.axis()
                    .scale(xScale)
                    .orient('bottom')
                    .ticks(d3.time.minutes, 10)
                    .tickFormat(d3.time.format('%H:%M'))
                    .tickSize(5);

            var boxNoBarWidth = (svgWidth + widthPad) / (dataPassed.length + 3);

        }
        else if (dataPassed.length > 3 && dataPassed.length < 6)
        {
            var xAxis = d3.svg.axis()
                    .scale(xScale)
                    .orient('bottom')
                    .ticks(d3.time.minutes, 15)
                    .tickFormat(d3.time.format('%H:%M'))
                    .tickSize(5);

           var boxNoBarWidth = (svgWidth + widthPad) / (dataPassed.length + 8);
        }
        else if (dataPassed.length > 5) {
            var xAxis = d3.svg.axis()
                    .scale(xScale)
                    .orient('bottom')
                    .ticks(d3.time.minutes, 30)
                    .tickFormat(d3.time.format('%H:%M'))
                    .tickSize(5);

           var boxNoBarWidth = (svgWidth + widthPad) / (dataPassed.length + 10);
        }

I also want to adjust the bar width and place ticks in centre of bar. 我也想调整条形宽度并将刻度线放置在条形中心。 Is there any way to achieve this with dynamically by code in time scale. 有什么方法可以通过时间尺度上的代码动态地实现这一目标。

http://www.d3noob.org/2014/02/making-bar-chart-in-d3js.html , this tutorial does it but with ordinal axis. http://www.d3noob.org/2014/02/making-bar-chart-in-d3js.html ,本教程使用序数轴进行此操作。

[UPDATE]: Firstly, many thanks for the help. [更新]:首先,非常感谢您的帮助。 In regard to adjusting width, below are the links to screenshots of my graphs: 关于调整宽度,以下是指向我的图表的屏幕截图的链接:

http://imgur.com/mkvnltx,TijKM8B#0 , http://imgur.com/mkvnltx,TijKM8B#1 http://imgur.com/mkvnltx,TijKM8B#0http://imgur.com/mkvnltx,TijKM8B#1

Hope that gives some idea of what I mean by bar width. 希望能对我所说的条宽有所了解。 Let me know if it is not clear. 让我知道是否不清楚。

As Lars suggested I tried ordinal axis, jsfiddle of same is here: http://jsfiddle.net/hpjqt1dL/28/ . 正如Lars建议的那样,我尝试了序数轴,相同的jsfiddle在这里: http : //jsfiddle.net/hpjqt1dL/28/ If you observe there the tickvalues are exact values as in data like ..9:06, 10.45, 11.06. 如果您在那里观察到,滴答值就是精确的值,如..9:06、10.45、11.06等数据。 I want tick values to be ranged properly at regular intervals. 我希望刻度值能够以固定的间隔进行适当调整。

.ticks() doesn't take a function argument, but in your case you don't need it, because you're not referencing any dynamic data bound to any elements. .ticks()不需要函数参数,但是在您的情况下,您不需要它,因为您没有引用绑定到任何元素的任何动态数据。 All you need is 所有你需要的是

.ticks(dataPassed.length > 5 ? (d3.time.minutes, 30) : (d3.time.minutes, 10))

Complete demo here . 在此处完成演示。

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

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