-
Notifications
You must be signed in to change notification settings - Fork 47
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
Adding formatter to Donut/Pie Chart label causes JavaScript Error #92
Comments
I have the same issue while trying to set a formatter on the total label of donut chart.
|
I think the problem here is that the Java wrapper sends the formatter value as a String while JavaScript side expects a function. I don't think it's possible to send functions via JSON directly since JSON is not supposed to transfer functions: https://stackoverflow.com/questions/2001449/is-it-valid-to-define-functions-in-json-results A workaround is needed: e.g. you'll need to attach the functions beforehand, say to the
Then the wrapper code would accept the formatter Strings as function names, such as |
I am able to set a formatter on other chart/label and it works. private val jvmMemoryChart = ApexChartsBuilder.get()
.withChart(ChartBuilder.get()
.withType(Type.bar)
.build())
.withPlotOptions(PlotOptionsBuilder.get()
.withBar(BarBuilder.get()
.withHorizontal(false)
.withColumnWidth("55%")
.build())
.build())
.withTitle(TitleSubtitleBuilder.get().withText("JVM Memory").build())
.withDataLabels(DataLabelsBuilder.get()
.withEnabled(true)
.withFormatter(jvmMemFormatterJsFucntion)
.build())
[...] With this formatter string: private val jvmMemFormatterJsFucntion =
"""
function(val, {seriesIndex, dataPointIndex, w}) {
return (parseFloat(val).toFixed(2)) + " kB";
}
""".trimIndent() When digging in the wrapper's JS code, this snippet must be the reason it works: if (this.dataLabels) {
this.config.dataLabels = JSON.parse(this.dataLabels);
if (this.config.dataLabels.formatter) {
this.config.dataLabels.formatter = this.evalFunction(this.config.dataLabels.formatter);
}
} The evalFunction() does the trick. Also, by looking at the wrapper's JS code, I don't see anywhere that it handles the plotOptions Pie/Donut label configurations and evalutate the formatter's string. It is just not implemented for all types of label. |
Excellent find! I can indeed see at if (this.plotOptions.pie && this.plotOptions.pie.donut && this.plotOptions.pie.donut.labels && this.plotOptions.pie.donut.labels.total && this.plotOptions.pie.donut.labels.total.formatter) {
this.plotOptions.pie.donut.labels.total.formatter = this.evalFunction(this.plotOptions.pie.donut.labels.total.formatter);
} (the same for |
Describe the bug
Adding a formatter via com.github.appreciated.apexcharts.config.plotoptions.pie.builder.ValueBuilder.withFormatter(String) causes JavaScript error "TypeError: (0 , t.value.formatter) is not a function".
Steps or code example to Reproduce
Sample Code:
Expected behavior
A formatted number should appear in the middle of the pie chart e.g. 1,000,000
Screenshots
The text was updated successfully, but these errors were encountered: