Use AChartEngine and achartengine to create curves, bar charts, and pie charts for Android

Source: Internet
Author: User

Use AChartEngine and achartengine to create curves, bar charts, and pie charts for Android

Previously, in java Development, charts were implemented using the JFreeChar component. Recently, there was a small project that required data analysis on the Android end. How can we implement charts? I checked that google provides an open-source component.AChartengine is very easy to use and can be used to draw various types of images, fully meeting the development needs. The following describes how to use it.

I. Preparations

Download the jar package


Network Disk address: (including API documentation, code)

II. Key points -- Use of AchartEngined
1. ChartFactory component

The ChartFactory component provides the getXXXX method to obtain different images, for example

GetBarChartView () bar chart

GetPieChartView () pie chart

GetLineChartView (Context context, XYMultipleSeriesDataset, XYMultipleSeriesRenderer renderer) // line chart

GetCubeLineChartView () // smooth Graph

2. CategorySeries and DefaultRenderer

These methods generally have three parameters in sequence (some parameters will be added for different images)

  Context context, CategorySeries dataset, DefaultRenderer renderer

Context: indicates the application Context.

CategorySeries: it is the data information of the chart. The element name and value AchartEngined are the percentages calculated based on this data.

DefaultRenderer: sets the image style, such as the color, title size, and background color of each element.

The code for creating a pie chart is as follows:

// Create the CategorySeries component CategorySeries series = new CategorySeries ("first quarter sales analysis"); series. add ("January", 30000); series. add ("February", 40000); series. add ("March", 36000); // create the ultrenderer component DefaultRenderer renderer = new DefaultRenderer (); renderer. setMargins (new int [] {20, 30, 0}); // set the indirect unit between legends to pxrenderer. setLabelsTextSize (15); // set the font size of the tag renderer. setLegendTextSize (15); // set the font size of the legend SimpleSeriesRenderer r1 = new SimpleSeriesRenderer (); r1.setColor (Color. BLUE); renderer. addSeriesRenderer (r1); // set the Color of the pie chart. In February, SimpleSeriesRenderer r2 = new SimpleSeriesRenderer (); r1.setColor (Color. GREEN); renderer. addSeriesRenderer (r2); // set the Color of the pie chart. In February, SimpleSeriesRenderer r3 = new SimpleSeriesRenderer (); r1.setColor (Color. RED); renderer. addSeriesRenderer (r3); // sets the color of the pie chart. The pie chart is generated in May. GraphicalView = ChartFactory. getPieChartView (context, series, renderer );
3. GraphicalView

These methods return the View -- GraphicalView of the rendered chart. You can use the addView () method of Layout to add it to the Layout for display. For example:

GraphicalView view=ChartFactory.getPieChartView(context, series, renderer);layout.addView(charView);

The steps for drawing other images are similar to those for creating a pie chart. You can test them using the API documentation.

Iii. Implementation case

This case includes drawing curves, bar charts, and pie charts. It is encapsulated based on the AchartEngined component. The above example of the core code has been explained.

Source code:


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: 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.