PMSI-AlignAlytics / dimple

An object-oriented API for business analytics
Other
2.73k stars 556 forks source link

Correctly initialize min/max of boundary search #256

Open lucaswerkmeister opened 8 years ago

lucaswerkmeister commented 8 years ago

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.

CariMbo33 commented 6 years ago

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 commented 6 years ago

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).