Question

I made a hard-coded-input pie chart using AChartEngine. When I test it, I get this output (URL for the picture): http://postimg.org/image/xd1tbjssj/

The code what produce this:

import android.content.Context;
import android.graphics.Color;
import org.achartengine.ChartFactory;
import org.achartengine.GraphicalView;
import org.achartengine.model.CategorySeries;
import org.achartengine.renderer.DefaultRenderer;
import org.achartengine.renderer.SimpleSeriesRenderer;

public class PieChart {

    // colors to be used for the pie slices
    private static int[] colors = new int[]{
            Color.GREEN, Color.BLUE, Color.MAGENTA, Color.BLUE
    };

    private Context context;
    private CategorySeries dataset;
    private DefaultRenderer renderer;

    public PieChart(Context context) {
        this.context = context;
    }

    public GraphicalView getChart() {
        createDataSet();
        createRenderer();
        return ChartFactory.getPieChartView(context, dataset, renderer);
    }

    /**
     * Create the data set used by the pie chart by adding
     * a string representation and it's double value to a CategorySeries object
     */
    private void createDataSet() {
        String[] movies = new String[]{
                "Terminator", "Eredet", "A Wall Street farkasa"
        };

        double[] values = new double[]{
                5, 2, 3
        };

        dataset = new CategorySeries("");
        for (int i = 0; i < values.length; i++) {
            dataset.add(movies[i], values[i]);
        }
    }

    /**
     * Creates the renderer for the pie chart with the basic color scheme
     */
    private void createRenderer() {
        renderer = new DefaultRenderer();
        for (int i=0; i<dataset.getItemCount(); i++) {
            SimpleSeriesRenderer simpleSeriesRenderer = new SimpleSeriesRenderer();
            simpleSeriesRenderer.setColor(colors[i]);
            renderer.addSeriesRenderer(simpleSeriesRenderer);
        }
        renderer.setStartAngle(180);
        renderer.setShowLabels(true);
        renderer.setShowLegend(false);
    }

}

The function what use this class above:

private void displayChart() {
        if (mChartView == null) {
            chartContainer.removeAllViews();
            PieChart chart = new PieChart(CinemaActivity.this);
            mChartView = chart.getChart();
            chartContainer.addView(mChartView);
        } else {
            mChartView.repaint();
        }
    }

Has somebody met with this issue? Thanks in advance!


The extended code where I use displayChart() method (not relevant parts deleted):

private LinearLayout chartContainer;
private GraphicalView mChartView;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    // Check login status in database
    if (userFunctions.getinstance().isUserLoggedIn(getApplicationContext())) {
        // user already logged in show database
        prepareActionBar();
        setLayout();
        initViews();
        getActions();
    } else {
        // user is not logged in show login screen
   }
}

private void setLayout() {
    setContentView(R.layout.activity_cinema_dashboard);
}

private void initViews() {
    incomeTextView = (TextView) findViewById(R.id.income);
    content = (LinearLayout) findViewById(R.id.cinema_dashboard);
    chartContainer = (LinearLayout) findViewById(R.id.chart);
    progress = (ProgressBar) findViewById(R.id.cinema_dashboard_progress);
}

private void getActions() {
    displayChart();
    new GetIncome(sourceActivity.toString()).execute();
}

The XML layout:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:layout_width="match_parent"
                android:layout_height="match_parent">

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
                  android:id="@+id/cinema_dashboard"
                  android:orientation="vertical"
                  android:layout_width="match_parent"
                  android:layout_height="wrap_content"
                  android:layout_marginTop="@dimen/margin_medium"
                  android:layout_marginRight="@dimen/margin_medium"
                  android:layout_marginBottom="@dimen/margin_medium"
                  android:layout_marginLeft="@dimen/margin_medium">

        <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
                      android:layout_width="match_parent"
                      android:layout_height="wrap_content"
                      android:orientation="vertical">

            <TextView/>

            <TextView/>

        </LinearLayout>

    </LinearLayout>   

    <LinearLayout
                android:id="@+id/chart"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_centerInParent="true"
                android:layout_weight="1"
                android:orientation="horizontal" />

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
                  android:orientation="vertical"
                  android:layout_width="match_parent"
                  android:layout_height="wrap_content"
                  android:layout_alignParentBottom="true"
                  android:layout_marginTop="@dimen/margin_medium"
                  android:layout_marginRight="@dimen/margin_medium"
                  android:layout_marginBottom="@dimen/margin_medium"
                  android:layout_marginLeft="@dimen/margin_medium">

        <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
                      android:layout_width="match_parent"
                      android:layout_height="wrap_content"
                      android:orientation="vertical">

            <Button/>

            <Button/>

            <Button/>

        </LinearLayout>

    </LinearLayout>

    <ProgressBar />

</RelativeLayout>
Was it helpful?

Solution

This looks like a display issue that happens when the chart is placed in a certain combination of containers. In order to fix it, you can do:

renderer.setInScroll(true);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top