2017-03-16 2 views
1

DEMOErhalten konstanten Abstand von Zecken Wert in d3.scaleTime()

Ich brauche einen konstanten Abstand von Zecken Wert zwischen jedem beliebigen Datum zu erhalten, mit d3.scaleTime() und Grundstück in einer Achse.

Aber

timeScale.nice(intervalValue); 
//or 
axisXScale.tickValues(timeScale.ticks(10)); 

hilft nicht, ich bin immer wieder zufällige Anzahl der Ticks Werte zu bekommen.

Wie pro D3 Dokumentation

# continuous.ticks([count]) 

Returns approximately count representative values from the scale’s domain. If count is not specified, it defaults to 10. 
The returned tick values are uniformly spaced, have human-readable values (such as multiples of powers of 10), and are guaranteed to be within the extent of the domain. 
Ticks are often used to display reference lines, or tick marks, in conjunction with the visualized data. 
The specified count is only a hint; the scale may return more or fewer values depending on the domain. 

So ist es eine Möglichkeit, wir eine konstante Zahl bekommen, statt Annäherung

Antwort

0

DEMO

Also ging ich nur mit dem Ansatz eine Reverse-Zeitskala mit und das Datum aus dem x-Wert finden.

var timeScaleReverse = d3.scaleTime().range([start, end]).domain([0, width]); 

    timeScale.nice(intervalValue); 

    var tickValues = []; 

    for (var i = 0; i < width; i += (width/intervalValue)) { 
    tickValues.push(new Date(timeScaleReverse(i))); 
    } 
0

Ich denke, Sie, so etwas zu tun:

.ticks(d3.timeMonth.every(1)) 

Für weitere Details unter http://www.d3noob.org/2016/08/changing-number-of-ticks-on-axis-in.html.

Hoffe diese Hilfe!

+0

Aber wie wir eine konsistente Anzahl zwischen zwei Terminen mit 'd3.timeMonth' – Sarath

+0

bekommen können, können Sie es gerne verwenden: d3.axisBottom (Zeitskala) .tickSize (0) .ticks (d3.timeMonth.every (1)) –

+0

Wenn ich ein einjähriges Intervall habe, gibt es 12 und wenn sein 6-Monats-Intervall 6 ist, Was ich will, ist in allen Fällen eine konstante Zahl – Sarath