Refactoring and improvements related to grouped bars

This commit is contained in:
Philipp Jahoda 2016-06-06 17:23:26 +02:00
parent 40917f0dda
commit 68947023d4
8 changed files with 41 additions and 101 deletions

View file

@ -190,9 +190,10 @@ public class BarChartActivityMultiDataset extends DemoBase implements OnSeekBarC
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
float groupSpace = 0.04f;
float barSpace = 0.02f; // x3
float barWidth = 0.3f; // x3
// (0.3 + 0.02) * 3 + 0.04 = 1.00
float barSpace = 0.02f; // x3 dataset
float barWidth = 0.3f; // x3 dataset
// (0.3 + 0.02) * 3 + 0.04 = 1.00 -> interval per "group"
int startYear = 1980;
int endYear = startYear + mSeekBarX.getProgress();
@ -258,10 +259,9 @@ public class BarChartActivityMultiDataset extends DemoBase implements OnSeekBarC
}
mChart.getBarData().setBarWidth(barWidth);
mChart.getBarData().groupBars(startYear, groupSpace, barSpace);
mChart.getXAxis().setAxisMinValue(startYear);
mChart.getXAxis().setAxisMaxValue(mChart.getBarData().getIntervalWidth(groupSpace, barSpace) * mSeekBarX.getProgress() + startYear);
mChart.notifyDataSetChanged();
mChart.getXAxis().setAxisMaxValue(mChart.getBarData().getGroupWidth(groupSpace, barSpace) * mSeekBarX.getProgress() + startYear);
mChart.groupBars(startYear, groupSpace, barSpace);
mChart.invalidate();
}

View file

