package clem.clem.solar; import android.content.Context; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.graphics.Paint.Align; import android.view.View; public class GraphView extends View { public static boolean BAR = true; public static boolean LINE = false; private Paint paint; private float[] values; private String[] horlabels; private String[] horlabelsaved; private String[] verlabels; private String title; private boolean type; public GraphView(Context context, float[] values, String title, boolean type) { super(context); if (values == null) values = new float[0]; else this.values = values; if (title == null) title = ""; else this.title = title; if(values.length < 13) // si nombre de val < 13, c'est pour afficher un an (12 barettes / an) {horlabelsaved = new String[] { "jan", "fev", "avr", "mai", "ju", "jul", "aou", "sept", "oct", "no", "dec" };} else if (values.length < 25) // si le nombre recu de valeurs est plus petit que 25, c'est que c'est une journée (de 24h) {horlabelsaved = new String[] { "0h", "4h", "8h", "12h", "16h", "20h", ""};} else // sinon c'est un mois de + de 25 valeurs ! {horlabelsaved = new String[] { "1", "7", "14", "21", "28"};} horlabels = horlabelsaved; float val=getMax(), decr=getMax()/5; String sval0, sval1, sval2, sval3, sval4, sval5; sval0=String.valueOf((float) Math.round(val*100)/100);val-=decr; sval1=String.valueOf((float) Math.round(val*100)/100);val-=decr; sval2=String.valueOf((float) Math.round(val*100)/100);val-=decr; sval3=String.valueOf((float) Math.round(val*100)/100);val-=decr; sval4=String.valueOf((float) Math.round(val*100)/100);val-=decr; sval5=String.valueOf((float) Math.round(val*100)/100); verlabels = new String[] { sval0, sval1, sval2, sval3, sval4, sval5 }; this.type = type; paint = new Paint(); } @Override protected void onDraw(Canvas canvas) { float border = 20; float horstart = border * 2; float height = getHeight(); float width = getWidth() - 1; float max = getMax(); float min = 0; float diff = max - min; float graphheight = height - (2 * border); float graphwidth = width - (2 * border); paint.setTextAlign(Align.LEFT); int vers = verlabels.length - 1; for (int i = 0; i < verlabels.length; i++) { paint.setColor(Color.DKGRAY); // couleur trait horisontaux float y = ((graphheight / vers) * i) + border; canvas.drawLine(horstart, y, width, y, paint); paint.setColor(Color.WHITE); canvas.drawText(verlabels[i], 0, y, paint); } int hors = horlabels.length - 1; for (int i = 0; i < horlabels.length; i++) { paint.setColor(Color.TRANSPARENT); // coulaur des traits verticaux float x = ((graphwidth / hors) * i) + horstart; canvas.drawLine(x, height - border, x, border, paint); paint.setTextAlign(Align.CENTER); if (i==horlabels.length-1) paint.setTextAlign(Align.RIGHT); if (i==0) paint.setTextAlign(Align.LEFT); paint.setColor(Color.WHITE); canvas.drawText(horlabels[i], x, height - 4, paint); } paint.setTextAlign(Align.CENTER); canvas.drawText(title, (graphwidth / 2) + horstart, border - 4, paint); if (max != min) { paint.setColor(Color.GREEN); // couleur bâtonnets if (type == BAR) { float datalength = values.length; float colwidth = (width - (2 * border)) / datalength; for (int i = 0; i < values.length; i++) { float val = values[i] - min; float rat = val / diff; float h = graphheight * rat; canvas.drawRect((i * colwidth) + horstart, (border - h) + graphheight, ((i * colwidth) + horstart) + (colwidth - 1), height - (border - 1), paint); } } else { float datalength = values.length; float colwidth = (width - (2 * border)) / datalength; float halfcol = colwidth / 2; float lasth = 0; for (int i = 0; i < values.length; i++) { float val = values[i] - min; float rat = val / diff; float h = graphheight * rat; if (i > 0) canvas.drawLine(((i - 1) * colwidth) + (horstart + 1) + halfcol, (border - lasth) + graphheight, (i * colwidth) + (horstart + 1) + halfcol, (border - h) + graphheight, paint); lasth = h; } } } } private float getMax() { // renvoie la valeur max float largest = 0; int i=0; while (i < values.length) { if (values[i] >= largest) {largest = values[i]; } i++;} return largest; } }