Android LineChart绘制多条曲线的方法

这篇文章主要为大家详细介绍了Android LineChart绘制多条曲线的方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

本文实例为大家分享了Android LineChart绘制多条曲线的具体代码,供大家参考,具体内容如下

目标效果:

1.新建custom_marker_view.xml页面作为点击弹出框的页面:

   

2.activity_main.xml页面:

  

3.新建MyMarkerView.java重写MarkView控件:

 package com.example.weixu.drawline; import android.content.Context; import android.widget.TextView; import com.github.mikephil.charting.data.CandleEntry; import com.github.mikephil.charting.data.Entry; import com.github.mikephil.charting.utils.MarkerView; import com.github.mikephil.charting.utils.Utils; public class MyMarkerView extends MarkerView { private TextView tvContent; public MyMarkerView(Context context, int layoutResource) { super(context, layoutResource); tvContent = (TextView) findViewById(R.id.tvContent); } @Override public void refreshContent(Entry e, int dataSetIndex) { if (e instanceof CandleEntry) { CandleEntry ce = (CandleEntry) e; tvContent.setText("" + Utils.formatNumber(ce.getHigh(), 0, true)); } else { tvContent.setText("" +e.getVal()); } } }

4.MainActivity.java页面:

 package com.example.weixu.drawline; import java.util.ArrayList; import android.app.Activity; import android.graphics.Color; import android.graphics.Typeface; import android.os.Bundle; import android.view.WindowManager; import com.github.mikephil.charting.charts.BarLineChartBase; import com.github.mikephil.charting.charts.BarLineChartBase.BorderPosition; import com.github.mikephil.charting.charts.LineChart; import com.github.mikephil.charting.data.Entry; import com.github.mikephil.charting.data.LineData; import com.github.mikephil.charting.data.LineDataSet; import com.github.mikephil.charting.utils.Legend; import com.github.mikephil.charting.utils.Legend.LegendForm; import com.github.mikephil.charting.utils.XLabels; import com.github.mikephil.charting.utils.XLabels.XLabelPosition; import com.github.mikephil.charting.utils.YLabels; public class MainActivity extends Activity { private LineChart chartTall; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); setContentView(R.layout.activity_main); chartTall = (LineChart) findViewById(R.id.chartTall); setType(); // 刷新图表 chartTall.invalidate(); } private void setType() { // 设置在Y轴上是否是从0开始显示 chartTall.setStartAtZero(true); //是否在Y轴显示数据,就是曲线上的数据 chartTall.setDrawYValues(true); //设置网格 chartTall.setDrawBorder(true); chartTall.setBorderPositions(new BarLineChartBase.BorderPosition[] { BorderPosition.BOTTOM}); //在chart上的右下角加描述 chartTall.setDescription("身高曲线图"); //设置Y轴上的单位 chartTall.setUnit("cm"); //设置透明度 chartTall.setAlpha(0.8f); //设置网格底下的那条线的颜色 chartTall.setBorderColor(Color.rgb(213, 216, 214)); //设置Y轴前后倒置 chartTall.setInvertYAxisEnabled(false); //设置高亮显示 chartTall.setHighlightEnabled(true); //设置是否可以触摸,如为false,则不能拖动,缩放等 chartTall.setTouchEnabled(true); //设置是否可以拖拽,缩放 chartTall.setDragEnabled(true); chartTall.setScaleEnabled(true); //设置是否能扩大扩小 chartTall.setPinchZoom(true); //设置点击chart图对应的数据弹出标注 MyMarkerView mv = new MyMarkerView(this, R.layout.custom_marker_view); mv.setOffsets(-mv.getMeasuredWidth() / 2, -mv.getMeasuredHeight()); chartTall.setMarkerView(mv); chartTall.setHighlightIndicatorEnabled(false); //设置字体格式,如正楷 Typeface tf = Typeface.createFromAsset(getAssets(), "OpenSans-Regular.ttf"); chartTall.setValueTypeface(tf); XLabels xl = chartTall.getXLabels(); xl.setPosition(XLabelPosition.BOTTOM); // 设置X轴的数据在底部显示 xl.setTypeface(tf); // 设置字体 xl.setTextSize(10f); // 设置字体大小 xl.setSpaceBetweenLabels(3); // 设置数据之间的间距 YLabels yl = chartTall.getYLabels(); yl.setTypeface(tf); // 设置字体 yl.setTextSize(10f); // s设置字体大小 yl.setLabelCount(5); // 设置Y轴最多显示的数据个数 // 加载数据 setData(); //从X轴进入的动画 chartTall.animateX(4000); chartTall.animateY(3000); //从Y轴进入的动画 chartTall.animateXY(3000, 3000); //从XY轴一起进入的动画 //设置最小的缩放 chartTall.setScaleMinima(0.5f, 1f); } private void setData() { String[] babAge = {"0","1","2","3","4","5","6"}; //连线的x轴数据 String[] babyTall = {"50","60","90","110","130","135","140"}; String[] usuaTall = {"55","65","95","115","125","135","145"};//连线的y轴数据 LineData data=new LineData(babAge,setLine(babAge,babyTall,1,"宝宝身高")); //创建LineData实体类并添加第一条曲线 data.addDataSet(setLine(babAge,usuaTall,2,"正常身高")); //添加第二条曲线 chartTall.setData(data); } //画线 private LineDataSet setLine(String[] babAge, String[] Tall,int flag,String name) { ArrayList xValsAge = new ArrayList(); for (int i = 0; i  yValsBabyTall = new ArrayList(); for (int i = 0; i 

源码:点击打开链接

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持html中文网。

以上就是Android LineChart绘制多条曲线的方法的详细内容,更多请关注0133技术站其它相关文章!

赞(0) 打赏
未经允许不得转载:0133技术站首页 » 移动