How to set value in x axis label in achartengine

AndroidDev picture AndroidDev · Mar 28, 2012 · Viewed 14.5k times · Source

Actually, in my app, I have created a graph using achartEngine. In these in the x-axis initially, I am displaying value from 1 to 31. Now instead of displaying this is it possible to display values like 1-03-2012, 2-03-2012, 3-03-2012 ... and so on up to last day current month and when it reaches last day of current month i.e. 31-03-2012 the value display after that should be 1-04-2012 2-04-2012...and so on. The code I have used while displaying value like 1 2 3 4 5... is given below. Can anyone help me to solve this out?

code for graph

 renderer = new XYMultipleSeriesRenderer(2);        
    int length = colors.length;     
    for (int i = 0; i < length; i++) 
    {
        XYSeriesRenderer r = new XYSeriesRenderer();
        r.setColor(colors[i]);
        r.setPointStyle(styles[i]);
        renderer.addSeriesRenderer(r);
    }
    
    int rendererLength = renderer.getSeriesRendererCount();
    for (int i = 0; i < rendererLength; i++) 
    {
      ((XYSeriesRenderer) renderer.getSeriesRendererAt(i)).setFillPoints(true);
    }               
    renderer.setAxisTitleTextSize(12);
    renderer.setChartTitleTextSize(12);
    renderer.setLabelsTextSize(10);
    renderer.setLegendTextSize(12);
    renderer.setPointSize(5f);
    
    renderer.setApplyBackgroundColor(true);
    renderer.setBackgroundColor(Color.parseColor("#F5F5F5"));
    renderer.setMarginsColor(Color.parseColor("#F5F5F5"));
    
    renderer.setChartTitle("Weight / Temperature");
    renderer.setXLabels(20);
    renderer.setXTitle(sdFormatter.format(currentDate));        
    renderer.setXAxisMin(1);
    renderer.setXAxisMax(nMaxDay);
    renderer.setXLabelsAlign(Align.CENTER);
                
    renderer.setYLabels(10);  
    renderer.setYTitle("Weight", 0);
    renderer.setYTitle("Temperature", 1);
    renderer.setYAxisMin(10, 0);
    renderer.setYAxisMax(90, 0);
    renderer.setYAxisMin(10, 1);
    renderer.setYAxisMax(90, 1);
    renderer.setYAxisAlign(Align.LEFT, 0);
    renderer.setYAxisAlign(Align.RIGHT, 1);
    renderer.setYLabelsAlign(Align.LEFT, 0);
    renderer.setYLabelsAlign(Align.RIGHT, 1);
    
    renderer.setAxesColor(Color.LTGRAY);
    renderer.setLabelsColor(Color.parseColor("#5f5f5f"));          
    renderer.setShowGrid(true);
    renderer.setGridColor(Color.GRAY);

screenshot shot enter image description here

Answer

Abhi picture Abhi · Mar 28, 2012

Use AverageTemperatureChart class from the Demo.

setChartSettings(renderer, "Average temperature", "Month", "Temperature", 0.5, 5.5, 0, 4,
        Color.LTGRAY, Color.LTGRAY);

then change like this and check the changes

 for (int i = 0; i < titles.length; i++) 
    {
      x.add(new double[] { 1, 2, 3, 4 });
       }

add four just for demo

And add label through renderer.addTextLabel();

ex:

String[] date={"1-3-2012","1-4-2012","1-5-2012","1-6-2012"}; 
for (int i = 0; i < date.length; i++) 
    { 

    renderer.addTextLabel(i+1, date[i]);
    }
    renderer.setXLabelsAlign(Align.CENTER);
    renderer.setXLabels(0);