@ -6,7 +6,6 @@ import com.github.mikephil.charting.interfaces.datasets.IBarDataSet;
public class BarBuffer extends AbstractBuffer<IBarDataSet> {
protected float mGroupSpace = 0f;
protected int mDataSetIndex = 0;
protected int mDataSetCount = 1;
protected boolean mContainsStacks = false;
@ -15,9 +14,8 @@ public class BarBuffer extends AbstractBuffer<IBarDataSet> {
/** width of the bar on the x-axis, in values (not pixels) */
protected float mBarWidth = 1f;
public BarBuffer(int size, float groupspace, int dataSetCount, boolean containsStacks) {
public BarBuffer(int size, int dataSetCount, boolean containsStacks) {
super(size);
this.mGroupSpace = groupspace;
this.mDataSetCount = dataSetCount;
this.mContainsStacks = containsStacks;
}

View file

@ -6,8 +6,8 @@ import com.github.mikephil.charting.interfaces.datasets.IBarDataSet;
public class HorizontalBarBuffer extends BarBuffer {
public HorizontalBarBuffer(int size, float groupspace, int dataSetCount, boolean containsStacks) {
super(size, groupspace, dataSetCount, containsStacks);
public HorizontalBarBuffer(int size, int dataSetCount, boolean containsStacks) {
super(size, dataSetCount, containsStacks);
}
@Override

View file

@ -198,4 +198,23 @@ public class BarChart extends BarLineChartBase<BarData> implements BarDataProvid
public void setFitBars(boolean enabled) {
mFitBars = enabled;
}
/**
* Groups all BarDataSet objects this data object holds together by modifying the x-position of their entries.
* Leaves space as specified by the parameters.
* Calls notifyDataSetChanged() afterwards.
*
* @param fromX the starting point on the x-axis where the grouping should begin
* @param groupSpace the space between groups of bars in values (not pixels) e.g. 0.8f for bar width 1f
* @param barSpace the space between individual bars in values (not pixels) e.g. 0.1f for bar width 1f
*/
public void groupBars(float fromX, float groupSpace, float barSpace) {
if (getBarData() == null) {
throw new RuntimeException("You need to set data for the chart before grouping bars.");
} else {
getBarData().groupBars(fromX, groupSpace, barSpace);
notifyDataSetChanged();
}
}
}

View file

@ -3,8 +3,6 @@ package com.github.mikephil.charting.data;
import com.github.mikephil.charting.interfaces.datasets.IBarDataSet;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
/**
@ -19,11 +17,6 @@ public class BarData extends BarLineScatterCandleBubbleData<IBarDataSet> {
*/
private float mBarWidth = 1f;
// /**
// * The maximum space (in pixels on the screen) a single bar can consume.
// */
// private float mMaximumBarWidth = 100f;
public BarData() {
super();
}
@ -36,17 +29,6 @@ public class BarData extends BarLineScatterCandleBubbleData<IBarDataSet> {
super(dataSets);
}
/**
* Returns the space that is left out between groups of bars. Always returns
* 0 if the BarData object only contains one DataSet (because for one
* DataSet, there is no group-space needed).
*
* @return
*/
public float getGroupSpace() {
return 0f;
}
/**
* Sets the width each bar should have on the x-axis (in values, not pixels).
* Default 1f
@ -61,20 +43,12 @@ public class BarData extends BarLineScatterCandleBubbleData<IBarDataSet> {
return mBarWidth;
}
// /**
// * Returns true if this BarData object contains grouped DataSets (more than
// * 1 DataSet).
// *
// * @return
// */
// public boolean isGrouped() {
// return mDataSets.size() > 1 ? true : false;
// }
/**
* Groups all BarDataSet objects this data object holds together. Leaves space as specified by the parameters.
* Groups all BarDataSet objects this data object holds together by modifying the x-position of their entries.
* Leaves space as specified by the parameters.
* Do not forget to call notifyDataSetChanged() on your BarChart object after calling this method.
*
* @param fromX
* @param fromX the starting point on the x-axis where the grouping should begin
* @param groupSpace the space between groups of bars in values (not pixels) e.g. 0.8f for bar width 1f
* @param barSpace the space between individual bars in values (not pixels) e.g. 0.1f for bar width 1f
*/
@ -92,7 +66,7 @@ public class BarData extends BarLineScatterCandleBubbleData<IBarDataSet> {
float barSpaceHalf = barSpace / 2f;
float barWidthHalf = mBarWidth / 2f;
float interval = getIntervalWidth(groupSpace, barSpace);
float interval = getGroupWidth(groupSpace, barSpace);
for (int i = 0; i < maxEntryCount; i++) {
@ -131,28 +105,14 @@ public class BarData extends BarLineScatterCandleBubbleData<IBarDataSet> {
notifyDataChanged();
}
public float getIntervalWidth(float groupSpace, float barSpace) {
/**
* In case of grouped bars, this method returns the space an individual group of bar needs on the x-axis.
*
* @param groupSpace
* @param barSpace
* @return
*/
public float getGroupWidth(float groupSpace, float barSpace) {
return mDataSets.size() * (mBarWidth + barSpace) + groupSpace;
}
//
// /**
// * Sets the maximum width (in density pixels) a single bar in the barchart
// * should consume.
// *
// * @param max
// */
// public void setBarWidthMaximum(float max) {
// mMaximumBarWidth = Utils.convertDpToPixel(max);
// }
//
// /**
// * Returns the maximum width (in density pixels) a single bar in the
// * barchart should consume.
// *
// * @return
// */
// public float getBarWidthMaximum() {
// return mMaximumBarWidth;
// }
}

View file

@ -62,7 +62,6 @@ public class BarChartRenderer extends DataRenderer {
for (int i = 0; i < mBarBuffers.length; i++) {
IBarDataSet set = barData.getDataSetByIndex(i);
mBarBuffers[i] = new BarBuffer(set.getEntryCount() * 4 * (set.isStacked() ? set.getStackSize() : 1),
barData.getGroupSpace(),
barData.getDataSetCount(), set.isStacked());
}
}

View file

@ -41,7 +41,6 @@ public class HorizontalBarChartRenderer extends BarChartRenderer {
for (int i = 0; i < mBarBuffers.length; i++) {
IBarDataSet set = barData.getDataSetByIndex(i);
mBarBuffers[i] = new HorizontalBarBuffer(set.getEntryCount() * 4 * (set.isStacked() ? set.getStackSize() : 1),
barData.getGroupSpace(),
barData.getDataSetCount(), set.isStacked());
}
}

View file

@ -209,41 +209,6 @@ public class Transformer {
return valuePoints;
}
/**
* Transforms an List of Entry into a float array containing the x and
* y values transformed with all matrices for the BARCHART.
*
* @param data
* @param dataSet the dataset index
* @return
*/
public float[] generateTransformedValuesHorizontalBarChart(IBarDataSet data,
int dataSet, BarData bd, float phaseY) {
float[] valuePoints = new float[data.getEntryCount() * 2];
int setCount = bd.getDataSetCount();
float space = bd.getGroupSpace();
for (int j = 0; j < valuePoints.length; j += 2) {
Entry e = data.getEntryForIndex(j / 2);
float i = e.getX();
// calculate the x-position, depending on datasetcount
float x = i + i * (setCount - 1) + dataSet + space * i
+ space / 2f;
float y = e.getY();
valuePoints[j] = y * phaseY;
valuePoints[j + 1] = x;
}
getValueToPixelMatrix().mapPoints(valuePoints);
return valuePoints;
}
/**
* transform a path with all the given matrices VERY IMPORTANT: keep order
* to value-touch-offset