knowm / XChart

XChart is a light-weight Java library for plotting data.
http://knowm.org/open-source/xchart
Apache License 2.0
1.5k stars 397 forks source link

Label of Last Bar in CategoryChart Not Visible #707

Open daniel-fontaine opened 1 year ago

daniel-fontaine commented 1 year ago

When creating a CategoryChart with bar styling and visible labels, the label of the last bar in the series is not visible. This is a new bug introduced in the latest release of the library. The following code will demonstrate the issue:

        double[] xData = new double[] { 0.0, 1.0, 2.0, 3.0, 4.0 };                                                                                                                            
        double[] yData = new double[] { 2.0, 1.5, 4.0, 3.77, 2.5 };                                                                                                                           

        // Create Chart                                                                                                                                                                       
        CategoryChartBuilder builder = new CategoryChartBuilder();                                                                                                                            
        builder.title("Sample Chart")                                                                                                                                                         
            .xAxisTitle("X")                                                                                                                                                                  
            .yAxisTitle("Y")                                                                                                                                                                  
            .theme(Styler.ChartTheme.Matlab);                                                                                                                                                 
        CategoryChart chart = builder.build();                                                                                                                                                
        // chart.getStyler().setYAxisMin(1.0);                                                                                                                                                
        chart.getStyler().setLabelsVisible(true);                                                                                                                                             
        //.setDefaultSeriesRenderStyle(CategorySeries.CategorySeriesRenderStyle.Line).                                                                                                        

        chart.addSeries("y(x)", xData, yData);                                                                                                                                                

        // Show it                                                                                                                                                                            
        new SwingWrapper(chart).displayChart();

I have looked into the issue myself and the error occurs because the final bar in the series is drawn twice, with the second rendering overwriting the label. This extra rendering occurs due to a call to closePath on line 510 of PlotContent_Category_Bar.java that should not occur. I can fix this issue for the above example by setting 'path = null;' on line 312 following the first rendering of the bar. I am not sure if this bug occurs in other permutations of the CategoryChart. If so, my fix likely does not address those bugs. I hope this helps. Thanks for the hard work on this library.

timmolter commented 1 year ago

Thanks, I'll check it out soon hopefully. If you want, please create a PR with the fix.

mindlessolipsist commented 1 year ago

+1, running into this one, too.

Interestingly it doesn't happen all the time, but it does happen if I set the series colors on the ChartStyler. How is that related to the proposed "eliminate the redraw" fix?

abbas-aamir commented 1 year ago

@mindlessolipsist For me, this is appearing whether or not I set the series colors on the ChartStyler.

@timmolter Is there any update on this issue?

Anricx commented 1 year ago

+1, last bar color depper,label missing

image