AndroidCharts为折线图表添加y坐标 .

转自:http://blog.csdn.net/nekocode/article/details/18325459

 AndroidCharts 是一款轻量级的图表显示控件,对比起Android-Charts和AChartEngine来说简单和活泼了很多,适合数据展示不需要太过详细专业的场合,它支持简单且带动画的折线图,柱状图和饼状图。

Line ChartBar ChartClock Pie Chart

        其中的linechart不支持y坐标显示,我们可以自己修改添加上去,修改后类LineView的代码如下,其中YCOORD_TEXT_LEFT_MARGIN为图标向右的偏移量,用来空出y坐标文字的空间:

  1. package com.nekocode.xuedao.utils;  
  2.   
  3. import android.content.Context;  
  4. import android.graphics.Bitmap;  
  5. import android.graphics.BitmapFactory;  
  6. import android.graphics.Canvas;  
  7. import android.graphics.Color;  
  8. import android.graphics.Paint;  
  9. import android.graphics.Point;  
  10. import android.graphics.Rect;  
  11. import android.graphics.drawable.NinePatchDrawable;  
  12. import android.util.AttributeSet;  
  13. import android.view.View;  
  14.   
  15. import java.util.ArrayList;  
  16.   
  17. import com.nekocode.xuedao.R;  
  18.   
  19.   
  20. /** 
  21.  * Created by Dacer on 11/4/13. 
  22.  */  
  23. public class LineView extends View {  
  24.     private int mViewHeight;  
  25.     //drawBackground   
  26.     private boolean autoSetDataOfGird = true;  
  27.     private boolean autoSetGridWidth = true;  
  28.     private int dataOfAGird = 10;  
  29.     private int bottomTextHeight = 0;  
  30.     private ArrayList<String> bottomTextList;  
  31.     private ArrayList<Integer> dataList;  
  32.     private ArrayList<Integer> xCoordinateList = new ArrayList<Integer>();  
  33.     private ArrayList<Integer> yCoordinateList = new ArrayList<Integer>();  
  34.     private ArrayList<Dot> drawDotList = new ArrayList<Dot>();;  
  35.     private Paint bottomTextPaint = new Paint();  
  36.     private Paint ycoordTextPaint = new Paint();  
  37.     private int bottomTextDescent;  
  38.   
  39.     //popup   
  40.     private Paint popupTextPaint = new Paint();  
  41.     private final int bottomTriangleHeight = 12;  
  42.     //private Dot selectedDot;   
  43.     private boolean mShowYCoordinate = true;  
  44.   
  45.     private int topLineLength = MyUtils.dip2px(getContext(), 12);; // | | 鈫this   
  46.                                                                    //-+-+-   
  47.     private int sideLineLength = MyUtils.dip2px(getContext(),45)/3*2;// --+--+--+--+--+--+--   
  48.                                                                //  鈫this           鈫   
  49.     private int backgroundGridWidth = MyUtils.dip2px(getContext(),45);  
  50.   
  51.     //Constants   
  52.     private final int popupTopPadding = MyUtils.dip2px(getContext(),2);  
  53.     private final int popupBottomMargin = MyUtils.dip2px(getContext(),5);  
  54.     private final int bottomTextTopMargin = MyUtils.sp2px(getContext(),5);  
  55.     private final int bottomLineLength = MyUtils.sp2px(getContext(), 22);  
  56.     private final int DOT_INNER_CIR_RADIUS = MyUtils.dip2px(getContext(), 2);  
  57.     private final int DOT_OUTER_CIR_RADIUS = MyUtils.dip2px(getContext(),5);  
  58.     private final int MIN_TOP_LINE_LENGTH = MyUtils.dip2px(getContext(),12);  
  59.     private final int MIN_VERTICAL_GRID_NUM = 4;  
  60.     private final int MIN_HORIZONTAL_GRID_NUM = 1;  
  61.     private final int BACKGROUND_LINE_COLOR = Color.parseColor("#EEEEEE");  
  62.     private final int BOTTOM_TEXT_COLOR = Color.parseColor("#9B9A9B");  
  63.     private final int YCOORD_TEXT_LEFT_MARGIN = MyUtils.dip2px(getContext(), 10);  
  64.       
  65.     class YCoordData {  
  66.         private int y;  
  67.         private int data;  
  68.         public int getY() {  
  69.             return y;  
  70.         }  
  71.         public void setY(int y) {  
  72.             this.y = y;  
  73.         }  
  74.         public int getData() {  
  75.             return data;  
  76.         }  
  77.         public void setData(int data) {  
  78.             this.data = data;  
  79.         }  
  80.     }  
  81.   
  82.     private Runnable animator = new Runnable() {  
  83.         @Override  
  84.         public void run() {  
  85.             boolean needNewFrame = false;  
  86.             for(Dot dot : drawDotList){  
  87.                 dot.update();  
  88.                 if(!dot.isAtRest()){  
  89.                     needNewFrame = true;  
  90.                 }  
  91.             }  
  92.             if (needNewFrame) {  
  93.                 postDelayed(this0);  
  94.             }  
  95.             invalidate();  
  96.         }  
  97.     };  
  98.   
  99.     public LineView(Context context){  
  100.         this(context,null);  
  101.     }  
  102.     public LineView(Context context, AttributeSet attrs){  
  103.         super(context, attrs);  
  104.         popupTextPaint.setAntiAlias(true);  
  105.         popupTextPaint.setColor(Color.WHITE);  
  106.         popupTextPaint.setTextSize(MyUtils.sp2px(getContext(), 13));  
  107.         popupTextPaint.setStrokeWidth(5);  
  108.         popupTextPaint.setTextAlign(Paint.Align.CENTER);  
  109.   
  110.         bottomTextPaint.setAntiAlias(true);  
  111.         bottomTextPaint.setTextSize(MyUtils.sp2px(getContext(),12));  
  112.         bottomTextPaint.setTextAlign(Paint.Align.CENTER);  
  113.         bottomTextPaint.setStyle(Paint.Style.FILL);  
  114.         bottomTextPaint.setColor(BOTTOM_TEXT_COLOR);  
  115.           
  116.         ycoordTextPaint.setAntiAlias(true);  
  117.         ycoordTextPaint.setTextSize(MyUtils.sp2px(getContext(),12));  
  118.         ycoordTextPaint.setTextAlign(Paint.Align.LEFT);  
  119.         ycoordTextPaint.setStyle(Paint.Style.FILL);  
  120.         ycoordTextPaint.setColor(BOTTOM_TEXT_COLOR);  
  121.     }  
  122.   
  123.     /** 
  124.      * dataList will be reset when called is method. 
  125.      * @param bottomTextList The String ArrayList in the bottom. 
  126.      */  
  127.     public void setBottomTextList(ArrayList<String> bottomTextList){  
  128.         this.dataList = null;  
  129.         this.bottomTextList = bottomTextList;  
  130.   
  131.         Rect r = new Rect();  
  132.         int longestWidth = 0;  
  133.         String longestStr = "";  
  134.         bottomTextDescent = 0;  
  135.         for(String s:bottomTextList){  
  136.             bottomTextPaint.getTextBounds(s,0,s.length(),r);  
  137.             if(bottomTextHeight<r.height()){  
  138.                 bottomTextHeight = r.height();  
  139.             }  
  140.             if(autoSetGridWidth&&(longestWidth<r.width())){  
  141.                 longestWidth = r.width();  
  142.                 longestStr = s;  
  143.             }  
  144.             if(bottomTextDescent<(Math.abs(r.bottom))){  
  145.                 bottomTextDescent = Math.abs(r.bottom);  
  146.             }  
  147.         }  
  148.   
  149.         if(autoSetGridWidth){  
  150.             if(backgroundGridWidth<longestWidth){  
  151.                 backgroundGridWidth = longestWidth+(int)bottomTextPaint.measureText(longestStr,0,1);  
  152.             }  
  153.             if(sideLineLength<longestWidth/2){  
  154.                 sideLineLength = longestWidth/2;  
  155.             }  
  156.         }  
  157.   
  158.         refreshXCoordinateList(getHorizontalGridNum());  
  159.     }  
  160.   
  161.     /** 
  162.      * 
  163.      * @param dataList The Integer ArrayList for showing, 
  164.      *                 dataList.size() must < bottomTextList.size() 
  165.      */  
  166.     public void setDataList(ArrayList<Integer> dataList){  
  167.         this.dataList = dataList;  
  168.         if(dataList.size() > bottomTextList.size()){  
  169.             throw new RuntimeException("dacer.LineView error:" +  
  170.                     " dataList.size() > bottomTextList.size() !!!");  
  171.         }  
  172.         if(autoSetDataOfGird){  
  173.             int biggestData = 0;  
  174.             for(Integer i:dataList){  
  175.                 if(biggestData<i){  
  176.                     biggestData = i;  
  177.                 }  
  178.             }  
  179.             dataOfAGird = 1;  
  180.             while(biggestData/10 > dataOfAGird){  
  181.                 dataOfAGird *= 10;  
  182.             }  
  183.         }  
  184.         refreshAfterDataChanged();  
  185.         setMinimumWidth(0); // It can help the LineView reset the Width,   
  186.                                 // I don't know the better way..   
  187.         postInvalidate();  
  188.     }  
  189.       
  190.     public void setShowYCoordinate(boolean showYCoordinate) {  
  191.         mShowYCoordinate = showYCoordinate;  
  192.     }  
  193.   
  194.     private void refreshAfterDataChanged(){  
  195.         int verticalGridNum = getVerticalGridlNum();  
  196.         refreshTopLineLength(verticalGridNum);  
  197.         refreshYCoordinateList(verticalGridNum);  
  198.         refreshDrawDotList(verticalGridNum);  
  199.     }  
  200.   
  201.     private int getVerticalGridlNum(){  
  202.         int verticalGridNum = MIN_VERTICAL_GRID_NUM;  
  203.         if(dataList != null && !dataList.isEmpty()){  
  204.             for(Integer integer:dataList){  
  205.                 if(verticalGridNum<(integer+1)){  
  206.                     verticalGridNum = integer+1;  
  207.                 }  
  208.             }  
  209.         }  
  210.         return verticalGridNum;  
  211.     }  
  212.   
  213.     private int getHorizontalGridNum(){  
  214.         int horizontalGridNum = bottomTextList.size()-1;  
  215.         if(horizontalGridNum<MIN_HORIZONTAL_GRID_NUM){  
  216.             horizontalGridNum = MIN_HORIZONTAL_GRID_NUM;  
  217.         }  
  218.         return horizontalGridNum;  
  219.     }  
  220.   
  221.     private void refreshXCoordinateList(int horizontalGridNum){  
  222.         xCoordinateList.clear();  
  223.         for(int i=0;i<(horizontalGridNum+1);i++){  
  224.             xCoordinateList.add(sideLineLength + backgroundGridWidth*i);  
  225.         }  
  226.   
  227.     }  
  228.   
  229.     private void refreshYCoordinateList(int verticalGridNum){  
  230.         yCoordinateList.clear();  
  231.         for(int i=0;i<(verticalGridNum+1);i++){  
  232.             yCoordinateList.add(topLineLength +  
  233.                     ((mViewHeight-topLineLength-bottomTextHeight-bottomTextTopMargin-  
  234.                             bottomLineLength-bottomTextDescent)*i/(verticalGridNum)));  
  235.         }  
  236.     }  
  237.   
  238.     private void refreshDrawDotList(int verticalGridNum){  
  239.         if(dataList != null && !dataList.isEmpty()){  
  240.             int drawDotSize = drawDotList.isEmpty()? 0:drawDotList.size();  
  241.             for(int i=0;i<dataList.size();i++){  
  242.                 int x = xCoordinateList.get(i);  
  243.                 int y = yCoordinateList.get(verticalGridNum - dataList.get(i));  
  244.                 if(i>drawDotSize-1){  
  245.                     drawDotList.add(new Dot(x, 0, x, y, dataList.get(i)));  
  246.                 }else{  
  247.                     drawDotList.set(i, drawDotList.get(i).setTargetData(x,y,dataList.get(i)));  
  248.                 }  
  249.             }  
  250.             int temp = drawDotList.size() - dataList.size();  
  251.             for(int i=0; i<temp; i++){  
  252.                 drawDotList.remove(drawDotList.size()-1);  
  253.             }  
  254.         }  
  255.         removeCallbacks(animator);  
  256.         post(animator);  
  257.     }  
  258.   
  259.     private void refreshTopLineLength(int verticalGridNum){  
  260.         // For prevent popup can't be completely showed when backgroundGridHeight is too small.   
  261.         // But this code not so good.   
  262.         if((mViewHeight-topLineLength-bottomTextHeight-bottomTextTopMargin)/  
  263.                 (verticalGridNum+2)<getPopupHeight()){  
  264.             topLineLength = getPopupHeight()+DOT_OUTER_CIR_RADIUS+DOT_INNER_CIR_RADIUS+2;  
  265.         }else{  
  266.             topLineLength = MIN_TOP_LINE_LENGTH;  
  267.         }  
  268.     }  
  269.   
  270.     @Override  
  271.     protected void onDraw(Canvas canvas) {  
  272.         drawBackgroundLines(canvas);  
  273.         drawLines(canvas);  
  274.         drawDots(canvas);  
  275.         for(Dot dot : drawDotList){  
  276.             drawPopup(canvas,  
  277.                     String.valueOf(dot.data),  
  278.                     dot.getPoint());  
  279.         }  
  280.         /* 
  281.         if(showPopup && selectedDot != null){ 
  282.             drawPopup(canvas, 
  283.                     String.valueOf(selectedDot.data), 
  284.                     selectedDot.getPoint()); 
  285.         }*/  
  286.     }  
  287.   
  288.     /** 
  289.      * 
  290.      * @param canvas  The canvas you need to draw on. 
  291.      * @param point   The Point consists of the x y coordinates from left bottom to right top. 
  292.      *                Like is              3 
  293.      *                2 
  294.      *                1 
  295.      *                0 1 2 3 4 5 
  296.      */  
  297.     private void drawPopup(Canvas canvas,String num, Point point){  
  298.         boolean singularNum = (num.length() == 1);  
  299.         int sidePadding = MyUtils.dip2px(getContext(),singularNum? 8:5);  
  300.         int x = point.x;  
  301.         if(mShowYCoordinate == true) x += YCOORD_TEXT_LEFT_MARGIN;  
  302.         int y = point.y-MyUtils.dip2px(getContext(),5);  
  303.         Rect popupTextRect = new Rect();  
  304.         popupTextPaint.getTextBounds(num,0,num.length(),popupTextRect);  
  305.         Rect r = new Rect(x-popupTextRect.width()/2-sidePadding,  
  306.                 y - popupTextRect.height()-bottomTriangleHeight-popupTopPadding*2-popupBottomMargin,  
  307.                 x + popupTextRect.width()/2+sidePadding,  
  308.                 y+popupTopPadding-popupBottomMargin);  
  309.   
  310.         Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.popup_red);  
  311.         byte chunk[] = bmp.getNinePatchChunk();  
  312.         NinePatchDrawable popup = new NinePatchDrawable(bmp, chunk, new Rect(), null);  
  313.         popup.setBounds(r);  
  314.         popup.draw(canvas);  
  315.         canvas.drawText(num, x, y-bottomTriangleHeight-popupBottomMargin, popupTextPaint);  
  316.     }  
  317.   
  318.     private int getPopupHeight(){  
  319.         Rect popupTextRect = new Rect();  
  320.         popupTextPaint.getTextBounds("9",0,1,popupTextRect);  
  321.         Rect r = new Rect(-popupTextRect.width()/2,  
  322.                  - popupTextRect.height()-bottomTriangleHeight-popupTopPadding*2-popupBottomMargin,  
  323.                  + popupTextRect.width()/2,  
  324.                 +popupTopPadding-popupBottomMargin);  
  325.         return r.height();  
  326.     }  
  327.   
  328.     private void drawDots(Canvas canvas){  
  329.         Paint bigCirPaint = new Paint();  
  330.         bigCirPaint.setAntiAlias(true);  
  331.         bigCirPaint.setColor(Color.parseColor("#FF0033"));  
  332.         Paint smallCirPaint = new Paint(bigCirPaint);  
  333.         smallCirPaint.setColor(Color.parseColor("#FFFFFF"));  
  334.         if(drawDotList!=null && !drawDotList.isEmpty()){  
  335.             for(Dot dot : drawDotList){  
  336.                 int x = dot.x;  
  337.                 if(mShowYCoordinate == true) x += YCOORD_TEXT_LEFT_MARGIN;  
  338.                 canvas.drawCircle(x,dot.y,DOT_OUTER_CIR_RADIUS,bigCirPaint);  
  339.                 canvas.drawCircle(x,dot.y,DOT_INNER_CIR_RADIUS,smallCirPaint);  
  340.             }  
  341.         }  
  342.     }  
  343.   
  344.     private void drawLines(Canvas canvas){  
  345.         Paint linePaint = new Paint();  
  346.         linePaint.setAntiAlias(true);  
  347.         linePaint.setColor(Color.parseColor("#FF0033"));  
  348.         linePaint.setStrokeWidth(MyUtils.dip2px(getContext(), 2));  
  349.         for(int i=0; i<drawDotList.size()-1; i++){  
  350.             int x1 = drawDotList.get(i).x;  
  351.             int x2 = drawDotList.get(i+1).x;  
  352.             if(mShowYCoordinate == true) {  
  353.                 x1 += YCOORD_TEXT_LEFT_MARGIN;  
  354.                 x2 += YCOORD_TEXT_LEFT_MARGIN;  
  355.             }  
  356.             canvas.drawLine(x1,  
  357.                     drawDotList.get(i).y,  
  358.                     x2,  
  359.                     drawDotList.get(i+1).y,  
  360.                     linePaint);  
  361.         }  
  362.     }  
  363.   
  364.     private void drawBackgroundLines(Canvas canvas){  
  365.         Paint paint = new Paint();  
  366.         paint.setStyle(Paint.Style.STROKE);  
  367.         paint.setStrokeWidth(MyUtils.dip2px(getContext(),1f));  
  368.         paint.setColor(BACKGROUND_LINE_COLOR);  
  369.   
  370.         //draw vertical lines   
  371.         for(int i=0;i<xCoordinateList.size();i++){  
  372.             int x = xCoordinateList.get(i);  
  373.             if(mShowYCoordinate == true) x += YCOORD_TEXT_LEFT_MARGIN;  
  374.             canvas.drawLine(x, 0, x,  
  375.                     mViewHeight - bottomTextTopMargin - bottomTextHeight-bottomTextDescent,  
  376.                     paint);  
  377.         }  
  378.   
  379.         for(int i=0;i<yCoordinateList.size();i++){  
  380.             if((yCoordinateList.size()-1-i)%dataOfAGird == 0){  
  381.                 int y = yCoordinateList.get(i);  
  382.                 canvas.drawLine(0, y, getWidth(), y, paint);  
  383.                   
  384.                 if(mShowYCoordinate == true)  
  385.                     canvas.drawText(String.valueOf(yCoordinateList.size()-i-1), 0, y, ycoordTextPaint);  
  386.             }  
  387.         }  
  388.   
  389.         //draw bottom text   
  390.         if(bottomTextList != null){  
  391.             for(int i=0;i<bottomTextList.size();i++){  
  392.                 int x = sideLineLength+backgroundGridWidth*i;  
  393.                 if(mShowYCoordinate == true) x += YCOORD_TEXT_LEFT_MARGIN;  
  394.                 canvas.drawText(bottomTextList.get(i), x,  
  395.                         mViewHeight-bottomTextDescent, bottomTextPaint);  
  396.             }  
  397.         }  
  398.     }  
  399.   
  400.     @Override  
  401.     protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {  
  402.         int mViewWidth = measureWidth(widthMeasureSpec);  
  403.         mViewHeight = measureHeight(heightMeasureSpec);  
  404.         refreshAfterDataChanged();  
  405.         setMeasuredDimension(mViewWidth,mViewHeight);  
  406.     }  
  407.   
  408.     private int measureWidth(int measureSpec){  
  409.         int horizontalGridNum = getHorizontalGridNum();  
  410.         int preferred = backgroundGridWidth*horizontalGridNum+sideLineLength*2;  
  411.         return getMeasurement(measureSpec, preferred);  
  412.     }  
  413.   
  414.     private int measureHeight(int measureSpec){  
  415.         int preferred = 0;  
  416.         return getMeasurement(measureSpec, preferred);  
  417.     }  
  418.   
  419.     private int getMeasurement(int measureSpec, int preferred){  
  420.         int specSize = MeasureSpec.getSize(measureSpec);  
  421.         int measurement;  
  422.         switch(MeasureSpec.getMode(measureSpec)){  
  423.             case MeasureSpec.EXACTLY:  
  424.                 measurement = specSize;  
  425.                 break;  
  426.             case MeasureSpec.AT_MOST:  
  427.                 measurement = Math.min(preferred, specSize);  
  428.                 break;  
  429.             default:  
  430.                 measurement = preferred;  
  431.                 break;  
  432.         }  
  433.         return measurement;  
  434.     }  
  435.   
  436.     /* 
  437.     @Override 
  438.     public boolean onTouchEvent(MotionEvent event) { 
  439.         Point point = new Point(); 
  440.         point.x = (int) event.getX(); 
  441.         point.y = (int) event.getY(); 
  442.         Region r = new Region(); 
  443.         int width = backgroundGridWidth/2; 
  444.         if(drawDotList != null || !drawDotList.isEmpty()){ 
  445.             for(Dot dot : drawDotList){ 
  446.                 r.set(dot.x-width,dot.y-width,dot.x+width,dot.y+width); 
  447.                 if (r.contains(point.x,point.y) && event.getAction() == MotionEvent.ACTION_DOWN){ 
  448.                     selectedDot = dot; 
  449.                 }else if (event.getAction() == MotionEvent.ACTION_UP){ 
  450.                     if (r.contains(point.x,point.y)){ 
  451.                         showPopup = true; 
  452.                     } 
  453.                 } 
  454.             } 
  455.         } 
  456.         if (event.getAction() == MotionEvent.ACTION_DOWN || 
  457.                 event.getAction() == MotionEvent.ACTION_UP){ 
  458.             postInvalidate(); 
  459.         } 
  460.         return true; 
  461.     } 
  462.     //*/  
  463.       
  464.     private int updateSelf(int origin, int target, int velocity){  
  465.         if (origin < target) {  
  466.             origin += velocity;  
  467.         } else if (origin > target){  
  468.             origin-= velocity;  
  469.         }  
  470.         if(Math.abs(target-origin)<velocity){  
  471.             origin = target;  
  472.         }  
  473.         return origin;  
  474.     }  
  475.       
  476.     class Dot{  
  477.         int x;  
  478.         int y;  
  479.         int data;  
  480.         int targetX;  
  481.         int targetY;  
  482.         int velocity = MyUtils.dip2px(getContext(),20);  
  483.   
  484.         Dot(int x,int y,int targetX,int targetY,Integer data){  
  485.             this.x = x;  
  486.             this.y = y;  
  487.             setTargetData(targetX, targetY,data);  
  488.         }  
  489.   
  490.         Point getPoint(){  
  491.             return new Point(x,y);  
  492.         }  
  493.   
  494.         Dot setTargetData(int targetX,int targetY,Integer data){  
  495.             this.targetX = targetX;  
  496.             this.targetY = targetY;  
  497.             this.data = data;  
  498.             return this;  
  499.         }  
  500.   
  501.         boolean isAtRest(){  
  502.             return (x==targetX)&&(y==targetY);  
  503.         }  
  504.   
  505.         void update(){  
  506.             x = updateSelf(x, targetX, velocity);  
  507.             y = updateSelf(y, targetY, velocity);  
  508.         }  
  509.     }  
  510. }  
