-
Notifications
You must be signed in to change notification settings - Fork 5
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
Setting currency format #233
Comments
Did a bit of digging inside the code and you can set the format like this: options
.getScales()
.addScale(Scales.ScaleAxis.Y, new LinearScaleOptions()
.setTicks(new LinearTickOptions()
.setFormat(new CurrencyFormatOptions("currency", "USD"))
));
// ...
public static class CurrencyFormatOptions // Could maybe be a record on Java 17+
{
private String style;
private String currency;
public CurrencyFormatOptions()
{
}
public CurrencyFormatOptions(final String style, final String currency)
{
this.style = style;
this.currency = currency;
}
public String getStyle()
{
return this.style;
}
public void setStyle(final String style)
{
this.style = style;
}
public String getCurrency()
{
return this.currency;
}
public void setCurrency(final String currency)
{
this.currency = currency;
}
} In ChartJS 3+ you can also use Example code: options
.getScales()
.addScale(Scales.ScaleAxis.Y, new LinearScaleOptions()
.setTicks(new LinearTickOptions()
.setCallback(new JavaScriptFunction("(label) => `$ ${label}`")))); // JS Code goes here |
I added some example code/tests: Note that there is a slight rendering difference between using |
Excellent, thanks for digging this up! The callback solution made more sense for me so I could have $0.00 on the axis: options.getScales().addScale(ScaleAxis.Y, new LinearScaleOptions().setTicks(new LinearTickOptions().setCallback(
new JavaScriptFunction("(label) => Intl.NumberFormat('en-US', { style: 'currency', currency: 'USD'}).format(label)")
))); |
How do you use the resulting json in your client? When I use your callback example and send it over to my angular client as json it cannot be parsed because of the javascript function in json. How do I properly parse and use it?
But this sadly resolves in the following error: |
@lprender ChartJS configuration is not valid JSON in the first place, because there is custom stuff like functions inside. The library is also primarily designed for server generated pages and not really for passing the configuration from the server to the client (it's better to just pass the corresponding data to the client instead and then handle chart creation there). However if you still want to get the configuration from the backend you can use something like a custom JSON parser or |
Checklist
What is/are your question(s)?
I'm working on migrating from the old PrimeFaces p:lineChart to the p:chart element, but I'm not sure how to give setFormat the object it requires for currencies in the axis.
Here's the original JS object:
I want to know the Java equivalent for setting format here -- what should myCurrencyFormat be? The given type is just "Object."
Additional information
No response
The text was updated successfully, but these errors were encountered: