customizing Piechart displayed by using ChartEngin

2019-07-27 22:32发布

问题:

I am successfully displaying charts by using Chart Engine but I am suffering from below problems:

  1. I am unable to reduce the Chart Size.
  2. Chart is moving on the screen but I don't want that.
  3. I want to display one more image in the bottom of the screen but here chart is displayed in the middle of the screen. I just want to display chart at the top and below image(How to set margins).

Please any one can help me.Thanks in advance.

Following is my code:

public class MainActivity extends Activity {    

private String[] mMonth = new String[] {
            "Jan", "Feb" , "Mar", "Apr", "May", "Jun",
            "Jul", "Aug" , "Sep", "Oct", "Nov", "Dec"
        };

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


    openChart();                        


}

private void openChart(){       

    double[] distribution = { 10,200,3,400,500,60} ;
    String[] dis = { "10","200","13","400","500","60"} ;
    // Color of each Pie Chart Sections
    int[] colors = { Color.BLUE, Color.MAGENTA, Color.GREEN, Color.CYAN, Color.RED,
                     Color.YELLOW };

    // Instantiating CategorySeries to plot Pie Chart       
    CategorySeries distributionSeries = new CategorySeries("Sample");
    for(int i=0 ;i < distribution.length;i++){
        // Adding a slice with its values and name to the Pie Chart
        distributionSeries.add(dis[i],distribution[i]);

    }   

    // Instantiating a renderer for the Pie Chart
    DefaultRenderer defaultRenderer  = new DefaultRenderer();       
    for(int i = 0 ;i<distribution.length;i++){          
        SimpleSeriesRenderer seriesRenderer = new SimpleSeriesRenderer();       
        seriesRenderer.setColor(colors[i]);
        seriesRenderer.setDisplayChartValues(true);

        // Adding a renderer for a slice
        defaultRenderer.setShowLegend(false);
        defaultRenderer.addSeriesRenderer(seriesRenderer);
        int mar[]=defaultRenderer.getMargins();
        System.out.println(mar);
    }

    // Creating an intent to plot bar chart using dataset and multipleRenderer      
    Intent intent = ChartFactory.getPieChartIntent(getBaseContext(), distributionSeries , defaultRenderer, "");     

    // Start Activity
    startActivity(intent);

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.activity_main, menu);
    return true;
}
}

回答1:

You can set the margins by using:

defaultRenderer.setMargins(margins);

You can disable panning this way:

defaultRenderer.setPanEnabled(false, false);

You can create a chart in a view and embed it inside an activity that you define the layout for, see this.