Skip to content

Commit

Permalink
refs jjoe64#14 fixed bar chart width calculation and labels position
Browse files Browse the repository at this point in the history
  • Loading branch information
jjoe64 committed Nov 20, 2013
1 parent 3173078 commit 3997e4e
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 17 deletions.
30 changes: 28 additions & 2 deletions src/com/jjoe64/graphview/BarGraphView.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint.Align;
import android.util.AttributeSet;

import com.jjoe64.graphview.GraphViewSeries.GraphViewSeriesStyle;
Expand All @@ -38,15 +39,39 @@ public BarGraphView(Context context, String title) {
super(context, title);
}

@Override
protected void drawHorizontalLabels(Canvas canvas, float border,
float horstart, float height, String[] horlabels, float graphwidth) {
// horizontal labels + lines
paint.setTextAlign(Align.CENTER);

int hors = horlabels.length;
float barwidth = graphwidth/horlabels.length;
float textOffset = barwidth/2;
for (int i = 0; i < horlabels.length; i++) {
// lines
float x = ((graphwidth / hors) * i) + horstart;
paint.setColor(graphViewStyle.getGridColor());
canvas.drawLine(x, height - border, x, border, paint);

// text
x = barwidth*i + textOffset + horstart;
paint.setColor(graphViewStyle.getHorizontalLabelsColor());
canvas.drawText(horlabels[i], x, height - 4, paint);
}
}

@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 colwidth = graphwidth / (values.length);

paint.setStrokeWidth(style.thickness);
paint.setColor(style.color);

float offset = 0;

// draw data
for (int i = 0; i < values.length; i++) {
float valY = (float) (values[i].getY() - minY);
Expand All @@ -58,7 +83,8 @@ 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 * colwidth) + horstart -offset, (border - y) + graphheight, ((i * colwidth) + horstart) + (colwidth - 1) -offset, graphheight + border - 1, paint);
}
}

}
35 changes: 20 additions & 15 deletions src/com/jjoe64/graphview/GraphView.java
Original file line number Diff line number Diff line change
Expand Up @@ -119,20 +119,7 @@ protected void onDraw(Canvas canvas) {
canvas.drawLine(horstart, y, width, y, paint);
}

// horizontal labels + lines
int hors = horlabels.length - 1;
for (int i = 0; i < horlabels.length; i++) {
paint.setColor(graphViewStyle.getGridColor());
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(graphViewStyle.getHorizontalLabelsColor());
canvas.drawText(horlabels[i], x, height - 4, paint);
}
drawHorizontalLabels(canvas, border, horstart, height, horlabels, graphwidth);

paint.setTextAlign(Align.CENTER);
canvas.drawText(title, (graphwidth / 2) + horstart, border - 4, paint);
Expand Down Expand Up @@ -337,7 +324,7 @@ protected void onDraw(Canvas canvas) {
private boolean manualYAxis;
private double manualMaxYValue;
private double manualMinYValue;
private GraphViewStyle graphViewStyle;
protected GraphViewStyle graphViewStyle;
private final GraphViewContentView graphViewContentView;
private CustomLabelFormatter customLabelFormatter;
private Integer labelTextHeight;
Expand Down Expand Up @@ -419,6 +406,24 @@ public void addSeries(GraphViewSeries series) {
redrawAll();
}

protected void drawHorizontalLabels(Canvas canvas, float border,
float horstart, float height, String[] horlabels, float graphwidth) {
// horizontal labels + lines
int hors = horlabels.length - 1;
for (int i = 0; i < horlabels.length; i++) {
paint.setColor(graphViewStyle.getGridColor());
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(graphViewStyle.getHorizontalLabelsColor());
canvas.drawText(horlabels[i], x, height - 4, paint);
}
}

protected void drawLegend(Canvas canvas, float height, float width) {
float textSize = paint.getTextSize();
int spacing = getGraphViewStyle().getLegendSpacing();
Expand Down

0 comments on commit 3997e4e

Please sign in to comment.