Skip to content

Commit

Permalink
Merged in faifai21/holographlibrary (pull request Androguide#11)
Browse files Browse the repository at this point in the history
added ability to add and remove points from a line, as well as rescaling of the graph on the fly.
  • Loading branch information
D4N14L committed Nov 26, 2013
2 parents b7e49db + 09d6859 commit 36f5aca
Show file tree
Hide file tree
Showing 3 changed files with 131 additions and 56 deletions.
22 changes: 22 additions & 0 deletions HoloGraphLibrary/src/com/echo/holographlibrary/Line.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,33 @@ public void setPoints(ArrayList<LinePoint> points) {
this.points = points;
}
public void addPoint(LinePoint point){
LinePoint p;
for(int i = 0; i < points.size(); i++){
p = points.get(i);
if(point.getX() < p.getX()){
points.add(i, point);
return;
}
}
points.add(point);
}

public void removePoint(LinePoint point){
points.remove(point);
}
public LinePoint getPoint(int index){
return points.get(index);
}

public LinePoint getPoint(float x, float y){
LinePoint p;
for(int i = 0; i < points.size(); i++){
p = points.get(i);
if(p.getX() == x && p.getY() == y)
return p;
}
return null;
}
public int getSize(){
return points.size();
}
Expand Down
152 changes: 97 additions & 55 deletions HoloGraphLibrary/src/com/echo/holographlibrary/LineGraph.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,26 +25,20 @@

import java.util.ArrayList;

import com.echo.holographlibrary.BarGraph.OnBarClickedListener;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.AvoidXfermode;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.DashPathEffect;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Point;
import android.graphics.Path.Direction;
import android.graphics.PixelFormat;
import android.graphics.Point;
import android.graphics.PorterDuffXfermode;
import android.graphics.Region;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;

public class LineGraph extends View {
Expand All @@ -53,12 +47,15 @@ public class LineGraph extends View {
Paint paint = new Paint();
private float minY = 0, minX = 0;
private float maxY = 0, maxX = 0;
private double rangeYRatio = 0;
private double rangeXRatio = 0;
private boolean isMaxYUserSet = false;
private int lineToFill = -1;
private int indexSelected = -1;
private OnPointClickedListener listener;
private Bitmap fullImage;
private boolean shouldUpdate = false;


public LineGraph(Context context){
super(context);
Expand All @@ -84,6 +81,58 @@ public void addLine(Line line) {
shouldUpdate = true;
postInvalidate();
}
public void addPointToLine(int lineIndex, double x, double y){
addPointToLine(lineIndex, (float) x, (float) y);
}
public void addPointToLine(int lineIndex, float x, float y){
LinePoint p = new LinePoint();
p.setX(x);
p.setY(y);
addPointToLine(lineIndex, p);
}

public double getRangeYRatio(){
return rangeYRatio;
}

public void setRangeYRatio(double rr){
this.rangeYRatio = rr;
}
public double getRangeXRatio(){
return rangeXRatio;
}

public void setRangeXRatio(double rr){
this.rangeXRatio = rr;
}
public void addPointToLine(int lineIndex, LinePoint point){
Line line = getLine(lineIndex);
line.addPoint(point);
lines.set(lineIndex, line);
resetYLimits();
shouldUpdate = true;
postInvalidate();
}

public void removePointFromLine(int lineIndex, float x, float y){
LinePoint p = null;
Line line = getLine(lineIndex);
p = line.getPoint(x, y);
removePointFromLine(lineIndex, p);
}
public void removePointFromLine(int lineIndex, LinePoint point){
Line line = getLine(lineIndex);
line.removePoint(point);
lines.set(lineIndex, line);
resetYLimits();
shouldUpdate = true;
postInvalidate();
}

public void resetYLimits(){
float range = getMaxY() - getMinY();
setRangeY(getMinY()-range*getRangeYRatio(), getMaxY()+range*getRangeYRatio());
}
public ArrayList<Line> getLines() {
return lines;
}
Expand All @@ -110,80 +159,82 @@ public void setRangeY(float min, float max) {
maxY = max;
isMaxYUserSet = true;
}
private void setRangeY(double min, double max){
minY = (float)min;
maxY = (float)max;
}
public float getMaxY(){
if (isMaxYUserSet){
return maxY;
} else {
maxY = lines.get(0).getPoint(0).getY();
for (Line line : lines){
for (LinePoint point : line.getPoints()){
if (point.getY() > maxY){
maxY = point.getY();
}
}
float max = lines.get(0).getPoint(0).getY();
for (Line line : lines){
for (LinePoint point : line.getPoints()){
max = point.getY() > max ? point.getY() : max;
}
return maxY;
}

maxY = max;
return maxY;
}

public float getMinY(){
if (isMaxYUserSet){
return minY;
} else {
float min = lines.get(0).getPoint(0).getY();
for (Line line : lines){
for (LinePoint point : line.getPoints()){
if (point.getY() < min) min = point.getY();
}
float min = lines.get(0).getPoint(0).getY();
for (Line line : lines){
for (LinePoint point : line.getPoints()){
min = point.getY() < min ? point.getY() : min;
}
minY = min;
return minY;
}
minY = min;
return minY;
}
public float getMinLimY(){
return minY;
}
public float getMaxLimY(){
return maxY;
}
public float getMaxX(){
float max = lines.get(0).getPoint(0).getX();
for (Line line : lines){
for (LinePoint point : line.getPoints()){
if (point.getX() > max) max = point.getX();
max = point.getX() > max ? point.getX() : max;
}
}
maxX = max;
return maxX;

}
public float getMinX(){
float max = lines.get(0).getPoint(0).getX();
float min = lines.get(0).getPoint(0).getX();
for (Line line : lines){
for (LinePoint point : line.getPoints()){
if (point.getX() < max) max = point.getX();
min = point.getX() < min ? point.getX() : min;
}
}
maxX = max;
return maxX;
minX = min;
return minX;
}

public void onDraw(Canvas ca) {
if (fullImage == null || shouldUpdate) {
fullImage = Bitmap.createBitmap(getWidth(), getHeight(), Config.ARGB_8888);
Canvas canvas = new Canvas(fullImage);

paint.reset();
Path path = new Path();

float bottomPadding = 10, topPadding = 10;
float sidePadding = 10;
float usableHeight = getHeight() - bottomPadding - topPadding;
float usableWidth = getWidth() - 2*sidePadding;

float maxY = getMaxLimY();
float minY = getMinLimY();
float maxX = (float)(getMaxX()*(1+getRangeXRatio()));
float minX = (float)(getMinX()*(1-getRangeXRatio()));

int lineCount = 0;
for (Line line : lines){
int count = 0;
float firstXPixels = 0, lastXPixels = 0, newYPixels = 0;
float lastYPixels = 0, newXPixels = 0;
float maxY = getMaxY();
float minY = getMinY();
float maxX = getMaxX();
float minX = getMinX();

if (lineCount == lineToFill){
paint.setColor(Color.BLACK);
Expand Down Expand Up @@ -253,15 +304,10 @@ public void onDraw(Canvas ca) {
canvas.drawLine(sidePadding, getHeight() - bottomPadding, getWidth()-sidePadding, getHeight()-bottomPadding, paint);
paint.setAlpha(255);


for (Line line : lines){
int count = 0;
float lastXPixels = 0, newYPixels = 0;
float lastYPixels = 0, newXPixels = 0;
float maxY = getMaxY();
float minY = getMinY();
float maxX = getMaxX();
float minX = getMinX();

paint.setColor(line.getColor());
paint.setStrokeWidth(6);
Expand All @@ -287,11 +333,7 @@ public void onDraw(Canvas ca) {
int pointCount = 0;

for (Line line : lines){
float maxY = getMaxY();
float minY = getMinY();
float maxX = getMaxX();
float minX = getMinX();


paint.setColor(line.getColor());
paint.setStrokeWidth(6);
paint.setStrokeCap(Paint.Cap.ROUND);
Expand All @@ -304,14 +346,14 @@ public void onDraw(Canvas ca) {
float yPixels = getHeight() - bottomPadding - (usableHeight*yPercent);

paint.setColor(Color.GRAY);
canvas.drawCircle(xPixels, yPixels, 10, paint);
canvas.drawCircle(xPixels, yPixels, 15, paint);
paint.setColor(Color.WHITE);
canvas.drawCircle(xPixels, yPixels, 5, paint);
canvas.drawCircle(xPixels, yPixels, 7, paint);

Path path2 = new Path();
path2.addCircle(xPixels, yPixels, 30, Direction.CW);
path2.addCircle(xPixels, yPixels, 40, Direction.CW);
p.setPath(path2);
p.setRegion(new Region((int)(xPixels-30), (int)(yPixels-30), (int)(xPixels+30), (int)(yPixels+30)));
p.setRegion(new Region((int)(xPixels-40), (int)(yPixels-40), (int)(xPixels+40), (int)(yPixels+40)));

if (indexSelected == pointCount && listener != null){
paint.setColor(Color.parseColor("#33B5E5"));
Expand Down
13 changes: 12 additions & 1 deletion HoloGraphLibrary/src/com/echo/holographlibrary/LinePoint.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,14 @@ public float getY() {
public void setY(float y) {
this.y = y;
}

public void setX(double x){
this.x = (float) x;
}

public void setY(double y){
this.y = (float) y;
}
public Region getRegion() {
return region;
}
Expand All @@ -58,6 +66,9 @@ public void setPath(Path path) {
this.path = path;
}


@Override
public String toString(){
return "x= " + x + ", y= " + y;
}

}

0 comments on commit 36f5aca

Please sign in to comment.