From: Neal Nelson Date: Thu, 17 Jun 2010 12:36:56 +0000 (+0200) Subject: Add yAxisLabelFormatter option and make it's default value yValueFormatter. X-Git-Tag: v1.0.0~636^2~8 X-Git-Url: https://adrianiainlam.tk/git/?a=commitdiff_plain;h=84fc6aa72eaae08080d95d56123d495a1cb11cde;p=dygraphs.git Add yAxisLabelFormatter option and make it's default value yValueFormatter. --- diff --git a/docs/index.html b/docs/index.html index a36fd2e..e95d540 100644 --- a/docs/index.html +++ b/docs/index.html @@ -706,6 +706,16 @@ perl -ne 'BEGIN{print "Month,Nominal,Real\n"} chomp; ($m,$cpi,$low,$close,$high) Function to call to format values along the x axis.
Tests: xAxisLabelFormatter
+ + + yAxisLabelFormatter + function(x) + yValueFormatter + + Function used to format values along the Y axis. By default it uses the same as the yValueFormatter unless specified. +
Tests: yAxisLabelFormatter
+ + rightGap integer @@ -823,6 +833,25 @@ perl -ne 'BEGIN{print "Month,Nominal,Real\n"} chomp; ($m,$cpi,$low,$close,$high) + + xValueFormatter + function(x) + + + Function to provide a custom display format the X value for mouseover. + + + + + yValueFormatter + function(x) + (Round to 2 decimal places) + + Function to provide a custom display format for the Y value for mouseover. +
Tests: yAxisLabelFormatter
+ + + diff --git a/dygraph.js b/dygraph.js index ee5306c..ee08802 100644 --- a/dygraph.js +++ b/dygraph.js @@ -1378,10 +1378,12 @@ Dygraph.dateTicker = function(startDate, endDate, self) { * Add ticks when the x axis has numbers on it (instead of dates) * @param {Number} startDate Start of the date window (millis since epoch) * @param {Number} endDate End of the date window (millis since epoch) + * @param self + * @param {function} formatter: Optional formatter to use for each tick value * @return {Array.} Array of {label, value} tuples. * @public */ -Dygraph.numericTicks = function(minV, maxV, self) { +Dygraph.numericTicks = function(minV, maxV, self, formatter) { // Basic idea: // Try labels every 1, 2, 5, 10, 20, 50, 100, etc. // Calculate the resulting tick spacing (i.e. this.height_ / nTicks). @@ -1433,7 +1435,12 @@ Dygraph.numericTicks = function(minV, maxV, self) { for (var i = 0; i < nTicks; i++) { var tickV = low_val + i * scale; var absTickV = Math.abs(tickV); - var label = Dygraph.round_(tickV, 2); + var label; + if (formatter != undefined) { + label = formatter(tickV); + } else { + label = Dygraph.round_(tickV, 2); + } if (k_labels.length) { // Round up to an appropriate unit. var n = k*k*k*k; @@ -1458,7 +1465,8 @@ Dygraph.numericTicks = function(minV, maxV, self) { Dygraph.prototype.addYTicks_ = function(minY, maxY) { // Set the number of ticks so that the labels are human-friendly. // TODO(danvk): make this an attribute as well. - var ticks = Dygraph.numericTicks(minY, maxY, this); + var formatter = this.attr_('yAxisLabelFormatter') ? this.attr_('yAxisLabelFormatter') : this.attr_('yValueFormatter'); + var ticks = Dygraph.numericTicks(minY, maxY, this, formatter); this.layout_.updateOptions( { yAxis: [minY, maxY], yTicks: ticks } ); }; diff --git a/tests/y-axis-formatter.html b/tests/y-axis-formatter.html new file mode 100644 index 0000000..346f319 --- /dev/null +++ b/tests/y-axis-formatter.html @@ -0,0 +1,86 @@ + + + dygraph + + + + + + + + +

Potential Y Axis formatting problems for small values

+ +

The problem using default y axis formatting for very small values:

+
+ + +

The solution using a Y axis formatting function:

+
+ + +

Different yValueFormatter and yAxisLabelFormatter functions:

+
+ + + +