我尝试使用AChartEngine在布局中加载图形,即数学图形。我从网站上下载了一个演示zip文件,我意识到我需要像sin-cosin grahp风格的东西。因此,这是我编写的代码:
package com.myproject;
import org.achartengine.ChartFactory;
import org.achartengine.GraphicalView;
import org.achartengine.chart.PointStyle;
import org.achartengine.model.XYMultipleSeriesDataset;
import org.achartengine.renderer.XYMultipleSeriesRenderer;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.view.View;
import android.webkit.WebView;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import com.myproject.clases.AbstractDemoChart;
public class MainActivity extends Activity {
private WebView ventana;
private GraphicalView mChartView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
XYMultipleSeriesDataset mDataset = null;//this line causes runtime error but I will see this issue later.
int [] colors = new int[] { Color.BLUE, Color.CYAN };
PointStyle[] styles = new PointStyle[] { PointStyle.POINT, PointStyle.POINT };
XYMultipleSeriesRenderer mRenderer = buildRenderer(colors, styles);
LinearLayout layout = (LinearLayout) findViewById(R.id.chart);
mChartView = ChartFactory.getLineChartView(this, mDataset, mRenderer);
layout.addView(mChartView, new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
}
}
但是线
XYMultipleSeriesRenderer mRenderer = buildRenderer(colors, styles);
由于方法buildRenderer()属于AbstractDemoChart类而导致编译错误。如何从另一个类中采用一种方法以在另一个类中使用?这可能是我所知道的对象编程的一个基本问题,但是我想在这里询问是否也可以在布局中加载grahp图。我正在Eclipse中工作。谢谢
最佳答案
buildRenderer
是AbstractDemoChart
中的一种受保护方法。但是在AChartExampleActivity样本中,buildRenderer
是执行此操作的私有方法:
private XYMultipleSeriesRenderer buildRenderer(int[] colors,PointStyle[] styles)
{
XYMultipleSeriesRenderer renderer = new XYMultipleSeriesRenderer();
setRenderer(renderer, colors, styles);
return renderer;
}
您看到GeneratedChartDemo代码了吗?它以这种方式创建渲染器:
private XYMultipleSeriesRenderer getDemoRenderer() {
XYMultipleSeriesRenderer renderer = new XYMultipleSeriesRenderer();
renderer.setAxisTitleTextSize(16);
renderer.setChartTitleTextSize(20);
renderer.setLabelsTextSize(15);
renderer.setLegendTextSize(15);
renderer.setPointSize(5f);
renderer.setMargins(new int[] {20, 30, 15, 0});
...
我认为您应该更改初始化
XYMultipleSeriesRenderer
的方式。