Axis value changes in barchart while swapping the phone using Achartengine

Posted by Vasu on Stack Overflow See other posts from Stack Overflow or by Vasu
Published on 2011-08-23T06:46:15Z Indexed on 2011/11/19 17:51 UTC
Read the original article Hit count: 387

Filed under:
|

Hi I have included the Barchart using AchartEngine API .When I swap the orientation of the screen the yaxis value is altered as same values. For eg. initially it is (0,10) , (10,25) but after swapping its changes to (0,10), (10,10) i could not understand why it is happening . And I have place string in x axis instead of numbers , I used addtextlabel method but the string is overlapped on the number . I need to display only the names. could you help on this. I have included my code here.

public class Analytics extends Activity implements OnClickListener {

    private Button settings_btn;
    private RelativeLayout relativeLayout3;
    private boolean isClicked = false;
    private static final int SERIES_NR = 1;
    static int multiple_of_five;
    private GraphicalView mChartView;
    XYMultipleSeriesRenderer renderer;
    static int value=20;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.analytics);
        settings_btn = (Button) findViewById(R.id.settings_btn);
        relativeLayout3 = (RelativeLayout) findViewById(R.id.relativeLayout3);
        settings_btn.setOnClickListener(this);
        if (SharedValues.isClicked) {
            relativeLayout3.setVisibility(View.VISIBLE);
        } else {
            relativeLayout3.setVisibility(View.GONE);
        }
         renderer = getBarDemoRenderer();
          setChartSettings(renderer);
          if (mChartView == null) {
                RelativeLayout layout = (RelativeLayout) findViewById(R.id.relativeLayout5);
//              mChartView= ChartFactory.getLineChartView(this, getDemoDataset(), getDemoRenderer());
                mChartView = ChartFactory.getBarChartView(getApplicationContext(),getBarDemoDataset(),renderer,Type.DEFAULT);
                layout.addView(mChartView,new LayoutParams(LayoutParams.FILL_PARENT, 280));

              } else {
                mChartView.repaint();
              }

//        Intent intent = ChartFactory.getLineChartIntent(this, getDemoDataset(), getDemoRenderer());
//        intent = ChartFactory.getBarChartIntent(this, getBarDemoDataset(), renderer, Type.DEFAULT);
//        startActivity(intent);
    }
    @Override
    protected void onResume() {
        super.onResume();


    }

    public XYMultipleSeriesRenderer getBarDemoRenderer() {
        XYMultipleSeriesRenderer renderer = new XYMultipleSeriesRenderer();
        renderer.setAxisTitleTextSize(16);
        renderer.setChartTitleTextSize(20);
        renderer.setLabelsTextSize(15);
        renderer.setLegendTextSize(15);
//        renderer.setApplyBackgroundColor(true); 
//        renderer.setBackgroundColor(R.color.chart_bg); 
//        renderer.setMarginsColor(R.color.settings_bg_color);
//        renderer.setBackgroundColor(getResources().getColor(R.color.background));
        renderer.setPanEnabled(false, false);
        renderer.setZoomEnabled(false, false);
        renderer.setMargins(new int[] {0, 10, 0, 0});
        SimpleSeriesRenderer r = new SimpleSeriesRenderer();
//        r.setColor(Color.MAGENTA);
//        renderer.addSeriesRenderer(r);
        r = new SimpleSeriesRenderer();
        r.setColor(Color.CYAN);
        renderer.addSeriesRenderer(r);
        return renderer;
      }
    private void setChartSettings(XYMultipleSeriesRenderer renderer) {
//        renderer.setChartTitle("Chart demo");
//        renderer.setXTitle("x values");
//        renderer.setYTitle("y values");
        renderer.setXAxisMin(2);
        renderer.setMarginsColor(Color.parseColor("#00F5DA81"));
        renderer.addXTextLabel(1.0, "Q1");
        renderer.addXTextLabel(3.0, "Q2");
        renderer.addXTextLabel(5.0, "Q3");
        renderer.addXTextLabel(7.0, "Q4");
        renderer.addXTextLabel(9.0, "Q5");
        renderer.setXAxisMax(20);
        renderer.setYAxisMin(0);
        renderer.setYAxisMax(100);
        renderer.setZoomEnabled(false, false);
//        renderer.setApplyBackgroundColor(true); 
//        renderer.setMarginsColor(R.color.settings_bg_color);
        renderer.setBackgroundColor(Color.TRANSPARENT);
//        renderer.setBackgroundColor(R.color.chart_bg); 
      }
    private XYMultipleSeriesDataset getDemoDataset() {
        XYMultipleSeriesDataset dataset = new XYMultipleSeriesDataset();
        final int nr = 10;
        Random r = new Random();
        for (int i = 0; i < SERIES_NR; i++) {
          XYSeries series = new XYSeries("");
          for (int k = 0; k < nr; k++) {
              if(k%2==1)
              {
                  series.add(0, 0);


              }
              else
              {
            series.add(k, 20);
              }
          }
          dataset.addSeries(series);
        }
        return dataset;
      }
    private XYMultipleSeriesRenderer getDemoRenderer() {
        XYMultipleSeriesRenderer renderer = new XYMultipleSeriesRenderer();
        renderer.setAxisTitleTextSize(6);
        renderer.setChartTitleTextSize(10);
        renderer.setLabelsTextSize(5);
        renderer.setLegendTextSize(5);
        renderer.setPointSize(5f);
//        renderer.setMarginsColor(R.color.settings_bg_color);
//        renderer.setApplyBackgroundColor(true); 
//        renderer.setBackgroundColor(R.color.chart_bg); 
        renderer.setMargins(new int[] {20, 30, 15, 0});
        XYSeriesRenderer r = new XYSeriesRenderer();
        r.setColor(Color.BLUE);
        r.setPointStyle(PointStyle.SQUARE);
        r.setFillBelowLine(true);
        r.setFillBelowLineColor(Color.WHITE);
        r.setFillPoints(true);
        renderer.addSeriesRenderer(r);
        r = new XYSeriesRenderer();
        r.setPointStyle(PointStyle.CIRCLE);
        r.setColor(Color.GREEN);
        r.setFillPoints(true);
        renderer.addSeriesRenderer(r);
        renderer.setAxesColor(Color.DKGRAY);
        renderer.setLabelsColor(Color.LTGRAY);
        return renderer;
      }
    private XYMultipleSeriesDataset getBarDemoDataset() {
        XYMultipleSeriesDataset dataset = new XYMultipleSeriesDataset();
        final int nr = 10;
        Random r = new Random();


        for (int i = 0; i < SERIES_NR; i++) {
          CategorySeries series = new CategorySeries("Quadrant");

          for (int k = 0; k < nr; k++) {
              value=value+5;

//            multiple_of_five=k+5;
//            Log.i("multiple_of_five", ""+multiple_of_five);
//            series.add(20 +multiple_of_five );
            if(k%2==1){
                series.add(value );

            }
            else
            {
                series.add(0);
            }
          }
          dataset.addSeries(series.toXYSeries());
        }
        return dataset;
      }
    @Override
    public void onClick(View v) {
        if (v == settings_btn) {

            if (SharedValues.isClicked) {
                relativeLayout3.setVisibility(View.GONE);
                SharedValues.isClicked = false;
            } else {
                relativeLayout3.setVisibility(View.VISIBLE);
                SharedValues.isClicked = true;
            }

        }

    }
}

© Stack Overflow or respective owner

Related posts about android

Related posts about achartengine