Android Open Source - AndroidGraph Line Graph






From Project

Back to project page AndroidGraph.

License

The source code is released under:

MIT License

If you think the Android project AndroidGraph 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 com.nimble.android_graph.Graph;
/*w w w.  jav  a 2  s  .  co  m*/
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.util.AttributeSet;

import com.nimble.android_graph.Graph_Base.Coord2d;
import com.nimble.android_graph.Graph_Base.Point;
import com.nimble.androidgraph.R;

/**
 * Created by Michael Leith on 22/07/2014.
 */
public class LineGraph extends ScatterGraph {

    public static final int CURRENT_LINE = 1;
    public static final int VOLTAGE_LINE = 0;

    public LineGraph(Context context)
    {
        super(context);

        backgroundColour = Color.WHITE;

        dataAdjusted = true;
        useGrid = false;

        axisColour = Color.parseColor("#000000");
        labelColour = Color.parseColor("#000000");

        createDefault();
    }

    public LineGraph(Context context, AttributeSet attrs)
    {
        super(context, attrs);

        TypedArray a = context.obtainStyledAttributes(attrs,
                R.styleable.Graph_Id);

        backgroundColour = Color.WHITE;

        dataAdjusted = true;
        useGrid = false;

        axisColour = Color.parseColor("#000000");
        labelColour = Color.parseColor("#000000");

        createDefault();
        setLabels( a.getString(R.styleable.Graph_Id_xLabel), a.getString(R.styleable.Graph_Id_yLabel) );

        int size = (int) a.getDimension(R.styleable.Graph_Id_textSize, labelSize);
        updateLabelSize(size);

        setWillNotDraw(false);
        this.start();
    }

    public void createDefault()
    {
        Line vLine = new Line();
        vLine.setColour(Color.parseColor("#33b5e5"));
        vLine.setShadow(Color.parseColor("#0099cc"));
        vLine.distBetweenPoints = 2f;
        addLine(vLine);

        Line cLine = new Line();
        cLine.setColour(Color.parseColor("#99cc00"));
        cLine.setShadow(Color.parseColor("#669900"));
        cLine.distBetweenPoints = 2f;
        addLine(cLine);

    }

    public LineGraph(Context context, String xLabel, String yLabel, boolean useShadow)
    {
        this(context, xLabel, yLabel);
        lines.get(0).setUseShadow(useShadow);
    }

    public LineGraph(Context context, String xLabel, String yLabel)
    {
        this(context);
        setLabels(xLabel, yLabel);
    }

    @Override
    protected void drawLine(Canvas canvas, Line line) {
        if (line.points == null || line.points.isEmpty()) // Null terminated, starts at 1
            return;

        paint.setColor(line.colour);
        int start = line.startPoint;

        Point current = line.points.get(start);
        Coord2d<Float> offset = new Coord2d<Float>(current.graphSpaceX, current.graphSpaceY);

        float yBound = graphHeight.y + delta;

        for (Point next : line.points.subList(start + 1, line.endPoint)) {
            float y = current.y - offset.y;
            if (y <= yBound)
                canvas.drawLine( current.x - offset.x,
                                 y,
                                 next.x - offset.x,
                                 next.y - offset.y,
                                 paint);

            current = next;
        }
    }

    @Override
    protected void drawLineWithShadow(Canvas canvas, Line line)
    {
        if(line.points == null || line.points.isEmpty())
            return;

        Point current = line.points.get(line.startPoint);
        Coord2d<Float> offset = new Coord2d<Float>(current.graphSpaceX, current.graphSpaceY);

        float yBound = graphHeight.y + delta;

        for(Point next : line.points.subList(line.startPoint + 1, line.endPoint)) {

            float x = current.x - offset.x;
            float y = current.y - offset.y;

            float nextY = next.y - offset.y;

            paint.setColor(line.colour);
            if ( y <= yBound) {

                paint.setColor(line.colour);
                canvas.drawLine(x, y, next.x - offset.x, nextY, paint);

                drawShadow(canvas, x, y, line.shadow);
            }
            else
               drawShadow(canvas, x, nextY, line.shadow);
            current = next;
        }
        paint.setColor(line.colour);
    }

    public void drawShadow(Canvas canvas, float x, float y, int colour)
    {
        paint.setColor(colour);
        canvas.drawLine(x, y, x + 0.1f, graphHeight.y, paint);
    }

    //float t = 0.1f;
    @Override
    public void onDraw(Canvas canvas) {

       // float y = 50* (float) Math.sin(t) + 80;

       // addPointToLine(0, new Point(lines.get(0).getNextX(), y));
       // t +=0.1;

        if (dimenChange)
            updateBackgroundBitmap();


        currentBackground.prepareToDraw();
        canvas.drawBitmap(currentBackground, 0, 0, null);

        for(Line line : lines) {
            synchronized (line) {
                drawLineWithShadow(canvas, line);
                if (dimenChange)
                    line.updateWidthLimit(graphWidth, graphHeight, margin);
            }
        }

        dimenChange = false;
    }

    @Override
    public void draw(Canvas canvas)
    {
        if (dimenChange)
            updateBackgroundBitmap();


        currentBackground.prepareToDraw();
        canvas.drawBitmap(currentBackground, 0, 0, null);

        for(Line line : lines) {
            synchronized (line) {
                drawLineWithShadow(canvas, line);
                if (dimenChange)
                    line.updateWidthLimit(graphWidth, graphHeight, margin);
            }
        }

        dimenChange = false;
    }
}




Java Source Code List

Nimble.com.androidgraph.ApplicationTest.java
com.nimble.android_graph.Activities.FragmentList.java
com.nimble.android_graph.Activities.GraphActivity.java
com.nimble.android_graph.Activities.GraphFragment.java
com.nimble.android_graph.Activities.GroupsFragment.java
com.nimble.android_graph.Graph.CurveGraph.java
com.nimble.android_graph.Graph.LineGraph.java
com.nimble.android_graph.Graph.Line.java
com.nimble.android_graph.Graph.ScatterGraph.java
com.nimble.android_graph.Graph.SineWave.java
com.nimble.android_graph.Graph_Base.Coord2d.java
com.nimble.android_graph.Graph_Base.GraphDataSettings.java
com.nimble.android_graph.Graph_Base.GraphDrawBackground.java
com.nimble.android_graph.Graph_Base.GraphRelateToScreen.java
com.nimble.android_graph.Graph_Base.GraphThreading.java
com.nimble.android_graph.Graph_Base.GraphTouchEvent.java
com.nimble.android_graph.Graph_Base.GraphViewDimensions.java
com.nimble.android_graph.Graph_Base.GraphView.java
com.nimble.android_graph.Graph_Base.Point.java
com.nimble.android_graph.Graph_Base.Range2d.java
com.nimble.android_graph.Graph.sine_scatter.java
com.nimble.android_graph.generics.Methods.java
com.nimble.android_graph.generics.ViewManipulators.java
com.nimble.android_graph.generics.testingSuite.java