BarSeries and fixed width?

2 posts, 0 answers
  1. Regis
    Regis avatar
    1 posts
    Member since:
    Mar 2015

    Posted 24 Mar 2015 Link to this post

    Hi,

    How I can set a fixed width in BarSeries?

    Thanks
  2. Victor
    Admin
    Victor avatar
    1351 posts

    Posted 25 Mar 2015 Link to this post

    Hello Regis,

    Thanks for writing.
    Currently the only way to do this is to override the rendering of the bars. For example:
    public class CustomBarRenderer extends BarPointRenderer {
        private HashMap<DataPoint, PaletteEntry> pointColors = new HashMap<DataPoint, PaletteEntry>();
        public CustomBarRenderer(BarSeries series) {
            super(series);
        }
        public HashMap<DataPoint, PaletteEntry> pointColors() {
            return this.pointColors;
        }
        @Override
        protected void renderPointCore(Canvas canvas, DataPoint point) {
            BarSeries series = this.getSeries();
            float roundBarsRadius = series.getRoundBarsRadius();
            RadRect layoutSlot = point.getLayoutSlot();
            if(layoutSlot.getHeight() == 0 || layoutSlot.getWidth() == 0) {
                return;
            }
            RectF pointRect = Util.convertToRectF(layoutSlot);
            Paint fillPaint;
            Paint strokePaint;
            if(series.isPaletteApplied() && this.pointColors.containsKey(point)) {
                PaletteEntry entry = this.pointColors.get(point);
                fillPaint = new Paint();
                fillPaint.setColor(entry.getFill());
                strokePaint = new Paint();
                strokePaint.setStyle(Paint.Style.STROKE);
                strokePaint.setColor(entry.getStroke());
            } else {
                strokePaint = series.getFillPaint();
                fillPaint = series.getStrokePaint();
            }
            float strokeWidth = this.getSeries().getStrokeWidth();
            strokePaint.setStrokeWidth(strokeWidth);
            strokeWidth /= 2.0f;
            pointRect.left += strokeWidth;
            pointRect.right -= strokeWidth;
            pointRect.top += strokeWidth;
            pointRect.bottom -= strokeWidth;
            this.clampPointRect(pointRect);
            if (series.getAreBarsRounded()) {
                canvas.drawRoundRect(pointRect, roundBarsRadius, roundBarsRadius, fillPaint);
                canvas.drawRoundRect(pointRect, roundBarsRadius, roundBarsRadius, strokePaint);
            } else {
                canvas.drawRect(pointRect, fillPaint);
                canvas.drawRect(pointRect, strokePaint);
            }
        }
        private void clampPointRect(RectF rect) {
            // adjust point rect here
        }
    }

    Just fill in the clampPointRect() method to make the bars as wide as you need.

    Then you have to call barSeries.setDataPointRenderer(new CustomBarRenderer(barSeries));
    Keep in mind that there appears to be an issue that causes the chart palette to not be applied to the custom renderer so you will have to adjust the bar colors as well.

    Please write again if you have more questions.

    Regards,
    Victor
    Telerik
     

    See What's Next in App Development. Register for TelerikNEXT.

     
  3. DevCraft banner
Back to Top