Skip to content

Commit

Permalink
Feature for making candle shadow color same as inc/dec
Browse files Browse the repository at this point in the history
  • Loading branch information
danielgindi committed Jul 9, 2015
1 parent 44e12a4 commit 9110391
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 23 deletions.
4 changes: 4 additions & 0 deletions MPChartExample/res/menu/candle.xml
Original file line number Diff line number Diff line change
Expand Up @@ -37,5 +37,9 @@
android:id="@+id/actionToggleAutoScaleMinMax"
android:title="Toggle auto scale min/max">
</item>
<item
android:id="@+id/actionToggleMakeShadowSameColorAsCandle"
android:title="Toggle shadow same color">
</item>

</menu>
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,15 @@ public boolean onOptionsItemSelected(MenuItem item) {
mChart.notifyDataSetChanged();
break;
}
case R.id.actionToggleMakeShadowSameColorAsCandle: {
for (CandleDataSet set : mChart.getData().getDataSets())
{
set.setShadowColorSameAsCandle(!set.getShadowColorSameAsCandle());
}

mChart.invalidate();
break;
}
case R.id.actionToggleStartzero: {
mChart.getAxisLeft().setStartAtZero(!mChart.getAxisLeft().isStartAtZeroEnabled());
mChart.getAxisRight().setStartAtZero(!mChart.getAxisRight().isStartAtZeroEnabled());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ public class CandleDataSet extends BarLineScatterCandleDataSet<CandleEntry> {
/** the space between the candle entries, default 0.1f (10%) */
private float mBodySpace = 0.1f;

/** use candle color for the shadow */
private boolean mShadowColorSameAsCandle = false;

/** paint style when open <= close */
protected Paint.Style mIncreasingPaintStyle = Paint.Style.FILL;

Expand Down Expand Up @@ -246,4 +249,22 @@ public int getShadowColor() {
public void setShadowColor(int shadowColor) {
this.mShadowColor = shadowColor;
}

/**
* Is the shadow color same as the candle color?
*
* @return
*/
public boolean getShadowColorSameAsCandle() {
return mShadowColorSameAsCandle;
}

/**
* Sets shadow color to be the same color as the candle color
*
* @param shadowColorSameAsCandle
*/
public void setShadowColorSameAsCandle(boolean shadowColorSameAsCandle) {
this.mShadowColorSameAsCandle = shadowColorSameAsCandle;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,15 @@ protected void drawDataSet(Canvas c, CandleDataSet dataSet) {
int range = (maxx - minx) * 4;
int to = (int)Math.ceil((maxx - minx) * phaseX + minx);

CandleBodyBuffer bodyBuffer = mBodyBuffers[dataSetIndex];
bodyBuffer.setBodySpace(dataSet.getBodySpace());
bodyBuffer.setPhases(phaseX, phaseY);
bodyBuffer.limitFrom(minx);
bodyBuffer.limitTo(maxx);
bodyBuffer.feed(entries);

trans.pointValuesToPixel(bodyBuffer.buffer);

CandleShadowBuffer shadowBuffer = mShadowBuffers[dataSetIndex];
shadowBuffer.setPhases(phaseX, phaseY);
shadowBuffer.limitFrom(minx);
Expand All @@ -87,29 +96,8 @@ protected void drawDataSet(Canvas c, CandleDataSet dataSet) {

trans.pointValuesToPixel(shadowBuffer.buffer);

mRenderPaint.setStyle(Paint.Style.STROKE);

// If not set, use default functionality for backward compatibility
if (dataSet.getShadowColor() == ColorTemplate.COLOR_NONE) {
mRenderPaint.setColor(dataSet.getColor());
} else {
mRenderPaint.setColor(dataSet.getShadowColor());
}

mRenderPaint.setStrokeWidth(dataSet.getShadowWidth());

// draw the shadow
c.drawLines(shadowBuffer.buffer, 0, range, mRenderPaint);

CandleBodyBuffer bodyBuffer = mBodyBuffers[dataSetIndex];
bodyBuffer.setBodySpace(dataSet.getBodySpace());
bodyBuffer.setPhases(phaseX, phaseY);
bodyBuffer.limitFrom(minx);
bodyBuffer.limitTo(maxx);
bodyBuffer.feed(entries);

trans.pointValuesToPixel(bodyBuffer.buffer);

// draw the body
for (int j = 0; j < range; j += 4) {

Expand All @@ -119,6 +107,44 @@ protected void drawDataSet(Canvas c, CandleDataSet dataSet) {
if (!fitsBounds(e.getXIndex(), mMinX, to))
continue;

if (dataSet.getShadowColorSameAsCandle()) {

if (e.getOpen() > e.getClose())
mRenderPaint.setColor(
dataSet.getDecreasingColor() == ColorTemplate.COLOR_NONE ?
dataSet.getColor(j) :
dataSet.getDecreasingColor()
);

else if (e.getOpen() < e.getClose())
mRenderPaint.setColor(
dataSet.getIncreasingColor() == ColorTemplate.COLOR_NONE ?
dataSet.getColor(j) :
dataSet.getIncreasingColor()
);

else
mRenderPaint.setColor(
dataSet.getShadowColor() == ColorTemplate.COLOR_NONE ?
dataSet.getColor(j) :
dataSet.getShadowColor()
);

} else {
mRenderPaint.setColor(
dataSet.getShadowColor() == ColorTemplate.COLOR_NONE ?
dataSet.getColor(j) :
dataSet.getShadowColor()
);
}

mRenderPaint.setStyle(Paint.Style.STROKE);

// draw the shadow
c.drawLine(shadowBuffer.buffer[j], shadowBuffer.buffer[j + 1],
shadowBuffer.buffer[j + 2], shadowBuffer.buffer[j + 3],
mRenderPaint);

float leftBody = bodyBuffer.buffer[j];
float open = bodyBuffer.buffer[j + 1];
float rightBody = bodyBuffer.buffer[j + 2];
Expand Down Expand Up @@ -150,8 +176,7 @@ protected void drawDataSet(Canvas c, CandleDataSet dataSet) {
c.drawRect(leftBody, open, rightBody, close, mRenderPaint);
} else { // equal values

mRenderPaint.setColor(Color.BLACK);
mRenderPaint.setStyle(Paint.Style.STROKE);
mRenderPaint.setColor(dataSet.getShadowColor());
c.drawLine(leftBody, open, rightBody, close, mRenderPaint);
}
}
Expand Down

0 comments on commit 9110391

Please sign in to comment.