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

Create a way to format the values in donuts #75

Closed
luizkowalski opened this issue Sep 19, 2012 · 2 comments
Closed

Create a way to format the values in donuts #75

luizkowalski opened this issue Sep 19, 2012 · 2 comments

Comments

@luizkowalski
Copy link

I came across this thing: I'm saving the values in seconds, but I need to show them in hours:minutes...
Would be nice if Morris had a formatter to show values on Donuts charts (and other as well)

@oesmith
Copy link
Contributor

oesmith commented Sep 20, 2012

I've just pushed a solution to master. You can now pass a function to Morris.Donut to format the value however you need to. Grab the latest morris.min.js and give this a go:

Morris.Donut({
  element: 'donut',
  data: /* your data here */
  formatter: function (x) { return Math.floor(x / 60) + ':' + Morris.pad2(x % 60) }
});

Disclaimer: again, a quick+dirty solution - feel free to customize the formatter to fit your needs :)

@luizkowalski
Copy link
Author

Worked like a charm!
Thank you. I'm closing this issue now

Actual code

<script>
function lpad(originalstr, length, strToPad) {
  originalstr = originalstr.toString();
  while (originalstr.length < length)
    originalstr = strToPad + originalstr;
  return originalstr;
}

Morris.Donut({
  element: 'donut',
  data: <%= @donut.map { |x| {:label => x['label'], :value => x['value'].to_i} }.to_json.to_s.html_safe %>,
  formatter: function (x) { return Math.floor(x / 3600) + ':' + lpad((Morris.pad2(x % 3600)/60), 2, "0") }
});
</script>

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

2 participants