0

我是新的 OOP 用户。

我在 Android 中有一个简单的问题。

我想用

new DrawMonthLineChart(titles, x, y).getChartView()

获取折线图视图。

(如果我在主代码中结合图表代码文件,折线图代码是正确的)

我将绘图部分放入独立文件。

但它会在这里显示错误。(下)

View chart = ChartFactory.getCubeLineChartView( this, dataset, renderer, 0.14f);

这是下面的错误消息:

The method getCubeLineChartView(Context, XYMultipleSeriesDataset,     XYMultipleSeriesRenderer, float) in the type ChartFactory is not applicable for the     arguments (DrawMonthLineChart, XYMultipleSeriesDataset, XYMultipleSeriesRenderer, float)

我认为问题是“这个”不意味着上下文。

我不知道如何将“this”修复为某事。

有没有人可以帮我一把?

非常感谢你。

我的主要代码如下:

    package net.test.achartenginedemo;

    import java.util.ArrayList;
    import java.util.List;

    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.os.Bundle;
    import android.app.Activity;
    import android.content.Intent;
    import android.graphics.Color;
    import android.graphics.Paint.Align;
    import android.util.Log;
    import android.view.Menu;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.view.ViewGroup.LayoutParams;
    import android.widget.LinearLayout;

    public class AchartengineMain extends Activity {

        private LinearLayout ll ;

                @Override
                protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_achartengine_main);

            ll = (LinearLayout) findViewById(R.id.chartLL);

            String[] titles = new String[] { "line1"};

            List<double[]> x = new ArrayList<double[]>();

            List<double[]> y = new ArrayList<double[]>();


            x.add(new double[] { 1, 2.2, 3.4, 4.6, 5.8, 10 });
            y.add(new double[] { 80, 78, 75, 82, 75, 78 });



            ll.addView(new DrawMonthLineChart(titles, x, y).getChartView(), new                 LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT));

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.achartengine_main, menu);
    return true;
}

}

我的 drawMonthChart 代码如下:

    package net.test.achartenginedemo;

    import java.util.List;

    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.graphics.Color;
    import android.graphics.Paint.Align;
    import android.view.View;

    public class DrawMonthLineChart {

        private String[] titles = new String[] { "line1" };
        private List<double[]> x;
        private List<double[]> y;
        public DrawMonthLineChart( String[] titles, List<double[]> x , List<double[]> y){
            this.titles = titles;
            this.x = x;
            this.y = y;

        }

        public View getChartView()
        {

            XYMultipleSeriesDataset dataset = buildDaset(titles, x, y);

            int[] colors = new int[] { Color.BLUE };

            PointStyle[] styles = new PointStyle[] { PointStyle.CIRCLE};


            XYMultipleSeriesRenderer renderer = buildRenderer(colors, styles, true);

            setChartSettings(renderer, "line chart test", "", "", 50, 200,-1, 13, 
            Color.BLACK,x);
            // ------------------***************here*******-------------
            View chart = ChartFactory.getCubeLineChartView( this, dataset, renderer, 0.14f);
            // ------------------***************here*******-------------

            return chart;

        }


private XYMultipleSeriesDataset buildDaset(String[] titles,
        List<double[]> xValues, List<double[]> yValues) {

    XYMultipleSeriesDataset dataset = new XYMultipleSeriesDataset();


    int length = titles.length;
    for (int i = 0; i < length; i++) {

        XYSeries series = new XYSeries(titles[i]);

        double[] xV = xValues.get(i);
        double[] yV = yValues.get(i);
        int seriesLength = xV.length;

        for (int k = 0; k < seriesLength; k++) {
            series.add(xV[k], yV[k]);
        }
        dataset.addSeries(series);
    }

    return dataset;
}


private XYMultipleSeriesRenderer buildRenderer(int[] colors,
        PointStyle[] styles, boolean fill) {

    XYMultipleSeriesRenderer renderer = new XYMultipleSeriesRenderer();
    int length = colors.length;

    for (int i = 0; i < length; i++) {
        XYSeriesRenderer r = new XYSeriesRenderer();
        r.setColor(colors[i]);
        r.setPointStyle(styles[i]);
        r.setFillPoints(fill);
        r.setLineWidth(2.5f);
        renderer.addSeriesRenderer(r);
    }


    return renderer;
}


private void setChartSettings(XYMultipleSeriesRenderer renderer,
        String title, String xTitle, String yTitle, double yMin,
        double yMax, double xMin, double xMax, int axesColor, List<double[]> x) {


            renderer.setChartTitle(title);
            renderer.setChartTitleTextSize(24);
            renderer.setXTitle(xTitle);
            renderer.setYTitle(yTitle);
            renderer.setXAxisMin(xMin);
            renderer.setXAxisMax(xMax);
            renderer.setYAxisMin(yMin);
            renderer.setYAxisMax(yMax);
            renderer.setYLabels(18);
            renderer.setXLabelsColor(axesColor);
            renderer.setLabelsColor(Color.BLACK);
            renderer.setShowGrid(true);
            renderer.setClickEnabled(false);
            renderer.setShowCustomTextGrid(true);
            renderer.setShowGridY(true);

        }
    }
4

1 回答 1

1

错误消息说明了一切:

Method needs:          (Context, XYMultipleSeriesDataset, XYMultipleSeriesRenderer, float)

Method gets:(DrawMonthLineChart, XYMultipleSeriesDataset, XYMultipleSeriesRenderer, float)

您的DrawMonthLineChart班级Object作为父母,但必须Context作为祖先。

因此,您需要类似的东西

public class DrawMonthLineChart extends Context {

或者

public class DrawMonthLineChart extends Activity {

或适当的东西。请参阅文档

于 2013-06-06T07:38:19.440 回答