0

我正在尝试将图表集成到我正在构建的应用程序中,但是当我尝试将来自 achartEngine 的元素集成到片段中时遇到了很多困难。

我在这里找到了一些关于这个主题的讨论,但没有人帮助我(比如thisthis)。

任何人都可以提供帮助或指导吗?


Jackyto,这是我根据您的建议编写的代码,但仍然不适合我(崩溃)。: (

非常感谢!

public class LayoutOne extends Fragment {

private GraphicalView mChart;

private XYMultipleSeriesDataset mDataset = new XYMultipleSeriesDataset();

private XYMultipleSeriesRenderer mRenderer = new XYMultipleSeriesRenderer();

private XYSeries mCurrentSeries;

private XYSeriesRenderer mCurrentRenderer;

LinearLayout layout;

private void initChart() {
    mCurrentSeries = new XYSeries("Sample Data");
    mDataset.addSeries(mCurrentSeries);
    mCurrentRenderer = new XYSeriesRenderer();
    mRenderer.addSeriesRenderer(mCurrentRenderer);
}

private void addSampleData() {
    mCurrentSeries.add(1, 2);
    mCurrentSeries.add(2, 3);
    mCurrentSeries.add(3, 2);
    mCurrentSeries.add(4, 5);
    mCurrentSeries.add(5, 4);
}

public static Fragment newInstance(Context context) {
    LayoutOne f = new LayoutOne();  

    return f;
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
    ViewGroup root = (ViewGroup) inflater.inflate(R.layout.layout_one, null);
    layout = (LinearLayout) root.findViewById(R.id.chart);
    return root;
}

 public void onResume() {
        super.onResume();
        if (mChart == null) {
            initChart();
            addSampleData();
            mChart = ChartFactory.getCubeLineChartView(getActivity(), mDataset, mRenderer, 0.3f);
            layout.addView(mChart);
        } else {
            mChart.repaint();
        }
    }

}

当然,我是在我的布局中定义 - android: id = "@ + id / chart" 的。

4

1 回答 1

0

将此代码放入 onActivityCreated

 initChart();
 addSampleData();
 mChart = ChartFactory.getCubeLineChartView(getActivity(), mDataset, mRenderer, 0.3f);
 layout.addView(mChart);`
于 2014-04-16T10:00:53.623 回答