2019独角兽企业重金招聘Python工程师标准>>>
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"><Buttonandroid:id="@+id/head"android:layout_width="fill_parent"android:layout_height="wrap_content"android:text="测试用顶部" /><LinearLayoutandroid:id="@+id/chart"android:layout_width="fill_parent"android:layout_height="wrap_content"android:layout_weight="1"android:orientation="horizontal" /><LinearLayoutandroid:layout_width="fill_parent"android:layout_height="wrap_content"android:orientation="horizontal" ><Buttonandroid:id="@+id/new_series"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="new_series" /></LinearLayout></LinearLayout>
activity_main.xml(经过测试 发现不需要 只有在图表生成Intent跳转新页面的时候才需要添加这个 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 {@Overrideprotected 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);}}