AndroidPlot: XYPlot How to make Bars with rounded corners

29 views
Skip to first unread message

Franzi Seidl

unread,
Nov 16, 2017, 8:35:46 AM11/16/17
to Androidplot

I am working with AndroidPlot since about one year to display different Diagrams in my app. Now I am working with BarCharts, the charts are finished but how can I make corners rounded?

I found some results for lines: How to make line with rounded (smooth) corners with AndroidPlot

I tried already:

BarFormatter formatter1,formatter2, formatter3;
formatter1.getBorderPaint().setStrokeJoin(Paint.Join.ROUND);
formatter1.getFillPaint().setStrokeJoin(Paint.Join.ROUND);

doesn't seems to have any impact.

then I tried:

XYPlot plot; //Initialized with UI-Diagrammm above
...
plot.setBorderStyle(XYPlot.BorderStyle.ROUNDED,5f,5f);

Does anyone had the problem already and got an answer how to do this.

Thanks for Help,

Franzi

Here is a link to my original post: https://stackoverflow.com/questions/47310224/androidplot-xyplot-how-to-make-bars-with-rounded-corners

Nick

unread,
Nov 16, 2017, 8:51:07 AM11/16/17
to andro...@googlegroups.com
Hi Franzi,

Just found your post on Stack Overflow.  I'll respond there.

Thx!
Nick

Franzi Seidl

unread,
Dec 5, 2017, 5:01:22 AM12/5/17
to Androidplot

Thanks for your hint. After long time I get to try your solution, but my problem now is now how can I replace the original Bar-Renderer inside the Plot with my own one?

public class RoundBarRenderer extends BarRenderer {

    public RoundBarRenderer(XYPlot plot){
        super(plot);
    }
    protected void drawBar(Canvas canvas, Bar bar, RectF rect) {

        // null yVals are skipped:
        if(bar.getY() == null) {
            return;
        }

        BarFormatter formatter = getFormatter(bar.i, bar.series);
        if(formatter == null) {
            formatter = bar.formatter;
        }
        if(rect.height() > 0 && rect.width() > 0) {
            if (formatter.hasFillPaint()) {
                canvas.drawRect(rect.left, rect.top, rect.right, rect.bottom,
                        formatter.getFillPaint());
            }

            if (formatter.hasLinePaint()) {
                //canvas.drawRect(rect.left, rect.top, rect.right, rect.bottom,
                        //formatter.getBorderPaint());
                canvas.drawRoundRect(rect.left,rect.top,rect.right,rect.bottom,5,5,formatter.getBorderPaint());
            }
        }

        PointLabelFormatter plf =
                formatter.hasPointLabelFormatter() ? formatter.getPointLabelFormatter() : null;

        PointLabeler pointLabeler =
                formatter != null ? formatter.getPointLabeler() : null;

        if (plf != null && plf.hasTextPaint() && pointLabeler != null) {
            canvas.drawText(pointLabeler.getLabel(bar.series, bar.i),
                    rect.centerX() + plf.hOffset, bar.yPix + plf.vOffset,
                    plf.getTextPaint());
        }
    }

}

Thanks

Franzi

Message has been deleted

Franzi

unread,
Dec 7, 2017, 9:25:55 AM12/7/17
to Androidplot

Bars are rendered by Androidplot's BarRenderer class which uses the Canvas.drawRect(...)method to draw individual bars. In order to get rounded edges it would either need to call Canvas.drawRoundRect or possibly even better, draw each bar as a path using Path.lineTo(...)and Path.arcTo(...) methods so that only the top of the bars are rounded. This functionality unfortunately does not exist yet but you could open a feature request if you want.

Or you could implement it yourself by creating a custom renderer that extends BarRenderer and overriding drawBar(...).

Reply all
Reply to author
Forward
0 new messages