package com.nekocode.xuedao.utils;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Point;
import android.graphics.Rect;
import android.graphics.drawable.NinePatchDrawable;
import android.util.AttributeSet;
import android.view.View;

import java.util.ArrayList;

import com.nekocode.xuedao.R;


/**
 * Created by Dacer on 11/4/13.
 */
public class LineView extends View {
    private int mViewHeight;
    //drawBackground
    private boolean autoSetDataOfGird = true;
    private boolean autoSetGridWidth = true;
    private int dataOfAGird = 10;
    private int bottomTextHeight = 0;
    private ArrayList<String> bottomTextList;
    private ArrayList<Integer> dataList;
    private ArrayList<Integer> xCoordinateList = new ArrayList<Integer>();
    private ArrayList<Integer> yCoordinateList = new ArrayList<Integer>();
    private ArrayList<Dot> drawDotList = new ArrayList<Dot>();;
    private Paint bottomTextPaint = new Paint();
    private Paint ycoordTextPaint = new Paint();
    private int bottomTextDescent;

    //popup
    private Paint popupTextPaint = new Paint();
    private final int bottomTriangleHeight = 12;
    //private Dot selectedDot;
    private boolean mShowYCoordinate = true;

    private int topLineLength = MyUtils.dip2px(getContext(), 12);; // | | 鈫this
                                                                   //-+-+-
    private int sideLineLength = MyUtils.dip2px(getContext(),45)/3*2;// --+--+--+--+--+--+--
                                                               //  鈫this           鈫
    private int backgroundGridWidth = MyUtils.dip2px(getContext(),45);

