https://github.com/PhilJay/MPAndroidChart
demo 里的代码:
xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <com.github.mikephil.charting.charts.LineChart android:id="@+id/chart1" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_above="@+id/seekBar1" /> </RelativeLayout>
java:
mChart = (LineChart) findViewById(R.id.chart1); mChart.setOnChartGestureListener(this);// 手势操作回调接口 mChart.setOnChartValueSelectedListener(this);// 选中某个点时的回调 mChart.setDrawGridBackground(false);// 禁用/启用网格线----但是现在我测试的没有效果 mChart.setDescription("");// 折线图的描述 mChart.setNoDataTextDescription("You need to provide data for the chart.");// 折线图没有数据时的描述 mChart.setTouchEnabled(true);// 启用/禁用所有手势和触摸图, mChart.setDragEnabled(true);// 设置拖动(曲线图不能在屏幕上完全显示的时候) mChart.setScaleEnabled(true);// 设置缩放 // mChart.setScaleXEnabled(true);// 当 mChart.setScaleEnabled(false); 时,可以单独启用x轴或者y轴上的缩放功能 // mChart.setScaleYEnabled(true); // 同上 mChart.setPinchZoom(true);// 如果设置为true,x和y轴可以同时用两手指伸缩,如果false,y轴可以分别按比例缩小的 这个建议大家自己测试下 体验下 描述的不是很直观 // mChart.setBackgroundColor(Color.GRAY);// 设置折线图的背景颜色 // 当折现图中的某个值被选中时用来显示 MyMarkerView mv = new MyMarkerView(this, R.layout.custom_marker_view); mChart.setMarkerView(mv); // // x-axis limit line // LimitLine llXAxis1 = new LimitLine(10f, "Index 10"); // llXAxis.setLineWidth(4f); // llXAxis.enableDashedLine(10f, 10f, 0f); // llXAxis.setLabelPosition(LimitLabelPosition.RIGHT_BOTTOM); // llXAxis.setTextSize(30f); // // XAxis xAxis = mChart.getXAxis(); // //xAxis.setValueFormatter(new MyCustomXAxisValueFormatter()); // //xAxis.addLimitLine(llXAxis); // add x-axis limit line Typeface tf = Typeface.createFromAsset(getAssets(), "OpenSans-Regular.ttf"); LimitLine ll1 = new LimitLine(130f, "Upper Limit");// 创建一个线对象 (位置,名称) ll1.setLineWidth(4f);// Upper Limit 的高 ll1.enableDashedLine(10f, 10f, 0);// 线的样式设置为虚线样式 三个参数分别是(长度、间距、没太看懂) ll1.setLabelPosition(LimitLabelPosition.RIGHT_TOP);// 设置线名称相对于线的位置 ll1.setTextSize(40f);// 线名称的字号 ll1.setTypeface(tf);// 字体 // 同上 LimitLine ll2 = new LimitLine(-30f, "Lower Limit"); ll2.setLineWidth(4f); ll2.enableDashedLine(10f, 10f, 0f); ll2.setLabelPosition(LimitLabelPosition.RIGHT_BOTTOM); ll2.setTextSize(10f); ll2.setTypeface(tf); YAxis leftAxis = mChart.getAxisLeft();// Y轴对象设置 leftAxis.removeAllLimitLines(); // reset all limit lines to avoid overlapping lines // leftAxis.addLimitLine(ll1);// 把线添加到轴上 // leftAxis.addLimitLine(ll2); leftAxis.setAxisMaxValue(220f);// Y轴最大值 leftAxis.setAxisMinValue(-50f);// Y轴最小值 leftAxis.setStartAtZero(false);// true:不管最小值是多少都强制这个轴从0开始 // leftAxis.setYOffset(120f);// 设置偏移量-- leftAxis.enableGridDashedLine(10f, 10f, 0f); // limit lines are drawn behind data (and not on top) leftAxis.setDrawLimitLinesBehindData(true); mChart.getAxisRight().setEnabled(false);// 折线图右侧的Y轴对象不可用 // x轴标签 XAxis xAxis = mChart.getXAxis(); xAxis.setTypeface(tf); xAxis.setPosition(XAxis.XAxisPosition.BOTTOM); xAxis.setTextColor(Color.DKGRAY); // add data setData(45, 100); mChart.animateX(2500, Easing.EasingOption.EaseInOutQuart);// 数据完全呈现在折线图上所需的时间 // get the legend (only possible after setting data) Legend l = mChart.getLegend(); l.setForm(LegendForm.LINE);