10

I am displaying 4 values in a bargraph where each bar is different color. when I am displaying the graph each bar overlapping each other. I want to make space between the bars I tried with setBarSpacing() by putting different values still its not working. is there any other idea.

package com.example.barchartview;

import org.achartengine.ChartFactory;
import org.achartengine.GraphicalView;
import org.achartengine.chart.BarChart.Type;
import org.achartengine.model.CategorySeries;
import org.achartengine.model.XYMultipleSeriesDataset;
import org.achartengine.renderer.XYMultipleSeriesRenderer;
import org.achartengine.renderer.XYSeriesRenderer;
import android.os.Bundle;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.graphics.Color;
import android.view.View;
import android.widget.LinearLayout;


@SuppressLint("ParserError")
public class MainActivity extends Activity {
    private GraphicalView BarChartView;


    @SuppressLint("ParserError")
    @Override
        public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        if (BarChartView==null )
        {

        XYMultipleSeriesRenderer renderer = buildBarRenderer();
        setChartSettings(renderer);
        BarChartView  = ChartFactory.getBarChartView(this, getBarDataset(), renderer, Type.DEFAULT);
        LinearLayout playout = (LinearLayout) findViewById(R.id.graph2);
        playout.addView(BarChartView);
        ;

        }
        else
        {
            BarChartView.repaint();
            BarChartView.setVisibility(View.GONE);
        }
    }

    private void setChartSettings(XYMultipleSeriesRenderer renderer)
    {
        renderer.setYTitle("Hits");
        renderer.setXAxisMin(0);
        renderer.setXAxisMax(7);
        renderer.setYAxisMin(0);
        renderer.setYAxisMax(900);
    }
    private XYMultipleSeriesDataset getBarDataset()
    {
        int[] y = {624,430,712,323};
        CategorySeries series1 = new CategorySeries("A");
        CategorySeries series2 = new CategorySeries("B");
        CategorySeries series3 = new CategorySeries("C");
        CategorySeries series4 = new CategorySeries("D");

        XYMultipleSeriesDataset dataset = new XYMultipleSeriesDataset();

        series1.add(y[0]);
        dataset.addSeries(series1.toXYSeries());

        series2.add(y[1]);
        dataset.addSeries(series2.toXYSeries());

        series3.add(y[2]);
        dataset.addSeries(series3.toXYSeries());

        series4.add(y[3]);
        dataset.addSeries(series4.toXYSeries());

        return dataset;
    }
    protected XYMultipleSeriesRenderer buildBarRenderer() {
        XYMultipleSeriesRenderer mrenderer = new XYMultipleSeriesRenderer();

        double[] range = {0,5,0,5};

        mrenderer.setAxisTitleTextSize(16);
        mrenderer.setChartTitleTextSize(20);
        mrenderer.setLabelsTextSize(15);
        mrenderer.setLegendTextSize(15);
        mrenderer.setBarSpacing(0);
        mrenderer.setXLabels(1);
        mrenderer.addXTextLabel(2.5, "");
        mrenderer.setMargins(new int[] {20, 30, 15, 0});
        mrenderer.setAxesColor(Color.WHITE);
        mrenderer.setChartTitle("");
        mrenderer.setXTitle("");
        mrenderer.setInitialRange(range, 1);

        XYSeriesRenderer renderer1 = new XYSeriesRenderer();
        renderer1.setColor(Color.GREEN);
        XYSeriesRenderer renderer2 = new XYSeriesRenderer();
        renderer2.setColor(Color.RED);
        XYSeriesRenderer renderer3 = new XYSeriesRenderer();
        renderer3.setColor(Color.BLUE);
        XYSeriesRenderer renderer4 = new XYSeriesRenderer();
        renderer4.setColor(Color.CYAN);

        mrenderer.addSeriesRenderer(renderer1);
        mrenderer.addSeriesRenderer(renderer2);
        mrenderer.addSeriesRenderer(renderer3);
        mrenderer.addSeriesRenderer(renderer4);

        return mrenderer;
    }
}

here is my code.

here is my activity_main.xml file

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/mainActivity"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <LinearLayout android:id="@+id/graph2"
       android:layout_width="160dip"
        android:layout_height="200dip"
        android:scaleType="fitEnd"
        android:layout_marginTop="40dip"
        android:layout_marginLeft="790dip"/>

  </RelativeLayout>
Cœur
  • 37,241
  • 25
  • 195
  • 267
Abhijit Chakra
  • 3,201
  • 37
  • 66
  • Hmm, changing `setBarSpacing()` makes a noticeable difference in the demo app, specifically `SalesStackedBarChart.java`. Post your code so we can see what's wrong. – Sam Dec 07 '12 at 05:16
  • yes i have putted my code please check it up thanks!!!! – Abhijit Chakra Dec 07 '12 at 05:43
  • 1
    You are calling a lot of methods twice, including `setBarSpacing()`. Which `setBarSpacing()` were you altering the first or the second? (You should remove the duplicate calls since they do nothing.) – Sam Dec 07 '12 at 06:04
  • the first one (removed the second setBarspacing()) i have edited my code but still the same effect. – Abhijit Chakra Dec 07 '12 at 06:10
  • The `setBarSpacing()` method adds a buffer between bars at _different_ indices, all four of your bars are on the _same_ index. You can switch to using only one renderer and dataset: [screenshot](http://i.stack.imgur.com/Z57h4.png), the bars all have the same color. I don't know of how to add spacing between bars on the same index. – Sam Dec 07 '12 at 07:38
  • i know how to make space between the bars if they are in same color .is it possible to make each bar with different indices here in the graph. – Abhijit Chakra Dec 07 '12 at 08:37
  • @rickky You have just accepted an answer that copies my answer almost entirely. Very nice of you! – Dan D. Apr 27 '13 at 19:39
  • I am also facing the issue, http://stackoverflow.com/questions/16355225/achartengine-xymultipleseriesrenderer-renderes-chart-for-null-values-and-shows-u Kindly share the code.. – user2190720 May 06 '13 at 11:34

1 Answers1

3

I changed all your series to XYSeries and add them simply to the dataset, without calling extra methods.

XYSeries series1 = new XYSeries("A");
dataset.addSeries(series1);

Then, add data like in a regular XYChart like this:

series1.add(1, value);
series2.add(2, value);
Dan D.
  • 32,246
  • 5
  • 63
  • 79
  • can you help me? Here is my question http://stackoverflow.com/questions/14851820/bar-chart-not-working-in-achartengine – moDev Feb 13 '13 at 11:03