    //Constants
    private final int popupTopPadding = MyUtils.dip2px(getContext(),2);
    private final int popupBottomMargin = MyUtils.dip2px(getContext(),5);
    private final int bottomTextTopMargin = MyUtils.sp2px(getContext(),5);
    private final int bottomLineLength = MyUtils.sp2px(getContext(), 22);
    private final int DOT_INNER_CIR_RADIUS = MyUtils.dip2px(getContext(), 2);
    private final int DOT_OUTER_CIR_RADIUS = MyUtils.dip2px(getContext(),5);
    private final int MIN_TOP_LINE_LENGTH = MyUtils.dip2px(getContext(),12);
    private final int MIN_VERTICAL_GRID_NUM = 4;
    private final int MIN_HORIZONTAL_GRID_NUM = 1;
    private final int BACKGROUND_LINE_COLOR = Color.parseColor("#EEEEEE");
    private final int BOTTOM_TEXT_COLOR = Color.parseColor("#9B9A9B");
    private final int YCOORD_TEXT_LEFT_MARGIN = MyUtils.dip2px(getContext(), 10);
    
    class YCoordData {
    	private int y;
    	private int data;
		public int getY() {
			return y;
		}
		public void setY(int y) {
			this.y = y;
		}
		public int getData() {
			return data;
		}
		public void setData(int data) {
			this.data = data;
		}
    }

