Skip to content

Commit

Permalink
Fixed bug in 3D BarChart.
Browse files Browse the repository at this point in the history
  • Loading branch information
PhilJay committed May 28, 2014
1 parent 245648f commit aba44b3
Show file tree
Hide file tree
Showing 7 changed files with 113 additions and 150 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ protected void onCreate(Bundle savedInstanceState) {
// mChart.setSpacePercent(20, 10);
mChart.setYLegendCount(5);
mChart.setTouchEnabled(true);
mChart.setDescription("");

mSeekBarX.setProgress(45);
mSeekBarY.setProgress(100);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ protected void onCreate(Bundle savedInstanceState) {

ColorTemplate ct = new ColorTemplate();
ct.addDataSetColors(ColorTemplate.COLORFUL_COLORS, this);
ct.addDataSetColors(ColorTemplate.LIBERTY_COLORS, this);
ct.addDataSetColors(ColorTemplate.FRESH_COLORS, this);
mChart.setColorTemplate(ct);

mChart.setDrawYValues(true);
Expand Down Expand Up @@ -148,7 +148,7 @@ public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
}

tvX.setText("" + (mSeekBarX.getProgress()));
tvY.setText("" + (mSeekBarY.getProgress()));
tvY.setText("" + (mSeekBarY.getProgress()));

ArrayList<String> xVals = new ArrayList<String>();

Expand Down
217 changes: 110 additions & 107 deletions MPChartLib/src/com/github/mikephil/charting/BarChart.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.graphics.Path;
import android.graphics.RectF;
import android.util.AttributeSet;
Expand Down Expand Up @@ -51,62 +50,74 @@ protected void init() {
// set alpha after color
mHighlightPaint.setAlpha(120);
}

@Override
public void setColorTemplate(ColorTemplate ct) {
super.setColorTemplate(ct);

calculate3DColors();
}

/** array that holds all the colors for the top 3D effect */
private int[] mTopColors;
private ArrayList<ArrayList<Integer>> mTopColors;

/** array that holds all the colors for the side 3D effect */
private int[] mSideColors;
private ArrayList<ArrayList<Integer>> mSideColors;

@Override
protected void prepareDataPaints(ColorTemplate ct) {

// // prepare the paints
// mDrawPaints = new Paint[ct.getColors().size()];
//
// for (int i = 0; i < ct.getColors().size(); i++) {
// mDrawPaints[i] = new Paint(Paint.ANTI_ALIAS_FLAG);
// mDrawPaints[i].setStyle(Style.FILL);
// mDrawPaints[i].setColor(Color.BLACK);
// }

// // generate the colors for the 3D effect
// mTopColors = new int[mDrawPaints.length];
// mSideColors = new int[mDrawPaints.length];
//
// float[] hsv = new float[3];
//
// for (int i = 0; i < mSideColors.length; i++) {
//
// // extract the color
// int c = mDrawPaints[i].getColor();
// Color.colorToHSV(c, hsv); // convert to hsv
//
// // make brighter
// hsv[1] = hsv[1] - 0.1f; // less saturation
// hsv[2] = hsv[2] + 0.1f; // more brightness
//
// // convert back
// c = Color.HSVToColor(hsv);
//
// // assign
// mTopColors[i] = c;
//
// // get color again
// c = mDrawPaints[i].getColor();
//
// // convert
// Color.colorToHSV(c, hsv);
//
// // make darker
// hsv[1] = hsv[1] + 0.1f; // more saturation
// hsv[2] = hsv[2] - 0.1f; // less brightness
//
// // reassing
// c = Color.HSVToColor(hsv);
//
// mSideColors[i] = c;
// }
/**
* calculates the 3D color arrays
*/
protected void calculate3DColors() {

// generate the colors for the 3D effect
mTopColors = new ArrayList<ArrayList<Integer>>();
mSideColors = new ArrayList<ArrayList<Integer>>();

float[] hsv = new float[3];

for (int i = 0; i < mCt.getColors().size(); i++) {

// Get the colors for the DataSet at the current index. If the index
// is out of bounds, reuse DataSet colors.
ArrayList<Integer> colors = mCt.getDataSetColors(i);
ArrayList<Integer> topColors = new ArrayList<Integer>();
ArrayList<Integer> sideColors = new ArrayList<Integer>();

for (int j = 0; j < colors.size(); j++) {

// extract the color
int c = colors.get(j);
Color.colorToHSV(c, hsv); // convert to hsv

// make brighter
hsv[1] = hsv[1] - 0.1f; // less saturation
hsv[2] = hsv[2] + 0.1f; // more brightness

// convert back
c = Color.HSVToColor(hsv);

// assign
topColors.add(c);

// get color again
c = colors.get(j);

// convert
Color.colorToHSV(c, hsv);

// make darker
hsv[1] = hsv[1] + 0.1f; // more saturation
hsv[2] = hsv[2] - 0.1f; // less brightness

// reassing
c = Color.HSVToColor(hsv);

sideColors.add(c);
}

mTopColors.add(topColors);
mSideColors.add(sideColors);
}
}

@Override
Expand Down Expand Up @@ -159,15 +170,6 @@ protected void drawHighlights() {
transformPath(arrow);
mDrawCanvas.drawPath(arrow, mHighlightPaint);
}

// float[] pts = new float[] {
// index + 0.5f, mYChartMax, index + 0.5f, mYChartMin,
// 0, mYVals.get(index), mDeltaX, mYVals.get(index)
// };
//
// transformPointArray(pts);
// // draw the highlight lines
// mDrawCanvas.drawLines(pts, mHighlightPaint);
}
}
}
Expand All @@ -181,6 +183,9 @@ protected void drawData() {
ArrayList<Path> topPaths = new ArrayList<Path>();
ArrayList<Path> sidePaths = new ArrayList<Path>();

ArrayList<DataSet> dataSets = mData.getDataSets();

// 3D drawing
if (m3DEnabled) {

float[] pts = new float[] {
Expand Down Expand Up @@ -209,37 +214,45 @@ protected void drawData() {

float depth = Math.abs(pts[3] - pts[1]) * mDepth;

for (int i = 0; i < mData.getYValCount(); i++) {
for (int i = 0; i < mData.getDataSetCount(); i++) {

float y = getYValue(i);
float left = i + mBarSpace / 2f;
float right = i + 1f - mBarSpace / 2f;
float top = y >= 0 ? y : 0;
DataSet dataSet = dataSets.get(i);
ArrayList<Series> series = dataSet.getYVals();

for (int j = 0; j < series.size(); j++) {

float x = series.get(j).getXIndex();
float y = series.get(j).getVal();
float left = x + mBarSpace / 2f;
float right = x + 1f - mBarSpace / 2f;
float top = y >= 0 ? y : 0;

// create the 3D effect paths for the top and side
Path topPath = new Path();
topPath.moveTo(left, top);
topPath.lineTo(left + mSkew, top + depth);
topPath.lineTo(right + mSkew, top + depth);
topPath.lineTo(right, top);
// create the 3D effect paths for the top and side
Path topPath = new Path();
topPath.moveTo(left, top);
topPath.lineTo(left + mSkew, top + depth);
topPath.lineTo(right + mSkew, top + depth);
topPath.lineTo(right, top);

topPaths.add(topPath);
topPaths.add(topPath);

Path sidePath = new Path();
sidePath.moveTo(right, top);
sidePath.lineTo(right + mSkew, top + depth);
sidePath.lineTo(right + mSkew, depth);
sidePath.lineTo(right, 0);
Path sidePath = new Path();
sidePath.moveTo(right, top);
sidePath.lineTo(right + mSkew, top + depth);
sidePath.lineTo(right + mSkew, depth);
sidePath.lineTo(right, 0);

sidePaths.add(sidePath);
sidePaths.add(sidePath);
}
}

transformPaths(topPaths);
transformPaths(sidePaths);
}

int cnt = 0;

ArrayList<DataSet> dataSets = mData.getDataSets();

// 2D drawing
for (int i = 0; i < mData.getDataSetCount(); i++) {

DataSet dataSet = dataSets.get(i);
Expand All @@ -248,6 +261,8 @@ protected void drawData() {
// Get the colors for the DataSet at the current index. If the index
// is out of bounds, reuse DataSet colors.
ArrayList<Integer> colors = mCt.getDataSetColors(i % mCt.getColors().size());
ArrayList<Integer> colors3DTop = mTopColors.get(i % mCt.getColors().size());
ArrayList<Integer> colors3DSide = mSideColors.get(i % mCt.getColors().size());

// do the drawing
for (int j = 0; j < dataSet.getSeriesCount(); j++) {
Expand All @@ -266,21 +281,27 @@ protected void drawData() {
mBarRect.set(left, top, right, bottom);

transformRect(mBarRect);

// avoid drawing outofbounds values
if(isOffContentRight(mBarRect.left)) break;

if(isOffContentLeft(mBarRect.right)) {
cnt++;
continue;
}

mDrawCanvas.drawRect(mBarRect, mRenderPaint);

if (m3DEnabled) {

int c = mRenderPaint.getColor();

mRenderPaint.setColor(mTopColors[j % mTopColors.length]);
mDrawCanvas.drawPath(topPaths.get(j), mRenderPaint);

mRenderPaint.setColor(mSideColors[j % mSideColors.length]);
mDrawCanvas.drawPath(sidePaths.get(j), mRenderPaint);
mRenderPaint.setColor(colors3DTop.get(j % colors3DTop.size()));
mDrawCanvas.drawPath(topPaths.get(cnt), mRenderPaint);

mRenderPaint.setColor(c);
mRenderPaint.setColor(colors3DSide.get(j % colors3DSide.size()));
mDrawCanvas.drawPath(sidePaths.get(cnt), mRenderPaint);
}

cnt++;
}
}
}
Expand Down Expand Up @@ -399,24 +420,6 @@ public boolean is3DEnabled() {
return m3DEnabled;
}

/**
* returns the top colors that define the color of the top 3D effect path
*
* @return
*/
public int[] getTopColors() {
return mTopColors;
}

/**
* returns the side colors that define the color of the side 3D effect path
*
* @return
*/
public int[] getSideColors() {
return mSideColors;
}

/**
* set this to true to draw the highlightning arrow
*
Expand Down
12 changes: 0 additions & 12 deletions MPChartLib/src/com/github/mikephil/charting/Chart.java
Original file line number Diff line number Diff line change
Expand Up @@ -254,16 +254,6 @@ public void setData(ArrayList<String> xVals, ArrayList<Float> yVals) {
setData(data);
}

/**
* Prepares all the paint objects that are used for drawing the lines, bars
* or pie-slices. The number of paint objects used depends on the number of
* colors in the colortemplate. If more values than colors need to be drawn,
* colors are repeated.
*
* @param ct
*/
protected abstract void prepareDataPaints(ColorTemplate ct);

/**
* does needed preparations for drawing
*/
Expand Down Expand Up @@ -965,8 +955,6 @@ public void setYStartEnd(float start, float end) {
*/
public void setColorTemplate(ColorTemplate ct) {
this.mCt = ct;

prepareDataPaints(ct);
}

/**
Expand Down
16 changes: 0 additions & 16 deletions MPChartLib/src/com/github/mikephil/charting/LineChart.java
Original file line number Diff line number Diff line change
Expand Up @@ -65,22 +65,6 @@ protected void init() {
mHighlightPaint.setColor(Color.rgb(255, 187, 115));
}

@Override
protected void prepareDataPaints(ColorTemplate ct) {
//
// if (ct == null)
// return;
//
// mDrawPaints = new Paint[ct.getColors().size()];
//
// for (int i = 0; i < ct.getColors().size(); i++) {
// mDrawPaints[i] = new Paint(Paint.ANTI_ALIAS_FLAG);
// mDrawPaints[i].setStrokeWidth(mLineWidth);
// mDrawPaints[i].setStyle(Style.FILL);
// // mDrawPaints[i].setColor(ct.getColors().get(i));
// }
}

@Override
protected void drawHighlights() {

Expand Down
13 changes: 0 additions & 13 deletions MPChartLib/src/com/github/mikephil/charting/PieChart.java
Original file line number Diff line number Diff line change
Expand Up @@ -251,19 +251,6 @@ protected void prepareContentRect() {
- mShift);
}

@Override
protected void prepareDataPaints(ColorTemplate ct) {

// mDrawPaints = new Paint[ct.getColors().size()];
//
// // setup all paint objects
// for (int i = 0; i < ct.getColors().size(); i++) {
// mDrawPaints[i] = new Paint(Paint.ANTI_ALIAS_FLAG);
// mDrawPaints[i].setStyle(Style.FILL);
// // mDrawPaints[i].setColor(ct.getColors().get(i));
// }
}

@Override
protected void calcMinMax() {
super.calcMinMax();
Expand Down
Binary file added screenshots/barchart2d_multi_dataset.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit aba44b3

Please sign in to comment.