This is a migrated thread and some comments may be shown as answers.

BarSeries and fixed width?

1 Answer 54 Views
Chart
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Regis
Top achievements
Rank 1
Regis asked on 24 Mar 2015, 03:49 PM
Hi,

How I can set a fixed width in BarSeries?

Thanks

1 Answer, 1 is accepted

Sort by
0
Victor
Telerik team
answered on 25 Mar 2015, 12:04 PM
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.

 
Tags
Chart
Asked by
Regis
Top achievements
Rank 1
Answers by
Victor
Telerik team
Share this question
or