    private Runnable animator = new Runnable() {
        @Override
        public void run() {
            boolean needNewFrame = false;
            for(Dot dot : drawDotList){
                dot.update();
                if(!dot.isAtRest()){
                    needNewFrame = true;
                }
            }
            if (needNewFrame) {
                postDelayed(this, 0);
            }
            invalidate();
        }
    };

    public LineView(Context context){
        this(context,null);
    }
    public LineView(Context context, AttributeSet attrs){
        super(context, attrs);
        popupTextPaint.setAntiAlias(true);
        popupTextPaint.setColor(Color.WHITE);
        popupTextPaint.setTextSize(MyUtils.sp2px(getContext(), 13));
        popupTextPaint.setStrokeWidth(5);
        popupTextPaint.setTextAlign(Paint.Align.CENTER);

        bottomTextPaint.setAntiAlias(true);
        bottomTextPaint.setTextSize(MyUtils.sp2px(getContext(),12));
        bottomTextPaint.setTextAlign(Paint.Align.CENTER);
        bottomTextPaint.setStyle(Paint.Style.FILL);
        bottomTextPaint.setColor(BOTTOM_TEXT_COLOR);
        
        ycoordTextPaint.setAntiAlias(true);
        ycoordTextPaint.setTextSize(MyUtils.sp2px(getContext(),12));
        ycoordTextPaint.setTextAlign(Paint.Align.LEFT);
        ycoordTextPaint.setStyle(Paint.Style.FILL);
        ycoordTextPaint.setColor(BOTTOM_TEXT_COLOR);
    }

