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

Proper way to do callbacks? #5

Open
Shpigford opened this issue Feb 28, 2023 · 3 comments
Open

Proper way to do callbacks? #5

Shpigford opened this issue Feb 28, 2023 · 3 comments

Comments

@Shpigford
Copy link

In the Chart.js docs, it gives an example of changing the format of axis labels with a callback:

const chart = new Chart(ctx, {
    type: 'line',
    data: data,
    options: {
        scales: {
            y: {
                ticks: {
                    // Include a dollar sign in the ticks
                    callback: function(value, index, ticks) {
                        return '$' + value;
                    }
                }
            }
        }
    }
});

How would that be used in the context of this Stimulus component?

@cristi
Copy link

cristi commented Mar 11, 2023

@Shpigford did you figure out a way to pass the callback? Thanks

@Shpigford
Copy link
Author

@cristi Negative. 🙁

@cristi
Copy link

cristi commented Mar 11, 2023

@Shpigford I was hoping there is a better way, but this is how I ended up doing it:

import Chart from "stimulus-chartjs";
import merge from "lodash.merge";

export default class extends Chart {
  get chartOptions() {
    const yAxisCallback = {
      scales: {
        yAxes: {
          ticks: {
            callback: function(value, index, values) {
              return value.toLocaleString("en-US", { style: "currency", currency: "USD" } );
            }
          }
        }
      },
    }
    return merge(super.chartOptions, yAxisCallback);
  }
}

I'm extending the charts controller, augmenting the chartOptions and then using this controller in views. You can use the same approach if you need the currency formatting in the tooltips too.

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