chart
chart copied to clipboard
Styling LinearScale needs full config of axis
Hi,
I'm trying to style a linear scale (just fontStyle
and fontColor
) of a LineChart:
LinearTicks ticks = new LinearTicks().setFontStyle(FontStyle.BOLD);
LinearScale axis = new LinearScale().setTicks(ticks);
LinearScales scales = new LinearScales().addxAxis(axis)
LineOptions options = new LineOptions().setScales(scales);
The problem is that the LinearScale always puts the property type: "linear"
into the the tick config, and chartjs then wants me to configure all the other stuff for the ticks/scale and no data is displayed if not.
If I remove the the property from the chart JSON config object before initializing the Chart with delete json.options.scales.xAxes[0].type
it's formating as expected.
Workaround for me was creating a class TypelessLinearScale extends LinearScale
with @JsonIgnore
on an overriden getType()
.
Not sure how to handle this problem "nicely", a type-less Scale would be nice, but setOptions(...)
on LineOptions
needs a LinearScale
and not a Scale<?>
or something.
Unfortunate issue, and nice of you to let me know. To resolve this, I was thinking of adding a custom serializer, that adds the type depending on some condition. However, at this point, I'm not entirely sure as to when adding the type will cause the chart to not be displayed. I've added a basic test to begin debugging the issue: https://github.com/mdewilde/chart/blob/58fb9147d436a42eca9f4472cc0051982716d9b7/src/test/java/be/ceau/chart/tests/TypelessLinearScaleTest.java
If you have any input on the issue, or any more info on which specific configurations trigger the problem, please do let me know.
Sorry for the late reply...
I looked around in the chart.js repo and found this issue (https://github.com/chartjs/Chart.js/issues/4687) and it seems that "I'm holding it wrong" and should have used a category axis