Work on realm and barchart

This commit is contained in:
Philipp Jahoda 2016-06-03 23:30:49 +02:00
parent 589bd0cbad
commit 40f078a2bf
8 changed files with 169 additions and 176 deletions

View file

@ -196,6 +196,7 @@ public class AnotherBarActivity extends DemoBase implements OnSeekBarChangeListe
BarData data = new BarData(dataSets);
mChart.setData(data);
mChart.setFitBars(true);
}
mChart.invalidate();

View file

@ -108,6 +108,7 @@ public class ListViewBarChartActivity extends DemoBase {
// set data
holder.chart.setData(data);
holder.chart.setFitBars(true);
// do not forget to refresh the chart
// holder.chart.invalidate();

View file

@ -19,9 +19,6 @@ public class RealmDemoData extends RealmObject {
private RealmList<RealmFloat> stackValues;
private String xAxisLabel;
private double xAxisPosition;
private String someStringField;
// ofc there could me more fields here...
@ -30,11 +27,9 @@ public class RealmDemoData extends RealmObject {
}
public RealmDemoData(float xValue, float yValue, double xAxisPosition, String xAxisLabel) {
public RealmDemoData(float xValue, float yValue) {
this.xValue = xValue;
this.yValue = yValue;
this.xAxisPosition = xAxisPosition;
this.xAxisLabel = xAxisLabel;
}
/**
@ -42,13 +37,9 @@ public class RealmDemoData extends RealmObject {
*
* @param xValue
* @param stackValues
* @param xAxisPosition
* @param xAxisLabel
*/
public RealmDemoData(float xValue, float[] stackValues, double xAxisPosition, String xAxisLabel) {
public RealmDemoData(float xValue, float[] stackValues) {
this.xValue = xValue;
this.xAxisPosition = xAxisPosition;
this.xAxisLabel = xAxisLabel;
this.stackValues = new RealmList<RealmFloat>();
for (float val : stackValues) {
@ -64,19 +55,14 @@ public class RealmDemoData extends RealmObject {
* @param low
* @param open
* @param close
* @param xAxisPosition
* @param xAxisLabel
*/
public RealmDemoData(float xValue, float high, float low, float open, float close, double xAxisPosition, String
xAxisLabel) {
public RealmDemoData(float xValue, float high, float low, float open, float close) {
this.yValue = (high + low) / 2f;
this.high = high;
this.low = low;
this.open = open;
this.close = close;
this.xValue = xValue;
this.xAxisPosition = xAxisPosition;
this.xAxisLabel = xAxisLabel;
}
/**
@ -85,15 +71,11 @@ public class RealmDemoData extends RealmObject {
* @param xValue
* @param yValue
* @param bubbleSize
* @param xAxisPosition
* @param xAxisLabel
*/
public RealmDemoData(float xValue, float yValue, float bubbleSize, double xAxisPosition, String xAxisLabel) {
public RealmDemoData(float xValue, float yValue, float bubbleSize) {
this.xValue = xValue;
this.yValue = yValue;
this.bubbleSize = bubbleSize;
this.xAxisPosition = xAxisPosition;
this.xAxisLabel = xAxisLabel;
}
public float getyValue() {
@ -168,19 +150,4 @@ public class RealmDemoData extends RealmObject {
this.someStringField = someStringField;
}
public double getxAxisPosition() {
return xAxisPosition;
}
public void setxAxisPosition(double xAxisPosition) {
this.xAxisPosition = xAxisPosition;
}
public String getxAxisLabel() {
return xAxisLabel;
}
public void setxAxisLabel(String xAxisLabel) {
this.xAxisLabel = xAxisLabel;
}
}

View file

@ -74,6 +74,7 @@ public class BarChartItem extends ChartItem {
// set data
holder.chart.setData((BarData) mChartData);
holder.chart.setFitBars(true);
// do not forget to refresh the chart
// holder.chart.invalidate();

View file

@ -110,7 +110,7 @@ public abstract class RealmBaseActivity extends DemoBase {
float value = 40f + (float) (Math.random() * 60f);
RealmDemoData d = new RealmDemoData(i, value, i, "" + i);
RealmDemoData d = new RealmDemoData(i, value);
mRealm.copyToRealm(d);
}
@ -129,7 +129,7 @@ public abstract class RealmBaseActivity extends DemoBase {
float val2 = 34f + (float) (Math.random() * 12.0f);
float[] stack = new float[]{val1, val2, 100 - val1 - val2};
RealmDemoData d = new RealmDemoData(i, stack, i, "" + i);
RealmDemoData d = new RealmDemoData(i, stack);
mRealm.copyToRealm(d);
}
@ -156,7 +156,7 @@ public abstract class RealmBaseActivity extends DemoBase {
boolean even = i % 2 == 0;
RealmDemoData d = new RealmDemoData(i, val + high, val - low, even ? val + open : val - open,
even ? val - close : val + close, i, i + "");
even ? val - close : val + close);
mRealm.copyToRealm(d);
}
@ -175,7 +175,7 @@ public abstract class RealmBaseActivity extends DemoBase {
float value = 30f + (float) (Math.random() * 100.0);
float size = 15f + (float) (Math.random() * 20.0);
RealmDemoData d = new RealmDemoData(i, value, size, "" + i);
RealmDemoData d = new RealmDemoData(i, value, size);
mRealm.copyToRealm(d);
}
@ -198,7 +198,7 @@ public abstract class RealmBaseActivity extends DemoBase {
String[] xValues = new String[]{ "iOS", "Android", "WP 10", "BlackBerry", "Other"};
for (int i = 0; i < values.length; i++) {
RealmDemoData d = new RealmDemoData(i, values[i], i, xValues[i]);
RealmDemoData d = new RealmDemoData(i, values[i]);
mRealm.copyToRealm(d);
}

View file

@ -50,7 +50,7 @@ public class RealmDatabaseActivityBar extends RealmBaseActivity {
RealmResults<RealmDemoData> result = mRealm.allObjects(RealmDemoData.class);
//RealmBarDataSet<RealmDemoData> set = new RealmBarDataSet<RealmDemoData>(result, "stackValues", "xIndex"); // normal entries
RealmBarDataSet<RealmDemoData> set = new RealmBarDataSet<RealmDemoData>(result, "yValue", "xValue"); // stacked entries
RealmBarDataSet<RealmDemoData> set = new RealmBarDataSet<RealmDemoData>(result, "xValue", "yValue"); // stacked entries
set.setColors(new int[] {ColorTemplate.rgb("#FF5722"), ColorTemplate.rgb("#03A9F4")});
set.setLabel("Realm BarDataSet");
@ -63,6 +63,7 @@ public class RealmDatabaseActivityBar extends RealmBaseActivity {
// set data
mChart.setData(data);
mChart.setFitBars(true);
mChart.animateY(1400, Easing.EasingOption.EaseInOutQuart);
}
}

View file

@ -5,6 +5,7 @@ import android.graphics.RectF;
import android.util.AttributeSet;
import android.util.Log;
import com.github.mikephil.charting.components.YAxis;
import com.github.mikephil.charting.data.BarData;
import com.github.mikephil.charting.data.BarEntry;
import com.github.mikephil.charting.highlight.BarHighlighter;
@ -15,170 +16,191 @@ import com.github.mikephil.charting.renderer.BarChartRenderer;
/**
* Chart that draws bars.
*
*
* @author Philipp Jahoda
*/
public class BarChart extends BarLineChartBase<BarData> implements BarDataProvider {
/** flag that enables or disables the highlighting arrow */
private boolean mDrawHighlightArrow = false;
/**
* flag that enables or disables the highlighting arrow
*/
private boolean mDrawHighlightArrow = false;
/**
* if set to true, all values are drawn above their bars, instead of below their top
*/
private boolean mDrawValueAboveBar = true;
/**
* if set to true, all values are drawn above their bars, instead of below their top
*/
private boolean mDrawValueAboveBar = true;
/**
* if set to true, a grey area is drawn behind each bar that indicates the maximum yValue
*/
private boolean mDrawBarShadow = false;
/**
* if set to true, a grey area is drawn behind each bar that indicates the maximum yValue
*/
private boolean mDrawBarShadow = false;
public BarChart(Context context) {
super(context);
}
private boolean mFitBars = false;
public BarChart(Context context, AttributeSet attrs) {
super(context, attrs);
}
public BarChart(Context context) {
super(context);
}
public BarChart(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
public BarChart(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
protected void init() {
super.init();
public BarChart(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
mRenderer = new BarChartRenderer(this, mAnimator, mViewPortHandler);
@Override
protected void init() {
super.init();
setHighlighter(new BarHighlighter(this));
mRenderer = new BarChartRenderer(this, mAnimator, mViewPortHandler);
//mXAxis.mAxisMinimum = -0.5f;
}
setHighlighter(new BarHighlighter(this));
@Override
protected void calcMinMax() {
super.calcMinMax();
//
// // increase deltax by 1 because the bars have a width of 1
// mXAxis.mAxisRange += 0.5f;
//
// // extend xDelta to make space for multiple datasets (if ther are one)
// mXAxis.mAxisRange *= mData.getDataSetCount();
//
// float groupSpace = mData.getGroupSpace();
// mXAxis.mAxisRange += mData.getXValCount() * groupSpace;
// mXAxis.mAxisMaximum = mXAxis.mAxisRange - mXAxis.mAxisMinimum;
}
//mXAxis.mAxisMinimum = -0.5f;
}
/**
* Returns the Highlight object (contains xPx-index and DataSet index) of the selected yValue at the given touch point
* inside the BarChart.
*
* @param x
* @param y
* @return
*/
@Override
public Highlight getHighlightByTouchPoint(float x, float y) {
@Override
protected void calcMinMax() {
if (mData == null) {
Log.e(LOG_TAG, "Can't select by touch. No data set.");
return null;
} else
return getHighlighter().getHighlight(x, y);
}
if (mAutoScaleMinMaxEnabled)
mData.calcMinMax();
/**
* Returns the bounding box of the specified Entry in the specified DataSet. Returns null if the Entry could not be
* found in the charts data.
*
* @param e
* @return
*/
public RectF getBarBounds(BarEntry e) {
if (mFitBars) {
mXAxis.calculate(mData.getXMin() - mData.getBarWidth() / 2f, mData.getXMax() + mData.getBarWidth() / 2f);
} else {
mXAxis.calculate(mData.getXMin(), mData.getXMax());
}
IBarDataSet set = mData.getDataSetForEntry(e);
// calculate axis range (min / max) according to provided data
mAxisLeft.calculate(mData.getYMin(YAxis.AxisDependency.LEFT), mData.getYMax(YAxis.AxisDependency.LEFT));
mAxisRight.calculate(mData.getYMin(YAxis.AxisDependency.RIGHT), mData.getYMax(YAxis.AxisDependency
.RIGHT));
}
if (set == null)
return null;
/**
* Returns the Highlight object (contains xPx-index and DataSet index) of the selected yValue at the given touch
* point
* inside the BarChart.
*
* @param x
* @param y
* @return
*/
@Override
public Highlight getHighlightByTouchPoint(float x, float y) {
float y = e.getY();
float x = e.getX();
if (mData == null) {
Log.e(LOG_TAG, "Can't select by touch. No data set.");
return null;
} else
return getHighlighter().getHighlight(x, y);
}
float barWidth = mData.getBarWidth();
/**
* Returns the bounding box of the specified Entry in the specified DataSet. Returns null if the Entry could not be
* found in the charts data.
*
* @param e
* @return
*/
public RectF getBarBounds(BarEntry e) {
float left = x - barWidth / 2f;
float right = x + barWidth / 2f;
float top = y >= 0 ? y : 0;
float bottom = y <= 0 ? y : 0;
IBarDataSet set = mData.getDataSetForEntry(e);
RectF bounds = new RectF(left, top, right, bottom);
if (set == null)
return null;
getTransformer(set.getAxisDependency()).rectValueToPixel(bounds);
float y = e.getY();
float x = e.getX();
return bounds;
}
float barWidth = mData.getBarWidth();
/**
* set this to true to draw the highlightning arrow
*
* @param enabled
*/
public void setDrawHighlightArrow(boolean enabled) {
mDrawHighlightArrow = enabled;
}
float left = x - barWidth / 2f;
float right = x + barWidth / 2f;
float top = y >= 0 ? y : 0;
float bottom = y <= 0 ? y : 0;
/**
* returns true if drawing the highlighting arrow is enabled, false if not
*
* @return
*/
public boolean isDrawHighlightArrowEnabled() {
return mDrawHighlightArrow;
}
RectF bounds = new RectF(left, top, right, bottom);
/**
* If set to true, all values are drawn above their bars, instead of below their top.
*
* @param enabled
*/
public void setDrawValueAboveBar(boolean enabled) {
mDrawValueAboveBar = enabled;
}
getTransformer(set.getAxisDependency()).rectValueToPixel(bounds);
/**
* returns true if drawing values above bars is enabled, false if not
*
* @return
*/
public boolean isDrawValueAboveBarEnabled() {
return mDrawValueAboveBar;
}
return bounds;
}
/**
* If set to true, a grey area is drawn behind each bar that indicates the maximum yValue. Enabling his will reduce
* performance by about 50%.
*
* @param enabled
*/
public void setDrawBarShadow(boolean enabled) {
mDrawBarShadow = enabled;
}
/**
* set this to true to draw the highlightning arrow
*
* @param enabled
*/
public void setDrawHighlightArrow(boolean enabled) {
mDrawHighlightArrow = enabled;
}
/**
* returns true if drawing shadows (maxvalue) for each bar is enabled, false if not
*
* @return
*/
public boolean isDrawBarShadowEnabled() {
return mDrawBarShadow;
}
/**
* returns true if drawing the highlighting arrow is enabled, false if not
*
* @return
*/
public boolean isDrawHighlightArrowEnabled() {
return mDrawHighlightArrow;
}
/**
* If set to true, all values are drawn above their bars, instead of below their top.
*
* @param enabled
*/
public void setDrawValueAboveBar(boolean enabled) {
mDrawValueAboveBar = enabled;
}
/**
* returns true if drawing values above bars is enabled, false if not
*
* @return
*/
public boolean isDrawValueAboveBarEnabled() {
return mDrawValueAboveBar;
}
/**
* If set to true, a grey area is drawn behind each bar that indicates the maximum yValue. Enabling his will reduce
* performance by about 50%.
*
* @param enabled
*/
public void setDrawBarShadow(boolean enabled) {
mDrawBarShadow = enabled;
}
/**
* returns true if drawing shadows (maxvalue) for each bar is enabled, false if not
*
* @return
*/
public boolean isDrawBarShadowEnabled() {
return mDrawBarShadow;
}
@Override
public BarData getBarData() {
return mData;
}
/**
* Adds half of the bar width to each side of the x-axis range in order to allow the bars of the barchart to be
* fully displayed.
* Default: false
*
* @param enabled
*/
public void setFitBars(boolean enabled) {
mFitBars = enabled;
}
@Override
public BarData getBarData() {
return mData;
}
// /**
// * Returns the lowest xPx-index (yValue on the xPx-axis) that is still visible on the chart.

View file

@ -57,7 +57,7 @@ public abstract class BarLineChartBase<T extends BarLineScatterCandleBubbleData<
/**
* flag that indicates if auto scaling on the yPx axis is enabled
*/
private boolean mAutoScaleMinMaxEnabled = false;
protected boolean mAutoScaleMinMaxEnabled = false;
private Float mAutoScaleLastLowestVisibleXIndex = null;
private Float mAutoScaleLastHighestVisibleXIndex = null;