android使用MPAndroidChart开源图表折线图


android使用MPAndroidChart开源图表折线图_第1张图片



1. 将mpandroidchartlibrary-2-1-6.ja包copy到项目的libs中在引用


2:布局文件

 


3:后台代码

public class OilReportformsFragment extends Fragment{

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// TODO Auto-generated method stub		
		//return super.onCreateView(inflater, container, savedInstanceState);
		return inflater.inflate(R.layout.oilreportforms, container, false);
	}
	
	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		
		//getActivity().getActionBar().setDisplayHomeAsUpEnabled(true); 		
		super.onActivityCreated(savedInstanceState);
		
		
		LineChart  mChart = (LineChart)getActivity().findViewById(R.id.chart1);
		
		
		mChart.setDescription("");
		mChart.setDrawGridBackground(false);

	        
	        ///aj
		mChart.setTouchEnabled(true);
		mChart.setDragEnabled(true);
		mChart.setScaleEnabled(true);
	        
		Typeface mTf = Typeface.createFromAsset(getActivity().getApplicationContext().getAssets(), "OpenSans-Regular.ttf");
	        
	        XAxis xAxis = mChart.getXAxis();
	        xAxis.setPosition(XAxisPosition.BOTTOM);
	        xAxis.setTypeface(mTf);
	        xAxis.setDrawGridLines(false);
	        xAxis.setDrawAxisLine(true);

	        YAxis leftAxis = mChart.getAxisLeft();
	        leftAxis.setTypeface(mTf);
	        leftAxis.setLabelCount(5, false);
	        
	        YAxis rightAxis = mChart.getAxisRight();
	        rightAxis.setTypeface(mTf);
	        rightAxis.setLabelCount(5, false);
	        rightAxis.setDrawGridLines(false);

	        // set data
	        mChart.setData((LineData)generateDataLine(1));

	        // do not forget to refresh the chart
	        // holder.chart.invalidate();
	        mChart.animateX(750);
	}
	
	 private LineData generateDataLine(int cnt) {

	        ArrayList e1 = new ArrayList();

	        
	        for (int i = 0; i < 12; i++) {
	            e1.add(new Entry((int) (Math.random() * 65), i));
	        }
	       

	        LineDataSet d1 = new LineDataSet(e1, "New DataSet " + cnt + ", (1)");
	        /*   d1.setLineWidth(2.5f);
	        d1.setCircleSize(4.5f);*/
	        
	        d1.setLineWidth(2.5f);
	        d1.setCircleSize(5.5f);//折线的圆点大小
	        d1.setHighLightColor(Color.rgb(244, 117, 117));
	        d1.setDrawValues(true);
	        
	        ArrayList e2 = new ArrayList();

	        for (int i = 0; i < 12; i++) {
	            e2.add(new Entry(e1.get(i).getVal() - 30, i));
	        }
        
	        ArrayList sets = new ArrayList();
	        sets.add(d1);
	        
	        //这个图标坐标显示对应的坐标是按照顺序对应的,第一个y对应第一个x,这么来的
	        ArrayList m = new ArrayList();
	        m.add("9:00:37");
	        m.add("9:30:28");
	        m.add("10:00");
	        m.add("10:30");
	        m.add("May");
	        m.add("Jun");
	        m.add("12:00:59");
	        m.add("Aug");
	        m.add("Sep");
	        m.add("Okt");
	        m.add("Nov");
	        m.add("Dec");
	        
	        
	        LineData cd = new LineData(m, sets);
	        return cd;
	    }

	@Override
	public void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setHasOptionsMenu(true);//添加菜单不调用该方法是没有用的
	}
	
	
	public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
		// TODO Auto-generated method stub
		menu.clear();
	    inflater.inflate(R.menu.backaj, menu);  		     	       	    
		super.onCreateOptionsMenu(menu, inflater);		
	}
	

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {  
		if(item.getItemId() == android.R.id.home)
		{ 
			getActivity().getActionBar().setDisplayHomeAsUpEnabled(false); 
			MainActivity mc =(MainActivity)getActivity();
			mc.switchContent(this,getActivity().getSupportFragmentManager().findFragmentByTag("more"),"");
		}	
		if(item.getItemId() == R.id.action_back)
		{ 
			MainActivity mc =(MainActivity)getActivity();
			mc.switchContent(this,getActivity().getSupportFragmentManager().findFragmentByTag("more"),"");
		}
	    return true;
	} 
}



4:常见异常处理

Typeface mTf = Typeface.createFromAsset(getActivity().getApplicationContext().getAssets(), "OpenSans-Regular.ttf");

java.lang.RuntimeException: native typeface cannot be made

需要把对应的文件复制到assets目录里边不然找不到这里的文件是OpenSans-Regular.ttf


5:mpandroidchart开源git地址

https://github.com/PhilJay/MPAndroidChart

git文档

https://github.com/PhilJay/MPAndroidChart/wiki


6:android mpandroidchart 设置y抽不从0开始,让y轴更具坐标自适应

YAxis yal =mChart.getAxisLeft();
		yal.setStartAtZero(false);
		
		YAxis yar =mChart.getAxisRight();
		yar.setStartAtZero(false);






你可能感兴趣的:(android,AndroidUi设计)