From 0533179643d1cb7baeefb9844ba2c82157f307d0 Mon Sep 17 00:00:00 2001 From: Devids Date: Mon, 5 Aug 2013 20:40:25 +0200 Subject: [PATCH 1/4] BarGraph tweak --- src/com/jjoe64/graphview/BarGraphView.java | 7 ++++--- src/com/jjoe64/graphview/GraphView.java | 10 +++++----- src/com/jjoe64/graphview/LineGraphView.java | 2 +- 3 files changed, 10 insertions(+), 9 deletions(-) diff --git a/src/com/jjoe64/graphview/BarGraphView.java b/src/com/jjoe64/graphview/BarGraphView.java index 2a3a87c7f..1116ae75f 100644 --- a/src/com/jjoe64/graphview/BarGraphView.java +++ b/src/com/jjoe64/graphview/BarGraphView.java @@ -41,8 +41,9 @@ public BarGraphView(Context context, String title) { @Override public void drawSeries(Canvas canvas, GraphViewDataInterface[] values, float graphwidth, float graphheight, float border, double minX, double minY, double diffX, double diffY, - float horstart, GraphViewSeriesStyle style) { - float colwidth = (graphwidth - (2 * border)) / values.length; + float horstart, GraphViewSeriesStyle style, int serieNum, int seriesCount) { + //float colwidth = (graphwidth - (2 * border)) / values.length / seriesCount; + float colwidth = (graphwidth) / values.length / seriesCount; paint.setStrokeWidth(style.thickness); paint.setColor(style.color); @@ -58,7 +59,7 @@ public void drawSeries(Canvas canvas, GraphViewDataInterface[] values, float gra paint.setColor(style.getValueDependentColor().get(values[i])); } - canvas.drawRect((i * colwidth) + horstart, (border - y) + graphheight, ((i * colwidth) + horstart) + (colwidth - 1), graphheight + border - 1, paint); + canvas.drawRect((i * seriesCount * colwidth) + horstart + (colwidth * serieNum), (border - y) + graphheight, ((i * seriesCount * colwidth) + horstart) + (colwidth - 1) + (colwidth * serieNum), graphheight + border - 1, paint); } } } diff --git a/src/com/jjoe64/graphview/GraphView.java b/src/com/jjoe64/graphview/GraphView.java index 61acf00bf..270fe2689 100755 --- a/src/com/jjoe64/graphview/GraphView.java +++ b/src/com/jjoe64/graphview/GraphView.java @@ -1,5 +1,5 @@ /** - * This file is part of GraphView. +f * This file is part of GraphView. * * GraphView is free software: you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License as published by @@ -151,7 +151,7 @@ protected void onDraw(Canvas canvas) { paint.setStrokeCap(Paint.Cap.ROUND); for (int i=0; i Date: Mon, 5 Aug 2013 21:04:09 +0200 Subject: [PATCH 2/4] getSeriesCount() method added --- src/com/jjoe64/graphview/BarGraphView.java | 6 +++--- src/com/jjoe64/graphview/GraphView.java | 8 ++++++-- src/com/jjoe64/graphview/LineGraphView.java | 2 +- 3 files changed, 10 insertions(+), 6 deletions(-) diff --git a/src/com/jjoe64/graphview/BarGraphView.java b/src/com/jjoe64/graphview/BarGraphView.java index 1116ae75f..1bb675afa 100644 --- a/src/com/jjoe64/graphview/BarGraphView.java +++ b/src/com/jjoe64/graphview/BarGraphView.java @@ -41,9 +41,9 @@ public BarGraphView(Context context, String title) { @Override public void drawSeries(Canvas canvas, GraphViewDataInterface[] values, float graphwidth, float graphheight, float border, double minX, double minY, double diffX, double diffY, - float horstart, GraphViewSeriesStyle style, int serieNum, int seriesCount) { + float horstart, GraphViewSeriesStyle style, int serieNum) { //float colwidth = (graphwidth - (2 * border)) / values.length / seriesCount; - float colwidth = (graphwidth) / values.length / seriesCount; + float colwidth = (graphwidth) / values.length / getSeriesCount(); paint.setStrokeWidth(style.thickness); paint.setColor(style.color); @@ -59,7 +59,7 @@ public void drawSeries(Canvas canvas, GraphViewDataInterface[] values, float gra paint.setColor(style.getValueDependentColor().get(values[i])); } - canvas.drawRect((i * seriesCount * colwidth) + horstart + (colwidth * serieNum), (border - y) + graphheight, ((i * seriesCount * colwidth) + horstart) + (colwidth - 1) + (colwidth * serieNum), graphheight + border - 1, paint); + canvas.drawRect((i * getSeriesCount() * colwidth) + horstart + (colwidth * serieNum), (border - y) + graphheight, ((i * getSeriesCount() * colwidth) + horstart) + (colwidth - 1) + (colwidth * serieNum), graphheight + border - 1, paint); } } } diff --git a/src/com/jjoe64/graphview/GraphView.java b/src/com/jjoe64/graphview/GraphView.java index 270fe2689..1133330f1 100755 --- a/src/com/jjoe64/graphview/GraphView.java +++ b/src/com/jjoe64/graphview/GraphView.java @@ -151,7 +151,7 @@ protected void onDraw(Canvas canvas) { paint.setStrokeCap(Paint.Cap.ROUND); for (int i=0; i Date: Wed, 7 Aug 2013 22:29:33 +0200 Subject: [PATCH 3/4] seekBarMove, setMinX --- src/com/jjoe64/graphview/GraphView.java | 38 ++++++++++++++++++++++--- 1 file changed, 34 insertions(+), 4 deletions(-) diff --git a/src/com/jjoe64/graphview/GraphView.java b/src/com/jjoe64/graphview/GraphView.java index ca0b57410..81107c4d4 100755 --- a/src/com/jjoe64/graphview/GraphView.java +++ b/src/com/jjoe64/graphview/GraphView.java @@ -65,7 +65,7 @@ private class GraphViewContentView extends View { */ public GraphViewContentView(Context context) { super(context); - setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT)); + setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT)); } /** @@ -163,6 +163,7 @@ private void onMoveGesture(float f) { // view port update if (viewportSize != 0) { viewportStart -= f*viewportSize/graphwidth; + Log.d("graphView", f+" viewPortStart on move: "+viewportStart); // minimal and maximal view limit double minX = getMinX(true); @@ -336,6 +337,8 @@ protected void onDraw(Canvas canvas) { private final Rect textBounds = new Rect(); private boolean staticHorizontalLabels; private boolean staticVerticalLabels; + private double minx = 0.0; + private double maxx = 0.0; public GraphView(Context context, AttributeSet attrs) { this(context, attrs.getAttributeValue(null, "title")); @@ -366,7 +369,7 @@ public GraphView(Context context, String title) { viewVerLabels = new VerLabelsView(context); addView(viewVerLabels); graphViewContentView = new GraphViewContentView(context); - addView(graphViewContentView, new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT, 1)); + addView(graphViewContentView, new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT, 1)); } private GraphViewDataInterface[] _values(int idxSeries) { @@ -560,7 +563,7 @@ public float getLegendWidth() { protected double getMaxX(boolean ignoreViewport) { // if viewport is set, use this if (!ignoreViewport && viewportSize != 0) { - return viewportStart+viewportSize; + return viewportStart+viewportSize+minx; } else { // otherwise use the max x value // values must be sorted by x, so the last value has the largest X value @@ -614,7 +617,7 @@ protected double getMaxY() { protected double getMinX(boolean ignoreViewport) { // if viewport is set, use this if (!ignoreViewport && viewportSize != 0) { - return viewportStart; + return viewportStart + minx; } else { // otherwise use the min x value // values must be sorted by x, so the first value has the smallest X value @@ -887,4 +890,31 @@ public void setViewPort(double start, double size) { protected int getSeriesCount(){ return graphSeries.size(); } + + public void setMinX(double minx){ + this.minx = minx; + } + + public void seekBarMove(int i){ + // view port update + if (viewportSize != 0) { + viewportStart -= ((double)i)*0.594*viewportSize/graphViewContentView.graphwidth; + Log.d("graphView", i+" viewPortStart: "+viewportStart); + + // minimal and maximal view limit + double minX = getMinX(true); + double maxX = getMaxX(true); + if (viewportStart < minX) { + viewportStart = minX; + } else if (viewportStart+viewportSize > maxX) { + viewportStart = maxX - viewportSize; + } + + // labels have to be regenerated + if (!staticHorizontalLabels) horlabels = null; + if (!staticVerticalLabels) verlabels = null; + viewVerLabels.invalidate(); + } + graphViewContentView.invalidate(); + } } From f2e6907410cd7b5a2a4daecf0aa5bb0c3fd5e2c5 Mon Sep 17 00:00:00 2001 From: Devids Date: Tue, 27 Aug 2013 20:13:03 +0200 Subject: [PATCH 4/4] Save canvas as image --- src/com/jjoe64/graphview/GraphView.java | 30 +++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/src/com/jjoe64/graphview/GraphView.java b/src/com/jjoe64/graphview/GraphView.java index 81107c4d4..5f540b168 100755 --- a/src/com/jjoe64/graphview/GraphView.java +++ b/src/com/jjoe64/graphview/GraphView.java @@ -19,17 +19,22 @@ package com.jjoe64.graphview; +import java.io.File; +import java.io.FileOutputStream; import java.text.NumberFormat; import java.util.ArrayList; import java.util.List; +import java.util.Random; import android.content.Context; +import android.graphics.Bitmap; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.graphics.Paint.Align; import android.graphics.Rect; import android.graphics.RectF; +import android.os.Environment; import android.util.AttributeSet; import android.util.Log; import android.view.MotionEvent; @@ -917,4 +922,29 @@ public void seekBarMove(int i){ } graphViewContentView.invalidate(); } + + public void saveCanvasAsImage(){ + Bitmap bitmap = Bitmap.createBitmap( this.getWidth(), this.getHeight(), Bitmap.Config.ARGB_8888); + Canvas canvas = new Canvas(bitmap); + this.draw(canvas); + + String root = Environment.getExternalStorageDirectory().toString(); + File myDir = new File(root + "/measuready_images"); + myDir.mkdirs(); + Random generator = new Random(); + int n = 10000; + n = generator.nextInt(n); + String fname = "Graph-"+ n +".jpg"; + File file = new File (myDir, fname); + if (file.exists ()) file.delete (); + try { + FileOutputStream out = new FileOutputStream(file); + bitmap.compress(Bitmap.CompressFormat.JPEG, 90, out); + out.flush(); + out.close(); + + } catch (Exception e) { + e.printStackTrace(); + } + } }