    /**
     * dataList will be reset when called is method.
     * @param bottomTextList The String ArrayList in the bottom.
     */
    public void setBottomTextList(ArrayList<String> bottomTextList){
        this.dataList = null;
        this.bottomTextList = bottomTextList;

        Rect r = new Rect();
        int longestWidth = 0;
        String longestStr = "";
        bottomTextDescent = 0;
        for(String s:bottomTextList){
            bottomTextPaint.getTextBounds(s,0,s.length(),r);
            if(bottomTextHeight<r.height()){
                bottomTextHeight = r.height();
            }
            if(autoSetGridWidth&&(longestWidth<r.width())){
                longestWidth = r.width();
                longestStr = s;
            }
            if(bottomTextDescent<(Math.abs(r.bottom))){
                bottomTextDescent = Math.abs(r.bottom);
            }
        }

        if(autoSetGridWidth){
            if(backgroundGridWidth<longestWidth){
                backgroundGridWidth = longestWidth+(int)bottomTextPaint.measureText(longestStr,0,1);
            }
            if(sideLineLength<longestWidth/2){
                sideLineLength = longestWidth/2;
            }
        }

        refreshXCoordinateList(getHorizontalGridNum());
    }

    /**
     *
     * @param dataList The Integer ArrayList for showing,
     *                 dataList.size() must < bottomTextList.size()
     */
    public void setDataList(ArrayList<Integer> dataList){
        this.dataList = dataList;
        if(dataList.size() > bottomTextList.size()){
            throw new RuntimeException("dacer.LineView error:" +
                    " dataList.size() > bottomTextList.size() !!!");
        }
        if(autoSetDataOfGird){
            int biggestData = 0;
            for(Integer i:dataList){
                if(biggestData<i){
                    biggestData = i;
                }
            }
            dataOfAGird = 1;
            while(biggestData/10 > dataOfAGird){
                dataOfAGird *= 10;
            }
        }
        refreshAfterDataChanged();
        setMinimumWidth(0); // It can help the LineView reset the Width,
                                // I don't know the better way..
        postInvalidate();
    }
    
