Android Open Source - customhellochartdemo Broken Line View






From Project

Back to project page customhellochartdemo.

License

The source code is released under:

Apache License

If you think the Android project customhellochartdemo listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package lecho.lib.hellocharts.samples;
//from w  ww.  j  a v  a  2s.c o  m
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.DashPathEffect;
import android.graphics.LinearGradient;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PathEffect;
import android.graphics.Shader;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup.LayoutParams;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;

/**
 * ???
 *
 * @author Administrator
 *
 */
public class BrokenLineView extends View {

    private List<Integer> milliliter;
    private float tb;
    private float interval_left_right;
    private float interval_left;
    private float margin_bottom;
    private Paint paint_date, paint_brokenLine, paint_dottedline,
            paint_brokenline_big, framPanint;

    private int time_index;
    private Bitmap bitmap_point;
    private Path path;
    private float dotted_text;

    public float getDotted_text() {
        return dotted_text;
    }

    public void setDotted_text(float dotted_text) {
        this.dotted_text = dotted_text;
    }

    private int fineLineColor = 0x5faaaaaa; // ????
    private int blueLineColor = 0xff00ffff; // ???
    private int orangeLineColor = 0xffd56f2b; // ??
    private int lineColor = 0;

    public BrokenLineView(Context context, List<Integer> milliliter,int color) {
        super(context);
        this.lineColor = color;
        init(milliliter);
    }

    public void init(List<Integer> milliliter) {
        if (null == milliliter || milliliter.size() == 0)
            return;
        this.milliliter = delZero(milliliter);
        Resources res = getResources();
        tb = res.getDimension(R.dimen.historyscore_tb);
        interval_left_right = tb * 5.0f;
        interval_left = tb * 0.5f;
        margin_bottom=8*tb * 0.2f;

        paint_date = new Paint();
        paint_date.setStrokeWidth(tb * 0.1f);
        paint_date.setTextSize(tb * 1.2f);
        paint_date.setColor(fineLineColor);

        paint_brokenLine = new Paint();
        paint_brokenLine.setStrokeWidth(tb * 0.1f);
        paint_brokenLine.setColor(lineColor);
        paint_brokenLine.setAntiAlias(true);

        paint_dottedline = new Paint();
        paint_dottedline.setStyle(Paint.Style.STROKE);
        paint_dottedline.setColor(fineLineColor);

        paint_brokenline_big = new Paint();
        paint_brokenline_big.setStrokeWidth(tb * 0.2f);
        paint_brokenline_big.setColor(fineLineColor);
        paint_brokenline_big.setAntiAlias(true);

        framPanint = new Paint();
        framPanint.setAntiAlias(true);
        framPanint.setStrokeWidth(2f);

        path = new Path();
        bitmap_point = BitmapFactory.decodeResource(getResources(),
                R.drawable.point);
        setLayoutParams(new LayoutParams(
                (int) (this.milliliter.size() * interval_left_right),
                LayoutParams.MATCH_PARENT));
    }

    /**
     * ?????????????
     *
     * @return
     */
    public List<Integer> delZero(List<Integer> milliliter) {
        List<Integer> list = new ArrayList<Integer>();
        int sta = 0;
        int end = 0;
        for (int i = 0; i < milliliter.size(); i++) {
            if (milliliter.get(i) != 0) {
                sta = i;
                break;
            }
        }
        for (int i = milliliter.size() - 1; i >= 0; i--) {
            if (milliliter.get(i) != 0) {
                end = i;
                break;
            }
        }
        for (int i = 0; i < milliliter.size(); i++) {
            if (i >= sta && i <= end) {
                list.add(milliliter.get(i));
            }
        }
        time_index = sta;
//    dotted_text = ((Collections.max(milliliter) - Collections .min(milliliter)) / 12.0f * 5.0f);
        return list;
    }

