diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/data/BarDataSet.java b/MPChartLib/src/main/java/com/github/mikephil/charting/data/BarDataSet.java deleted file mode 100644 index b8df00e865..0000000000 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/data/BarDataSet.java +++ /dev/null @@ -1,273 +0,0 @@ - -package com.github.mikephil.charting.data; - -import android.graphics.Color; - -import com.github.mikephil.charting.interfaces.datasets.IBarDataSet; -import com.github.mikephil.charting.utils.Fill; - -import java.util.ArrayList; -import java.util.List; - -public class BarDataSet extends BarLineScatterCandleBubbleDataSet implements IBarDataSet { - - /** - * the maximum number of bars that are stacked upon each other, this value - * is calculated from the Entries that are added to the DataSet - */ - private int mStackSize = 1; - - /** - * the color used for drawing the bar shadows - */ - private int mBarShadowColor = Color.rgb(215, 215, 215); - - private float mBarBorderWidth = 0.0f; - - private int mBarBorderColor = Color.BLACK; - - /** - * the alpha value used to draw the highlight indicator bar - */ - private int mHighLightAlpha = 120; - - /** - * the overall entry count, including counting each stack-value individually - */ - private int mEntryCountStacks = 0; - - /** - * array of labels used to describe the different values of the stacked bars - */ - private String[] mStackLabels = new String[]{}; - - protected List mFills = null; - - public BarDataSet(List yVals, String label) { - super(yVals, label); - - mHighLightColor = Color.rgb(0, 0, 0); - - calcStackSize(yVals); - calcEntryCountIncludingStacks(yVals); - } - - @Override - public DataSet copy() { - List entries = new ArrayList<>(); - for (int i = 0; i < mEntries.size(); i++) { - entries.add(mEntries.get(i).copy()); - } - BarDataSet copied = new BarDataSet(entries, getLabel()); - copy(copied); - return copied; - } - - protected void copy(BarDataSet barDataSet) { - super.copy((BaseDataSet) barDataSet); - barDataSet.mStackSize = mStackSize; - barDataSet.mBarShadowColor = mBarShadowColor; - barDataSet.mBarBorderWidth = mBarBorderWidth; - barDataSet.mStackLabels = mStackLabels; - barDataSet.mHighLightAlpha = mHighLightAlpha; - } - - @Override - public List getFills() { - return mFills; - } - - @Override - public Fill getFill(int index) { - return mFills.get(index % mFills.size()); - } - - /** - * This method is deprecated. - * Use getFills() instead. - */ - @Deprecated - public List getGradients() { - return mFills; - } - - /** - * This method is deprecated. - * Use getFill(...) instead. - */ - @Deprecated - public Fill getGradient(int index) { - return getFill(index); - } - - /** - * Sets the start and end color for gradient color, ONLY color that should be used for this DataSet. - */ - public void setGradientColor(int startColor, int endColor) { - mFills.clear(); - mFills.add(new Fill(startColor, endColor)); - } - - /** - * This method is deprecated. - * Use setFills(...) instead. - */ - @Deprecated - public void setGradientColors(List gradientColors) { - this.mFills = gradientColors; - } - - /** - * Sets the fills for the bars in this dataset. - */ - public void setFills(List fills) { - this.mFills = fills; - } - - /** - * Calculates the total number of entries this DataSet represents, including - * stacks. All values belonging to a stack are calculated separately. - */ - private void calcEntryCountIncludingStacks(List yVals) { - - mEntryCountStacks = 0; - - for (int i = 0; i < yVals.size(); i++) { - - float[] vals = yVals.get(i).getYVals(); - - if (vals == null) - mEntryCountStacks++; - else - mEntryCountStacks += vals.length; - } - } - - /** - * calculates the maximum stacksize that occurs in the Entries array of this - * DataSet - */ - private void calcStackSize(List yVals) { - - for (int i = 0; i < yVals.size(); i++) { - - float[] vals = yVals.get(i).getYVals(); - - if (vals != null && vals.length > mStackSize) - mStackSize = vals.length; - } - } - - @Override - protected void calcMinMax(BarEntry e) { - - if (e != null && !Float.isNaN(e.getY())) { - - if (e.getYVals() == null) { - - if (e.getY() < mYMin) - mYMin = e.getY(); - - if (e.getY() > mYMax) - mYMax = e.getY(); - } else { - - if (-e.getNegativeSum() < mYMin) - mYMin = -e.getNegativeSum(); - - if (e.getPositiveSum() > mYMax) - mYMax = e.getPositiveSum(); - } - - calcMinMaxX(e); - } - } - - @Override - public int getStackSize() { - return mStackSize; - } - - @Override - public boolean isStacked() { - return mStackSize > 1 ? true : false; - } - - /** - * returns the overall entry count, including counting each stack-value individually - */ - public int getEntryCountStacks() { - return mEntryCountStacks; - } - - /** - * Sets the color used for drawing the bar-shadows. The bar shadows is a - * surface behind the bar that indicates the maximum value. Don't for get to - * use getResources().getColor(...) to set this. Or Color.rgb(...). - */ - public void setBarShadowColor(int color) { - mBarShadowColor = color; - } - - @Override - public int getBarShadowColor() { - return mBarShadowColor; - } - - /** - * Sets the width used for drawing borders around the bars. - * If borderWidth == 0, no border will be drawn. - */ - public void setBarBorderWidth(float width) { - mBarBorderWidth = width; - } - - /** - * Returns the width used for drawing borders around the bars. - * If borderWidth == 0, no border will be drawn. - */ - @Override - public float getBarBorderWidth() { - return mBarBorderWidth; - } - - /** - * Sets the color drawing borders around the bars. - */ - public void setBarBorderColor(int color) { - mBarBorderColor = color; - } - - /** - * Returns the color drawing borders around the bars. - */ - @Override - public int getBarBorderColor() { - return mBarBorderColor; - } - - /** - * Set the alpha value (transparency) that is used for drawing the highlight - * indicator bar. min = 0 (fully transparent), max = 255 (fully opaque) - */ - public void setHighLightAlpha(int alpha) { - mHighLightAlpha = alpha; - } - - @Override - public int getHighLightAlpha() { - return mHighLightAlpha; - } - - /** - * Sets labels for different values of bar-stacks, in case there are one. - */ - public void setStackLabels(String[] labels) { - mStackLabels = labels; - } - - @Override - public String[] getStackLabels() { - return mStackLabels; - } -} diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/data/BarDataSet.kt b/MPChartLib/src/main/java/com/github/mikephil/charting/data/BarDataSet.kt new file mode 100644 index 0000000000..c164fcde37 --- /dev/null +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/data/BarDataSet.kt @@ -0,0 +1,244 @@ +package com.github.mikephil.charting.data + +import android.graphics.Color +import com.github.mikephil.charting.interfaces.datasets.IBarDataSet +import com.github.mikephil.charting.utils.Fill +import java.lang.Float +import kotlin.Array +import kotlin.Boolean +import kotlin.Deprecated +import kotlin.Int +import kotlin.String +import kotlin.arrayOf + +open class BarDataSet(yVals: MutableList, label: String?) : BarLineScatterCandleBubbleDataSet(yVals, label), IBarDataSet { + /** + * the maximum number of bars that are stacked upon each other, this value + * is calculated from the Entries that are added to the DataSet + */ + private var mStackSize = 1 + + /** + * the color used for drawing the bar shadows + */ + private var mBarShadowColor = Color.rgb(215, 215, 215) + + private var mBarBorderWidth = 0.0f + + private var mBarBorderColor = Color.BLACK + + /** + * the alpha value used to draw the highlight indicator bar + */ + private var mHighLightAlpha = 120 + + /** + * returns the overall entry count, including counting each stack-value individually + */ + /** + * the overall entry count, including counting each stack-value individually + */ + var entryCountStacks: Int = 0 + private set + + /** + * array of labels used to describe the different values of the stacked bars + */ + private var mStackLabels: Array? = arrayOf() + + /** + * This method is deprecated. + * Use getFills() instead. + */ + @get:Deprecated("") + var gradients: MutableList? = null + protected set + + init { + mHighLightColor = Color.rgb(0, 0, 0) + + calcStackSize(yVals) + calcEntryCountIncludingStacks(yVals) + } + + override fun copy(): DataSet { + val entries: MutableList = ArrayList() + for (i in mEntries.indices) { + entries.add(mEntries[i]!!.copy()) + } + val copied = BarDataSet(entries, label) + copy(copied) + return copied + } + + protected fun copy(barDataSet: BarDataSet) { + super.copy((barDataSet as BaseDataSet<*>?)!!) + barDataSet.mStackSize = mStackSize + barDataSet.mBarShadowColor = mBarShadowColor + barDataSet.mBarBorderWidth = mBarBorderWidth + barDataSet.mStackLabels = mStackLabels + barDataSet.mHighLightAlpha = mHighLightAlpha + } + + override fun getFills(): MutableList? { + return this.gradients + } + + override fun getFill(index: Int): Fill? { + return gradients!!.get(index % gradients!!.size) + } + + /** + * This method is deprecated. + * Use getFill(...) instead. + */ + @Deprecated("") + fun getGradient(index: Int): Fill? { + return getFill(index) + } + + /** + * Sets the start and end color for gradient color, ONLY color that should be used for this DataSet. + */ + fun setGradientColor(startColor: Int, endColor: Int) { + gradients!!.clear() + gradients!!.add(Fill(startColor, endColor)) + } + + /** + * This method is deprecated. + * Use setFills(...) instead. + */ + @Deprecated("") + fun setGradientColors(gradientColors: MutableList?) { + this.gradients = gradientColors + } + + /** + * Sets the fills for the bars in this dataset. + */ + fun setFills(fills: MutableList?) { + this.gradients = fills + } + + /** + * Calculates the total number of entries this DataSet represents, including + * stacks. All values belonging to a stack are calculated separately. + */ + private fun calcEntryCountIncludingStacks(yVals: MutableList) { + this.entryCountStacks = 0 + + for (i in yVals.indices) { + val vals = yVals.get(i)!!.yVals + + if (vals == null) this.entryCountStacks++ + else this.entryCountStacks += vals.size + } + } + + /** + * calculates the maximum stacksize that occurs in the Entries array of this + * DataSet + */ + private fun calcStackSize(yVals: MutableList) { + for (i in yVals.indices) { + val vals = yVals[i]?.yVals + + if (vals != null && vals.size > mStackSize) mStackSize = vals.size + } + } + + override fun calcMinMax(e: BarEntry?) { + if (e != null && !Float.isNaN(e.y)) { + if (e.yVals == null) { + if (e.y < mYMin) mYMin = e.y + + if (e.y > mYMax) mYMax = e.y + } else { + if (-e.negativeSum < mYMin) mYMin = -e.negativeSum + + if (e.positiveSum > mYMax) mYMax = e.positiveSum + } + + calcMinMaxX(e) + } + } + + override fun getStackSize(): Int { + return mStackSize + } + + override fun isStacked(): Boolean { + return mStackSize > 1 + } + + /** + * Sets the color used for drawing the bar-shadows. The bar shadows is a + * surface behind the bar that indicates the maximum value. Don't for get to + * use getResources().getColor(...) to set this. Or Color.rgb(...). + */ + fun setBarShadowColor(color: Int) { + mBarShadowColor = color + } + + override fun getBarShadowColor(): Int { + return mBarShadowColor + } + + /** + * Sets the width used for drawing borders around the bars. + * If borderWidth == 0, no border will be drawn. + */ + fun setBarBorderWidth(width: kotlin.Float) { + mBarBorderWidth = width + } + + /** + * Returns the width used for drawing borders around the bars. + * If borderWidth == 0, no border will be drawn. + */ + override fun getBarBorderWidth(): kotlin.Float { + return mBarBorderWidth + } + + /** + * Sets the color drawing borders around the bars. + */ + fun setBarBorderColor(color: Int) { + mBarBorderColor = color + } + + /** + * Returns the color drawing borders around the bars. + */ + override fun getBarBorderColor(): Int { + return mBarBorderColor + } + + /** + * Set the alpha value (transparency) that is used for drawing the highlight + * indicator bar. min = 0 (fully transparent), max = 255 (fully opaque) + */ + fun setHighLightAlpha(alpha: Int) { + mHighLightAlpha = alpha + } + + override fun getHighLightAlpha(): Int { + return mHighLightAlpha + } + + /** + * Sets labels for different values of bar-stacks, in case there are one. + */ + fun setStackLabels(labels: Array?) { + mStackLabels = labels + } + + override fun getStackLabels(): Array? { + return mStackLabels + } + + override fun getEntryIndex(entry: BarEntry?): Int { + return this.getEntryIndex(entry) + } +} diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/data/BarLineScatterCandleBubbleDataSet.java b/MPChartLib/src/main/java/com/github/mikephil/charting/data/BarLineScatterCandleBubbleDataSet.java index fc8cc36e06..f4422e8c4a 100644 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/data/BarLineScatterCandleBubbleDataSet.java +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/data/BarLineScatterCandleBubbleDataSet.java @@ -29,8 +29,6 @@ public BarLineScatterCandleBubbleDataSet(List yVals, String label) { * Sets the color that is used for drawing the highlight indicators. Dont * forget to resolve the color using getResources().getColor(...) or * Color.rgb(...). - * - * @param color */ public void setHighLightColor(int color) { mHighLightColor = color; diff --git a/MPChartLib/src/test/kotlin/com/github/mikephil/charting/test/BarDataTest.kt b/MPChartLib/src/test/kotlin/com/github/mikephil/charting/test/BarDataTest.kt index 67d2194dbc..868c8d03f9 100644 --- a/MPChartLib/src/test/kotlin/com/github/mikephil/charting/test/BarDataTest.kt +++ b/MPChartLib/src/test/kotlin/com/github/mikephil/charting/test/BarDataTest.kt @@ -12,8 +12,8 @@ class BarDataTest { val groupSpace = 5f val barSpace = 1f - val values1: MutableList = ArrayList() - val values2: MutableList = ArrayList() + val values1: MutableList = ArrayList() + val values2: MutableList = ArrayList() for (i in 0..4) { values1.add(BarEntry(i.toFloat(), 50f)) @@ -29,23 +29,23 @@ class BarDataTest { var groupWidth = data.getGroupWidth(groupSpace, barSpace) Assert.assertEquals(27f, groupWidth, 0.01f) - Assert.assertEquals(0f, values1[0].x, 0.01f) - Assert.assertEquals(1f, values1[1].x, 0.01f) + Assert.assertEquals(0f, values1[0]!!.x, 0.01f) + Assert.assertEquals(1f, values1[1]!!.x, 0.01f) data.groupBars(1000f, groupSpace, barSpace) // 1000 + 2.5 + 0.5 + 5 - Assert.assertEquals(1008f, values1[0].x, 0.01f) - Assert.assertEquals(1019f, values2[0].x, 0.01f) - Assert.assertEquals(1035f, values1[1].x, 0.01f) - Assert.assertEquals(1046f, values2[1].x, 0.01f) + Assert.assertEquals(1008f, values1[0]!!.x, 0.01f) + Assert.assertEquals(1019f, values2[0]!!.x, 0.01f) + Assert.assertEquals(1035f, values1[1]!!.x, 0.01f) + Assert.assertEquals(1046f, values2[1]!!.x, 0.01f) data.groupBars(-1000f, groupSpace, barSpace) - Assert.assertEquals(-992f, values1[0].x, 0.01f) - Assert.assertEquals(-981f, values2[0].x, 0.01f) - Assert.assertEquals(-965f, values1[1].x, 0.01f) - Assert.assertEquals(-954f, values2[1].x, 0.01f) + Assert.assertEquals(-992f, values1[0]!!.x, 0.01f) + Assert.assertEquals(-981f, values2[0]!!.x, 0.01f) + Assert.assertEquals(-965f, values1[1]!!.x, 0.01f) + Assert.assertEquals(-954f, values2[1]!!.x, 0.01f) data.barWidth = 20f groupWidth = data.getGroupWidth(groupSpace, barSpace) @@ -54,9 +54,9 @@ class BarDataTest { data.barWidth = 10f data.groupBars(-20f, groupSpace, barSpace) - Assert.assertEquals(-12f, values1[0].x, 0.01f) - Assert.assertEquals(-1f, values2[0].x, 0.01f) - Assert.assertEquals(15f, values1[1].x, 0.01f) - Assert.assertEquals(26f, values2[1].x, 0.01f) + Assert.assertEquals(-12f, values1[0]!!.x, 0.01f) + Assert.assertEquals(-1f, values2[0]!!.x, 0.01f) + Assert.assertEquals(15f, values1[1]!!.x, 0.01f) + Assert.assertEquals(26f, values2[1]!!.x, 0.01f) } } diff --git a/app/src/main/kotlin/info/appdev/chartexample/compose/HorizontalBarComposeActivity.kt b/app/src/main/kotlin/info/appdev/chartexample/compose/HorizontalBarComposeActivity.kt index bde83693fd..ca6940aaf5 100644 --- a/app/src/main/kotlin/info/appdev/chartexample/compose/HorizontalBarComposeActivity.kt +++ b/app/src/main/kotlin/info/appdev/chartexample/compose/HorizontalBarComposeActivity.kt @@ -335,7 +335,7 @@ class HorizontalBarComposeActivity : DemoBaseCompose() { val barWidth = 9f val spaceForBar = 10f - val values = ArrayList() + val values = ArrayList() val sampleValues = getValues(100) for (i in 0..