    public void setShowYCoordinate(boolean showYCoordinate) {
    	mShowYCoordinate = showYCoordinate;
    }

    private void refreshAfterDataChanged(){
        int verticalGridNum = getVerticalGridlNum();
        refreshTopLineLength(verticalGridNum);
        refreshYCoordinateList(verticalGridNum);
        refreshDrawDotList(verticalGridNum);
    }

    private int getVerticalGridlNum(){
        int verticalGridNum = MIN_VERTICAL_GRID_NUM;
        if(dataList != null && !dataList.isEmpty()){
            for(Integer integer:dataList){
                if(verticalGridNum<(integer+1)){
                    verticalGridNum = integer+1;
                }
            }
        }
        return verticalGridNum;
    }

    private int getHorizontalGridNum(){
        int horizontalGridNum = bottomTextList.size()-1;
        if(horizontalGridNum<MIN_HORIZONTAL_GRID_NUM){
            horizontalGridNum = MIN_HORIZONTAL_GRID_NUM;
        }
        return horizontalGridNum;
    }

    private void refreshXCoordinateList(int horizontalGridNum){
        xCoordinateList.clear();
        for(int i=0;i<(horizontalGridNum+1);i++){
			xCoordinateList.add(sideLineLength + backgroundGridWidth*i);
        }

    }

