Cleanup y-axis renderers

This commit is contained in:
Philipp Jahoda 2016-06-04 11:14:39 +02:00
parent 3076f259f9
commit 084935d9e3
4 changed files with 81 additions and 149 deletions

View file

@ -91,7 +91,6 @@ public class HorizontalBarChartActivity extends DemoBase implements OnSeekBarCha
yl.setTypeface(tf);
yl.setDrawAxisLine(true);
yl.setDrawGridLines(true);
yl.setGridLineWidth(0.3f);
yl.setAxisMinValue(0f); // this replaces setStartAtZero(true)
// yl.setInverted(true);

View file

@ -1,73 +0,0 @@
package com.github.mikephil.charting.renderer;
//public class XAxisRendererBarChart extends XAxisRenderer {
//
// protected BarChart mChart;
//
// public XAxisRendererBarChart(ViewPortHandler viewPortHandler, XAxis xAxis, Transformer trans,
// BarChart chart) {
// super(viewPortHandler, xAxis, trans);
//
// this.mChart = chart;
// }
//
// /**
// * draws the xPx-labels on the specified yPx-position
// *
// * @param pos
// */
// @Override
// protected void drawLabels(Canvas c, float pos, PointF anchor) {
//
// final float labelRotationAngleDegrees = mXAxis.getLabelRotationAngle();
//
// // pre allocate to save performance (dont allocate in loop)
// float[] position = new float[] {
// 0f, 0f
// };
//
// BarData bd = mChart.getData();
// int step = bd.getDataSetCount();
//
//// for (int i = mMinX; i <= mMaxX; i += mXAxis.mAxisLabelModulus) {
////
//// position[0] = i * step + i * bd.getGroupSpace()
//// + bd.getGroupSpace() / 2f;
////
//// // consider groups (center label for each group)
//// if (step > 1) {
//// position[0] += ((float) step - 1f) / 2f;
//// }
////
//// mTrans.pointValuesToPixel(position);
////
//// if (mViewPortHandler.isInBoundsX(position[0]) && i >= 0
//// && i < mXAxis.getValues().size()) {
////
//// String label = mXAxis.getValues().get(i).getLabel();
////
//// if (mXAxis.isAvoidFirstLastClippingEnabled()) {
////
//// // avoid clipping of the last
//// if (i == mXAxis.getValues().size() - 1) {
//// float width = Utils.calcTextWidth(mAxisLabelPaint, label);
////
//// if (position[0] + width / 2.f > mViewPortHandler.contentRight())
//// position[0] = mViewPortHandler.contentRight() - (width / 2.f);
////
//// // avoid clipping of the first
//// } else if (i == 0) {
////
//// float width = Utils.calcTextWidth(mAxisLabelPaint, label);
////
//// if (position[0] - width / 2.f < mViewPortHandler.contentLeft())
//// position[0] = mViewPortHandler.contentLeft() + (width / 2.f);
//// }
//// }
////
//// drawLabel(c, label, i, position[0], pos, anchor, labelRotationAngleDegrees);
//// }
//// }
// }
//}

View file

