Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Correctly initialize min/max of boundary search #256

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

lucaswerkmeister
Copy link

If the minimum value is greater than 0, or the maximum value is less than 0, then initializing those values with 0 leads to incorrect results.


Example:

<html>
  <div id="chartContainer">
  <script src="/lib/d3.v3.4.8.js"></script>
  <script src="/lib/dimple.v2.2.0.min.js"></script>
  <script type="text/javascript">
    var svg = dimple.newSvg("#chartContainer", 590, 400);
    d3.tsv("/data/example_data.tsv", function (data) {
      var c = new dimple.chart(svg, data);
      c.addMeasureAxis("x", "Year").tickFormat = "d";
      c.addMeasureAxis("y", "Count");
      c.addSeries("Year", dimple.plot.line);
      c.draw();
    });
  </script>
</div>
</html>
Year	Count
2010	100
2011	97
2013	127
2014	106
2015	108
2016	78

Without this patch, X and Y axis will start at 0, squashing all the data into a tiny area of an otherwise empty chart.

If the minimum value is greater than 0, or the maximum value is less
than 0, then initializing those values with 0 leads to incorrect
results.
@CariMbo33
Copy link

If I get your point right,
try to use c.addTimeAxis("x", "Year"), instead of addMeasureAxis
Then you can apply date formatting as you want.
Hope it helps.
Scattolini

@lucaswerkmeister
Copy link
Author

That only works if the x axis is a time axis, which might be the case in this example, but isn’t always true in general. So using a time axis is only a workaround for a few cases.

More to the point – why does the time axis not have the same problem? Because the time axis is initialized correctly:

} else if (axis._hasTimeField()) {
    // Parse the dates and assign the min and max
    axis._min = null;
    axis._max = null;
    // …
        var dt = axis._parseDate(d[dimension]);
        if (axis._min === null || dt < axis._min) {
            axis._min = dt;
        }
        if (axis._max === null || dt > axis._max) {
            axis._max = dt;
        }
    // …
}

The axis doesn’t start with min, max = 0, it starts with them being unset and overwrites them with the first actual min and max found in the data. This pull request suggests doing the same thing for numeric axes (except that we start with ±∞ instead).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants