LinearLayoutでaddViewのグラフを

3626 ワード

activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
   >

    <Button
        android:id="@+id/head"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text=" " />

    <LinearLayout
        android:id="@+id/chart"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:orientation="horizontal" />

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <Button
            android:id="@+id/new_series"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="new_series" />
    </LinearLayout>
    	    
    
</LinearLayout>

activity_main.xml(このaddViewを追加する必要はありません)
 <activity android:name="org.achartengine.GraphicalActivity" />

MainActivity.java
package com.example.chartdemo_31_3;

import java.util.Random;

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

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        
        //  
        XYMultipleSeriesRenderer renderer = new XYMultipleSeriesRenderer();
        // 
        XYMultipleSeriesDataset dataset = new XYMultipleSeriesDataset();
        
        // 
        Random random = new Random();
        
        for (int i = 0; i < 2; i++)//2 
		{
			XYSeries series = new XYSeries("Test"+(i+1));// Title
        
	        //  
	        for (int j = 0; j < 10; j++)
			{
				series.add(j, 20+random.nextInt()%100);// xy 
			}
	        
	        // dataset 
	        dataset.addSeries(series);
		}
        
        // 
        XYSeriesRenderer xySeriesRenderer = new XYSeriesRenderer();
        //1 
        xySeriesRenderer.setColor(Color.GRAY);
        //2 
        xySeriesRenderer.setPointStyle(PointStyle.SQUARE);
        //3,  
        renderer.addSeriesRenderer(xySeriesRenderer);
        
        xySeriesRenderer = new XYSeriesRenderer();
        xySeriesRenderer.setColor(Color.BLUE);
        xySeriesRenderer.setPointStyle(PointStyle.CIRCLE);
        renderer.addSeriesRenderer(xySeriesRenderer);
        
//        renderer.setPanEnabled(false);// 
        
//        Intent intent = ChartFactory.getLineChartIntent(this, dataset, renderer);
//        startActivity(intent);
        View view = ChartFactory.getLineChartView(this, dataset, renderer);
        LinearLayout linearLayout = (LinearLayout) findViewById(R.id.chart);
        linearLayout.addView(view);
    }

}