Use 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.
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 px renderer. 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.
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.