Use AchartEngineActivity engine to draw bar charts and graphs for Android

Source: Internet
Author: User

1. Introduction AChartEngine (ACE) is an open-source chart Library (for Android) of Google ). It is powerful and supports scatter plots and line charts. For more information about how to use the internal class, download the response documentation (available on the home page ).
2. Development steps
1) create a new folder in the project, such as lib, used to store ACE Libraries, copy the achartegine-0.5.0.jar package to lib. Then add the jar Path to the Build Path of the project. 2) Modify AndroidManifest. xml is mainly used to add <activity >:< activity android: name = "org. achartengine. graphicalActivity "/> 3) Draw a bar chart public class main extends ListActivity {private static final int SERIES_NR = 2;/** Called when the activity is first created. */private ArrayList <Map <String, String> maps = new ArrayList <Map <String, String> (); @ Override public void onCreate (Bundle savedInstanceState) {super. onCrea Te (savedInstanceState); // setContentView (R. layout. main); // Add the ListItem "scheduling query" HashMap <String, String> map = new HashMap <String, String> (); map. put ("name", "Bar Chart"); map. put ("desc", "display Bar Chart"); maps. add (map); // create the listView adapter SimpleAdapter = new SimpleAdapter (this, maps, android. r. layout. simple_list_item_2, // The layout new String [] {"name", "desc"} that contains two textviews In the SDK library "}, // Two key new int [] {android. r. id. text1, android. r. id. text2} // two TextView IDs); this. setListAdapter (adapter);} // ListItem listener method protected void onListItemClick (ListView l, View v, int position, long id) {super. onListItemClick (l, v, position, id); XYMultipleSeriesRenderer renderer = getBarDemoRenderer (); Intent intent = ChartFactory. getBarChartIntent (this, getBarDemoDataset (), Renderer, Type. DEFAULT); startActivity (intent);} 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 ("Demo series" + (I + 1 )); for (int k = 0; k <nr; k ++) {series. add (100 + r. nextInt () % 100);} da Taset. addSeries (series. toXYSeries ();} return dataset;} public XYMultipleSeriesRenderer getBarDemoRenderer () {XYMultipleSeriesRenderer renderer = new enderer (); SimpleSeriesRenderer r = new SimpleSeriesRenderer (); r. setColor (Color. BLUE); renderer. addSeriesRenderer (r); r = new SimpleSeriesRenderer (); r. setColor (Color. GREEN); renderer. addSeriesRenderer (r); setChartSettings (re Nderer); return renderer;} private void setChartSettings (XYMultipleSeriesRenderer renderer) {renderer. setChartTitle ("Chart demo"); renderer. setXTitle ("x values"); renderer. setYTitle ("y values"); renderer. setXAxisMin (0.5); renderer. setXAxisMax (10.5); renderer. setYAxisMin (0); renderer. setYAxisMax (210) ;}} code parsing: In the onListItemClick method, after the user clicks "display Chart", constructs an intent object and sends a message to the AndroidM before using startActivity. Anifest. xml declares the activity (GraphicalActivity ). The chart is displayed. The key is the intent constructor ChartFactory. getBarChartIntent. ChartFactory provides many useful factory methods. If you need to generate a line chart, you can use its getLineChartIntent method. The getBarChartIntent method is a bit complicated and requires many parameters to be passed in. One of the objects is XYMultipleSeriesDataset, which is used to provide the dataset to be represented by the chart. Here we use getBarDemoDataset to get it. Another object of the XYMultipleSeriesRenderer type is used to provide some styles for Chart display. Here we use the getBarDemoRenderer method to obtain it. The getLineChartIntent method is boring and uses random numbers as chart data. Note that the bar chart supports multiple series. Two series of data are generated here. The getBarDemoRenderer method constructs a XYMultipleSeriesRenderer to set the colors of the two series, and then calls the setChartSettings method to set the following axis style. 4) Draw a curve import java. util. arrayList; import java. util. list; import org. achartengine. chartFactory; import org. achartengine. chart. pointStyle; import org. achartengine. model. XYMultipleSeriesDataset; import org. achartengine. model. XYSeries; import org. achartengine. renderer. XYMultipleSeriesRenderer; import org. achartengine. renderer. XYSeriesRenderer; import android. app. activity; import android. graphics. color; import android. OS. bundle; import android. view. view; public class chartDemo extends Activity {@ Override public void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); String [] titles = new String [] {"First", "Second"}; List x = new ArrayList (); List y = new ArrayList (); x. add (new double [] {1, 3, 5, 7, 9, 11}); x. add (new double [] {0, 2, 4, 6, 8, 10}); y. add (new double [] {3, 14, 5, 30, 20, 25}); y. add (new double [] {18, 9, 21, 15, 10, 6}); XYMultipleSeriesDataset dataset = buildDataset (titles, x, y ); int [] colors = new int [] {Color. BLUE, Color. GREEN}; PointStyle [] styles = new PointStyle [] {PointStyle. CIRCLE, PointStyle. DIAMOND}; XYMultipleSeriesRenderer renderer = buildRenderer (colors, styles, true); setChartSettings (renderer, "Line Chart Demo", "X", "Y",-1, 12, 0, 35, Color. WHITE, Color. WHITE); View chart = ChartFactory. getLineChartView (this, dataset, renderer); setContentView (chart);} protected inclubuilddataset (String [] titles, List xValues, List yValues) {includataset = new XYMultipleSeriesDataset (); int length = titles. length; // There are several lines for (int I = 0; I <length; I ++) {XYSeries series = new XYSeries (titles [I]); // create double [] xV = xValues Based on the name of each line. get (I); // obtain the data of line I, double [] yV = yValues. get (I); int seriesLength = xV. length; // There are several points for (int k = 0; k <seriesLength; k ++) // There are several points in each line {series. add (xV [k], yV [k]);} dataset. addSeries (series) ;}return dataset;} protected XYMultipleSeriesRenderer buildRenderer (int [] colors, PointStyle [] styles, boolean fill) {XYMultipleSeriesRenderer renderer = new XYMultipleSeriesRenderer (); int length = colors. length; for (int I = 0; I <length; I ++) {XYSeriesRenderer r = new XYSeriesRenderer (); r. setColor (colors [I]); r. setPointStyle (styles [I]); r. setFillPoints (fill); renderer. addSeriesRenderer (r);} return renderer;} protected void setChartSettings (XYMultipleSeriesRenderer renderer, String title, String xTitle, String yTitle, double xMin, double xMax, double yMin, double yMax, int axesColor, int labelsColor) {renderer. setChartTitle (title); renderer. setXTitle (xTitle); renderer. setYTitle (yTitle); renderer. setXAxisMin (xMin); renderer. setXAxisMax (xMax); renderer. setYAxisMin (yMin); renderer. setYAxisMax (yMax); renderer. setAxesColor (axesColor); renderer. setLabelsColor (labelsColor );}}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.