diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/data/CandleData.java b/MPChartLib/src/main/java/com/github/mikephil/charting/data/CandleData.java index 1e90db2ba5..73728c91a7 100644 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/data/CandleData.java +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/data/CandleData.java @@ -2,20 +2,19 @@ import com.github.mikephil.charting.interfaces.datasets.ICandleDataSet; -import java.util.ArrayList; import java.util.List; public class CandleData extends BarLineScatterCandleBubbleData { - public CandleData() { - super(); - } + public CandleData() { + super(); + } - public CandleData(List dataSets) { - super(dataSets); - } + public CandleData(List dataSets) { + super(dataSets); + } - public CandleData(ICandleDataSet... dataSets) { - super(dataSets); - } + public CandleData(ICandleDataSet... dataSets) { + super(dataSets); + } } diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/data/CandleDataSet.kt b/MPChartLib/src/main/java/com/github/mikephil/charting/data/CandleDataSet.kt index 3fee5fce56..b6615c57fc 100644 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/data/CandleDataSet.kt +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/data/CandleDataSet.kt @@ -114,121 +114,84 @@ open class CandleDataSet(yVals: MutableList?, label: String = "") : } } - /** - * Sets the space that is left out on the left and right side of each - * candle, default 0.1f (10%), max 0.45f, min 0f - */ - fun setBarSpace(space: Float) { - var space = space - if (space < 0f) space = 0f - if (space > 0.45f) space = 0.45f - - mBarSpace = space - } - - override fun getBarSpace(): Float { - return mBarSpace - } - - /** - * Sets the width of the candle-shadow-line in pixels. Default 3f. - */ - fun setShadowWidth(width: Float) { - mShadowWidth = width.convertDpToPixel() - } - - override fun getShadowWidth(): Float { - return mShadowWidth - } - - /** - * Sets whether the candle bars should show? - */ - fun setShowCandleBar(showCandleBar: Boolean) { - mShowCandleBar = showCandleBar - } - - override fun getShowCandleBar(): Boolean { - return mShowCandleBar - } - - /** - * Sets the one and ONLY color that should be used for this DataSet when - * open == close. - */ - fun setNeutralColor(color: Int) { - mNeutralColor = color - } - - override fun getNeutralColor(): Int { - return mNeutralColor - } - - /** - * Sets the one and ONLY color that should be used for this DataSet when - * open <= close. - */ - fun setIncreasingColor(color: Int) { - mIncreasingColor = color - } - - override fun getIncreasingColor(): Int { - return mIncreasingColor - } - - /** - * Sets the one and ONLY color that should be used for this DataSet when - * open > close. - */ - fun setDecreasingColor(color: Int) { - mDecreasingColor = color - } - - override fun getDecreasingColor(): Int { - return mDecreasingColor - } - - override fun getIncreasingPaintStyle(): Paint.Style? { - return mIncreasingPaintStyle - } - - /** - * Sets paint style when open < close - */ - fun setIncreasingPaintStyle(paintStyle: Paint.Style?) { - this.mIncreasingPaintStyle = paintStyle - } - - override fun getDecreasingPaintStyle(): Paint.Style? { - return mDecreasingPaintStyle - } - - /** - * Sets paint style when open > close - */ - fun setDecreasingPaintStyle(decreasingPaintStyle: Paint.Style?) { - this.mDecreasingPaintStyle = decreasingPaintStyle - } - - override fun getShadowColor(): Int { - return mShadowColor - } - - /** - * Sets shadow color for all entries - */ - fun setShadowColor(shadowColor: Int) { - this.mShadowColor = shadowColor - } - - override fun getShadowColorSameAsCandle(): Boolean { - return mShadowColorSameAsCandle - } + override var barSpace: Float + get() = mBarSpace + set(value) { + /** + * Sets the space that is left out on the left and right side of each + * candle, default 0.1f (10%), max 0.45f, min 0f + */ + var space = value + if (space < 0f) space = 0f + if (space > 0.45f) space = 0.45f + + mBarSpace = space + } - /** - * Sets shadow color to be the same color as the candle color - */ - fun setShadowColorSameAsCandle(shadowColorSameAsCandle: Boolean) { - this.mShadowColorSameAsCandle = shadowColorSameAsCandle - } + override var showCandleBar: Boolean + get() = mShowCandleBar + set(value) { + mShowCandleBar = value + } + override var shadowWidth: Float + get() = mShadowWidth + set(value) { + mShadowWidth = value.convertDpToPixel() + } + override var shadowColor: Int + get() = mShadowColor + set(value) { + mShadowColor = value + } + override var neutralColor: Int + get() = mNeutralColor + set(value) { + /** + * Sets the one and ONLY color that should be used for this DataSet when + * open == close. + */ + mNeutralColor = value + } + override var increasingColor: Int + get() = mIncreasingColor + set(value) { + /** + * Sets the one and ONLY color that should be used for this DataSet when + * open <= close. + */ + mIncreasingColor = value + } + override var decreasingColor: Int + get() = mDecreasingColor + set(value) { + /** + * Sets the one and ONLY color that should be used for this DataSet when + * open > close. + */ + mDecreasingColor = value + } + override var increasingPaintStyle: Paint.Style? + get() = mIncreasingPaintStyle + set(value) { + /** + * Sets paint style when open < close + */ + mIncreasingPaintStyle = value + } + override var decreasingPaintStyle: Paint.Style? + get() = mDecreasingPaintStyle + set(value) { + /** + * Sets paint style when open > close + */ + mDecreasingPaintStyle = value + } + override var shadowColorSameAsCandle: Boolean + get() = mShadowColorSameAsCandle + set(value) { + /** + * Sets shadow color to be the same color as the candle color + */ + mShadowColorSameAsCandle = value + } } diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/interfaces/datasets/ICandleDataSet.java b/MPChartLib/src/main/java/com/github/mikephil/charting/interfaces/datasets/ICandleDataSet.kt similarity index 55% rename from MPChartLib/src/main/java/com/github/mikephil/charting/interfaces/datasets/ICandleDataSet.java rename to MPChartLib/src/main/java/com/github/mikephil/charting/interfaces/datasets/ICandleDataSet.kt index d220895f77..e6304556f2 100644 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/interfaces/datasets/ICandleDataSet.java +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/interfaces/datasets/ICandleDataSet.kt @@ -1,64 +1,58 @@ -package com.github.mikephil.charting.interfaces.datasets; +package com.github.mikephil.charting.interfaces.datasets -import android.graphics.Paint; - -import com.github.mikephil.charting.data.CandleEntry; - -/** - * Created by philipp on 21/10/15. - */ -public interface ICandleDataSet extends ILineScatterCandleRadarDataSet { +import android.graphics.Paint +import com.github.mikephil.charting.data.CandleEntry +interface ICandleDataSet : ILineScatterCandleRadarDataSet { /** - * Returns the space that is left out on the left and right side of each - * candle. + * Returns the space that is left out on the left and right side of each candle. */ - float getBarSpace(); + val barSpace: Float /** * Returns whether the candle bars should show? * When false, only "ticks" will show * - default: true */ - boolean getShowCandleBar(); + val showCandleBar: Boolean /** * Returns the width of the candle-shadow-line in pixels. */ - float getShadowWidth(); + val shadowWidth: Float /** * Returns shadow color for all entries */ - int getShadowColor(); + val shadowColor: Int /** * Returns the neutral color (for open == close) */ - int getNeutralColor(); + val neutralColor: Int /** * Returns the increasing color (for open < close). */ - int getIncreasingColor(); + val increasingColor: Int /** * Returns the decreasing color (for open > close). */ - int getDecreasingColor(); + val decreasingColor: Int /** * Returns paint style when open < close */ - Paint.Style getIncreasingPaintStyle(); + val increasingPaintStyle: Paint.Style? /** * Returns paint style when open > close */ - Paint.Style getDecreasingPaintStyle(); + val decreasingPaintStyle: Paint.Style? /** * Is the shadow color same as the candle color? */ - boolean getShadowColorSameAsCandle(); + val shadowColorSameAsCandle: Boolean } diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/LegendRenderer.kt b/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/LegendRenderer.kt index cbfbeceea0..2799b6c966 100644 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/LegendRenderer.kt +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/LegendRenderer.kt @@ -54,34 +54,33 @@ open class LegendRenderer( val clrs = dataSet.colors val entryCount = dataSet.entryCount // if we have a barchart with stacked bars - if (dataSet is IBarDataSet && dataSet.isStacked) { - val sLabels = dataSet.stackLabels - - val minEntries = min(clrs.size.toDouble(), dataSet.stackSize.toDouble()).toInt() - - for (j in 0.. { + val sLabels = dataSet.stackLabels - computedEntries.add( - LegendEntry( - label, - dataSet.form, - dataSet.formSize, - dataSet.formLineWidth, - dataSet.formLineDashEffect, - clrs[j] + val minEntries = min(clrs.size.toDouble(), dataSet.stackSize.toDouble()).toInt() + + for (j in 0.. - computedEntries.add( - LegendEntry( - pieEntry.label, - dataSet.form, - dataSet.formSize, - dataSet.formLineWidth, - dataSet.formLineDashEffect, - clrs[j] + is IPieDataSet -> { + + var j = 0 + while (j < clrs.size && j < entryCount) { + dataSet.getEntryForIndex(j)?.let { pieEntry -> + computedEntries.add( + LegendEntry( + pieEntry.label, + dataSet.form, + dataSet.formSize, + dataSet.formLineWidth, + dataSet.formLineDashEffect, + clrs[j] + ) ) - ) + } + j++ } - j++ - } - if (dataSet.label != null) { - // add the legend description label computedEntries.add( LegendEntry( dataSet.label, @@ -125,62 +123,64 @@ open class LegendRenderer( ) ) } - } else if (dataSet is ICandleDataSet && dataSet.decreasingColor != - ColorTemplate.COLOR_NONE - ) { - val decreasingColor = dataSet.decreasingColor - val increasingColor = dataSet.increasingColor - - computedEntries.add( - LegendEntry( - null, - dataSet.form, - dataSet.formSize, - dataSet.formLineWidth, - dataSet.formLineDashEffect, - decreasingColor - ) - ) - - computedEntries.add( - LegendEntry( - dataSet.label, - dataSet.form, - dataSet.formSize, - dataSet.formLineWidth, - dataSet.formLineDashEffect, - increasingColor + + is ICandleDataSet if dataSet.decreasingColor != + ColorTemplate.COLOR_NONE + -> { + val decreasingColor = dataSet.decreasingColor + val increasingColor = dataSet.increasingColor + + computedEntries.add( + LegendEntry( + null, + dataSet.form, + dataSet.formSize, + dataSet.formLineWidth, + dataSet.formLineDashEffect, + decreasingColor + ) ) - ) - } else { // all others - - var j = 0 - while (j < clrs.size && j < entryCount) { - // if multiple colors are set for a DataSet, group them - val label = if (j < clrs.size - 1 && j < entryCount - 1) { - null - } else { // add label to the last entry - data.getDataSetByIndex(i).label - } computedEntries.add( LegendEntry( - label, + dataSet.label, dataSet.form, dataSet.formSize, dataSet.formLineWidth, dataSet.formLineDashEffect, - clrs[j] + increasingColor ) ) - j++ + } + + else -> { // all others + + var j = 0 + while (j < clrs.size && j < entryCount) { + // if multiple colors are set for a DataSet, group them + val label = if (j < clrs.size - 1 && j < entryCount - 1) { + null + } else { // add label to the last entry + data.getDataSetByIndex(i).label + } + + computedEntries.add( + LegendEntry( + label, + dataSet.form, + dataSet.formSize, + dataSet.formLineWidth, + dataSet.formLineDashEffect, + clrs[j] + ) + ) + j++ + } } } } - if (legend.extraEntries != null) { - Collections.addAll(computedEntries, *legend.extraEntries) - } + Collections.addAll(computedEntries, *legend.extraEntries) legend.setEntries(computedEntries) } @@ -228,7 +228,7 @@ open class LegendRenderer( val yOffset = legend.yOffset val xOffset = legend.xOffset - var originPosX: Float = 0f + var originPosX = 0f when (horizontalAlignment) { LegendHorizontalAlignment.LEFT -> { @@ -283,7 +283,7 @@ open class LegendRenderer( LegendVerticalAlignment.BOTTOM -> viewPortHandler.chartHeight - yOffset - legend.mNeededHeight LegendVerticalAlignment.CENTER -> (viewPortHandler.chartHeight - legend.mNeededHeight) / 2f + yOffset else -> { - Log.w("Chart", "Legend verticalAlignment not set"); + Log.w("Chart", "Legend verticalAlignment not set") 0f } } @@ -344,7 +344,7 @@ open class LegendRenderer( // contains the stacked legend size in pixels var stack = 0f var wasStacked = false - var posY: Float = 0f + var posY = 0f when (verticalAlignment) { LegendVerticalAlignment.TOP -> { @@ -367,7 +367,7 @@ open class LegendRenderer( - legend.mNeededHeight / 2f + legend.yOffset) - null -> Log.w("Chart", "Legend verticalAlignment is null"); + null -> Log.w("Chart", "Legend verticalAlignment is null") } var i = 0 diff --git a/app/src/main/kotlin/info/appdev/chartexample/CombinedChartActivity.kt b/app/src/main/kotlin/info/appdev/chartexample/CombinedChartActivity.kt index 7213690ff1..7871e499fe 100644 --- a/app/src/main/kotlin/info/appdev/chartexample/CombinedChartActivity.kt +++ b/app/src/main/kotlin/info/appdev/chartexample/CombinedChartActivity.kt @@ -193,7 +193,7 @@ class CombinedChartActivity : DemoBase() { val set = CandleDataSet(entries, "Candle DataSet") set.decreasingColor = Color.rgb(142, 150, 175) set.shadowColor = Color.DKGRAY - set.setBarSpace(0.3f) + set.barSpace = 0.3f set.valueTextSize = 10f set.isDrawValues = false d.addDataSet(set)