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

Rounding problems #1498

Closed
jibaku opened this issue Sep 29, 2015 · 4 comments
Closed

Rounding problems #1498

jibaku opened this issue Sep 29, 2015 · 4 comments
Assignees

Comments

@jibaku
Copy link

jibaku commented Sep 29, 2015

On some graph (using 2.0.0-dev), I've rounding problems, where I get 103.9999999999 instead of 104 etc.

Rounding problem
Rounding problems

The code for the rounding with 103 / 104 issue

<script src="/static/js/bootstrap.min.js"></script>

<script type="text/javascript" src="/static/js/Chart.min.js"></script>
<script type="text/javascript">
  var lineChartData = {"tension": 0, "legendTemplate": "", "type": "line", "data": {"labels": ["22/09", "23/09", "24/09", "25/09", "26/09", "27/09", "28/09", "29/09"], "datasets": [{"borderColor": "rgba(220,220,220,0.5)", "pointBorderWidth": "rgba(220,220,220,0.5)", "backgroundColor": "rgba(220,220,220,0.5)", "borderDash": [5, 5], "pointBorderColor": "rgba(220,220,220,0.5)", "pointBackgroundColor": "rgba(220,220,220,0.5)", "data": [103, 103, 103, 103, 104, 104, 104, 104], "label": "Likes"}]}, "tooltipTemplate": "", "options": {"responsive": true, "scales": {"xAxes": [{"display": true}], "yAxes": [{"display": true}]}}};
  function updateLegend() {
      $legendContainer = $('#legendContainer');
      $legendContainer.empty();
      $legendContainer.append(window.myBar.generateLegend());
  }
  window.onload = function() {

      Chart.defaults.global.elements.line.tension = 0;
      var ctx2 = document.getElementById("line").getContext("2d");
      window.myBar = new Chart(ctx2, lineChartData);
  };
</script>
@etimberg
Copy link
Member

@jibaku we probably need a better default template function. In your case you could change the template to "<%=value.toFixed(1)%>" which would probably fix the problem

@artesea
Copy link

artesea commented Oct 2, 2015

I've forced value.toFixed() on both tooltips (where the floats have lots of dps) and also on the axis to avoid this issue. Although it means some graphs go from 0.0 to 1000.0 whilst others are 0.0 to 1.8

@etimberg
Copy link
Member

etimberg commented Oct 2, 2015

@artesea you could try running the ticks through a function that does something like the following.

  1. Determine numerical distance between ticks
  2. numDecimal = -1 * Math.floor(Math.log10(tickDistance))
  3. numDecimal = Math. Max(numDecimal, 0)
  4. return tickValue.toFixed(numDecimal)

That should show the best number of decimal places given the scale range

@etimberg
Copy link
Member

Closing since #1567 was merged

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

No branches or pull requests

3 participants