Android开发中如何使用achartengine绘制各种图表
来源:爱站网时间:2021-03-15编辑:网友分享
Android平台上的AchartEngine可以制作折线图、曲线图、直方图、饼图等等的图标,本文是爱站技术频道小编带给大家的Android开发中如何使用achartengine绘制各种图表,一起进入下文学习吧!
Android平台上的AchartEngine可以制作折线图、曲线图、直方图、饼图等等的图标,本文是爱站技术频道小编带给大家的Android开发中如何使用achartengine绘制各种图表,一起进入下文学习吧!
分享给大家供大家参考,具体如下:
1. ABarChart.java
package com.anjoyo.achartengine; import java.util.Random; import org.achartengine.ChartFactory; import org.achartengine.chart.BarChart.Type; import org.achartengine.model.CategorySeries; import org.achartengine.model.XYMultipleSeriesDataset; import org.achartengine.renderer.SimpleSeriesRenderer; import org.achartengine.renderer.XYMultipleSeriesRenderer; import android.content.Context; import android.content.Intent; import android.graphics.Color; public class ABarChart { public Intent execute(Context context) { return ChartFactory.getBarChartIntent(context, getBarChartDataset(), getBarChartRenderer(), Type.DEFAULT); } private XYMultipleSeriesRenderer getBarChartRenderer() { // TODO Auto-generated method stub XYMultipleSeriesRenderer renderer=new XYMultipleSeriesRenderer(); SimpleSeriesRenderer r; r=new SimpleSeriesRenderer(); r.setColor(Color.BLUE); renderer.addSeriesRenderer(r); r=new SimpleSeriesRenderer(); r.setColor(Color.GREEN); renderer.addSeriesRenderer(r); setBarChartRenderer(renderer); return renderer; } private void setBarChartRenderer(XYMultipleSeriesRenderer renderer) { // TODO Auto-generated method stub renderer.setChartTitle("Bar Chart"); renderer.setXTitle("X Axis"); renderer.setYTitle("Y Axis"); renderer.setXAxisMax(10.5); renderer.setXAxisMin(0.5); renderer.setYAxisMax(210); renderer.setYAxisMin(0); } private XYMultipleSeriesDataset getBarChartDataset() { // TODO Auto-generated method stub XYMultipleSeriesDataset dataset=new XYMultipleSeriesDataset(); final int SERIES_NR=2; final int NR=10; Random random=new Random(); for (int i = 0; i
2. ABubbleChart.java
package com.anjoyo.achartengine; import java.util.Random; import org.achartengine.ChartFactory; import org.achartengine.chart.PointStyle; import org.achartengine.model.XYMultipleSeriesDataset; import org.achartengine.model.XYValueSeries; import org.achartengine.renderer.XYMultipleSeriesRenderer; import org.achartengine.renderer.XYSeriesRenderer; import android.content.Context; import android.content.Intent; import android.graphics.Color; public class ABubbleChart { private final int SERIES_NR = 3; private final int NR = 20; public Intent execute(Context context) { return ChartFactory.getBubbleChartIntent(context, getBubbleChartDataset(), getBubbleChartRenderer(), "Bubble Chart Demo"); } private XYMultipleSeriesRenderer getBubbleChartRenderer() { // TODO Auto-generated method stub XYMultipleSeriesRenderer renderer = new XYMultipleSeriesRenderer(); int[] linearColor = { Color.RED, Color.GREEN, Color.BLUE }; XYSeriesRenderer r; for (int i = 0; i (key, value); r = new XYSeriesRenderer(); r.setColor(linearColor[i]); r.setPointStyle(PointStyle.POINT); r.setLineWidth(2); renderer.addSeriesRenderer(r); } setBarChartRenderer(renderer); return renderer; } private XYMultipleSeriesDataset getBubbleChartDataset() { // TODO Auto-generated method stub XYMultipleSeriesDataset dataset = new XYMultipleSeriesDataset(); for (int i = 0; i
3. ALinearChart.java
package com.anjoyo.achartengine; import java.util.Random; 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.content.Context; import android.content.Intent; import android.graphics.Color; public class ALinearChart { private final int SERIES_NR=3; private final int NR=20; public Intent execute(Context context) { return ChartFactory.getLineChartIntent(context, getLinearChartDataset(), getLinearChartRenderer(), "Linear Chart Another"); } private XYMultipleSeriesRenderer getLinearChartRenderer() { // TODO Auto-generated method stub int[] linearColor={Color.RED, Color.GREEN, Color.BLUE}; int[] belowLinearColor={Color.YELLOW, Color.CYAN, Color.GRAY}; XYMultipleSeriesRenderer renderer=new XYMultipleSeriesRenderer(); XYSeriesRenderer tempRender; for (int i = 0; i
4. APieChart.java
package com.anjoyo.achartengine; import org.achartengine.ChartFactory; import org.achartengine.model.CategorySeries; import org.achartengine.renderer.DefaultRenderer; import org.achartengine.renderer.SimpleSeriesRenderer; import android.content.Context; import android.content.Intent; import android.graphics.Color; public class APieChart { private double[] values = { 25, 30, 45 }; public APieChart() { // TODO Auto-generated constructor stub } public APieChart(double[] values) { if (values.length == 3) { for (int i = 0; i
5. MainActivity.java
package com.anjoyo.achartengine; import android.os.Bundle; import android.app.Activity; import android.content.Intent; import android.view.Menu; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; public class MainActivity extends Activity { private Button lineChartBtn; private Button areaChartBtn; private Button scatterChartBtn; private Button timeChartBtn; private Button barChartBtn; private Button pieChartBtn; private Button bubbleChartBtn; private Button doughnutChartBtn; private Button rangeBarChartBtn; private Button dialChartBtn; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); lineChartBtn=(Button) findViewById(R.id.line_chart); areaChartBtn=(Button) findViewById(R.id.area_chart); scatterChartBtn=(Button) findViewById(R.id.scatter_chart); timeChartBtn=(Button) findViewById(R.id.time_chart); barChartBtn=(Button) findViewById(R.id.bar_chart); pieChartBtn=(Button) findViewById(R.id.pie_chart); bubbleChartBtn=(Button) findViewById(R.id.bubble_chart); doughnutChartBtn=(Button) findViewById(R.id.doughnut_chart); rangeBarChartBtn=(Button) findViewById(R.id.range_bar_chart); dialChartBtn=(Button) findViewById(R.id.dial_chart); OnClickListener listener=new AChartEngineListener(); lineChartBtn.setOnClickListener(listener); pieChartBtn.setOnClickListener(listener); barChartBtn.setOnClickListener(listener); bubbleChartBtn.setOnClickListener(listener); } class AChartEngineListener implements OnClickListener{ @Override public void onClick(View v) { // TODO Auto-generated method stub switch (v.getId()) { case R.id.line_chart: // Intent intent=new Intent(MainActivity.this, LinearChartActivity.class); // startActivity(intent); Intent linearChartIntent=new ALinearChart().execute(MainActivity.this); startActivity(linearChartIntent); break; case R.id.area_chart: break; case R.id.scatter_chart: break; case R.id.time_chart: break; case R.id.bar_chart: Intent barChartIntent=new ABarChart().execute(MainActivity.this); startActivity(barChartIntent); break; case R.id.pie_chart: Intent pieChartIntent=new APieChart().execute(MainActivity.this); startActivity(pieChartIntent); break; case R.id.bubble_chart: Intent bubbleChartIntent=new ABubbleChart().execute(MainActivity.this); startActivity(bubbleChartIntent); break; case R.id.doughnut_chart: break; case R.id.range_bar_chart: break; case R.id.dial_chart: break; default: break; } } } }
6. LinearChartActivity.java
package com.anjoyo.achartengine; import java.util.Random; import org.achartengine.ChartFactory; import org.achartengine.GraphicalView; 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.ViewGroup.LayoutParams; import android.widget.LinearLayout; public class LinearChartActivity extends Activity { private XYMultipleSeriesDataset dataset = new XYMultipleSeriesDataset(); private XYMultipleSeriesRenderer renderer = new XYMultipleSeriesRenderer(); private XYSeries currentSeries; private XYSeriesRenderer currentRenderer; private String dateFormat; private GraphicalView chartView; @Override protected void onRestoreInstanceState(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onRestoreInstanceState(savedInstanceState); dataset = (XYMultipleSeriesDataset) savedInstanceState .getSerializable("dataset"); renderer = (XYMultipleSeriesRenderer) savedInstanceState .getSerializable("renderer"); currentSeries = (XYSeries) savedInstanceState .getSerializable("current_series"); currentRenderer = (XYSeriesRenderer) savedInstanceState .getSerializable("current_renderer"); dateFormat = savedInstanceState.getString("date_format"); } @Override protected void onSaveInstanceState(Bundle outState) { // TODO Auto-generated method stub super.onSaveInstanceState(outState); outState.putSerializable("dataset", dataset); outState.putSerializable("renderer", renderer); outState.putSerializable("current_series", currentSeries); outState.putSerializable("current_renderer", currentRenderer); outState.putString("date_format", dateFormat); } @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.stock_chart); String seriesTitle = "上证指数"; XYSeries series = new XYSeries(seriesTitle); dataset.addSeries(series); currentSeries = series; XYSeriesRenderer seriesRenderer = new XYSeriesRenderer(); seriesRenderer.setColor(Color.BLUE); seriesRenderer.setFillBelowLine(true); // seriesRenderer.setFillBelowLineColor(Color.RED);//折线下面区域是否添加颜色 seriesRenderer.setPointStyle(PointStyle.POINT); seriesRenderer.setLineWidth(3); renderer.addSeriesRenderer(seriesRenderer); // 添加另外一条曲线图 // seriesRenderer = new XYSeriesRenderer(); // seriesRenderer.setColor(Color.GREEN); // seriesRenderer.setFillBelowLine(true); // seriesRenderer.setFillBelowLineColor(Color.YELLOW); // seriesRenderer.setPointStyle(PointStyle.POINT); // seriesRenderer.setLineWidth(3); // renderer.addSeriesRenderer(seriesRenderer); renderer.setYAxisMin(0); renderer.setYAxisMax(100); renderer.setXAxisMax(20); renderer.setShowGrid(false); renderer.setXLabels(20); renderer.setChartTitle("上证指数-24小时趋势图"); currentRenderer = seriesRenderer; double x = 0; double y = 0; int[][] xyValues = getValues(); // for (int i = 0; i
7. AndroidManifest.xml
本文是爱站技术频道小编为大家带来的Android开发中如何使用achartengine绘制各种图表,大家学习的怎样了呢?更多专业知识,大家可来到js.aizhan.com学习哦。