1

我尝试在布局中加载图形,使用 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 类,导致编译错误。如何从另一个类中获取方法以在另一个类中使用?这可能是我知道的对象编程的一个基本问题,但我在这里问这个也知道我是否在打算在布局中加载图形图表方面做得很好。我在 Eclipse 中工作。谢谢

4

1 回答 1

1

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.

于 2013-05-18T20:08:43.000 回答