实现思路,先计算保存这些实际值,然实际值对应的坐标轴上的真正位置,从而绘制绘制出来。主要代码如下
/** * 绘制所有实际值坐标 * @param canvas */ public void drawPoints(Canvas canvas) { if(pointQueue.isEmpty()) return; //首先计算取1xValue多少px float unitXValue_px = xUnit / xValue; Paint paint = new Paint();//画点的画笔 paint.setColor(Color.GREEN);//设置点为绿色 paint.setStrokeWidth(30);//设置画笔为30粗度 //遍历每一个点绘制出来,并移除在队列中位置 while (!pointQueue.isEmpty()) { LineChartPoint point = pointQueue.poll(); //算出这点x, y坐标 float x = point.xValue * unitXValue_px+chardPading; float y = getMeasuredHeight() - chardPading - point.yValue * unitXValue_px; canvas.drawPoint(x, y , paint);//绘制点 } } /** * 添加折线图坐标轴点坐标 * @param points */ public void setLineChartPoint(LineChartPoint ...points) { if(null == points || points.length < 1) return; for(LineChartPoint point : points) { pointQueue.add(point); } invalidate();//通知view重新绘制界面,也就是view会重新调用onDraw方法 }
全部代码:
public class MainActivity extends AppCompatActivity { private LineChartView lineChartView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); lineChartView = (LineChartView) findViewById(R.id.lineChartView); //甲公司第20天盈利 100万 LineChartView.LineChartPoint point = new LineChartView.LineChartPoint(); point.setxValue(20); point.setyValue(100); //乙公司第60天盈利 20万 LineChartView.LineChartPoint point1 = new LineChartView.LineChartPoint(); point1.setxValue(60); point1.setyValue(20); lineChartView.setLineChartPoint(point, point1); } }
xml version="1.0" encoding="utf-8"?>name="LineChartView"> name="chartPadding" format="dimension"> name="xUnit" format="dimension"> name="xValue" format="integer"> name="unitTextSize" format="dimension"> name="xName" format="string"> name="yUnit" format="dimension"> name="yValue" format="integer"> name="yName" format="string">
/** * 绘制折线图详情 */ public class LineChartView extends View { /** * 折线图padding */ private float chardPading; /** * x单位长度 */ private float xUnit; /** * y单位长度 */ private float yUnit; private float textSize; /** * 单位名字 */ private String xName; /** * 单位值 */ private int xValue; private String yName; private int yValue; public LineChartView(Context context, AttributeSet attrs) { super(context, attrs); //获取提供的chardPading长度, 如果不提供,默认为100px. 注意,拿到的dip单位最终会转化为px TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.LineChartView); chardPading = typedArray.getDimension(R.styleable.LineChartView_chartPadding, 100);//默认视图距离边界的长度 xUnit = typedArray.getDimension(R.styleable.LineChartView_xUnit, 50);//默认单位长度为50px xName = typedArray.getString(R.styleable.LineChartView_xName);//x 轴 名字 xName = null != xName ? xName : "x";//如果不提供名字,则默认为x xValue = typedArray.getInt(R.styleable.LineChartView_xValue, 10);//默认单位值为10 textSize = typedArray.getDimension(R.styleable.LineChartView_unitTextSize, 25);//默认25px字体大小 yUnit = typedArray.getDimension(R.styleable.LineChartView_yUnit, xUnit);//默认单位值为与xUnit一样 yName = typedArray.getString(R.styleable.LineChartView_yName);//y 轴 名字 yName = null != yName ? yName : "y";//如果不提供名字,则默认为y yValue = typedArray.getInt(R.styleable.LineChartView_yValue, xValue);//默认单位值与xValue一样 } @Override protected void onDraw(Canvas canvas) { //绘制坐标轴 drawAxis(canvas); //绘制x坐标轴详情 drawX_AxisState(canvas); drawY_AxisState(canvas); drawPoints(canvas); } /** * 为了不遮住坐标轴,给予100padding给它 */ int arrowPadding = 40; /** * 绘制x轴名称,按提供的长度来标识长度值 * * @param canvas */ private void drawX_AxisState(Canvas canvas) { Paint paint = new Paint(); //直线画笔 paint.setColor(Color.BLACK);//设置画笔为黑色 paint.setStrokeWidth(2);//设置画笔尖大小为2px int height = getMeasuredHeight() - (int) chardPading; int drawXlengh = getMeasuredWidth() - (int) chardPading * 2 - arrowPadding;//保留能看到箭头 int xUnit = (int) this.xUnit; Rect nRect = new Rect(); Paint xValuePaint = new Paint(); //画笔 xValuePaint.setColor(Color.BLACK); xValuePaint.setTextSize(textSize); xValuePaint.getTextBounds(xName, 0, xName.length(), nRect); int nWith = nRect.width();//获取x轴名字长度 int nHeight = nRect.height();//获取x轴名字高度 canvas.drawText(xName, getMeasuredWidth() - chardPading - nWith / 2, height + nHeight + 15, xValuePaint); for (int i = xUnit, j = 1; i < drawXlengh; i += xUnit, j++) { canvas.drawLine(chardPading + i, height, chardPading + i, height - 15, paint); int drawvalue = xValue * j; Rect rect = new Rect(); xValuePaint.getTextBounds(drawvalue + "", 0, (drawvalue + "").length(), rect); int txtWith = rect.width(); int txtHeght = rect.height(); canvas.drawText(drawvalue + "", chardPading + i - txtWith / 2, height + txtHeght + 15, xValuePaint); } } /** * 绘制y轴名称,按提供的长度来标识长度值 * * @param canvas */ private void drawY_AxisState(Canvas canvas) { Paint paint = new Paint(); //直线画笔 Paint yValuePaint = new Paint(); //画笔 yValuePaint.setColor(Color.BLACK); yValuePaint.setTextSize(textSize); paint.setColor(Color.BLACK);//设置画笔为黑色 paint.setStrokeWidth(2);//设置画笔尖大小为2px int height = getMeasuredHeight() - (int) chardPading; int drawYlengh = getMeasuredHeight() - (int) chardPading * 2 - arrowPadding;//保留能看到箭头 int yUnit = (int) this.yUnit; Rect nRect = new Rect(); yValuePaint.getTextBounds(yName, 0, yName.length(), nRect); int nWith = nRect.width(); int nHeight = nRect.height(); canvas.drawText(yName, chardPading - nWith - 15, chardPading + nHeight / 2, yValuePaint); for (int i = yUnit, j = 1; i < drawYlengh; i += yUnit, j++) { int drawvalue = yValue * j; Rect rect = new Rect(); yValuePaint.getTextBounds(drawvalue + "", 0, (drawvalue + "").length(), rect); int txtWith = rect.width(); int txtHeght = rect.height(); canvas.drawLine(chardPading, height - i, chardPading + 15, height - i, paint); canvas.drawText(drawvalue + "", chardPading - txtWith - 15, height - i + txtHeght / 2, yValuePaint); } } /** * 为了浪费资源控件,一次性用的东西,我们采取使用队列来缓存这些点 */ private QueuepointQueue = new LinkedList (); /** * 绘制所有实际值坐标 * @param canvas */ public void drawPoints(Canvas canvas) { if(pointQueue.isEmpty()) return; //首先计算取1xValue多少px float unitXValue_px = xUnit / xValue; Paint paint = new Paint();//画点的画笔 paint.setColor(Color.GREEN);//设置点为绿色 paint.setStrokeWidth(30);//设置画笔为30粗度 //遍历每一个点绘制出来,并移除在队列中位置 while (!pointQueue.isEmpty()) { LineChartPoint point = pointQueue.poll(); //算出这点x, y坐标 float x = point.xValue * unitXValue_px+chardPading; float y = getMeasuredHeight() - chardPading - point.yValue * unitXValue_px; canvas.drawPoint(x, y , paint);//绘制点 } } /** * 添加折线图坐标轴点坐标 * @param points */ public void setLineChartPoint(LineChartPoint ...points) { if (null == points || points.length < 1) return; for (LineChartPoint point : points) { pointQueue.add(point); } invalidate();//通知view重新绘制界面,也就是view会重新调用onDraw方法 } /** * 折线坐标点 */ public static class LineChartPoint { private float xValue; private float yValue; public float getxValue() { return xValue; } public void setxValue(float xValue) { this.xValue = xValue; } public float getyValue() { return yValue; } public void setyValue(float yValue) { this.yValue = yValue; } } /** * 绘制坐标轴, 并且设置100padding给LineChartView * * @param canvas */ private void drawAxis(Canvas canvas) { int chartViewHeight = getMeasuredHeight(); //获取LineChartView高度 int charViewWidth = getMeasuredWidth(); //获取LineChartView宽度 Paint paint = new Paint(); //画笔 paint.setColor(Color.BLACK);//设置画笔为黑色 paint.setStrokeWidth(4);//设置画笔尖大小为4px /* * 绘制直线.在2点之间连线 * startX 起点的x坐标 * startY 起点的y坐标 * endX 终点的x坐标 * endY 终点的y坐标 * canvas.drawLine(startX, startY, endX, endY) */ //绘制直线paddingTop 了chardPading的y轴 canvas.drawLine(chardPading, chartViewHeight - chardPading, chardPading, chardPading, paint); //绘制y轴左边半边箭头 canvas.drawLine(chardPading, chardPading, chardPading - 10, chardPading + 10, paint); //绘制y轴右边半边箭头 canvas.drawLine(chardPading, chardPading, chardPading + 10, chardPading + 10, paint); //绘制直线paddingLeft 了chardPading的x轴, canvas.drawLine(chardPading, chartViewHeight - chardPading, charViewWidth - chardPading, chartViewHeight - chardPading, paint); //绘制x轴上半部分箭头 canvas.drawLine(charViewWidth - chardPading, chartViewHeight - chardPading, charViewWidth - chardPading - 10, chartViewHeight - chardPading - 10, paint); //绘制x轴下半部分箭头 canvas.drawLine(charViewWidth - chardPading, chartViewHeight - chardPading, charViewWidth - chardPading - 10, chartViewHeight - chardPading + 10, paint); } }
xml version="1.0" encoding="utf-8"?>xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity"> android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="折线图(五)各公司N天盈利N元" android:layout_centerHorizontal="true" /> android:id="@+id/lineChartView" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent" app:chartPadding="30dp" app:xValue="10" app:xUnit="30dp" app:xName="万元" app:unitTextSize="12sp" app:yValue="10" app:yUnit="30dp" app:yName="天数" />
未更新完,待更新6...........