Back to project page AndroidGraph.
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.
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; } }