在Android片段中正确显示图表

时间:2014-05-01 19:51:08

标签: android android-fragments charts fragment

我正试图在我的一个片段中设置一个图表。

问题是当我调用片段时应该调用图表我没有得到正确的布局。

以下是发生的事情:

enter image description here

我不明白为什么当我尝试添加图表时,布局会调整大小。 我正在使用aChartEngine。

这是我的“graph_fragment”的XML

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <LinearLayout
        android:id="@+id/linearLayout_graphFragment_conteiner"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:orientation="vertical" >
    </LinearLayout>

</RelativeLayout>

这就是我在片段中做的事情(BaseData extends Fragment):

public class LineGraphFragment extends BaseData {

    private GraphicalView mChartView;

    public LineGraphFragment() {
        // Empty constructor required for fragment subclasses
    }

    //----------------------------------------------------
    //Chart variables

    private TimeSeries series; //A single series or line is defined.
    private XYSeriesRenderer renderer; //A single render is responsible for a single series or line.
    private XYMultipleSeriesDataset mSeriesDataset; //All series or lines are added here to be displayed later.
    private XYMultipleSeriesRenderer mSeriesRendered; // All renders are added here. 
    //----------------------------------------------------




    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup conteiner, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.graph_fragment, conteiner, false);
    }

    @Override
    public void onStart(){
        super.onStart();

        Log.w("LineGraphFragment","Inside onStart");

        displayChart();
    }



    private void initializeChartValues() {
        int[] x = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
        int[] y = { 30, 34, 45, 57, 77, 90, 123, 23, 123, 92 };

        //Fill the series with data
        series = new TimeSeries("Line1");
        for (int i = 0; i < x.length; i++) {
            series.add(x[i], y[i]);
        }
        //Define a render for the series
        renderer = new XYSeriesRenderer();

        //Add the series in multiple series dataset
        mSeriesDataset = new XYMultipleSeriesDataset();
        mSeriesDataset.addSeries(series);

        //Add the renderer in the multiple series renderer
        mSeriesRendered = new XYMultipleSeriesRenderer();
        mSeriesRendered.addSeriesRenderer(renderer);
    }



    // WOrking on half screen
    private void displayChart() {

        //First initialize the values of the chart.
        initializeChartValues();

        //Define the parent where the chart will go
        LinearLayout chartContainer;
        chartContainer = (LinearLayout) getActivity().findViewById(R.id.linearLayout_graphFragment_conteiner);

        //Create and add the chart to the parent
        mChartView = ChartFactory.getLineChartView(getActivity(), mSeriesDataset, mSeriesRendered);
        chartContainer.addView(mChartView);

        //  viewParrent.addView(chartContainer);


    }
}

非常感谢任何帮助和信息。

1 个答案:

答案 0 :(得分:0)

我真的设法解决了我的问题。

以下是一些感兴趣的示例代码:

//----------------------------------------------------
    //Chart variables

    private GraphicalView mChartView;
    private TimeSeries series; //A single series or line is defined.
    private XYSeriesRenderer renderer; //A single render is responsible for a single series or line.
    private XYMultipleSeriesDataset mSeriesDataset; //All series or lines are added here to be displayed later.
    private XYMultipleSeriesRenderer mSeriesRendered; // All renders are added here. 

    //----------------------------------------------------


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.graph_fragment, container, false);

        // add the data here if necessary
        // ...
        LinearLayout layout = (LinearLayout) view.findViewById(R.id.graph1);

        layout.addView(displayChart());

        return view;
    }



    private void initializeChartValues() {
        int[] x = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
        int[] y = { 30, 34, 45, 57, 77, 90, 123, 111, 123, 92 };

        //Fill the series with data
        series = new TimeSeries("Line1");
        for (int i = 0; i < x.length; i++) {
            series.add(x[i], y[i]);
        }
        //Define a render for the series
        renderer = new XYSeriesRenderer();
        renderer.setColor(Color.RED);
        renderer.setPointStyle(PointStyle.SQUARE);
        renderer.setFillPoints(true);


        //Add the series in multiple series dataset
        mSeriesDataset = new XYMultipleSeriesDataset();
        mSeriesDataset.addSeries(series);

        //Add the renderer in the multiple series renderer
        mSeriesRendered = new XYMultipleSeriesRenderer();
        mSeriesRendered.addSeriesRenderer(renderer);
        //Customize graph itself
        mSeriesRendered.setChartTitle("Тегло");
    }


    // WOrking on half screen
    private GraphicalView displayChart() {

        //First initialize the values of the chart.
        initializeChartValues();

        //Define the parent where the chart will go
        LinearLayout chartContainer;
        chartContainer = (LinearLayout) getActivity().findViewById(R.id.linearLayout_graphFragment_conteiner);

        //Create and add the chart to the parent
        mChartView = ChartFactory.getLineChartView(getActivity(), mSeriesDataset, mSeriesRendered);


        return mChartView;

    }

我实际上从不使用onStart。 我还在onCreateView中初始化图表,然后在onCreateView

中返回视图