Click here to Skip to main content
15,844,387 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Need to Draw a Graph in AndroidPlot as shown in the image

Hi guys! I need to plot the below graph as it is in Android using AndroidPlot:

Target Graph: http://i.stack.imgur.com/vbMsl.png
My Current Graph: http://i.stack.imgur.com/TXQyn.png

I need exactly 4 grid line along x axis and the labels as show in the above image 

This my code:

    import java.text.DecimalFormat;
    import java.util.Arrays;
    import com.androidplot.series.XYSeries;
    import com.androidplot.ui.SizeLayoutType;
    import com.androidplot.ui.SizeMetrics;
    import com.androidplot.xy.BoundaryMode;
    import com.androidplot.xy.LineAndPointFormatter;
    import com.androidplot.xy.SimpleXYSeries;
    import com.androidplot.xy.XYPlot;
    import com.androidplot.xy.XYStepMode;
    import android.app.Activity;
    import android.graphics.Color;
    import android.os.Bundle;

    public class MultitouchAndroidplotActivity extends Activity 
    {

	@Override
    public void onCreate(Bundle savedInstanceState) 
	{
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
		MultitouchPlot multitouchPlot = (MultitouchPlot) findViewById(R.id.multitouchPlot);
		
        // Create two arrays of y-values to plot:
        Number[] series1Numbers = {90, 85, 80, 75, 50, 40};
		Number[] series2Numbers = {50, 50, 50, 50, 50, 50};
		
		//Adding a White Background setting Borders etc..
		multitouchPlot.setBorderStyle(XYPlot.BorderStyle.NONE, null, null);
		multitouchPlot.setPlotMargins(0, 0, 0, 0);
		multitouchPlot.setPlotPadding(0, 0, 0, 0);
		multitouchPlot.setGridPadding(0, 10, 5, 0);

		multitouchPlot.setBackgroundColor(Color.WHITE);

		multitouchPlot.getGraphWidget().setSize(new SizeMetrics(
	            0, SizeLayoutType.FILL,
	            0, SizeLayoutType.FILL));

		multitouchPlot.getGraphWidget().getBackgroundPaint().setColor(Color.WHITE);
		multitouchPlot.getGraphWidget().getGridBackgroundPaint().setColor(Color.WHITE);

		multitouchPlot.getGraphWidget().getDomainLabelPaint().setColor(Color.BLACK);
		multitouchPlot.getGraphWidget().getRangeLabelPaint().setColor(Color.BLACK);

		multitouchPlot.getGraphWidget().getDomainOriginLabelPaint().setColor(Color.BLACK);
		multitouchPlot.getGraphWidget().getDomainOriginLinePaint().setColor(Color.BLACK);
		multitouchPlot.getGraphWidget().getRangeOriginLinePaint().setColor(Color.BLACK);
	    
		 //Remove legend
		multitouchPlot.getLayoutManager().remove(multitouchPlot.getLegendWidget());
		multitouchPlot.getLayoutManager().remove(multitouchPlot.getDomainLabelWidget());
		multitouchPlot.getLayoutManager().remove(multitouchPlot.getRangeLabelWidget());
		multitouchPlot.getLayoutManager().remove(multitouchPlot.getTitleWidget());

		//Domain
        multitouchPlot.setDomainStep(XYStepMode.INCREMENT_BY_VAL, 1);
        // get rid of the decimal place on the display:
        multitouchPlot.setDomainValueFormat(new DecimalFormat("#"));
        

        // Reduce the number of Domain labels
        //multitouchPlot.setTicksPerDomainLabel(3);
        
        //Range
        multitouchPlot.setRangeStep(XYStepMode.INCREMENT_BY_VAL, 10);
        // get rid of the decimal place on the display:
        multitouchPlot.setRangeValueFormat(new DecimalFormat("#"));
        //multitouchPlot.setRangeValueFormat(new DecimalFormat("0"));
        
        // Reduce the number of range labels
        //multitouchPlot.setTicksPerRangeLabel(3);

        // By default, AndroidPlot displays developer guides to aid in laying out your plot.
        // To get rid of them call disableAllMarkup():
        multitouchPlot.disableAllMarkup();
        
        //Setting up fix Boundaries for x and y
        multitouchPlot.setRangeBoundaries(0, 100, BoundaryMode.GROW);
		multitouchPlot.setDomainBoundaries(0, 5, BoundaryMode.GROW);
		
		// Turn the above arrays into XYSeries:
        XYSeries series1 = new SimpleXYSeries(
                Arrays.asList(series1Numbers),          // SimpleXYSeries takes a List so turn our array into a List
                SimpleXYSeries.ArrayFormat.Y_VALS_ONLY, // Y_VALS_ONLY means use the element index as the x value
                "Obw—d brzucha");                       // Set the display title of the series

		// Same as above, for series2
        XYSeries series2 = new SimpleXYSeries(Arrays.asList(series2Numbers), 
        		SimpleXYSeries.ArrayFormat.Y_VALS_ONLY,
                "Series2");
       
     // Create a formatter to use for drawing a series using LineAndPointRenderer:
        LineAndPointFormatter series1Format = new LineAndPointFormatter(
                Color.rgb(29, 95, 155),                   // line color
                Color.rgb(29, 95, 155),                   // point color
                Color.rgb(210,223,235));              // fill color (optional)
        
        
        // Add series1 to the xyplot:
        multitouchPlot.addSeries(series1, series1Format);

        // Same as above, with series2:
        multitouchPlot.addSeries(series2, new LineAndPointFormatter(
        		Color.rgb(20, 160, 229), 
        		Color.rgb(29, 95, 155), 
        		null));

        
    
    }
}
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900