chart icon indicating copy to clipboard operation
chart copied to clipboard

Styling LinearScale needs full config of axis

Open benweidig opened this issue 7 years ago • 2 comments

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.

benweidig avatar Nov 02 '17 10:11 benweidig

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.

mdewilde avatar Nov 14 '17 20:11 mdewilde

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

benweidig avatar Dec 04 '17 15:12 benweidig