use of com.github.mikephil.charting.data.CandleEntry in project MPAndroidChart by PhilJay.
the class CandleStickChartActivity method onProgressChanged.
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
int prog = (mSeekBarX.getProgress() + 1);
tvX.setText("" + prog);
tvY.setText("" + (mSeekBarY.getProgress()));
mChart.resetTracking();
ArrayList<CandleEntry> yVals1 = new ArrayList<CandleEntry>();
for (int i = 0; i < prog; i++) {
float mult = (mSeekBarY.getProgress() + 1);
float val = (float) (Math.random() * 40) + mult;
float high = (float) (Math.random() * 9) + 8f;
float low = (float) (Math.random() * 9) + 8f;
float open = (float) (Math.random() * 6) + 1f;
float close = (float) (Math.random() * 6) + 1f;
boolean even = i % 2 == 0;
yVals1.add(new CandleEntry(i, val + high, val - low, even ? val + open : val - open, even ? val - close : val + close, getResources().getDrawable(R.drawable.star)));
}
CandleDataSet set1 = new CandleDataSet(yVals1, "Data Set");
set1.setDrawIcons(false);
set1.setAxisDependency(AxisDependency.LEFT);
// set1.setColor(Color.rgb(80, 80, 80));
set1.setShadowColor(Color.DKGRAY);
set1.setShadowWidth(0.7f);
set1.setDecreasingColor(Color.RED);
set1.setDecreasingPaintStyle(Paint.Style.FILL);
set1.setIncreasingColor(Color.rgb(122, 242, 84));
set1.setIncreasingPaintStyle(Paint.Style.STROKE);
set1.setNeutralColor(Color.BLUE);
//set1.setHighlightLineWidth(1f);
CandleData data = new CandleData(set1);
mChart.setData(data);
mChart.invalidate();
}
use of com.github.mikephil.charting.data.CandleEntry in project MPAndroidChart by PhilJay.
the class CandleStickChartRenderer method drawDataSet.
@SuppressWarnings("ResourceAsColor")
protected void drawDataSet(Canvas c, ICandleDataSet dataSet) {
Transformer trans = mChart.getTransformer(dataSet.getAxisDependency());
float phaseY = mAnimator.getPhaseY();
float barSpace = dataSet.getBarSpace();
boolean showCandleBar = dataSet.getShowCandleBar();
mXBounds.set(mChart, dataSet);
mRenderPaint.setStrokeWidth(dataSet.getShadowWidth());
// draw the body
for (int j = mXBounds.min; j <= mXBounds.range + mXBounds.min; j++) {
// get the entry
CandleEntry e = dataSet.getEntryForIndex(j);
if (e == null)
continue;
final float xPos = e.getX();
final float open = e.getOpen();
final float close = e.getClose();
final float high = e.getHigh();
final float low = e.getLow();
if (showCandleBar) {
// calculate the shadow
mShadowBuffers[0] = xPos;
mShadowBuffers[2] = xPos;
mShadowBuffers[4] = xPos;
mShadowBuffers[6] = xPos;
if (open > close) {
mShadowBuffers[1] = high * phaseY;
mShadowBuffers[3] = open * phaseY;
mShadowBuffers[5] = low * phaseY;
mShadowBuffers[7] = close * phaseY;
} else if (open < close) {
mShadowBuffers[1] = high * phaseY;
mShadowBuffers[3] = close * phaseY;
mShadowBuffers[5] = low * phaseY;
mShadowBuffers[7] = open * phaseY;
} else {
mShadowBuffers[1] = high * phaseY;
mShadowBuffers[3] = open * phaseY;
mShadowBuffers[5] = low * phaseY;
mShadowBuffers[7] = mShadowBuffers[3];
}
trans.pointValuesToPixel(mShadowBuffers);
if (dataSet.getShadowColorSameAsCandle()) {
if (open > close)
mRenderPaint.setColor(dataSet.getDecreasingColor() == ColorTemplate.COLOR_NONE ? dataSet.getColor(j) : dataSet.getDecreasingColor());
else if (open < close)
mRenderPaint.setColor(dataSet.getIncreasingColor() == ColorTemplate.COLOR_NONE ? dataSet.getColor(j) : dataSet.getIncreasingColor());
else
mRenderPaint.setColor(dataSet.getNeutralColor() == ColorTemplate.COLOR_NONE ? dataSet.getColor(j) : dataSet.getNeutralColor());
} else {
mRenderPaint.setColor(dataSet.getShadowColor() == ColorTemplate.COLOR_NONE ? dataSet.getColor(j) : dataSet.getShadowColor());
}
mRenderPaint.setStyle(Paint.Style.STROKE);
c.drawLines(mShadowBuffers, mRenderPaint);
// calculate the body
mBodyBuffers[0] = xPos - 0.5f + barSpace;
mBodyBuffers[1] = close * phaseY;
mBodyBuffers[2] = (xPos + 0.5f - barSpace);
mBodyBuffers[3] = open * phaseY;
trans.pointValuesToPixel(mBodyBuffers);
// draw body differently for increasing and decreasing entry
if (open > close) {
if (dataSet.getDecreasingColor() == ColorTemplate.COLOR_NONE) {
mRenderPaint.setColor(dataSet.getColor(j));
} else {
mRenderPaint.setColor(dataSet.getDecreasingColor());
}
mRenderPaint.setStyle(dataSet.getDecreasingPaintStyle());
c.drawRect(mBodyBuffers[0], mBodyBuffers[3], mBodyBuffers[2], mBodyBuffers[1], mRenderPaint);
} else if (open < close) {
if (dataSet.getIncreasingColor() == ColorTemplate.COLOR_NONE) {
mRenderPaint.setColor(dataSet.getColor(j));
} else {
mRenderPaint.setColor(dataSet.getIncreasingColor());
}
mRenderPaint.setStyle(dataSet.getIncreasingPaintStyle());
c.drawRect(mBodyBuffers[0], mBodyBuffers[1], mBodyBuffers[2], mBodyBuffers[3], mRenderPaint);
} else {
if (dataSet.getNeutralColor() == ColorTemplate.COLOR_NONE) {
mRenderPaint.setColor(dataSet.getColor(j));
} else {
mRenderPaint.setColor(dataSet.getNeutralColor());
}
c.drawLine(mBodyBuffers[0], mBodyBuffers[1], mBodyBuffers[2], mBodyBuffers[3], mRenderPaint);
}
} else {
mRangeBuffers[0] = xPos;
mRangeBuffers[1] = high * phaseY;
mRangeBuffers[2] = xPos;
mRangeBuffers[3] = low * phaseY;
mOpenBuffers[0] = xPos - 0.5f + barSpace;
mOpenBuffers[1] = open * phaseY;
mOpenBuffers[2] = xPos;
mOpenBuffers[3] = open * phaseY;
mCloseBuffers[0] = xPos + 0.5f - barSpace;
mCloseBuffers[1] = close * phaseY;
mCloseBuffers[2] = xPos;
mCloseBuffers[3] = close * phaseY;
trans.pointValuesToPixel(mRangeBuffers);
trans.pointValuesToPixel(mOpenBuffers);
trans.pointValuesToPixel(mCloseBuffers);
// draw the ranges
int barColor;
if (open > close)
barColor = dataSet.getDecreasingColor() == ColorTemplate.COLOR_NONE ? dataSet.getColor(j) : dataSet.getDecreasingColor();
else if (open < close)
barColor = dataSet.getIncreasingColor() == ColorTemplate.COLOR_NONE ? dataSet.getColor(j) : dataSet.getIncreasingColor();
else
barColor = dataSet.getNeutralColor() == ColorTemplate.COLOR_NONE ? dataSet.getColor(j) : dataSet.getNeutralColor();
mRenderPaint.setColor(barColor);
c.drawLine(mRangeBuffers[0], mRangeBuffers[1], mRangeBuffers[2], mRangeBuffers[3], mRenderPaint);
c.drawLine(mOpenBuffers[0], mOpenBuffers[1], mOpenBuffers[2], mOpenBuffers[3], mRenderPaint);
c.drawLine(mCloseBuffers[0], mCloseBuffers[1], mCloseBuffers[2], mCloseBuffers[3], mRenderPaint);
}
}
}
use of com.github.mikephil.charting.data.CandleEntry in project MPAndroidChart by PhilJay.
the class CandleStickChartRenderer method drawHighlighted.
@Override
public void drawHighlighted(Canvas c, Highlight[] indices) {
CandleData candleData = mChart.getCandleData();
for (Highlight high : indices) {
ICandleDataSet set = candleData.getDataSetByIndex(high.getDataSetIndex());
if (set == null || !set.isHighlightEnabled())
continue;
CandleEntry e = set.getEntryForXValue(high.getX(), high.getY());
if (!isInBoundsX(e, set))
continue;
float lowValue = e.getLow() * mAnimator.getPhaseY();
float highValue = e.getHigh() * mAnimator.getPhaseY();
float y = (lowValue + highValue) / 2f;
MPPointD pix = mChart.getTransformer(set.getAxisDependency()).getPixelForValues(e.getX(), y);
high.setDraw((float) pix.x, (float) pix.y);
// draw the lines
drawHighlightLines(c, (float) pix.x, (float) pix.y, set);
}
}
use of com.github.mikephil.charting.data.CandleEntry in project MPAndroidChart by PhilJay.
the class Transformer method generateTransformedValuesCandle.
/**
* Transforms an List of Entry into a float array containing the x and
* y values transformed with all matrices for the CANDLESTICKCHART.
*
* @param data
* @return
*/
public float[] generateTransformedValuesCandle(ICandleDataSet data, float phaseX, float phaseY, int from, int to) {
final int count = (int) ((to - from) * phaseX + 1) * 2;
if (valuePointsForGenerateTransformedValuesCandle.length != count) {
valuePointsForGenerateTransformedValuesCandle = new float[count];
}
float[] valuePoints = valuePointsForGenerateTransformedValuesCandle;
for (int j = 0; j < count; j += 2) {
CandleEntry e = data.getEntryForIndex(j / 2 + from);
if (e != null) {
valuePoints[j] = e.getX();
valuePoints[j + 1] = e.getHigh() * phaseY;
} else {
valuePoints[j] = 0;
valuePoints[j + 1] = 0;
}
}
getValueToPixelMatrix().mapPoints(valuePoints);
return valuePoints;
}
Aggregations