learner
learner

Reputation: 1321

AChartEngine Barchart bar alignment

I having some problems with Achartengine. It's very useful, I know. I'm facing a bar alignment problem. My labels are in center, while my bars seems to be a little bit displaced. I put a link for the image:

BarChart Alignment

The label alignment are circled in orange, while bar alignment are in green. Like you'll notice this behavior occurs on the whole barchart. This is my code:

public void execute(){
    for(int i = 0; i < xyRenderer.getSeriesRendererCount(); i++){
        SimpleSeriesRenderer seriesRenderer = xyRenderer.getSeriesRendererAt(i);
        seriesRenderer.setDisplayChartValues(true);
    }
    chartView = ChartFactory.getBarChartView(this.context, xyDataset, xyRenderer, BarChart.Type.STACKED);
}

private void setupRenderer(XYMultipleSeriesRenderer renderer){
    renderer.setXLabels(0);
    renderer.setMargins(new int[]{20, 20, 20, 20});
    renderer.setYLabels(5);
    renderer.setOrientation(Orientation.HORIZONTAL);
    renderer.setAxisTitleTextSize(12);
    renderer.setChartTitleTextSize(12);
    renderer.setLabelsTextSize(10);
    renderer.setLegendTextSize(10);
    renderer.setShowGrid(true);
    renderer.setFitLegend(true);    
    renderer.setXAxisMin(0);
    renderer.setYAxisMin(0);
    renderer.setXAxisMax(7);
    renderer.setYAxisMax(100);
    renderer.setXLabelsAlign(android.graphics.Paint.Align.CENTER);
    renderer.setXLabelsColor(Color.BLACK);
    renderer.setYLabelsColor(0, Color.BLACK);
    renderer.setAxesColor(Color.BLUE);
    renderer.setLabelsColor(Color.RED);
    renderer.setPanEnabled(false, false);
    renderer.setZoomEnabled(false, false);
    renderer.setApplyBackgroundColor(true);
    renderer.setGridColor(Color.argb(0x00, 0x01, 0x01, 0x01));
    renderer.setMarginsColor(Color.argb(0x00, 0x01, 0x01, 0x01));
    renderer.setBackgroundColor(Color.argb(0x00, 0x01, 0x01, 0x01));
    renderer.setBarSpacing(0.75);
    renderer.clearXTextLabels();
    renderer.setInScroll(true);
}

public void setChartData(HashMap<String, Double> teamData){
    createSeriesRenderer(1);

    double space = 0.75f;
    CategorySeries categorySeries = new CategorySeries("% aproveitamento");
    for(Map.Entry<String, Double> currentEntry : teamData.entrySet()){
        String label = currentEntry.getKey();
        DecimalFormat decimalFormat = new DecimalFormat("#0.0");
        String formattedNum = decimalFormat.format(currentEntry.getValue());
        xyRenderer.addXTextLabel(space, label);
        categorySeries.add(Double.parseDouble(formattedNum));
        space += 0.75;
    }

    xyDataset.addSeries(categorySeries.toXYSeries());
}

private void createSeriesRenderer(int howMany){
    for(int i = 0; i < howMany; i++){
        SimpleSeriesRenderer seriesRenderer = new SimpleSeriesRenderer();
        seriesRenderer.setColor(Color.RED);
        xyRenderer.addSeriesRenderer(seriesRenderer);
    }
}

The most strange is on the AChartEngine bar samples, this behavior doesn't occur and bar are align properly. Is this a bug of AChartEngine?

Upvotes: 3

Views: 2187

Answers (2)

user1703603
user1703603

Reputation:

Make your spacing renderer.setBarSpacing(-0.5) and see it will perfectly work for you, and you will get proper alignment for your bars.

Upvotes: 2

Dan D.
Dan D.

Reputation: 32391

Try to comment out the renderer.setBarSpacing(0.75) call and you should get nice alignment of bars.

Upvotes: 1

Related Questions