我正在尝试使用 AFreeChart 在我的 Activity 中显示图表,我已经在互联网上检查了很多文档,但我没有找到任何完整的示例,我的意思是如何构建图表并在我的布局中显示它,我需要用 GUI 来显示它(与我的 GUI 中的图像相同),您正在使用 eclipse (android 4.2)。
有人知道如何在android中使用AfreeChart吗?谢谢
最佳答案
好吧,因为这是一篇较旧的帖子,我不确定您是否能够找到这个问题的答案。这是在您的 Activity 中显示免费图表所需执行的操作。
创建一个扩展 ImageView 的自定义 View ,例如 blelow
public class ChartView extends ImageView { private Bitmap bitmap; private RectShape rectArea; private Canvas canvas; private AFreeChart chart; public ChartView( Context context, AttributeSet attributeSet ) { super(context, attributeSet); } public ChartView( Context context ) { super(context); intChart(); } private void intChart() { //Setting different width and height based on the orientation. if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) { bitmap = Bitmap.createBitmap(400, 200, Bitmap.Config.ARGB_8888); rectArea = new RectShape(0.0, 0.0, 400, 200); } else { bitmap = Bitmap.createBitmap(200, 200, Bitmap.Config.ARGB_8888); rectArea = new RectShape(0.0, 0.0, 200, 200); } } public void drawChart( AFreeChart chart ) { canvas = new Canvas(bitmap); this.chart = chart; this.chart.draw(canvas, rectArea); setImageBitmap(bitmap); } @Override protected void onDraw( Canvas canvas ) { super.onDraw(canvas); } }
创建一个 Activity ,如下所示,然后就可以开始了。我假设您已经创建了要传递给 View 的 AFreeChart 对象。
public class ChartActivity extends Activity { @Override protected void onCreate( Bundle savedInstanceState ) { super.onCreate(savedInstanceState); setContentView(R.layout.chart); ViewGroup viewGroup = (ViewGroup)getWindow().getDecorView().findViewById(android.R.id.content); ChartView chartView = new ChartView(this); chartView.drawChart(ChartFactory.createChart()/*Returns AFreechart object*/); viewGroup.addView(chartView); } }
chart.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > </LinearLayout>
希望这有帮助
关于android - afreechart Android eclipse esample,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15432373/