    private void refreshYCoordinateList(int verticalGridNum){
        yCoordinateList.clear();
        for(int i=0;i<(verticalGridNum+1);i++){
            yCoordinateList.add(topLineLength +
            		((mViewHeight-topLineLength-bottomTextHeight-bottomTextTopMargin-
            				bottomLineLength-bottomTextDescent)*i/(verticalGridNum)));
        }
    }

    private void refreshDrawDotList(int verticalGridNum){
        if(dataList != null && !dataList.isEmpty()){
            int drawDotSize = drawDotList.isEmpty()? 0:drawDotList.size();
            for(int i=0;i<dataList.size();i++){
                int x = xCoordinateList.get(i);
                int y = yCoordinateList.get(verticalGridNum - dataList.get(i));
                if(i>drawDotSize-1){
                    drawDotList.add(new Dot(x, 0, x, y, dataList.get(i)));
                }else{
                    drawDotList.set(i, drawDotList.get(i).setTargetData(x,y,dataList.get(i)));
                }
            }
            int temp = drawDotList.size() - dataList.size();
            for(int i=0; i<temp; i++){
                drawDotList.remove(drawDotList.size()-1);
            }
        }
        removeCallbacks(animator);
        post(animator);
    }

    private void refreshTopLineLength(int verticalGridNum){
        // For prevent popup can't be completely showed when backgroundGridHeight is too small.
        // But this code not so good.
        if((mViewHeight-topLineLength-bottomTextHeight-bottomTextTopMargin)/
                (verticalGridNum+2)<getPopupHeight()){
            topLineLength = getPopupHeight()+DOT_OUTER_CIR_RADIUS+DOT_INNER_CIR_RADIUS+2;
        }else{
            topLineLength = MIN_TOP_LINE_LENGTH;
        }
    }

    @Override
    protected void onDraw(Canvas canvas) {
        drawBackgroundLines(canvas);
        drawLines(canvas);
        drawDots(canvas);
        for(Dot dot : drawDotList){
        	drawPopup(canvas,
                    String.valueOf(dot.data),
                    dot.getPoint());
        }
        /*
        if(showPopup && selectedDot != null){
            drawPopup(canvas,
                    String.valueOf(selectedDot.data),
                    selectedDot.getPoint());
        }*/
    }

