Android开发中使用achartengine绘制各种图表的方法
程序员文章站
2023-12-15 10:03:58
本文实例讲述了android开发中使用achartengine绘制各种图表的方法。分享给大家供大家参考,具体如下:
1. abarchart.java
pack...
本文实例讲述了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 < series_nr; i++) { categoryseries series=new categoryseries("bar chart---"+(i+1)); for (int j = 0; j < nr; j++) { series.add(100+random.nextint(100)); } dataset.addseries(series.toxyseries()); } return dataset; } }
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 < series_nr; i++) { // entry=new xyentry<integer, integer>(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 < series_nr; i++) { xyvalueseries series = new xyvalueseries("bubble chart---" + (i + 1)); int temp = 10 * i; int[][] data = getbasicdata(); for (int j = 0; j < data.length; j++) { if((j+1)%5 ==0){ int x = data[j][0]; int y = temp; int value=data[j][1]+temp; series.add(x, y, value); } } dataset.addseries(series); } return dataset; } private int[][] getbasicdata() { int[][] data = new int[20][2]; random rand = new random(); for (int i = 0; i < data.length; i++) { data[i][0] = i; data[i][1] = 20 + rand.nextint(20) % 20; } return data; } private void setbarchartrenderer(xymultipleseriesrenderer renderer) { // todo auto-generated method stub renderer.setcharttitle("bubble chart"); renderer.setxtitle("x axis"); renderer.setytitle("y axis"); renderer.setxaxismax(20); renderer.setxaxismin(0); renderer.setyaxismax(30); renderer.setyaxismin(0); renderer.setantialiasing(true);//没有看出有啥效果来 renderer.setapplybackgroundcolor(true);//设置背景颜色 renderer.setbackgroundcolor(color.transparent); renderer.setshowaxes(true);// renderer.setaxescolor(color.gray); renderer.setclickenabled(true);//设置了之后,不可再拖动 renderer.setzoombuttonsvisible(true);//设置伸缩按钮是否可见 renderer.setzoomenabled(true); renderer.setexternalzoomenabled(true);//设置是否可以向伸缩 renderer.setlabelscolor(color.green); renderer.setlabelstextsize(16); } }
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 < series_nr; i++) { temprender=new xyseriesrenderer(); temprender.setcolor(linearcolor[i]); // temprender.setfillbelowline(true); // temprender.setfillbelowlinecolor(belowlinearcolor[i]); temprender.setpointstyle(pointstyle.point); temprender.setlinewidth(2); renderer.addseriesrenderer(temprender); } setlinearchartrenderer(renderer); return renderer; } private void setlinearchartrenderer(xymultipleseriesrenderer renderer) { // todo auto-generated method stub renderer.setcharttitle("three linear chart"); renderer.setxaxismax(20); renderer.setxaxismin(0); renderer.setyaxismax(60); renderer.setyaxismin(0); } private xymultipleseriesdataset getlinearchartdataset() { // todo auto-generated method stub xymultipleseriesdataset dataset=new xymultipleseriesdataset(); for (int i = 0; i < series_nr; i++) { xyseries series=new xyseries("stock trend---"+(i+1)); int temp=10*i; int[][] data=getbasicdata(); for (int j = 0; j < data.length; j++) { int x=data[j][0]; int y=temp+data[j][1]; series.add(x, y); } dataset.addseries(series); } return dataset; } private int[][] getbasicdata() { int[][] data=new int[20][2]; random rand=new random(); for (int i = 0; i < data.length; i++) { data[i][0]=i; data[i][1]=20+rand.nextint(20)%20; } return data; } }
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 < values.length; i++) { this.values[i] = values[i]; } } } public intent execute(context context) { int[] colors={color.blue, color.green, color.yellow}; defaultrenderer renderer=buildcategoryrenderer(colors); renderer.setantialiasing(true);//没有看出有啥效果来 renderer.setapplybackgroundcolor(true);//设置背景颜色 renderer.setbackgroundcolor(color.transparent); renderer.setshowaxes(true);// renderer.setaxescolor(color.gray); renderer.setclickenabled(true);//设置了之后,不可再拖动 renderer.setzoombuttonsvisible(true);//设置伸缩按钮是否可见 renderer.setzoomenabled(true); renderer.setexternalzoomenabled(true);//设置是否可以向伸缩 renderer.setlabelscolor(color.green); renderer.setlabelstextsize(16); // renderer.setlegendheight(50); // renderer.setpanenabled(true); renderer.setstartangle(180);//好像不好用啊 categoryseries series=new categoryseries("fruit ratio"); series.add("apple", values[0]); series.add("banana", values[1]); series.add("orange", values[2]); return chartfactory.getpiechartintent(context, series, renderer, "水果"); } private defaultrenderer buildcategoryrenderer(int[] colors) { defaultrenderer renderer=new defaultrenderer(); renderer.setapplybackgroundcolor(true); renderer.setbackgroundcolor(color.gray); renderer.setlabelstextsize(20); renderer.setcharttitle("饼状图"); renderer.setcharttitletextsize(20); renderer.setlegendtextsize(20); renderer.setlegendheight(50); for (int i = 0; i < colors.length; i++) { int j = colors[i]; simpleseriesrenderer r=new simpleseriesrenderer(); r.setcolor(j); renderer.addseriesrenderer(r); } return renderer; } }
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 < 2; i++) { // for (int j = 0; j < xyvalues.length; j++) { x=xyvalues[j][0]; y=xyvalues[j][1]; currentseries.add(x, y); } // } } @override protected void onresume() { // todo auto-generated method stub if(chartview == null){ linearlayout layout=(linearlayout) findviewbyid(r.id.chart); chartview=chartfactory.getlinechartview(this, dataset, renderer); layout.addview(chartview, new layoutparams(layoutparams.fill_parent, layoutparams.fill_parent)); }else { chartview.setbackgroundresource(r.id.chart); chartview.repaint(); } super.onresume(); } private int[][] getvalues() { // todo auto-generated method stub int[][] xyvalues = new int[20][2]; random rand=new random(); for (int i = 0; i < xyvalues.length; i++) { xyvalues[i][0]=i; xyvalues[i][1]=rand.nextint(100); } return xyvalues; } }
7. androidmanifest.xml
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.anjoyo.achartengine" android:versioncode="1" android:versionname="1.0" > <uses-sdk android:minsdkversion="8" android:targetsdkversion="17" /> <application android:allowbackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/appbasetheme" > <activity android:name="com.anjoyo.achartengine.mainactivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.main" /> <category android:name="android.intent.category.launcher" /> </intent-filter> </activity> <activity android:name=".linearchartactivity"></activity> <activity android:name="org.achartengine.graphicalactivity"></activity> </application> </manifest>
更多关于android相关内容感兴趣的读者可查看本站专题:《android图形与图像处理技巧总结》、《android开发入门与进阶教程》、《android调试技巧与常见问题解决方法汇总》、《android基本组件用法总结》、《android视图view技巧总结》、《android布局layout技巧总结》及《android控件用法总结》
希望本文所述对大家android程序设计有所帮助。
推荐阅读
-
Android开发中使用achartengine绘制各种图表的方法
-
Android系统开发中log的使用方法及简单的原理
-
Android开发使用自定义View将圆角矩形绘制在Canvas上的方法
-
Android开发中使用achartengine绘制各种图表的方法
-
Android系统开发中log的使用方法及简单的原理
-
Android开发使用自定义View将圆角矩形绘制在Canvas上的方法
-
Android中使用achartengine生成图表的具体方法
-
Android开发中button按钮的使用及动态添加组件方法示例
-
Android开发中TextView各种常见使用方法小结
-
Android开发 RadioButton使用中遇到的问题与解决方法