    protected void onDraw(Canvas c) {
        if (null == milliliter || milliliter.size() == 0)
            return;
        drawStraightLine(c);
        drawBrokenLine(c);
        drawDate(c);
    }

    /**
     * ????
     *
     * @param c
     */
    public void drawStraightLine(Canvas c) {

        int count_line = 0;
        for (int i = 0; i < milliliter.size(); i++) {
            if (i==0) {//??Y?
                c.drawLine(interval_left_right * i, 0, interval_left_right * i, getHeight() - margin_bottom, paint_date);
                for (int j = 0; j <10; j++) {
                    c.drawText(String.valueOf(10*(j+1)), 0 ,(getHeight()-margin_bottom)/10*(10-(j+1)), paint_date);
                    if (j==5){//????
                        paint_dottedline.setColor(orangeLineColor);
                        Path path = new Path();
                        path.moveTo(0, (getHeight()-margin_bottom)/10*(10-(j+1)));
                        path.lineTo(getWidth(), (getHeight()-margin_bottom)/10*(10-(j+1)));
                        PathEffect effects = new DashPathEffect(new float[] { tb * 0.3f,
                                tb * 0.3f, tb * 0.3f, tb * 0.3f }, tb * 0.1f);
                        paint_dottedline.setPathEffect(effects);
                        c.drawPath(path, paint_dottedline);
                    }

                }
                continue;
            }
            //???? ?????????
            paint_dottedline.setColor(fineLineColor);
            if (count_line == 0) {
                c.drawLine(interval_left_right * i, 0, interval_left_right * i, getHeight() - margin_bottom, paint_date);
            }
            if (count_line == 2) {
                c.drawLine(interval_left_right * i, tb * 1.5f, interval_left_right * i, getHeight() - margin_bottom, paint_date);
            }
            if (count_line == 1 || count_line == 3) {
                Path path = new Path();
                path.moveTo(interval_left_right * i, tb * 1.5f);
                path.lineTo(interval_left_right * i, getHeight() - margin_bottom);
                PathEffect effects = new DashPathEffect(new float[] { tb * 0.3f, tb * 0.3f, tb * 0.3f, tb * 0.3f }, tb * 0.1f);
                paint_dottedline.setPathEffect(effects);
                c.drawPath(path, paint_dottedline);
            }
            count_line++;
            if (count_line >= 4) {
                count_line = 0;
            }
        }
        //??X?
        c.drawLine(0, getHeight() - margin_bottom, getWidth(), getHeight() - margin_bottom, paint_brokenline_big);
    }

    /**
     * ????
     *
     * @param c
     */
    public void drawBrokenLine(Canvas c) {
        int index = 0;
        float temp_x = 0;
        float temp_y = 0;
//    float base = (getHeight() - tb * 3.0f) / (Collections.max(milliliter) - Collections.min(milliliter));
        float base = (getHeight() - margin_bottom) / 100;

        Shader mShader = new LinearGradient(0, 0, 0, getHeight(), new int[] {
                Color.argb(0, 0, 0, 0), Color.argb(0, 0, 0, 0),
                Color.argb(0, 0, 0, 0) }, null, Shader.TileMode.CLAMP);
        framPanint.setShader(mShader);

        for (int i = 0; i < milliliter.size() - 1; i++) {
            Log.e("i", ""+milliliter.get(i));
            float x1 = interval_left_right * i;
            float y1 =  getHeight() - margin_bottom - (base * milliliter.get(i));
            float Y1 =  milliliter.get(i);
            float x2 = interval_left_right * (i + 1);
            float y2 =  getHeight() - margin_bottom - (base * milliliter.get(i + 1));
            float Y2 =  milliliter.get(i+1);

            if ((int) (base * milliliter.get(i + 1)) == 0 && index == 0) {
                index++;
                temp_x = x1;
                temp_y = y1;
            }
            if ((int) (base * milliliter.get(i + 1)) != 0 && index != 0) {
                index = 0;
                x1 = temp_x;
                y1 = temp_y;
            }

            paint_date.setColor(orangeLineColor);
            if (i==0) c.drawText(String.valueOf(Y1), x1, y1, paint_date);//??????????

            if (index == 0) {
                c.drawText(String.valueOf(Y2), x2, y2, paint_date);//???i+1??????
                c.drawLine(x1, y1, x2, y2, paint_brokenLine);
                path.lineTo(x1, y1);
                if (i != 0)
                    c.drawBitmap(bitmap_point,
                            x1 - bitmap_point.getWidth() / 2,
                            y1 - bitmap_point.getHeight() / 2, null);
                if (i == milliliter.size() - 2) {
                    path.lineTo(x2, y2);
                    path.lineTo(x2, getHeight());
                    path.lineTo(0, getHeight());
                    path.close();
                    c.drawPath(path, framPanint);
                    c.drawBitmap(bitmap_point,
                            x2 - bitmap_point.getWidth() / 2,
                            y2 - bitmap_point.getHeight() / 2, null);
                }
            }
        }
        paint_date.setColor(fineLineColor);

    }

