0

I have a fragment called NewFragment from where I am trying to call a method in a class called PerformanceGraph.

The class is supposed to plot a graph which I intend to display using the fragment. This is returned in an intent.

Here is my Fragment class:

public class NewFragment extends Fragment {

public static final NewFragment newInstance() {
    NewFragment f = new NewFragment();
    return f;
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.newfragment, container, false);
    PerformanceGraph line = new PerformanceGraph();
    Intent intent = line.getIntent(getApplicationContext());
    startActivity(intent);
    return v;
}
}

And here is my PerformanceGraph class:

public class PerformanceGraph {
public Intent getIntent(Context context) {

    int x[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 };
    int y[] = { 1, 4, 9, 16, 25, 36, 38, 39, 38, 37, 36, 32, 31, 30, 30 };
    int z[] = { 3, 4, 6, 7, 4, 3, 2, 2, 4, 3, 5, 5, 4, 6, 7 };
    String xaxis[] = { "jan", "feb", "mar", "apr", "may", "jun", "jul",
            "aug", "sept", "oct", "nov", "dec", "jan", "feb", "mar" };

    TimeSeries series = new TimeSeries("line1");
    TimeSeries series2 = new TimeSeries("line2");

    for (int i = 0; i < x.length; i++) {
        series.add(x[i], y[i]);
        series2.add(x[i], z[i]);

    }

    XYMultipleSeriesDataset dataset = new XYMultipleSeriesDataset();
    dataset.addSeries(series);
    dataset.addSeries(series2);

    XYSeriesRenderer renderer = new XYSeriesRenderer();
    renderer.setColor(Color.BLUE);
    renderer.setPointStyle(PointStyle.SQUARE);
    renderer.setFillPoints(true);

    XYSeriesRenderer renderer2 = new XYSeriesRenderer();
    renderer2.setColor(Color.DKGRAY);
    renderer2.setPointStyle(PointStyle.CIRCLE);
    renderer2.setFillPoints(false);

    XYMultipleSeriesRenderer mRenderer = new XYMultipleSeriesRenderer();
    mRenderer.addSeriesRenderer(renderer);
    mRenderer.addSeriesRenderer(renderer2);
    mRenderer.setChartTitle("haha");
    mRenderer.setXLabels(0);
    mRenderer.setXTitle("Duration");
    mRenderer.setYTitle("Price (ksh)");
    mRenderer.setApplyBackgroundColor(true);
    mRenderer.setAxisTitleTextSize(15);
    mRenderer.setChartTitleTextSize(20);
    mRenderer.setLabelsTextSize(15);
    mRenderer.setLegendTextSize(12);
    mRenderer.setPointSize(2f);
    mRenderer.setAxesColor(Color.BLACK);
    mRenderer.setYLabelsAlign(Align.RIGHT);
    mRenderer.setYLabelsVerticalPadding(2);
    mRenderer.setShowGridY(true);
    mRenderer.setGridColor(Color.GRAY);
    mRenderer.setLabelsColor(Color.parseColor("#5f5f5f"));
    mRenderer.setBackgroundColor(Color.parseColor("#F5F5F5"));
    mRenderer.setMarginsColor(Color.parseColor("#F5F5F5"));
    mRenderer.setPanEnabled(false, false);
    mRenderer.setZoomEnabled(false, false);

    for (int i = 0; i < x.length; i++) {
        mRenderer.addXTextLabel(x[i], xaxis[i]);
    }

    Intent intent = ChartFactory.getLineChartIntent(context, dataset,
            mRenderer, "Line graph title");
    return intent;
}
}

Problem is, in the fragment class am getting errors trying to call getIntent(). The application context has to be supplied in the argument.

How can I achieve this? Trying getIntent(getApplicationContext()) or getIntent(this) doesn't solve the problem.

4

1 回答 1

1

在您的 xml 片段中,将线性布局放置在您想要并自定义的位置。

 <LinearLayout
    android:layout_width="wrap_content"
    android:id="@+id/chart"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/textView1"
    android:layout_below="@+id/textView1"
    android:layout_marginLeft="22dp"
    android:layout_marginTop="23dp"
    android:orientation="vertical" >
</LinearLayout>

然后

 public View displayLineChart()
{
    int x[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 };
    int y[] = { 1, 4, 9, 16, 25, 36, 38, 39, 38, 37, 36, 32, 31, 30, 30 };
    int z[] = { 3, 4, 6, 7, 4, 3, 2, 2, 4, 3, 5, 5, 4, 6, 7 };
    String xaxis[] = { "jan", "feb", "mar", "apr", "may", "jun", "jul",
            "aug", "sept", "oct", "nov", "dec", "jan", "feb", "mar" };

    TimeSeries series = new TimeSeries("line1");
    TimeSeries series2 = new TimeSeries("line2");

    for (int i = 0; i < x.length; i++) {
        series.add(x[i], y[i]);
        series2.add(x[i], z[i]);

    }

    XYMultipleSeriesDataset dataset = new XYMultipleSeriesDataset();
    dataset.addSeries(series);
    dataset.addSeries(series2);

    XYSeriesRenderer renderer = new XYSeriesRenderer();
    renderer.setColor(Color.BLUE);
    renderer.setPointStyle(PointStyle.SQUARE);
    renderer.setFillPoints(true);

    XYSeriesRenderer renderer2 = new XYSeriesRenderer();
    renderer2.setColor(Color.DKGRAY);
    renderer2.setPointStyle(PointStyle.CIRCLE);
    renderer2.setFillPoints(false);

    XYMultipleSeriesRenderer mRenderer = new XYMultipleSeriesRenderer();
    mRenderer.addSeriesRenderer(renderer);
    mRenderer.addSeriesRenderer(renderer2);
    mRenderer.setChartTitle("haha");
    mRenderer.setXLabels(0);
    mRenderer.setXTitle("Duration");
    mRenderer.setYTitle("Price (ksh)");
    mRenderer.setApplyBackgroundColor(true);
    mRenderer.setAxisTitleTextSize(15);
    mRenderer.setChartTitleTextSize(20);
    mRenderer.setLabelsTextSize(15);
    mRenderer.setLegendTextSize(12);
    mRenderer.setPointSize(2f);
    mRenderer.setAxesColor(Color.BLACK);
    mRenderer.setYLabelsAlign(Align.RIGHT);
   // mRenderer.setYLabelsVerticalPadding(2);
    mRenderer.setShowGridY(true);
    mRenderer.setGridColor(Color.GRAY);
    mRenderer.setLabelsColor(Color.parseColor("#5f5f5f"));
    mRenderer.setBackgroundColor(Color.parseColor("#F5F5F5"));
    mRenderer.setMarginsColor(Color.parseColor("#F5F5F5"));
    mRenderer.setPanEnabled(false, false);
    mRenderer.setZoomEnabled(false, false);

    for (int i = 0; i < x.length; i++) {
        mRenderer.addXTextLabel(x[i], xaxis[i]);
    }

    mChartView = ChartFactory.getLineChartView(getActivity(), dataset,
            mRenderer);

    return mChartView;
}

然后在onCreateView

View v = inflater.inflate(R.layout.newfragment, container, false);
LinearLayout layout = (LinearLayout) view.findViewById(R.id.chart);
layout.addView(displayLineChart());

快照

在此处输入图像描述

于 2013-09-20T15:05:41.967 回答