@ -147,16 +147,7 @@ public class YAxisRenderer extends AxisRenderer {
if (!mYAxis.isEnabled() || !mYAxis.isDrawLabelsEnabled())
return;
float[] positions = new float[mYAxis.mEntryCount * 2];
for (int i = 0; i < positions.length; i += 2) {
// only fill yPx values, xPx values are not needed since the yPx-labels
// are
// static on the xPx-axis
positions[i + 1] = mYAxis.mEntries[i / 2];
}
mTrans.pointValuesToPixel(positions);
float[] positions = getTransformedPositions();
mAxisLabelPaint.setTypeface(mYAxis.getTypeface());
mAxisLabelPaint.setTextSize(mYAxis.getTextSize());
@ -238,61 +229,80 @@ public class YAxisRenderer extends AxisRenderer {
if (!mYAxis.isEnabled())
return;
// pre alloc
float[] position = new float[2];
if (mYAxis.isDrawGridLinesEnabled()) {
float[] positions = getTransformedPositions();
mGridPaint.setColor(mYAxis.getGridColor());
mGridPaint.setStrokeWidth(mYAxis.getGridLineWidth());
mGridPaint.setPathEffect(mYAxis.getGridDashPathEffect());
Path gridLinePath = new Path();
// draw the horizontal grid
for (int i = 0; i < mYAxis.mEntryCount; i++) {
position[1] = mYAxis.mEntries[i];
mTrans.pointValuesToPixel(position);
gridLinePath.moveTo(mViewPortHandler.offsetLeft(), position[1]);
gridLinePath.lineTo(mViewPortHandler.contentRight(), position[1]);
// draw the grid
for (int i = 0; i < positions.length; i += 2) {
// draw a path because lines don't support dashing on lower android versions
c.drawPath(gridLinePath, mGridPaint);
c.drawPath(linePath(gridLinePath, i, positions), mGridPaint);
gridLinePath.reset();
}
}
if (mYAxis.isDrawZeroLineEnabled()) {
// draw zero line
position[1] = 0f;
mTrans.pointValuesToPixel(position);
drawZeroLine(c, mViewPortHandler.offsetLeft(), mViewPortHandler.contentRight(), position[1] - 1, position[1] - 1);
drawZeroLine(c);
}
}
/**
* Draws the zero line at the specified position.
* Calculates the path for a grid line.
*
* @param c
* @param x1
* @param x2
* @param y1
* @param y2
* @param p
* @param i
* @param positions
* @return
*/
protected void drawZeroLine(Canvas c, float x1, float x2, float y1, float y2) {
protected Path linePath(Path p, int i, float[] positions) {
p.moveTo(mViewPortHandler.offsetLeft(), positions[i + 1]);
p.lineTo(mViewPortHandler.contentRight(), positions[i + 1]);
return p;
}
/**
* Transforms the values contained in the axis entries to screen pixels and returns them in form of a float array
* of x- and y-coordinates.
*
* @return
*/
protected float[] getTransformedPositions() {
float[] positions = new float[mYAxis.mEntryCount * 2];
for (int i = 0; i < positions.length; i += 2) {
// only fill y values, x values are not needed for y-labels
positions[i + 1] = mYAxis.mEntries[i / 2];
}
mTrans.pointValuesToPixel(positions);
return positions;
}
/**
* Draws the zero line.
*/
protected void drawZeroLine(Canvas c) {
// draw zero line
PointD pos = mTrans.getPixelsForValues(0f, 0f);
mZeroLinePaint.setColor(mYAxis.getZeroLineColor());
mZeroLinePaint.setStrokeWidth(mYAxis.getZeroLineWidth());
Path zeroLinePath = new Path();
zeroLinePath.moveTo(x1, y1);
zeroLinePath.lineTo(x2, y2);
zeroLinePath.moveTo(mViewPortHandler.contentLeft(), (float) pos.y - 1);
zeroLinePath.lineTo(mViewPortHandler.contentRight(), (float) pos.y - 1);
// draw a path because lines don't support dashing on lower android versions
c.drawPath(zeroLinePath, mZeroLinePaint);

View file

@ -66,16 +66,7 @@ public class YAxisRendererHorizontalBarChart extends YAxisRenderer {
if (!mYAxis.isEnabled() || !mYAxis.isDrawLabelsEnabled())
return;
float[] positions = new float[mYAxis.mEntryCount * 2];
for (int i = 0; i < positions.length; i += 2) {
// only fill yPx values, xPx values are not needed since the yPx-labels
// are
// static on the xPx-axis
positions[i] = mYAxis.mEntries[i / 2];
}
mTrans.pointValuesToPixel(positions);
float[] positions = getTransformedPositions();
mAxisLabelPaint.setTypeface(mYAxis.getTypeface());
mAxisLabelPaint.setTextSize(mYAxis.getTextSize());
@ -155,39 +146,44 @@ public class YAxisRendererHorizontalBarChart extends YAxisRenderer {
}
@Override
public void renderGridLines(Canvas c) {
protected float[] getTransformedPositions() {
if (!mYAxis.isEnabled())
return;
float[] positions = new float[mYAxis.mEntryCount * 2];
// pre alloc
float[] position = new float[2];
if (mYAxis.isDrawGridLinesEnabled()) {
mGridPaint.setColor(mYAxis.getGridColor());
mGridPaint.setStrokeWidth(mYAxis.getGridLineWidth());
// draw the horizontal grid
for (int i = 0; i < mYAxis.mEntryCount; i++) {
position[0] = mYAxis.mEntries[i];
mTrans.pointValuesToPixel(position);
c.drawLine(position[0], mViewPortHandler.contentTop(), position[0],
mViewPortHandler.contentBottom(),
mGridPaint);
}
for (int i = 0; i < positions.length; i += 2) {
// only fill x values, y values are not needed for x-labels
positions[i] = mYAxis.mEntries[i / 2];
}
if (mYAxis.isDrawZeroLineEnabled()) {
mTrans.pointValuesToPixel(positions);
return positions;
}
// draw zero line
position[0] = 0f;
mTrans.pointValuesToPixel(position);
@Override
protected Path linePath(Path p, int i, float[] positions) {
drawZeroLine(c, position[0]+1, position[0]+1, mViewPortHandler.contentTop(), mViewPortHandler.contentBottom());
}
p.moveTo(positions[i], mViewPortHandler.contentTop());
p.lineTo(positions[i], mViewPortHandler.contentBottom());
return p;
}
@Override
protected void drawZeroLine(Canvas c) {
// draw zero line
PointD pos = mTrans.getPixelsForValues(0f, 0f);
mZeroLinePaint.setColor(mYAxis.getZeroLineColor());
mZeroLinePaint.setStrokeWidth(mYAxis.getZeroLineWidth());
Path zeroLinePath = new Path();
zeroLinePath.moveTo((float) pos.x - 1, mViewPortHandler.contentTop());
zeroLinePath.lineTo((float) pos.x - 1, mViewPortHandler.contentBottom());
// draw a path because lines don't support dashing on lower android versions
c.drawPath(zeroLinePath, mZeroLinePaint);
}
/**