Android Open Source - AndroidGraph Scatter 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  ww .  j a va  2 s  .  c  om*/
import android.content.Context;
import android.graphics.Canvas;

import android.util.AttributeSet;

import com.nimble.android_graph.Graph_Base.GraphView;
import com.nimble.android_graph.Graph_Base.Point;

import java.util.*;

/**
 * Created by Michael Leith on 18/07/2014.
 */
public class ScatterGraph extends GraphView
{
    protected ArrayList<Line> lines;

    Context c;
    protected boolean dataAdjusted = false;

    public ScatterGraph(Context context)
    {
        super(context);
        useGrid = true;
        c = context;
        lines = new ArrayList<Line>();
    }

    public ScatterGraph(Context context, AttributeSet attr)
    {
        super(context, attr);
        useGrid = true;
        c = context;
        lines = new ArrayList<Line>();
    }

    /**
     *
     * @param line
     * @return The index of the line added.
     */
    public int addLine(Line line)
    {
        lines.add(line);
        dataAdjusted = false;
        return (lines.size() - 1);
    }

    public boolean fitAllLinesToScreen()
    {
        if(graphHeight.length <= 0)
            return false;
        int linesSize = lines.size();
        for(int i = 0; i < linesSize; i++) {
            Line line = lines.get(i);
            int size = line.points.size();
            for (int j = 0; j < size; j++) {
                Point point = convToScreenCoords(line.points.get(j), ORIGIN_LOWER);
                line.points.set(j, point);
            }
            lines.set(i, line);
        }
        return true;
    }

    public int newLine()
    {
        return addLine(new Line());
    }

    public void replaceLine(int index,  Line line)
    {
        lines.ensureCapacity(index);
        lines.set(index, line);
    }

    /**
     * Should be used once the app is running.
     * @param index
     * @param point
     */
    public void addPointToLine(int index, Point point)
    {
        lines.ensureCapacity(index);

        point = convToScreenCoords(point, ORIGIN_LOWER);
        synchronized (lines.get(index)) {
            lines.get(index).addPoint(point);
        }
    }

    public void addPointToLine(int index, float x, float y)
    {
        lines.ensureCapacity(index);

        Point point = convToScreenCoords(x, y, ORIGIN_LOWER);
        lines.get(index).addPoint(point);
    }

    public void addPointToLine(int index, float y)
    {
        addPointToLine(index, new Point(lines.get(index).getNextX(), y));
    }

    protected void drawLine(Canvas canvas, Line line)
    {
        paint.setColor(line.colour);

        int start = line.startPoint;

        float xOffset = line.points.get(start).graphSpaceX;
        float yOffset = line.points.get(start).graphSpaceY;

        for(int i = start; i < line.points.size(); i++) {

            Point coordinate = line.points.get(i);
            float x = coordinate.x - xOffset;
            float y = coordinate.y - yOffset;

            if(  x >= 0  && x <= graphWidth.x + delta &&
                    y <= graphHeight.y + delta && y >= margin.y)
                canvas.drawCircle(x, y, coordinate.radius, paint);
            }
    }

    protected void drawLineWithShadow(Canvas canvas, Line line)
    {
        int start = line.startPoint;
        float xOffset = line.points.get(start).graphSpaceX;
        float yOffset = line.points.get(start).graphSpaceY;
        
        for(int i = start; i < line.points.size(); i++) {
            
            Point coordinate = line.points.get(i);
            float x = coordinate.x - xOffset;
            float y = coordinate.y - yOffset;

            if(  x >= 0  && x <= graphWidth.y + delta ) {

                if( y <= graphHeight.y + delta ) {
                    if( y >= margin.y ) {
                        paint.setColor(line.colour);
                        canvas.drawCircle(x, y, coordinate.radius, paint);

                        // Draw shadow
                        paint.setColor(line.shadow);
                        canvas.drawLine(x, y, x + 0.1f, graphHeight.y, paint);
                    }
                    else { //Show shadow if just out of view.
                        // Draw shadow
                        paint.setColor(line.shadow);
                        canvas.drawLine(x, margin.y, x + 0.1f, graphHeight.y, paint);
                    }
                }

            }
        }
        paint.setColor(line.colour);
    }

    @Override
    public void onDraw(Canvas canvas)
    {
        super.onDraw(canvas);
        if(!dataAdjusted)
            dataAdjusted = fitAllLinesToScreen();
        for( Line l : lines)
        {
            if(dimenChange)
                l.updateWidthLimit(graphWidth, graphHeight, margin);
            if(l.isUseShadow())
                drawLineWithShadow(canvas, l);
            else
                drawLine(canvas, l);
        }
        dimenChange = false;
    }

    @Override
    public void moveEvent(float x, float y)
    {
        float deltaX = oldX - x;
        float deltaY = oldY - y;
        oldX = x;
        oldY = y;
        //Log.e("x", Float.toString(deltaX));
        if(Math.abs(deltaX) > xSensitivity) {

            if(deltaX > 0)
                startPointOffset = xChange; //random
            else
                startPointOffset = -xChange;
            for(Line line : lines) {
                line.setIncrementStartPoint(false);
                line.modifyStartPoint(startPointOffset);
            }
        }
    }

    @Override
    public void cancelEvent()
    {
        for(Line line : lines)
            line.setIncrementStartPoint(true);
    }

}




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