    /**
     * ????
     *
     * @param c
     */
    public void drawDate(Canvas c) {

        Date date=new Date();//?????
        Calendar calendar =new  GregorianCalendar();
        calendar.setTime(date);
        SimpleDateFormat dateFormat=new SimpleDateFormat("MM-dd");
        String[] dates=new String[milliliter.size()];
        for (int i = 0; i < milliliter.size(); i++) {
            calendar.add(calendar.DATE,1);//???????????.???????,???????
            date=calendar.getTime();   //??????????????????
            dates[i]=dateFormat.format(date).toString();
        }

        for (int i = 0; i < milliliter.size(); i += 1) {
            paint_date.setStrokeWidth(tb * 2.8f);
            c.drawText(dates[i], interval_left_right * i ,getHeight(), paint_date);
        }

    }
}




Java Source Code List

lecho.lib.hellocharts.ChartComputator.java
lecho.lib.hellocharts.DummyChartAnimationListener.java
lecho.lib.hellocharts.DummyVieportChangeListener.java
lecho.lib.hellocharts.PreviewChartComputator.java
lecho.lib.hellocharts.ViewportChangeListener.java
lecho.lib.hellocharts.animation.ChartAnimationListener.java
lecho.lib.hellocharts.animation.ChartDataAnimatorV14.java
lecho.lib.hellocharts.animation.ChartDataAnimatorV8.java
lecho.lib.hellocharts.animation.ChartDataAnimator.java
lecho.lib.hellocharts.animation.ChartViewportAnimatorV14.java
lecho.lib.hellocharts.animation.ChartViewportAnimatorV8.java
lecho.lib.hellocharts.animation.ChartViewportAnimator.java
lecho.lib.hellocharts.animation.PieChartRotationAnimatorV14.java
lecho.lib.hellocharts.animation.PieChartRotationAnimatorV8.java
lecho.lib.hellocharts.animation.PieChartRotationAnimator.java
lecho.lib.hellocharts.gesture.ChartScroller.java
lecho.lib.hellocharts.gesture.ChartTouchHandler.java
lecho.lib.hellocharts.gesture.ChartZoomer.java
lecho.lib.hellocharts.gesture.ContainerScrollType.java
lecho.lib.hellocharts.gesture.PieChartTouchHandler.java
lecho.lib.hellocharts.gesture.PreviewChartTouchHandler.java
lecho.lib.hellocharts.gesture.ZoomType.java
lecho.lib.hellocharts.gesture.ZoomerCompat.java
lecho.lib.hellocharts.model.AbstractChartData.java
lecho.lib.hellocharts.model.ArcValue.java
lecho.lib.hellocharts.model.AxisValue.java
lecho.lib.hellocharts.model.Axis.java
lecho.lib.hellocharts.model.BubbleChartData.java
lecho.lib.hellocharts.model.BubbleValue.java
lecho.lib.hellocharts.model.ChartData.java
lecho.lib.hellocharts.model.ColumnChartData.java
lecho.lib.hellocharts.model.ColumnValue.java
lecho.lib.hellocharts.model.Column.java
lecho.lib.hellocharts.model.ComboLineColumnChartData.java
lecho.lib.hellocharts.model.LineChartData.java
lecho.lib.hellocharts.model.Line.java
lecho.lib.hellocharts.model.PieChartData.java
lecho.lib.hellocharts.model.PointValue.java
lecho.lib.hellocharts.model.SelectedValue.java
lecho.lib.hellocharts.model.SimpleValueFormatter.java
lecho.lib.hellocharts.model.ValueFormatter.java
lecho.lib.hellocharts.model.ValueShape.java
lecho.lib.hellocharts.model.Viewport.java
lecho.lib.hellocharts.provider.BubbleChartDataProvider.java
lecho.lib.hellocharts.provider.ColumnChartDataProvider.java
lecho.lib.hellocharts.provider.ComboLineColumnChartDataProvider.java
lecho.lib.hellocharts.provider.LineChartDataProvider.java
lecho.lib.hellocharts.provider.PieChartDataProvider.java
lecho.lib.hellocharts.renderer.AbstractChartRenderer.java
lecho.lib.hellocharts.renderer.AxesRenderer.java
lecho.lib.hellocharts.renderer.BubbleChartRenderer.java
lecho.lib.hellocharts.renderer.ChartRenderer.java
lecho.lib.hellocharts.renderer.ColumnChartRenderer.java
lecho.lib.hellocharts.renderer.ComboLineColumnChartRenderer.java
lecho.lib.hellocharts.renderer.LineChartRenderer.java
lecho.lib.hellocharts.renderer.PieChartRenderer.java
lecho.lib.hellocharts.renderer.PreviewColumnChartRenderer.java
lecho.lib.hellocharts.renderer.PreviewLineChartRenderer.java
lecho.lib.hellocharts.samples.AboutActivity.java
lecho.lib.hellocharts.samples.BrokenLineChartActivity.java
lecho.lib.hellocharts.samples.BrokenLineView.java
lecho.lib.hellocharts.samples.BubbleChartActivity.java
lecho.lib.hellocharts.samples.ColumnChartActivity.java
lecho.lib.hellocharts.samples.ComboLineColumnChartActivity.java
lecho.lib.hellocharts.samples.GoodBadChartActivity.java
lecho.lib.hellocharts.samples.LineChartActivity.java
lecho.lib.hellocharts.samples.LineColumnDependencyActivity.java
lecho.lib.hellocharts.samples.MainActivity.java
lecho.lib.hellocharts.samples.PieChartActivity.java
lecho.lib.hellocharts.samples.PreviewColumnChartActivity.java
lecho.lib.hellocharts.samples.PreviewLineChartActivity.java
lecho.lib.hellocharts.samples.SpeedChartActivity.java
lecho.lib.hellocharts.samples.TempoChartActivity.java
lecho.lib.hellocharts.samples.ViewPagerChartsActivity.java
lecho.lib.hellocharts.util.AxisAutoValues.java
lecho.lib.hellocharts.util.Utils.java
lecho.lib.hellocharts.view.AbstractChartView.java
lecho.lib.hellocharts.view.BubbleChartView.java
lecho.lib.hellocharts.view.Chart.java
lecho.lib.hellocharts.view.ColumnChartView.java
lecho.lib.hellocharts.view.ComboLineColumnChartView.java
lecho.lib.hellocharts.view.LineChartView.java
lecho.lib.hellocharts.view.PieChartView.java
lecho.lib.hellocharts.view.PreviewColumnChartView.java
lecho.lib.hellocharts.view.PreviewLineChartView.java
lecho.lib.hellocharts.view.hack.HackyDrawerLayout.java
lecho.lib.hellocharts.view.hack.HackyViewPager.java