    /**
     *
     * @param canvas  The canvas you need to draw on.
     * @param point   The Point consists of the x y coordinates from left bottom to right top.
     *                Like is              3
     *                2
     *                1
     *                0 1 2 3 4 5
     */
    private void drawPopup(Canvas canvas,String num, Point point){
        boolean singularNum = (num.length() == 1);
        int sidePadding = MyUtils.dip2px(getContext(),singularNum? 8:5);
        int x = point.x;
        if(mShowYCoordinate == true) x += YCOORD_TEXT_LEFT_MARGIN;
        int y = point.y-MyUtils.dip2px(getContext(),5);
        Rect popupTextRect = new Rect();
        popupTextPaint.getTextBounds(num,0,num.length(),popupTextRect);
        Rect r = new Rect(x-popupTextRect.width()/2-sidePadding,
                y - popupTextRect.height()-bottomTriangleHeight-popupTopPadding*2-popupBottomMargin,
                x + popupTextRect.width()/2+sidePadding,
                y+popupTopPadding-popupBottomMargin);

        Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.popup_red);
        byte chunk[] = bmp.getNinePatchChunk();
        NinePatchDrawable popup = new NinePatchDrawable(bmp, chunk, new Rect(), null);
        popup.setBounds(r);
        popup.draw(canvas);
        canvas.drawText(num, x, y-bottomTriangleHeight-popupBottomMargin, popupTextPaint);
    }

    private int getPopupHeight(){
        Rect popupTextRect = new Rect();
        popupTextPaint.getTextBounds("9",0,1,popupTextRect);
        Rect r = new Rect(-popupTextRect.width()/2,
                 - popupTextRect.height()-bottomTriangleHeight-popupTopPadding*2-popupBottomMargin,
                 + popupTextRect.width()/2,
                +popupTopPadding-popupBottomMargin);
        return r.height();
    }

    private void drawDots(Canvas canvas){
        Paint bigCirPaint = new Paint();
        bigCirPaint.setAntiAlias(true);
        bigCirPaint.setColor(Color.parseColor("#FF0033"));
        Paint smallCirPaint = new Paint(bigCirPaint);
        smallCirPaint.setColor(Color.parseColor("#FFFFFF"));
        if(drawDotList!=null && !drawDotList.isEmpty()){
            for(Dot dot : drawDotList){
            	int x = dot.x;
            	if(mShowYCoordinate == true) x += YCOORD_TEXT_LEFT_MARGIN;
                canvas.drawCircle(x,dot.y,DOT_OUTER_CIR_RADIUS,bigCirPaint);
                canvas.drawCircle(x,dot.y,DOT_INNER_CIR_RADIUS,smallCirPaint);
            }
        }
    }

    private void drawLines(Canvas canvas){
        Paint linePaint = new Paint();
        linePaint.setAntiAlias(true);
        linePaint.setColor(Color.parseColor("#FF0033"));
        linePaint.setStrokeWidth(MyUtils.dip2px(getContext(), 2));
        for(int i=0; i<drawDotList.size()-1; i++){
        	int x1 = drawDotList.get(i).x;
        	int x2 = drawDotList.get(i+1).x;
        	if(mShowYCoordinate == true) {
        		x1 += YCOORD_TEXT_LEFT_MARGIN;
        		x2 += YCOORD_TEXT_LEFT_MARGIN;
        	}
            canvas.drawLine(x1,
                    drawDotList.get(i).y,
                    x2,
                    drawDotList.get(i+1).y,
                    linePaint);
        }
    }

    private void drawBackgroundLines(Canvas canvas){
        Paint paint = new Paint();
        paint.setStyle(Paint.Style.STROKE);
        paint.setStrokeWidth(MyUtils.dip2px(getContext(),1f));
        paint.setColor(BACKGROUND_LINE_COLOR);

        //draw vertical lines
        for(int i=0;i<xCoordinateList.size();i++){
        	int x = xCoordinateList.get(i);
        	if(mShowYCoordinate == true) x += YCOORD_TEXT_LEFT_MARGIN;
            canvas.drawLine(x, 0, x,
                    mViewHeight - bottomTextTopMargin - bottomTextHeight-bottomTextDescent,
                    paint);
        }

        for(int i=0;i<yCoordinateList.size();i++){
            if((yCoordinateList.size()-1-i)%dataOfAGird == 0){
            	int y = yCoordinateList.get(i);
                canvas.drawLine(0, y, getWidth(), y, paint);
                
                if(mShowYCoordinate == true)
                	canvas.drawText(String.valueOf(yCoordinateList.size()-i-1), 0, y, ycoordTextPaint);
            }
        }

        //draw bottom text
        if(bottomTextList != null){
            for(int i=0;i<bottomTextList.size();i++){
            	int x = sideLineLength+backgroundGridWidth*i;
            	if(mShowYCoordinate == true) x += YCOORD_TEXT_LEFT_MARGIN;
                canvas.drawText(bottomTextList.get(i), x,
                        mViewHeight-bottomTextDescent, bottomTextPaint);
            }
        }
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int mViewWidth = measureWidth(widthMeasureSpec);
        mViewHeight = measureHeight(heightMeasureSpec);
        refreshAfterDataChanged();
        setMeasuredDimension(mViewWidth,mViewHeight);
    }

    private int measureWidth(int measureSpec){
        int horizontalGridNum = getHorizontalGridNum();
        int preferred = backgroundGridWidth*horizontalGridNum+sideLineLength*2;
        return getMeasurement(measureSpec, preferred);
    }

    private int measureHeight(int measureSpec){
        int preferred = 0;
        return getMeasurement(measureSpec, preferred);
    }

    private int getMeasurement(int measureSpec, int preferred){
        int specSize = MeasureSpec.getSize(measureSpec);
        int measurement;
        switch(MeasureSpec.getMode(measureSpec)){
            case MeasureSpec.EXACTLY:
                measurement = specSize;
                break;
            case MeasureSpec.AT_MOST:
                measurement = Math.min(preferred, specSize);
                break;
            default:
                measurement = preferred;
                break;
        }
        return measurement;
    }

    /*
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        Point point = new Point();
        point.x = (int) event.getX();
        point.y = (int) event.getY();
        Region r = new Region();
        int width = backgroundGridWidth/2;
        if(drawDotList != null || !drawDotList.isEmpty()){
            for(Dot dot : drawDotList){
                r.set(dot.x-width,dot.y-width,dot.x+width,dot.y+width);
                if (r.contains(point.x,point.y) && event.getAction() == MotionEvent.ACTION_DOWN){
                    selectedDot = dot;
                }else if (event.getAction() == MotionEvent.ACTION_UP){
                    if (r.contains(point.x,point.y)){
                        showPopup = true;
                    }
                }
            }
        }
        if (event.getAction() == MotionEvent.ACTION_DOWN ||
                event.getAction() == MotionEvent.ACTION_UP){
            postInvalidate();
        }
        return true;
    }
	//*/
    
    private int updateSelf(int origin, int target, int velocity){
        if (origin < target) {
            origin += velocity;
        } else if (origin > target){
            origin-= velocity;
        }
        if(Math.abs(target-origin)<velocity){
            origin = target;
        }
        return origin;
    }
    
    class Dot{
        int x;
        int y;
        int data;
        int targetX;
        int targetY;
        int velocity = MyUtils.dip2px(getContext(),20);

        Dot(int x,int y,int targetX,int targetY,Integer data){
            this.x = x;
            this.y = y;
            setTargetData(targetX, targetY,data);
        }

        Point getPoint(){
            return new Point(x,y);
        }

        Dot setTargetData(int targetX,int targetY,Integer data){
            this.targetX = targetX;
            this.targetY = targetY;
            this.data = data;
            return this;
        }

        boolean isAtRest(){
            return (x==targetX)&&(y==targetY);
        }

        void update(){
            x = updateSelf(x, targetX, velocity);
            y = updateSelf(y, targetY, velocity);
        }
    }
}

        修改后我们可以通过增加的setShowYCoordinate方法设置是否显示y坐标文字。


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值