10

I implemented the Line graph using the achartengine. But i want change the line graph background color. Somebody suggest that the following code for changing the background color.

mRenderer.setApplyBackgroundColor(true); mRenderer.setBackgroundColor(Color.RED);

But it will not change the entire background. I want to change the entire background is it possible? if yes, then how to do it please can anybody help me.The following image is the output of the previous code.I want to change the entire bgcolor(means remaining black color part to white also)Grpah without entire bgcolor

Alinegraph.java

public class ALinegraph extends Activity {

/** Called when the activity is first created. */

@Override

public void onCreate(Bundle savedInstanceState)

{

    super.onCreate(savedInstanceState);      
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);   
    setContentView(R.layout.main);

    Button Linegraph = (Button) findViewById(R.id.Linegraph);
    Linegraph.setOnClickListener(new OnClickListener(){

        @Override
        public void onClick(View v) {
              Linegraphpage ACTC = new Linegraphpage();
              Intent intent = ACTC.execute(ALinegraph.this);
              startActivity(intent);                  
        }});           
}

}

Linegraphpage.java

public class Linegraphpage extends AbstractDemoChart

{

  /**
   * Returns the chart name.
   * 
   * @return the chart name
   */
  public String getName() {
    return "Average temperature";
  }

  /**
   * Returns the chart description.
   * 
   * @return the chart description
   */
  public String getDesc() {
    return "The average temperature in 4 Greek islands (line chart)";
  }

  /**
   * Executes the chart demo.
   * 
   * @param context the context
   * @return the built intent
   */
  public Intent execute(Context context) {
    String[] titles = new String[] { "Crete","Corfu"};
    List<double[]> x = new ArrayList<double[]>();
    for (int i = 0; i < titles.length; i++) {
      x.add(new double[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 });
    }
    List<double[]> values = new ArrayList<double[]>();
    values.add(new double[] { 12.3, 12.5, 13.8, 16.8, 20.4, 24.4, 26.4, 26.1, 23.6, 20.3 });
    values.add(new double[] { 100, 100, 100, 100, 100, 100, 100, 100, 100, 100 });

    int[] colors = new int[] { Color.CYAN,Color.GREEN};
    PointStyle[] styles = new PointStyle[] { PointStyle.CIRCLE,PointStyle.POINT};
    XYMultipleSeriesRenderer renderer = buildRenderer(colors, styles);
    int length = renderer.getSeriesRendererCount();
    for (int i = 0; i < length; i++) {
      ((XYSeriesRenderer) renderer.getSeriesRendererAt(i)).setFillPoints(true);
    }
    setChartSettings(renderer, "", "", "price", 0, 12, 0, 190,
        Color.GREEN, Color.GREEN);
    renderer.setXLabels(10);
    renderer.setYLabels(7);    
    renderer.setShowGrid(false);
    renderer.setXLabelsAlign(Align.RIGHT);
    renderer.setYLabelsAlign(Align.RIGHT);
    renderer.setApplyBackgroundColor(true);
    renderer.setBackgroundColor(Color.WHITE);
    renderer.setZoomButtonsVisible(true);
    renderer.setPanLimits(new double[] { -10, 20, -10, 40 });
    renderer.setZoomLimits(new double[] { -10, 20, -10, 40 });

    Intent intent = ChartFactory.getLineChartIntent(context, buildDataset(titles, x, values),
        renderer, "");
    return intent;
  }

}

Updated image

enter image description here

Tae-Sung Shin
  • 20,215
  • 33
  • 138
  • 240
naresh
  • 10,332
  • 25
  • 81
  • 124

1 Answers1

44

you should also set your margincolor:

mRenderer.setApplyBackgroundColor(true);
mRenderer.setBackgroundColor(Color.RED);
mRenderer.setMarginsColor(Color.RED);

This will give a whole view of your graph background to red color.

Hanry
  • 5,481
  • 2
  • 40
  • 53
  • thanks it's working and more thing how to separate the y-axis labels and text(price) like X-axis also? Please refer the image bottom of the question. – naresh Oct 10 '11 at 11:11
  • 1
    i think you can use mRenderer.setMargins(new int[] {20, 30, 15, 0}); – Hanry Oct 10 '11 at 11:19
  • 1
    ya its working. Here 30 means distance between y-axis value and text. 15 means distance between x-axis text and symbols. i don't know the values 20 and 0. please can u explain these? – naresh Oct 10 '11 at 11:40
  • '20' means using this value u can to reduce the height of the y-axis. "0" means using this value u can to reduce the height of the x-axis. – naresh Oct 10 '11 at 11:50
  • one more thing how to move the price in the image to after label 100 in the yaxis.is it possible? if yes, then how to do it – naresh Oct 10 '11 at 11:53
  • you can refer [here](http://www.java2s.com/Open-Source/Android/Chart/achartengine/org/achartengine/renderer/DefaultRenderer.java.java-doc.htm#setMarginsint) – Hanry Oct 10 '11 at 11:54
  • unfortunately, setting a margin color of transparent results in a color of black ... huh. – Jeffrey Blattman Aug 21 '13 at 01:05
  • how can i show two color like above the line and below line – Issac Balaji Oct 13 '15 at 10:21
  • @IssacBalaji I have left working on this graph so long, but I think there is no provision of using different color in same line graph as it is defined once only and the color represents about the same entity that is plotted. May there be any update recently for this in library but I don't think its usual way to plotting the same graph in different color. – Hanry Oct 14 '15 at 06:16
  • i got solution adding these lines incomeRenderer.setFillBelowLine(true); incomeRenderer.setFillBelowLineColor(getResources().getColor(R.color.graph_shadow)); incomeRenderer.setFillPoints(true); – Issac Balaji Oct 14 '15 at 07:15