diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/charts/HorizontalBarChart.java b/MPChartLib/src/main/java/com/github/mikephil/charting/charts/HorizontalBarChart.java index cc1ac21f7..93c78295d 100644 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/charts/HorizontalBarChart.java +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/charts/HorizontalBarChart.java @@ -224,11 +224,6 @@ public void getBarBounds(BarEntry barEntry, RectF outputRect) { RectF bounds = outputRect; IBarDataSet set = mData.getDataSetForEntry(barEntry); - if (set == null) { - outputRect.set(Float.MIN_VALUE, Float.MIN_VALUE, Float.MIN_VALUE, Float.MIN_VALUE); - return; - } - float y = barEntry.getY(); float x = barEntry.getX(); diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/charts/PieRadarChartBase.java b/MPChartLib/src/main/java/com/github/mikephil/charting/charts/PieRadarChartBase.java deleted file mode 100644 index a7d09f181..000000000 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/charts/PieRadarChartBase.java +++ /dev/null @@ -1,456 +0,0 @@ - -package com.github.mikephil.charting.charts; - -import android.animation.ObjectAnimator; -import android.animation.ValueAnimator; -import android.animation.ValueAnimator.AnimatorUpdateListener; -import android.annotation.SuppressLint; -import android.content.Context; -import android.graphics.RectF; -import android.util.AttributeSet; -import android.util.Log; -import android.view.MotionEvent; - -import com.github.mikephil.charting.animation.Easing.EasingFunction; -import com.github.mikephil.charting.components.Legend; -import com.github.mikephil.charting.components.XAxis; -import com.github.mikephil.charting.data.ChartData; -import com.github.mikephil.charting.data.Entry; -import com.github.mikephil.charting.interfaces.datasets.IDataSet; -import com.github.mikephil.charting.listener.PieRadarChartTouchListener; -import com.github.mikephil.charting.utils.MPPointF; -import com.github.mikephil.charting.utils.Utils; -import com.github.mikephil.charting.utils.UtilsKtKt; - -/** - * Baseclass of PieChart and RadarChart. - * - * @author Philipp Jahoda - */ -public abstract class PieRadarChartBase>> - extends Chart { - - /** - * holds the normalized version of the current rotation angle of the chart - */ - private float mRotationAngle = 270f; - - /** - * holds the raw version of the current rotation angle of the chart - */ - private float mRawRotationAngle = 270f; - - /** - * flag that indicates if rotation is enabled or not - */ - protected boolean mRotateEnabled = true; - - /** - * Sets the minimum offset (padding) around the chart, defaults to 0.f - */ - protected float mMinOffset = 0.f; - - public PieRadarChartBase(Context context) { - super(context); - } - - public PieRadarChartBase(Context context, AttributeSet attrs) { - super(context, attrs); - } - - public PieRadarChartBase(Context context, AttributeSet attrs, int defStyle) { - super(context, attrs, defStyle); - } - - @Override - protected void init() { - super.init(); - - mChartTouchListener = new PieRadarChartTouchListener(this); - } - - @Override - protected void calcMinMax() { - //mXAxis.mAxisRange = mData.getXVals().size() - 1; - } - - @Override - public int getMaxVisibleCount() { - return mData.getEntryCount(); - } - - @Override - public boolean onTouchEvent(MotionEvent event) { - // use the pie- and radarchart listener own listener - if (mTouchEnabled && mChartTouchListener != null) - return mChartTouchListener.onTouch(this, event); - else - return super.onTouchEvent(event); - } - - @Override - public void computeScroll() { - - if (mChartTouchListener instanceof PieRadarChartTouchListener) - ((PieRadarChartTouchListener) mChartTouchListener).computeScroll(); - } - - @Override - public void notifyDataSetChanged() { - if (mData == null) - return; - - calcMinMax(); - - if (mLegend != null) - mLegendRenderer.computeLegend(mData); - - calculateOffsets(); - } - - @Override - public void calculateOffsets() { - - float legendLeft = 0f, legendRight = 0f, legendBottom = 0f, legendTop = 0f; - - if (mLegend != null && mLegend.isEnabled() && !mLegend.isDrawInsideEnabled()) { - - float fullLegendWidth = Math.min(mLegend.mNeededWidth, - mViewPortHandler.getChartWidth() * mLegend.getMaxSizePercent()); - - switch (mLegend.getOrientation()) { - case VERTICAL: { - float xLegendOffset = 0.f; - - if (mLegend.getHorizontalAlignment() == Legend.LegendHorizontalAlignment.LEFT - || mLegend.getHorizontalAlignment() == Legend.LegendHorizontalAlignment.RIGHT) { - if (mLegend.getVerticalAlignment() == Legend.LegendVerticalAlignment.CENTER) { - // this is the space between the legend and the chart - final float spacing = UtilsKtKt.convertDpToPixel(13f); - - xLegendOffset = fullLegendWidth + spacing; - - } else { - // this is the space between the legend and the chart - float spacing = UtilsKtKt.convertDpToPixel(8f); - - float legendWidth = fullLegendWidth + spacing; - float legendHeight = mLegend.mNeededHeight + mLegend.mTextHeightMax; - - MPPointF center = getCenter(); - - float bottomX = mLegend.getHorizontalAlignment() == - Legend.LegendHorizontalAlignment.RIGHT - ? getWidth() - legendWidth + 15.f - : legendWidth - 15.f; - float bottomY = legendHeight + 15.f; - float distLegend = distanceToCenter(bottomX, bottomY); - - MPPointF reference = getPosition(center, getRadius(), - getAngleForPoint(bottomX, bottomY)); - - float distReference = distanceToCenter(reference.getX(), reference.getY()); - float minOffset = UtilsKtKt.convertDpToPixel(5f); - - if (bottomY >= center.getY() && getHeight() - legendWidth > getWidth()) { - xLegendOffset = legendWidth; - } else if (distLegend < distReference) { - - float diff = distReference - distLegend; - xLegendOffset = minOffset + diff; - } - - MPPointF.recycleInstance(center); - MPPointF.recycleInstance(reference); - } - } - - switch (mLegend.getHorizontalAlignment()) { - case LEFT: - legendLeft = xLegendOffset; - break; - - case RIGHT: - legendRight = xLegendOffset; - break; - - case CENTER: - switch (mLegend.getVerticalAlignment()) { - case TOP: - legendTop = Math.min(mLegend.mNeededHeight, - mViewPortHandler.getChartHeight() * mLegend.getMaxSizePercent()); - break; - case BOTTOM: - legendBottom = Math.min(mLegend.mNeededHeight, - mViewPortHandler.getChartHeight() * mLegend.getMaxSizePercent()); - break; - } - break; - } - } - break; - - case HORIZONTAL: - float yLegendOffset; - - if (mLegend.getVerticalAlignment() == Legend.LegendVerticalAlignment.TOP || - mLegend.getVerticalAlignment() == Legend.LegendVerticalAlignment.BOTTOM) { - - // It's possible that we do not need this offset anymore as it - // is available through the extraOffsets, but changing it can mean - // changing default visibility for existing apps. - float yOffset = getRequiredLegendOffset(); - - yLegendOffset = Math.min(mLegend.mNeededHeight + yOffset, - mViewPortHandler.getChartHeight() * mLegend.getMaxSizePercent()); - - switch (mLegend.getVerticalAlignment()) { - case TOP: - legendTop = yLegendOffset; - break; - case BOTTOM: - legendBottom = yLegendOffset; - break; - } - } - break; - } - - legendLeft += getRequiredBaseOffset(); - legendRight += getRequiredBaseOffset(); - legendTop += getRequiredBaseOffset(); - legendBottom += getRequiredBaseOffset(); - } - - float minOffset = UtilsKtKt.convertDpToPixel(mMinOffset); - - if (this instanceof RadarChart) { - XAxis x = this.getXAxis(); - - if (x.isEnabled() && x.isDrawLabelsEnabled()) { - minOffset = Math.max(minOffset, x.mLabelWidth); - } - } - - legendTop += getExtraTopOffset(); - legendRight += getExtraRightOffset(); - legendBottom += getExtraBottomOffset(); - legendLeft += getExtraLeftOffset(); - - float offsetLeft = Math.max(minOffset, legendLeft); - float offsetTop = Math.max(minOffset, legendTop); - float offsetRight = Math.max(minOffset, legendRight); - float offsetBottom = Math.max(minOffset, Math.max(getRequiredBaseOffset(), legendBottom)); - - mViewPortHandler.restrainViewPort(offsetLeft, offsetTop, offsetRight, offsetBottom); - - if (mLogEnabled) - Log.i(LOG_TAG, "offsetLeft: " + offsetLeft + ", offsetTop: " + offsetTop - + ", offsetRight: " + offsetRight + ", offsetBottom: " + offsetBottom); - } - - /** - * returns the angle relative to the chart center for the given point on the - * chart in degrees. The angle is always between 0 and 360°, 0° is NORTH, - * 90° is EAST, ... - */ - public float getAngleForPoint(float x, float y) { - - MPPointF c = getCenterOffsets(); - - double tx = x - c.getX(), ty = y - c.getY(); - double length = Math.sqrt(tx * tx + ty * ty); - double r = Math.acos(ty / length); - - float angle = (float) Math.toDegrees(r); - - if (x > c.getX()) - angle = 360f - angle; - - // add 90° because chart starts EAST - angle = angle + 90f; - - // neutralize overflow - if (angle > 360f) - angle = angle - 360f; - - MPPointF.recycleInstance(c); - - return angle; - } - - /** - * Returns a recyclable MPPointF instance. - * Calculates the position around a center point, depending on the distance - * from the center, and the angle of the position around the center. - * @param angle in degrees, converted to radians internally - */ - public MPPointF getPosition(MPPointF center, float dist, float angle) { - - MPPointF p = MPPointF.Companion.getInstance(0, 0); - getPosition(center, dist, angle, p); - return p; - } - - public void getPosition(MPPointF center, float dist, float angle, MPPointF outputPoint) { - outputPoint.setX((float) (center.getX() + dist * Math.cos(Math.toRadians(angle)))); - outputPoint.setY((float) (center.getY() + dist * Math.sin(Math.toRadians(angle)))); - } - - /** - * Returns the distance of a certain point on the chart to the center of the chart. - */ - public float distanceToCenter(float x, float y) { - - MPPointF c = getCenterOffsets(); - - float dist; - - float xDist; - float yDist; - - if (x > c.getX()) { - xDist = x - c.getX(); - } else { - xDist = c.getX() - x; - } - - if (y > c.getY()) { - yDist = y - c.getY(); - } else { - yDist = c.getY() - y; - } - - // pythagoras - dist = (float) Math.sqrt(Math.pow(xDist, 2.0) + Math.pow(yDist, 2.0)); - - MPPointF.recycleInstance(c); - - return dist; - } - - /** - * Returns the xIndex for the given angle around the center of the chart. - * Returns -1 if not found / outofbounds. - */ - public abstract int getIndexForAngle(float angle); - - /** - * Set an offset for the rotation of the RadarChart in degrees. Default 270f - * --> top (NORTH) - */ - public void setRotationAngle(float angle) { - mRawRotationAngle = angle; - mRotationAngle = Utils.getNormalizedAngle(mRawRotationAngle); - } - - /** - * gets the raw version of the current rotation angle of the pie chart the - * returned value could be any value, negative or positive, outside of the - * 360 degrees. this is used when working with rotation direction, mainly by - * gestures and animations. - */ - public float getRawRotationAngle() { - return mRawRotationAngle; - } - - /** - * gets a normalized version of the current rotation angle of the pie chart, - * which will always be between 0.0 < 360.0 - */ - public float getRotationAngle() { - return mRotationAngle; - } - - /** - * Set this to true to enable the rotation / spinning of the chart by touch. - * Set it to false to disable it. Default: true - */ - public void setRotationEnabled(boolean enabled) { - mRotateEnabled = enabled; - } - - /** - * Returns true if rotation of the chart by touch is enabled, false if not. - */ - public boolean isRotationEnabled() { - return mRotateEnabled; - } - - /** - * Gets the minimum offset (padding) around the chart, defaults to 0.f - */ - public float getMinOffset() { - return mMinOffset; - } - - /** - * Sets the minimum offset (padding) around the chart, defaults to 0.f - */ - public void setMinOffset(float minOffset) { - mMinOffset = minOffset; - } - - /** - * returns the diameter of the pie- or radar-chart - */ - public float getDiameter() { - RectF content = mViewPortHandler.getContentRect(); - content.left += getExtraLeftOffset(); - content.top += getExtraTopOffset(); - content.right -= getExtraRightOffset(); - content.bottom -= getExtraBottomOffset(); - return Math.min(content.width(), content.height()); - } - - /** - * Returns the radius of the chart in pixels. - */ - public abstract float getRadius(); - - /** - * Returns the required offset for the chart legend. - */ - protected abstract float getRequiredLegendOffset(); - - /** - * Returns the base offset needed for the chart without calculating the - * legend size. - */ - protected abstract float getRequiredBaseOffset(); - - @Override - public float getYChartMax() { - // TODO Auto-generated method stub - return 0; - } - - @Override - public float getYChartMin() { - // TODO Auto-generated method stub - return 0; - } - - /** - * Applys a spin animation to the Chart. - */ - @SuppressLint("NewApi") - public void spin(int durationMillis, float fromangle, float toangle, EasingFunction easing) { - - setRotationAngle(fromangle); - - ObjectAnimator spinAnimator = ObjectAnimator.ofFloat(this, "rotationAngle", fromangle, - toangle); - spinAnimator.setDuration(durationMillis); - spinAnimator.setInterpolator(easing); - - spinAnimator.addUpdateListener(new AnimatorUpdateListener() { - - @Override - public void onAnimationUpdate(ValueAnimator animation) { - postInvalidate(); - } - }); - spinAnimator.start(); - } -} diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/charts/PieRadarChartBase.kt b/MPChartLib/src/main/java/com/github/mikephil/charting/charts/PieRadarChartBase.kt new file mode 100644 index 000000000..ed4a3a95f --- /dev/null +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/charts/PieRadarChartBase.kt @@ -0,0 +1,406 @@ +package com.github.mikephil.charting.charts + +import android.animation.ObjectAnimator +import android.annotation.SuppressLint +import android.content.Context +import android.util.AttributeSet +import android.util.Log +import android.view.MotionEvent +import com.github.mikephil.charting.animation.Easing.EasingFunction +import com.github.mikephil.charting.components.Legend.LegendHorizontalAlignment +import com.github.mikephil.charting.components.Legend.LegendOrientation +import com.github.mikephil.charting.components.Legend.LegendVerticalAlignment +import com.github.mikephil.charting.data.ChartData +import com.github.mikephil.charting.data.Entry +import com.github.mikephil.charting.interfaces.datasets.IDataSet +import com.github.mikephil.charting.listener.PieRadarChartTouchListener +import com.github.mikephil.charting.utils.MPPointF +import com.github.mikephil.charting.utils.MPPointF.Companion.getInstance +import com.github.mikephil.charting.utils.MPPointF.Companion.recycleInstance +import com.github.mikephil.charting.utils.Utils +import com.github.mikephil.charting.utils.convertDpToPixel +import kotlin.math.acos +import kotlin.math.cos +import kotlin.math.max +import kotlin.math.min +import kotlin.math.pow +import kotlin.math.sin +import kotlin.math.sqrt + +/** + * Baseclass of PieChart and RadarChart. + */ +abstract class PieRadarChartBase>> + : Chart { + /** + * holds the normalized version of the current rotation angle of the chart + */ + private var mRotationAngle = 270f + + /** + * gets the raw version of the current rotation angle of the pie chart the + * returned value could be any value, negative or positive, outside of the + * 360 degrees. this is used when working with rotation direction, mainly by + * gestures and animations. + */ + /** + * holds the raw version of the current rotation angle of the chart + */ + var rawRotationAngle: Float = 270f + private set + + /** + * Returns true if rotation of the chart by touch is enabled, false if not. + */ + /** + * Set this to true to enable the rotation / spinning of the chart by touch. + * Set it to false to disable it. Default: true + */ + /** + * flag that indicates if rotation is enabled or not + */ + var isRotationEnabled: Boolean = true + + /** + * Gets the minimum offset (padding) around the chart, defaults to 0.f + */ + /** + * Sets the minimum offset (padding) around the chart, defaults to 0.f + */ + /** + * Sets the minimum offset (padding) around the chart, defaults to 0.f + */ + var minOffset: Float = 0f + + constructor(context: Context?) : super(context) + + constructor(context: Context?, attrs: AttributeSet?) : super(context, attrs) + + constructor(context: Context?, attrs: AttributeSet?, defStyle: Int) : super(context, attrs, defStyle) + + override fun init() { + super.init() + + mChartTouchListener = PieRadarChartTouchListener(this) + } + + override fun calcMinMax() { + //mXAxis.mAxisRange = mData.getXVals().size() - 1; + } + + override fun getMaxVisibleCount(): Int { + return mData!!.getEntryCount() + } + + override fun onTouchEvent(event: MotionEvent?): Boolean { + // use the Pie- and RadarChart listener own listener + return if (mTouchEnabled && mChartTouchListener != null) mChartTouchListener.onTouch(this, event) + else + super.onTouchEvent(event) + } + + override fun computeScroll() { + if (mChartTouchListener is PieRadarChartTouchListener) (mChartTouchListener as PieRadarChartTouchListener).computeScroll() + } + + override fun notifyDataSetChanged() { + if (mData == null) return + + calcMinMax() + + if (mLegend != null) mLegendRenderer.computeLegend(mData!!) + + calculateOffsets() + } + + public override fun calculateOffsets() { + var legendLeft = 0f + var legendRight = 0f + var legendBottom = 0f + var legendTop = 0f + + if (mLegend != null && mLegend.isEnabled && !mLegend.isDrawInsideEnabled) { + val fullLegendWidth = min( + mLegend.mNeededWidth, + mViewPortHandler.chartWidth * mLegend.maxSizePercent + ) + + when (mLegend.orientation) { + LegendOrientation.VERTICAL -> { + var xLegendOffset = 0f + + if (mLegend.horizontalAlignment == LegendHorizontalAlignment.LEFT + || mLegend.horizontalAlignment == LegendHorizontalAlignment.RIGHT + ) { + if (mLegend.verticalAlignment == LegendVerticalAlignment.CENTER) { + // this is the space between the legend and the chart + val spacing = 13f.convertDpToPixel() + + xLegendOffset = fullLegendWidth + spacing + } else { + // this is the space between the legend and the chart + val spacing = 8f.convertDpToPixel() + + val legendWidth = fullLegendWidth + spacing + val legendHeight = mLegend.mNeededHeight + mLegend.mTextHeightMax + + val center = getCenter() + + val bottomX = if (mLegend.horizontalAlignment == + LegendHorizontalAlignment.RIGHT + ) + width - legendWidth + 15f + else + legendWidth - 15f + val bottomY = legendHeight + 15f + val distLegend = distanceToCenter(bottomX, bottomY) + + val reference = getPosition( + center, this.radius, + getAngleForPoint(bottomX, bottomY) + ) + + val distReference = distanceToCenter(reference.x, reference.y) + val minOffset = 5f.convertDpToPixel() + + if (bottomY >= center.y && height - legendWidth > width) { + xLegendOffset = legendWidth + } else if (distLegend < distReference) { + val diff = distReference - distLegend + xLegendOffset = minOffset + diff + } + + recycleInstance(center) + recycleInstance(reference) + } + } + + when (mLegend.horizontalAlignment) { + LegendHorizontalAlignment.LEFT -> legendLeft = xLegendOffset + LegendHorizontalAlignment.RIGHT -> legendRight = xLegendOffset + LegendHorizontalAlignment.CENTER -> when (mLegend.verticalAlignment) { + LegendVerticalAlignment.TOP -> legendTop = min( + mLegend.mNeededHeight, + mViewPortHandler.chartHeight * mLegend.maxSizePercent + ) + LegendVerticalAlignment.BOTTOM -> legendBottom = min( + mLegend.mNeededHeight, + mViewPortHandler.chartHeight * mLegend.maxSizePercent + ) + LegendVerticalAlignment.CENTER -> Log.e(LOG_TAG, "LegendCenter/VerticalCenter not supported for PieRadarChart") + } + } + } + + LegendOrientation.HORIZONTAL -> { + val yLegendOffset: Float + + if (mLegend.verticalAlignment == LegendVerticalAlignment.TOP || + mLegend.verticalAlignment == LegendVerticalAlignment.BOTTOM + ) { + // It's possible that we do not need this offset anymore as it + // is available through the extraOffsets, but changing it can mean + // changing default visibility for existing apps. + + val yOffset = this.requiredLegendOffset + + yLegendOffset = min( + mLegend.mNeededHeight + yOffset, + mViewPortHandler.chartHeight * mLegend.maxSizePercent + ) + + when (mLegend.verticalAlignment) { + LegendVerticalAlignment.TOP -> legendTop = yLegendOffset + LegendVerticalAlignment.BOTTOM -> legendBottom = yLegendOffset + LegendVerticalAlignment.CENTER -> Log.e(LOG_TAG, "LegendCenter/HorizontalCenter not supported for PieRadarChart") + } + } + } + } + + legendLeft += this.requiredBaseOffset + legendRight += this.requiredBaseOffset + legendTop += this.requiredBaseOffset + legendBottom += this.requiredBaseOffset + } + + var minOffset = minOffset.convertDpToPixel() + + if (this is RadarChart) { + val x = this.xAxis + + if (x.isEnabled && x.isDrawLabelsEnabled) { + minOffset = max(minOffset, x.mLabelWidth.toFloat()) + } + } + + legendTop += extraTopOffset + legendRight += extraRightOffset + legendBottom += extraBottomOffset + legendLeft += extraLeftOffset + + val offsetLeft = max(minOffset, legendLeft) + val offsetTop = max(minOffset, legendTop) + val offsetRight = max(minOffset, legendRight) + val offsetBottom = max(minOffset, max(this.requiredBaseOffset, legendBottom)) + + mViewPortHandler.restrainViewPort(offsetLeft, offsetTop, offsetRight, offsetBottom) + + if (mLogEnabled) Log.i( + LOG_TAG, ("offsetLeft: " + offsetLeft + ", offsetTop: " + offsetTop + + ", offsetRight: " + offsetRight + ", offsetBottom: " + offsetBottom) + ) + } + + /** + * returns the angle relative to the chart center for the given point on the + * chart in degrees. The angle is always between 0 and 360°, 0° is NORTH, + * 90° is EAST, ... + */ + fun getAngleForPoint(x: Float, y: Float): Float { + val c = centerOffsets + + val tx = (x - c.x).toDouble() + val ty = (y - c.y).toDouble() + val length = sqrt(tx * tx + ty * ty) + val r = acos(ty / length) + + var angle = Math.toDegrees(r).toFloat() + + if (x > c.x) angle = 360f - angle + + // add 90° because chart starts EAST + angle += 90f + + // neutralize overflow + if (angle > 360f) + angle -= 360f + + recycleInstance(c) + + return angle + } + + /** + * Returns a recyclable MPPointF instance. + * Calculates the position around a center point, depending on the distance + * from the center, and the angle of the position around the center. + * @param angle in degrees, converted to radians internally + */ + fun getPosition(center: MPPointF, dist: Float, angle: Float): MPPointF { + val p = getInstance(0f, 0f) + getPosition(center, dist, angle, p) + return p + } + + fun getPosition(center: MPPointF, dist: Float, angle: Float, outputPoint: MPPointF) { + outputPoint.x = (center.x + dist * cos(Math.toRadians(angle.toDouble()))).toFloat() + outputPoint.y = (center.y + dist * sin(Math.toRadians(angle.toDouble()))).toFloat() + } + + /** + * Returns the distance of a certain point on the chart to the center of the chart. + */ + fun distanceToCenter(x: Float, y: Float): Float { + val c = centerOffsets + + val dist: Float + + val xDist: Float = if (x > c.x) { + x - c.x + } else { + c.x - x + } + + val yDist: Float = if (y > c.y) { + y - c.y + } else { + c.y - y + } + + // pythagoras + dist = sqrt(xDist.toDouble().pow(2.0) + yDist.toDouble().pow(2.0)).toFloat() + + recycleInstance(c) + + return dist + } + + /** + * Returns the xIndex for the given angle around the center of the chart. + * Returns -1 if not found / out of bounds. + */ + abstract fun getIndexForAngle(angle: Float): Int + + var rotationAngle: Float + /** + * gets a normalized version of the current rotation angle of the pie chart, + * which will always be between 0.0 < 360.0 + */ + get() = mRotationAngle + /** + * Set an offset for the rotation of the RadarChart in degrees. Default 270f + * --> top (NORTH) + */ + set(angle) { + this.rawRotationAngle = angle + mRotationAngle = Utils.getNormalizedAngle(this.rawRotationAngle) + } + + val diameter: Float + /** + * returns the diameter of the pie- or radar-chart + */ + get() { + val content = mViewPortHandler.contentRect + content.left += extraLeftOffset + content.top += extraTopOffset + content.right -= extraRightOffset + content.bottom -= extraBottomOffset + return min(content.width(), content.height()) + } + + /** + * Returns the radius of the chart in pixels. + */ + abstract val radius: Float + + /** + * Returns the required offset for the chart legend. + */ + protected abstract val requiredLegendOffset: Float + + /** + * Returns the base offset needed for the chart without calculating the + * legend size. + */ + protected abstract val requiredBaseOffset: Float + + override fun getYChartMax(): Float { + // TODO Auto-generated method stub + return 0f + } + + override fun getYChartMin(): Float { + // TODO Auto-generated method stub + return 0f + } + + /** + * Apply a spin animation to the Chart. + */ + @SuppressLint("NewApi") + fun spin(durationMillis: Int, fromAngle: Float, toAngle: Float, easing: EasingFunction?) { + this.rotationAngle = fromAngle + + val spinAnimator = ObjectAnimator.ofFloat( + this, "rotationAngle", fromAngle, + toAngle + ) + spinAnimator.duration = durationMillis.toLong() + spinAnimator.interpolator = easing + + spinAnimator.addUpdateListener { postInvalidate() } + spinAnimator.start() + } +} diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/components/Legend.kt b/MPChartLib/src/main/java/com/github/mikephil/charting/components/Legend.kt index a8596b132..4b0d4b9f5 100644 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/components/Legend.kt +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/components/Legend.kt @@ -18,7 +18,6 @@ import kotlin.collections.MutableList import kotlin.collections.toTypedArray import kotlin.math.max import kotlin.math.min -import kotlin.requireNotNull /** * Class representing the legend of the chart. The legend will contain one entry @@ -87,10 +86,6 @@ class Legend() : ComponentBase() { var extraEntries: Array = arrayOf() private set - /** - * @return true if a custom legend entries has been set default - * false (automatic legend) - */ /** * Are the legend labels/colors a custom value or auto calculated? If false, * then it's auto, if true, then custom. default false (automatic legend) @@ -98,23 +93,14 @@ class Legend() : ComponentBase() { var isLegendCustom: Boolean = false private set - /** - * returns the horizontal alignment of the legend - */ /** * sets the horizontal alignment of the legend */ - var horizontalAlignment: LegendHorizontalAlignment? = LegendHorizontalAlignment.LEFT - /** - * returns the vertical alignment of the legend - */ + var horizontalAlignment: LegendHorizontalAlignment = LegendHorizontalAlignment.LEFT /** * sets the vertical alignment of the legend */ - var verticalAlignment: LegendVerticalAlignment? = LegendVerticalAlignment.BOTTOM - /** - * returns the orientation of the legend - */ + var verticalAlignment: LegendVerticalAlignment = LegendVerticalAlignment.BOTTOM /** * sets the orientation of the legend */ @@ -126,93 +112,41 @@ class Legend() : ComponentBase() { var isDrawInsideEnabled: Boolean = false private set - /** - * returns the text direction of the legend - */ - /** - * sets the text direction of the legend - */ /** * the text direction for the legend */ - var direction: LegendDirection? = LegendDirection.LEFT_TO_RIGHT + var direction: LegendDirection = LegendDirection.LEFT_TO_RIGHT - /** - * returns the current form/shape that is set for the legend - */ - /** - * sets the form/shape of the legend forms - */ /** * the shape/form the legend colors are drawn in */ - var form: LegendForm? = LegendForm.SQUARE + var form: LegendForm = LegendForm.SQUARE - /** - * returns the size in dp of the legend forms - */ - /** - * sets the size in dp of the legend forms, default 8f - */ /** * the size of the legend forms/shapes */ var formSize: kotlin.Float = 8f - /** - * returns the line width in dp for drawing forms that consist of lines - */ - /** - * sets the line width in dp for forms that consist of lines, default 3f - */ /** * the size of the legend forms/shapes */ var formLineWidth: kotlin.Float = 3f - /** - * @return The line dash path effect used for shapes that consist of lines. - */ - /** - * Sets the line dash path effect used for shapes that consist of lines. - */ /** * Line dash path effect used for shapes that consist of lines. */ var formLineDashEffect: DashPathEffect? = null - /** - * returns the space between the legend entries on a horizontal axis in - * pixels - */ - /** - * sets the space between the legend entries on a horizontal axis in pixels, - * converts to dp internally - */ /** * the space between the legend entries on a horizontal axis, default 6f */ var xEntrySpace: kotlin.Float = 6f - /** - * returns the space between the legend entries on a vertical axis in pixels - */ - /** - * sets the space between the legend entries on a vertical axis in pixels, - * converts to dp internally - */ /** * the space between the legend entries on a vertical axis, default 5f */ var yEntrySpace: kotlin.Float = 0f - /** - * returns the space between the form and the actual label/text - */ - /** - * sets the space between the form and the actual label/text, converts to dp - * internally - */ /** * the space between the legend entries on a vertical axis, default 2f * private float mYEntrySpace = 2f; / ** the space between the form and the @@ -220,12 +154,6 @@ class Legend() : ComponentBase() { */ var formToTextSpace: kotlin.Float = 5f - /** - * returns the space that is left out between stacked forms (with no label) - */ - /** - * sets the space that is left out between stacked forms (with no label) - */ /** * the space that should be left between stacked forms */ @@ -239,23 +167,12 @@ class Legend() : ComponentBase() { * piechart, then this defines the size of the rectangular bounds out of the * size of the "hole". / default: 0.95f (95%) */ - /** - * The maximum relative size out of the whole chart view. / If - * the legend is to the right/left of the chart, then this affects the width - * of the legend. / If the legend is to the top/bottom of the chart, then - * this affects the height of the legend. / default: 0.95f (95%) - */ - /** - * the maximum relative size out of the whole chart view in percent - */ var maxSizePercent: kotlin.Float = 0.95f /** * Constructor. Provide entries for the legend. */ constructor(entries: Array) : this() { - requireNotNull(entries) { "entries array is NULL" } - this.entries = entries } diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/components/LegendEntry.kt b/MPChartLib/src/main/java/com/github/mikephil/charting/components/LegendEntry.kt index c435a1d2c..c0aafe36b 100644 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/components/LegendEntry.kt +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/components/LegendEntry.kt @@ -19,7 +19,7 @@ class LegendEntry { */ constructor( label: String?, - form: LegendForm?, + form: LegendForm, formSize: Float, formLineWidth: Float, formLineDashEffect: DashPathEffect?, @@ -46,7 +46,7 @@ class LegendEntry { * `EMPTY` will avoid drawing a form, but keep its space. * `DEFAULT` will use the Legend's default. */ - var form: LegendForm? = LegendForm.DEFAULT + var form: LegendForm = LegendForm.DEFAULT /** * Form size will be considered except for when .None is used diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/highlight/PieRadarHighlighter.kt b/MPChartLib/src/main/java/com/github/mikephil/charting/highlight/PieRadarHighlighter.kt index 47a55c817..fc366d298 100644 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/highlight/PieRadarHighlighter.kt +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/highlight/PieRadarHighlighter.kt @@ -13,7 +13,7 @@ abstract class PieRadarHighlighter>(protected var chart val touchDistanceToCenter = chartPieRadar.distanceToCenter(x, y) // check if a slice was touched - if (touchDistanceToCenter > chartPieRadar.getRadius()) { + if (touchDistanceToCenter > chartPieRadar.radius) { // if no slice was touched, highlight nothing return null diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/listener/PieRadarChartTouchListener.kt b/MPChartLib/src/main/java/com/github/mikephil/charting/listener/PieRadarChartTouchListener.kt index 8fcae2d89..095563794 100644 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/listener/PieRadarChartTouchListener.kt +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/listener/PieRadarChartTouchListener.kt @@ -65,7 +65,7 @@ class PieRadarChartTouchListener(chart: PieRadarChartBase<*>?) : ChartTouchListe chart!!.disableScroll() } else if (touchMode == ROTATE) { updateGestureRotation(x, y) - chart!!.invalidate() + chart?.invalidate() } endAction(event) @@ -215,7 +215,7 @@ class PieRadarChartTouchListener(chart: PieRadarChartBase<*>?) : ChartTouchListe * @param y */ fun updateGestureRotation(x: Float, y: Float) { - chart!!.setRotationAngle(chart!!.getAngleForPoint(x, y) - startAngle) + chart!!.rotationAngle = chart!!.getAngleForPoint(x, y) - startAngle } /** @@ -236,7 +236,7 @@ class PieRadarChartTouchListener(chart: PieRadarChartBase<*>?) : ChartTouchListe val timeInterval = (currentTime - decelerationLastTime).toFloat() / 1000f - chart!!.setRotationAngle(chart!!.rotationAngle + decelerationAngularVelocity * timeInterval) + chart!!.rotationAngle += decelerationAngularVelocity * timeInterval decelerationLastTime = currentTime 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 2799b6c96..7d9e190af 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 @@ -4,7 +4,6 @@ import android.graphics.Canvas import android.graphics.Paint import android.graphics.Paint.Align import android.graphics.Path -import android.util.Log import androidx.core.graphics.withSave import com.github.mikephil.charting.components.Legend import com.github.mikephil.charting.components.Legend.LegendDirection @@ -228,7 +227,7 @@ open class LegendRenderer( val yOffset = legend.yOffset val xOffset = legend.xOffset - var originPosX = 0f + var originPosX: Float when (horizontalAlignment) { LegendHorizontalAlignment.LEFT -> { @@ -266,8 +265,6 @@ open class LegendRenderer( legend.mNeededWidth / 2.0 - xOffset).toFloat() } } - - null -> Log.e("Chart", "Legend horizontalAlignment is null") } when (orientation) { @@ -282,10 +279,6 @@ open class LegendRenderer( LegendVerticalAlignment.TOP -> yOffset LegendVerticalAlignment.BOTTOM -> viewPortHandler.chartHeight - yOffset - legend.mNeededHeight LegendVerticalAlignment.CENTER -> (viewPortHandler.chartHeight - legend.mNeededHeight) / 2f + yOffset - else -> { - Log.w("Chart", "Legend verticalAlignment not set") - 0f - } } var lineIndex = 0 @@ -344,7 +337,7 @@ open class LegendRenderer( // contains the stacked legend size in pixels var stack = 0f var wasStacked = false - var posY = 0f + var posY: Float when (verticalAlignment) { LegendVerticalAlignment.TOP -> { @@ -366,8 +359,6 @@ open class LegendRenderer( LegendVerticalAlignment.CENTER -> posY = (viewPortHandler.chartHeight / 2f - legend.mNeededHeight / 2f + legend.yOffset) - - null -> Log.w("Chart", "Legend verticalAlignment is null") } var i = 0 @@ -496,9 +487,6 @@ open class LegendRenderer( mLineFormPath.lineTo(x + formSize, y) canvas.drawPath(mLineFormPath, formPaint) } - - null -> Log.w( "Chart", "Legend form is null") - } } diff --git a/app/src/main/kotlin/info/appdev/chartexample/AnotherBarActivity.kt b/app/src/main/kotlin/info/appdev/chartexample/AnotherBarActivity.kt index 4e67247fe..75c3e7c12 100644 --- a/app/src/main/kotlin/info/appdev/chartexample/AnotherBarActivity.kt +++ b/app/src/main/kotlin/info/appdev/chartexample/AnotherBarActivity.kt @@ -92,7 +92,7 @@ class AnotherBarActivity : DemoBase(), OnSeekBarChangeListener { set1 = chart!!.data!!.getDataSetByIndex(0) as BarDataSet set1.entries = values chart!!.data!!.notifyDataChanged() - chart!!.notifyDataSetChanged() + chart?.notifyDataSetChanged() } else { set1 = BarDataSet(values, "Data Set") set1.setColors(*ColorTemplate.VORDIPLOM_COLORS) @@ -106,7 +106,7 @@ class AnotherBarActivity : DemoBase(), OnSeekBarChangeListener { chart!!.setFitBars(true) } - chart!!.invalidate() + chart?.invalidate() } override fun onCreateOptionsMenu(menu: Menu): Boolean { @@ -128,13 +128,13 @@ class AnotherBarActivity : DemoBase(), OnSeekBarChangeListener { it?.isDrawValues = !it.isDrawValues } - chart!!.invalidate() + chart?.invalidate() } R.id.actionToggleHighlight -> { if (chart!!.data != null) { chart!!.data!!.isHighlightEnabled = !chart!!.data!!.isHighlightEnabled() - chart!!.invalidate() + chart?.invalidate() } } @@ -142,18 +142,18 @@ class AnotherBarActivity : DemoBase(), OnSeekBarChangeListener { if (chart!!.isPinchZoomEnabled) chart!!.setPinchZoom(false) else chart!!.setPinchZoom(true) - chart!!.invalidate() + chart?.invalidate() } R.id.actionToggleAutoScaleMinMax -> { chart!!.isAutoScaleMinMaxEnabled = !chart!!.isAutoScaleMinMaxEnabled - chart!!.notifyDataSetChanged() + chart?.notifyDataSetChanged() } R.id.actionToggleBarBorders -> { for (set in chart!!.data!!.dataSets) (set as BarDataSet).barBorderWidth = if (set.barBorderWidth == 1f) 0f else 1f - chart!!.invalidate() + chart?.invalidate() } R.id.animateX -> { diff --git a/app/src/main/kotlin/info/appdev/chartexample/BarChartActivity.kt b/app/src/main/kotlin/info/appdev/chartexample/BarChartActivity.kt index 661c0f064..9cde1a234 100644 --- a/app/src/main/kotlin/info/appdev/chartexample/BarChartActivity.kt +++ b/app/src/main/kotlin/info/appdev/chartexample/BarChartActivity.kt @@ -115,7 +115,7 @@ class BarChartActivity : DemoBase(), OnSeekBarChangeListener, OnChartValueSelect val mv = XYMarkerView(this, xAxisFormatter) mv.chartView = chart // For bounds control - chart!!.setMarker(mv) // Set the marker to the chart + chart?.setMarker(mv) // Set the marker to the chart // setting data seekBarY!!.progress = 50 @@ -150,7 +150,7 @@ class BarChartActivity : DemoBase(), OnSeekBarChangeListener, OnChartValueSelect set1 = chart!!.data!!.getDataSetByIndex(0) as BarDataSet set1.entries = values chart!!.data!!.notifyDataChanged() - chart!!.notifyDataSetChanged() + chart?.notifyDataSetChanged() } else { set1 = BarDataSet(values, "The year 2017") @@ -184,7 +184,7 @@ class BarChartActivity : DemoBase(), OnSeekBarChangeListener, OnChartValueSelect data.setValueTypeface(tfLight) data.barWidth = 0.9f - chart!!.setData(data) + chart?.setData(data) } } @@ -206,20 +206,20 @@ class BarChartActivity : DemoBase(), OnSeekBarChangeListener, OnChartValueSelect it?.isDrawValues = !it.isDrawValues } - chart!!.invalidate() + chart?.invalidate() } R.id.actionToggleIcons -> { for (set in chart!!.data!!.dataSets) set?.isDrawIcons = !set.isDrawIcons - chart!!.invalidate() + chart?.invalidate() } R.id.actionToggleHighlight -> { if (chart!!.data != null) { chart!!.data!!.isHighlightEnabled = !chart!!.data!!.isHighlightEnabled() - chart!!.invalidate() + chart?.invalidate() } } @@ -227,12 +227,12 @@ class BarChartActivity : DemoBase(), OnSeekBarChangeListener, OnChartValueSelect if (chart!!.isPinchZoomEnabled) chart!!.setPinchZoom(false) else chart!!.setPinchZoom(true) - chart!!.invalidate() + chart?.invalidate() } R.id.actionToggleAutoScaleMinMax -> { chart!!.isAutoScaleMinMaxEnabled = !chart!!.isAutoScaleMinMaxEnabled - chart!!.notifyDataSetChanged() + chart?.notifyDataSetChanged() } R.id.actionToggleBarBorders -> { @@ -242,7 +242,7 @@ class BarChartActivity : DemoBase(), OnSeekBarChangeListener, OnChartValueSelect else 1f - chart!!.invalidate() + chart?.invalidate() } R.id.animateX -> { @@ -267,8 +267,8 @@ class BarChartActivity : DemoBase(), OnSeekBarChangeListener, OnChartValueSelect R.id.actionRotateXAxisLabelsBy45Deg -> { chart!!.xAxis.labelRotationAngle = 45f - chart!!.notifyDataSetChanged() - chart!!.invalidate() + chart?.notifyDataSetChanged() + chart?.invalidate() } } return true @@ -279,7 +279,7 @@ class BarChartActivity : DemoBase(), OnSeekBarChangeListener, OnChartValueSelect tvY!!.text = seekBarY!!.progress.toString() setData(seekBarX!!.progress, seekBarY!!.progress.toFloat()) - chart!!.invalidate() + chart?.invalidate() } override fun saveToGallery() { diff --git a/app/src/main/kotlin/info/appdev/chartexample/BarChartActivityMultiDataset.kt b/app/src/main/kotlin/info/appdev/chartexample/BarChartActivityMultiDataset.kt index 0d6cc3a33..209f4c2b0 100644 --- a/app/src/main/kotlin/info/appdev/chartexample/BarChartActivityMultiDataset.kt +++ b/app/src/main/kotlin/info/appdev/chartexample/BarChartActivityMultiDataset.kt @@ -147,7 +147,7 @@ class BarChartActivityMultiDataset : DemoBase(), OnSeekBarChangeListener, OnChar set3.entries = values3 set4.entries = values4 chart!!.data!!.notifyDataChanged() - chart!!.notifyDataSetChanged() + chart?.notifyDataSetChanged() } else { // create 4 DataSets set1 = BarDataSet(values1, "Company A") @@ -175,7 +175,7 @@ class BarChartActivityMultiDataset : DemoBase(), OnSeekBarChangeListener, OnChar // barData.getGroupWith(...) is a helper that calculates the width each group needs based on the provided parameters chart!!.xAxis.axisMaximum = startYear + chart!!.barData.getGroupWidth(groupSpace, barSpace) * groupCount chart!!.groupBars(startYear.toFloat(), groupSpace, barSpace) - chart!!.invalidate() + chart?.invalidate() } override fun onCreateOptionsMenu(menu: Menu?): Boolean { @@ -197,31 +197,31 @@ class BarChartActivityMultiDataset : DemoBase(), OnSeekBarChangeListener, OnChar it?.isDrawValues = !it.isDrawValues } - chart!!.invalidate() + chart?.invalidate() } R.id.actionTogglePinch -> { if (chart!!.isPinchZoomEnabled) chart!!.setPinchZoom(false) else chart!!.setPinchZoom(true) - chart!!.invalidate() + chart?.invalidate() } R.id.actionToggleAutoScaleMinMax -> { chart!!.isAutoScaleMinMaxEnabled = !chart!!.isAutoScaleMinMaxEnabled - chart!!.notifyDataSetChanged() + chart?.notifyDataSetChanged() } R.id.actionToggleBarBorders -> { for (set in chart!!.data!!.dataSets) (set as BarDataSet).barBorderWidth = if (set.barBorderWidth == 1f) 0f else 1f - chart!!.invalidate() + chart?.invalidate() } R.id.actionToggleHighlight -> { if (chart!!.data != null) { chart!!.data!!.isHighlightEnabled = !chart!!.data!!.isHighlightEnabled() - chart!!.invalidate() + chart?.invalidate() } } diff --git a/app/src/main/kotlin/info/appdev/chartexample/BarChartPositiveNegative.kt b/app/src/main/kotlin/info/appdev/chartexample/BarChartPositiveNegative.kt index 2689fb268..78b511c1b 100644 --- a/app/src/main/kotlin/info/appdev/chartexample/BarChartPositiveNegative.kt +++ b/app/src/main/kotlin/info/appdev/chartexample/BarChartPositiveNegative.kt @@ -110,7 +110,7 @@ class BarChartPositiveNegative : DemoBase() { set = chart!!.data!!.getDataSetByIndex(0) as BarDataSet set.entries = values chart!!.data!!.notifyDataChanged() - chart!!.notifyDataSetChanged() + chart?.notifyDataSetChanged() } else { set = BarDataSet(values, "Values") set.setColors(colors) @@ -123,7 +123,7 @@ class BarChartPositiveNegative : DemoBase() { data.barWidth = 0.8f chart!!.setData(data) - chart!!.invalidate() + chart?.invalidate() } } diff --git a/app/src/main/kotlin/info/appdev/chartexample/BubbleChartActivity.kt b/app/src/main/kotlin/info/appdev/chartexample/BubbleChartActivity.kt index 16388d3be..9c0a70d42 100644 --- a/app/src/main/kotlin/info/appdev/chartexample/BubbleChartActivity.kt +++ b/app/src/main/kotlin/info/appdev/chartexample/BubbleChartActivity.kt @@ -150,7 +150,7 @@ class BubbleChartActivity : DemoBase(), OnSeekBarChangeListener, OnChartValueSel data.setHighlightCircleWidth(1.5f) chart!!.setData(data) - chart!!.invalidate() + chart?.invalidate() } override fun onCreateOptionsMenu(menu: Menu?): Boolean { @@ -170,19 +170,19 @@ class BubbleChartActivity : DemoBase(), OnSeekBarChangeListener, OnChartValueSel chart!!.data!!.dataSets.forEach { it?.isDrawValues = !it.isDrawValues } - chart!!.invalidate() + chart?.invalidate() } R.id.actionToggleIcons -> { for (set in chart!!.data!!.dataSets) set?.isDrawIcons = !set.isDrawIcons - chart!!.invalidate() + chart?.invalidate() } R.id.actionToggleHighlight -> { if (chart!!.data != null) { chart!!.data!!.isHighlightEnabled = !chart!!.data!!.isHighlightEnabled() - chart!!.invalidate() + chart?.invalidate() } } @@ -190,12 +190,12 @@ class BubbleChartActivity : DemoBase(), OnSeekBarChangeListener, OnChartValueSel if (chart!!.isPinchZoomEnabled) chart!!.setPinchZoom(false) else chart!!.setPinchZoom(true) - chart!!.invalidate() + chart?.invalidate() } R.id.actionToggleAutoScaleMinMax -> { chart!!.isAutoScaleMinMaxEnabled = !chart!!.isAutoScaleMinMaxEnabled - chart!!.notifyDataSetChanged() + chart?.notifyDataSetChanged() } R.id.actionSave -> { diff --git a/app/src/main/kotlin/info/appdev/chartexample/CandleStickChartActivity.kt b/app/src/main/kotlin/info/appdev/chartexample/CandleStickChartActivity.kt index 412f83ca2..da76c3a35 100644 --- a/app/src/main/kotlin/info/appdev/chartexample/CandleStickChartActivity.kt +++ b/app/src/main/kotlin/info/appdev/chartexample/CandleStickChartActivity.kt @@ -130,7 +130,7 @@ class CandleStickChartActivity : DemoBase(), OnSeekBarChangeListener { val data = CandleData(set1) chart!!.setData(data) - chart!!.invalidate() + chart?.invalidate() } override fun onCreateOptionsMenu(menu: Menu?): Boolean { @@ -151,19 +151,19 @@ class CandleStickChartActivity : DemoBase(), OnSeekBarChangeListener { chart!!.data!!.dataSets.forEach { it?.isDrawValues = !it.isDrawValues } - chart!!.invalidate() + chart?.invalidate() } R.id.actionToggleIcons -> { for (set in chart!!.data!!.dataSets) set?.isDrawIcons = !set.isDrawIcons - chart!!.invalidate() + chart?.invalidate() } R.id.actionToggleHighlight -> { if (chart!!.data != null) { chart!!.data!!.isHighlightEnabled = !chart!!.data!!.isHighlightEnabled() - chart!!.invalidate() + chart?.invalidate() } } @@ -171,12 +171,12 @@ class CandleStickChartActivity : DemoBase(), OnSeekBarChangeListener { if (chart!!.isPinchZoomEnabled) chart!!.setPinchZoom(false) else chart!!.setPinchZoom(true) - chart!!.invalidate() + chart?.invalidate() } R.id.actionToggleAutoScaleMinMax -> { chart!!.isAutoScaleMinMaxEnabled = !chart!!.isAutoScaleMinMaxEnabled - chart!!.notifyDataSetChanged() + chart?.notifyDataSetChanged() } R.id.actionToggleMakeShadowSameColorAsCandle -> { @@ -184,7 +184,7 @@ class CandleStickChartActivity : DemoBase(), OnSeekBarChangeListener { (set as CandleDataSet).shadowColorSameAsCandle = !set.shadowColorSameAsCandle } - chart!!.invalidate() + chart?.invalidate() } R.id.animateX -> { diff --git a/app/src/main/kotlin/info/appdev/chartexample/CombinedChartActivity.kt b/app/src/main/kotlin/info/appdev/chartexample/CombinedChartActivity.kt index 7871e499f..83a5a6d27 100644 --- a/app/src/main/kotlin/info/appdev/chartexample/CombinedChartActivity.kt +++ b/app/src/main/kotlin/info/appdev/chartexample/CombinedChartActivity.kt @@ -93,7 +93,7 @@ class CombinedChartActivity : DemoBase() { xAxis.axisMaximum = data.xMax + 0.25f chart!!.setData(data) - chart!!.invalidate() + chart?.invalidate() } private fun generateLineData(): LineData { @@ -242,7 +242,7 @@ class CombinedChartActivity : DemoBase() { if (it is LineDataSet) it.isDrawValues = !it.isDrawValues } - chart!!.invalidate() + chart?.invalidate() } R.id.actionToggleBarValues -> { @@ -250,15 +250,15 @@ class CombinedChartActivity : DemoBase() { if (it is BarDataSet) it.isDrawValues = !it.isDrawValues } - chart!!.invalidate() + chart?.invalidate() } R.id.actionRemoveDataSet -> { val rnd = values[sampleCount]!!.toFloat().toInt() * chart!!.data!!.getDataSetCount() chart!!.data!!.removeDataSet(chart!!.data!!.getDataSetByIndex(rnd)) chart!!.data!!.notifyDataChanged() - chart!!.notifyDataSetChanged() - chart!!.invalidate() + chart?.notifyDataSetChanged() + chart?.invalidate() } } return true diff --git a/app/src/main/kotlin/info/appdev/chartexample/DynamicalAddingActivity.kt b/app/src/main/kotlin/info/appdev/chartexample/DynamicalAddingActivity.kt index 3c82c0222..7ac0a78e2 100644 --- a/app/src/main/kotlin/info/appdev/chartexample/DynamicalAddingActivity.kt +++ b/app/src/main/kotlin/info/appdev/chartexample/DynamicalAddingActivity.kt @@ -37,7 +37,7 @@ class DynamicalAddingActivity : DemoBase(), OnChartValueSelectedListener { // chart.getXAxis().setDrawLabels(false); // chart.getXAxis().setDrawGridLines(false); - chart!!.invalidate() + chart?.invalidate() } private val colors: IntArray = ColorTemplate.VORDIPLOM_COLORS @@ -69,7 +69,7 @@ class DynamicalAddingActivity : DemoBase(), OnChartValueSelectedListener { data.notifyDataChanged() // let the chart know it's data has changed - chart!!.notifyDataSetChanged() + chart?.notifyDataSetChanged() chart!!.setVisibleXRangeMaximum(6f) //chart.setVisibleYRangeMaximum(15, AxisDependency.LEFT); @@ -92,8 +92,8 @@ class DynamicalAddingActivity : DemoBase(), OnChartValueSelectedListener { // or remove by index // mData.removeEntryByXValue(xIndex, dataSetIndex); data.notifyDataChanged() - chart!!.notifyDataSetChanged() - chart!!.invalidate() + chart?.notifyDataSetChanged() + chart?.invalidate() } } } @@ -129,8 +129,8 @@ class DynamicalAddingActivity : DemoBase(), OnChartValueSelectedListener { data.addDataSet(set) data.notifyDataChanged() - chart!!.notifyDataSetChanged() - chart!!.invalidate() + chart?.notifyDataSetChanged() + chart?.invalidate() } } @@ -140,8 +140,8 @@ class DynamicalAddingActivity : DemoBase(), OnChartValueSelectedListener { if (data != null) { data.removeDataSet(data.getDataSetByIndex(data.getDataSetCount() - 1)) - chart!!.notifyDataSetChanged() - chart!!.invalidate() + chart?.notifyDataSetChanged() + chart?.invalidate() } } diff --git a/app/src/main/kotlin/info/appdev/chartexample/HalfPieChartActivity.kt b/app/src/main/kotlin/info/appdev/chartexample/HalfPieChartActivity.kt index c9e984bfd..b85c39849 100644 --- a/app/src/main/kotlin/info/appdev/chartexample/HalfPieChartActivity.kt +++ b/app/src/main/kotlin/info/appdev/chartexample/HalfPieChartActivity.kt @@ -11,6 +11,8 @@ import android.text.style.StyleSpan import android.view.Menu import android.view.MenuItem import android.widget.RelativeLayout +import androidx.core.net.toUri +import androidx.window.layout.WindowMetricsCalculator import com.github.mikephil.charting.animation.Easing import com.github.mikephil.charting.charts.PieChart import com.github.mikephil.charting.components.Legend @@ -21,8 +23,6 @@ import com.github.mikephil.charting.formatter.PercentFormatter import com.github.mikephil.charting.utils.ColorTemplate import info.appdev.chartexample.DataTools.Companion.getValues import info.appdev.chartexample.notimportant.DemoBase -import androidx.core.net.toUri -import androidx.window.layout.WindowMetricsCalculator class HalfPieChartActivity : DemoBase() { private var chart: PieChart? = null @@ -57,7 +57,7 @@ class HalfPieChartActivity : DemoBase() { chart!!.isHighlightPerTapEnabled = true chart!!.setMaxAngle(180f) // HALF CHART - chart!!.setRotationAngle(180f) + chart!!.rotationAngle = 180f chart!!.setCenterTextOffset(0f, -20f) setData(100f) @@ -102,7 +102,7 @@ class HalfPieChartActivity : DemoBase() { data.setValueTypeface(tfLight) chart!!.setData(data) - chart!!.invalidate() + chart?.invalidate() } private fun generateCenterSpannableText(): SpannableString { diff --git a/app/src/main/kotlin/info/appdev/chartexample/HorizontalBarNegativeChartActivity.kt b/app/src/main/kotlin/info/appdev/chartexample/HorizontalBarNegativeChartActivity.kt index c9300b99b..0b6171ddc 100644 --- a/app/src/main/kotlin/info/appdev/chartexample/HorizontalBarNegativeChartActivity.kt +++ b/app/src/main/kotlin/info/appdev/chartexample/HorizontalBarNegativeChartActivity.kt @@ -128,7 +128,7 @@ class HorizontalBarNegativeChartActivity : DemoBase(), OnSeekBarChangeListener, set1 = chart!!.data!!.getDataSetByIndex(0) as BarDataSet set1.entries = values chart!!.data!!.notifyDataChanged() - chart!!.notifyDataSetChanged() + chart?.notifyDataSetChanged() } else { set1 = BarDataSet(values, "DataSet 1") @@ -163,7 +163,7 @@ class HorizontalBarNegativeChartActivity : DemoBase(), OnSeekBarChangeListener, chart!!.data!!.dataSets.forEach { it?.isDrawValues = !it.isDrawValues } - chart!!.invalidate() + chart?.invalidate() } R.id.actionToggleIcons -> { @@ -171,31 +171,31 @@ class HorizontalBarNegativeChartActivity : DemoBase(), OnSeekBarChangeListener, iSet.isDrawIcons = !iSet.isDrawIcons } - chart!!.invalidate() + chart?.invalidate() } R.id.actionToggleHighlight -> { if (chart!!.data != null) { chart!!.data!!.isHighlightEnabled = !chart!!.data!!.isHighlightEnabled() - chart!!.invalidate() + chart?.invalidate() } } R.id.actionTogglePinch -> { chart!!.setPinchZoom(!chart!!.isPinchZoomEnabled) - chart!!.invalidate() + chart?.invalidate() } R.id.actionToggleAutoScaleMinMax -> { chart!!.isAutoScaleMinMaxEnabled = !chart!!.isAutoScaleMinMaxEnabled - chart!!.notifyDataSetChanged() + chart?.notifyDataSetChanged() } R.id.actionToggleBarBorders -> { for (set in chart!!.data!!.dataSets) (set as BarDataSet).barBorderWidth = if (set.barBorderWidth == 1f) 0f else 1f - chart!!.invalidate() + chart?.invalidate() } R.id.animateX -> { @@ -227,7 +227,7 @@ class HorizontalBarNegativeChartActivity : DemoBase(), OnSeekBarChangeListener, setData(seekBarX!!.progress, seekBarY!!.progress.toFloat()) chart!!.setFitBars(true) - chart!!.invalidate() + chart?.invalidate() } override fun saveToGallery() { diff --git a/app/src/main/kotlin/info/appdev/chartexample/InvertedLineChartActivity.kt b/app/src/main/kotlin/info/appdev/chartexample/InvertedLineChartActivity.kt index f4b4bfcbd..72635d44b 100644 --- a/app/src/main/kotlin/info/appdev/chartexample/InvertedLineChartActivity.kt +++ b/app/src/main/kotlin/info/appdev/chartexample/InvertedLineChartActivity.kt @@ -99,7 +99,7 @@ class InvertedLineChartActivity : DemoBase(), OnSeekBarChangeListener, OnChartVa l.form = LegendForm.LINE // don't forget to refresh the drawing - chart!!.invalidate() + chart?.invalidate() } private fun setData(count: Int, range: Float) { @@ -146,13 +146,13 @@ class InvertedLineChartActivity : DemoBase(), OnSeekBarChangeListener, OnChartVa chart!!.data!!.dataSets.forEach { it?.isDrawValues = !it.isDrawValues } - chart!!.invalidate() + chart?.invalidate() } R.id.actionToggleHighlight -> { if (chart!!.data != null) { chart!!.data!!.isHighlightEnabled = !chart!!.data!!.isHighlightEnabled() - chart!!.invalidate() + chart?.invalidate() } } @@ -160,7 +160,7 @@ class InvertedLineChartActivity : DemoBase(), OnSeekBarChangeListener, OnChartVa chart!!.data!!.dataSets.forEach { set -> set?.setDrawFilled(!set.isDrawFilledEnabled) } - chart!!.invalidate() + chart?.invalidate() } R.id.actionToggleCircles -> { @@ -170,7 +170,7 @@ class InvertedLineChartActivity : DemoBase(), OnSeekBarChangeListener, OnChartVa val set = iSet as LineDataSet set.isDrawCirclesEnabled = !set.isDrawCirclesEnabled } - chart!!.invalidate() + chart?.invalidate() } R.id.animateX -> { @@ -188,12 +188,12 @@ class InvertedLineChartActivity : DemoBase(), OnSeekBarChangeListener, OnChartVa R.id.actionTogglePinch -> { chart!!.setPinchZoom(!chart!!.isPinchZoomEnabled) - chart!!.invalidate() + chart?.invalidate() } R.id.actionToggleAutoScaleMinMax -> { chart!!.isAutoScaleMinMaxEnabled = !chart!!.isAutoScaleMinMaxEnabled - chart!!.notifyDataSetChanged() + chart?.notifyDataSetChanged() } R.id.actionSave -> { @@ -214,7 +214,7 @@ class InvertedLineChartActivity : DemoBase(), OnSeekBarChangeListener, OnChartVa setData(seekBarX!!.progress, seekBarY!!.progress.toFloat()) // redraw - chart!!.invalidate() + chart?.invalidate() } override fun saveToGallery() { diff --git a/app/src/main/kotlin/info/appdev/chartexample/LineChartDualAxisActivity.kt b/app/src/main/kotlin/info/appdev/chartexample/LineChartDualAxisActivity.kt index 311fa7cb6..2a161d1e6 100644 --- a/app/src/main/kotlin/info/appdev/chartexample/LineChartDualAxisActivity.kt +++ b/app/src/main/kotlin/info/appdev/chartexample/LineChartDualAxisActivity.kt @@ -157,7 +157,7 @@ class LineChartDualAxisActivity : DemoBase(), OnSeekBarChangeListener, OnChartVa set2.entries = values2 set3.entries = values3 chart!!.data!!.notifyDataChanged() - chart!!.notifyDataSetChanged() + chart?.notifyDataSetChanged() } else { // create a dataset and give it a type set1 = LineDataSet(values1, "DataSet 1") @@ -229,13 +229,13 @@ class LineChartDualAxisActivity : DemoBase(), OnSeekBarChangeListener, OnChartVa chart!!.data!!.dataSets.forEach { set -> set?.isDrawValues = !set.isDrawValues } - chart!!.invalidate() + chart?.invalidate() } R.id.actionToggleHighlight -> { if (chart!!.data != null) { chart!!.data!!.isHighlightEnabled = !chart!!.data!!.isHighlightEnabled() - chart!!.invalidate() + chart?.invalidate() } } @@ -243,7 +243,7 @@ class LineChartDualAxisActivity : DemoBase(), OnSeekBarChangeListener, OnChartVa chart!!.data!!.dataSets.forEach { set -> set?.setDrawFilled(!set.isDrawFilledEnabled) } - chart!!.invalidate() + chart?.invalidate() } R.id.actionToggleCircles -> { @@ -253,7 +253,7 @@ class LineChartDualAxisActivity : DemoBase(), OnSeekBarChangeListener, OnChartVa val set = iSet as LineDataSet set.isDrawCirclesEnabled = !set.isDrawCirclesEnabled } - chart!!.invalidate() + chart?.invalidate() } R.id.actionToggleCubic -> { @@ -266,7 +266,7 @@ class LineChartDualAxisActivity : DemoBase(), OnSeekBarChangeListener, OnChartVa else LineDataSet.Mode.CUBIC_BEZIER } - chart!!.invalidate() + chart?.invalidate() } R.id.actionToggleStepped -> { @@ -279,7 +279,7 @@ class LineChartDualAxisActivity : DemoBase(), OnSeekBarChangeListener, OnChartVa else LineDataSet.Mode.STEPPED } - chart!!.invalidate() + chart?.invalidate() } R.id.actionToggleHorizontalCubic -> { @@ -292,18 +292,18 @@ class LineChartDualAxisActivity : DemoBase(), OnSeekBarChangeListener, OnChartVa else LineDataSet.Mode.HORIZONTAL_BEZIER } - chart!!.invalidate() + chart?.invalidate() } R.id.actionTogglePinch -> { chart!!.setPinchZoom(!chart!!.isPinchZoomEnabled) - chart!!.invalidate() + chart?.invalidate() } R.id.actionToggleAutoScaleMinMax -> { chart!!.isAutoScaleMinMaxEnabled = !chart!!.isAutoScaleMinMaxEnabled - chart!!.notifyDataSetChanged() + chart?.notifyDataSetChanged() } R.id.animateX -> { @@ -336,7 +336,7 @@ class LineChartDualAxisActivity : DemoBase(), OnSeekBarChangeListener, OnChartVa setData(seekBarX!!.progress, seekBarY!!.progress.toFloat()) // redraw - chart!!.invalidate() + chart?.invalidate() } override fun saveToGallery() { diff --git a/app/src/main/kotlin/info/appdev/chartexample/LineChartTimeActivity.kt b/app/src/main/kotlin/info/appdev/chartexample/LineChartTimeActivity.kt index 160bc0067..b4fe5ef94 100644 --- a/app/src/main/kotlin/info/appdev/chartexample/LineChartTimeActivity.kt +++ b/app/src/main/kotlin/info/appdev/chartexample/LineChartTimeActivity.kt @@ -166,13 +166,13 @@ class LineChartTimeActivity : DemoBase(), OnSeekBarChangeListener { chart!!.data!!.dataSets.forEach { set -> set?.isDrawValues = !set.isDrawValues } - chart!!.invalidate() + chart?.invalidate() } R.id.actionToggleHighlight -> { if (chart!!.data != null) { chart!!.data!!.isHighlightEnabled = !chart!!.data!!.isHighlightEnabled() - chart!!.invalidate() + chart?.invalidate() } } @@ -185,7 +185,7 @@ class LineChartTimeActivity : DemoBase(), OnSeekBarChangeListener { set.setDrawFilled(true) } } - chart!!.invalidate() + chart?.invalidate() } R.id.actionToggleCircles -> { @@ -195,7 +195,7 @@ class LineChartTimeActivity : DemoBase(), OnSeekBarChangeListener { val set = iSet as LineDataSet set.isDrawCirclesEnabled = !set.isDrawCirclesEnabled } - chart!!.invalidate() + chart?.invalidate() } R.id.actionToggleCubic -> { @@ -208,7 +208,7 @@ class LineChartTimeActivity : DemoBase(), OnSeekBarChangeListener { else set.lineMode = LineDataSet.Mode.CUBIC_BEZIER } - chart!!.invalidate() + chart?.invalidate() } R.id.actionToggleStepped -> { @@ -221,19 +221,19 @@ class LineChartTimeActivity : DemoBase(), OnSeekBarChangeListener { else set.lineMode = LineDataSet.Mode.STEPPED } - chart!!.invalidate() + chart?.invalidate() } R.id.actionTogglePinch -> { if (chart!!.isPinchZoomEnabled) chart!!.setPinchZoom(false) else chart!!.setPinchZoom(true) - chart!!.invalidate() + chart?.invalidate() } R.id.actionToggleAutoScaleMinMax -> { chart!!.isAutoScaleMinMaxEnabled = !chart!!.isAutoScaleMinMaxEnabled - chart!!.notifyDataSetChanged() + chart?.notifyDataSetChanged() } R.id.animateX -> { @@ -265,7 +265,7 @@ class LineChartTimeActivity : DemoBase(), OnSeekBarChangeListener { setData(seekBarX!!.progress) // redraw - chart!!.invalidate() + chart?.invalidate() } override fun saveToGallery() { diff --git a/app/src/main/kotlin/info/appdev/chartexample/PerformanceLineChart.kt b/app/src/main/kotlin/info/appdev/chartexample/PerformanceLineChart.kt index 0ff8d20d0..77f5f73e6 100644 --- a/app/src/main/kotlin/info/appdev/chartexample/PerformanceLineChart.kt +++ b/app/src/main/kotlin/info/appdev/chartexample/PerformanceLineChart.kt @@ -53,7 +53,7 @@ class PerformanceLineChart : DemoBase(), OnSeekBarChangeListener { seekBarValues!!.progress = 9000 // don't forget to refresh the drawing - chart!!.invalidate() + chart?.invalidate() } private fun setData(count: Int, range: Float) { @@ -113,7 +113,7 @@ class PerformanceLineChart : DemoBase(), OnSeekBarChangeListener { setData(count, 500f) // redraw - chart!!.invalidate() + chart?.invalidate() } public override fun saveToGallery() { /* Intentionally left empty */ diff --git a/app/src/main/kotlin/info/appdev/chartexample/PieChartActivity.kt b/app/src/main/kotlin/info/appdev/chartexample/PieChartActivity.kt index fc135c24d..649754eac 100644 --- a/app/src/main/kotlin/info/appdev/chartexample/PieChartActivity.kt +++ b/app/src/main/kotlin/info/appdev/chartexample/PieChartActivity.kt @@ -75,7 +75,7 @@ class PieChartActivity : DemoBase(), OnSeekBarChangeListener, OnChartValueSelect chart!!.setDrawCenterText(true) - chart!!.setRotationAngle(0f) + chart!!.rotationAngle = 0f // enable rotation of the chart by touch chart!!.isRotationEnabled = true chart!!.isHighlightPerTapEnabled = true @@ -159,7 +159,7 @@ class PieChartActivity : DemoBase(), OnSeekBarChangeListener, OnChartValueSelect // undo all highlights chart!!.highlightValues(null) - chart!!.invalidate() + chart?.invalidate() } override fun onCreateOptionsMenu(menu: Menu?): Boolean { @@ -180,26 +180,26 @@ class PieChartActivity : DemoBase(), OnSeekBarChangeListener, OnChartValueSelect chart!!.data!!.dataSets.forEach { it?.isDrawValues = !it.isDrawValues } - chart!!.invalidate() + chart?.invalidate() } R.id.actionToggleIcons -> { for (set in chart!!.data!!.getDataSets()) set?.isDrawIcons = !set.isDrawIcons - chart!!.invalidate() + chart?.invalidate() } R.id.actionToggleHole -> { chart!!.isDrawHoleEnabled = !chart!!.isDrawHoleEnabled - chart!!.invalidate() + chart?.invalidate() } R.id.actionToggleMinAngles -> { if (chart!!.minAngleForSlices == 0f) chart!!.setMinAngleForSlices(36f) else chart!!.setMinAngleForSlices(0f) - chart!!.notifyDataSetChanged() - chart!!.invalidate() + chart?.notifyDataSetChanged() + chart?.invalidate() } R.id.actionToggleCurvedSlices -> { @@ -211,22 +211,22 @@ class PieChartActivity : DemoBase(), OnSeekBarChangeListener, OnChartValueSelect if (toSet && chart!!.isDrawSlicesUnderHoleEnabled) { chart!!.setDrawSlicesUnderHole(false) } - chart!!.invalidate() + chart?.invalidate() } R.id.actionDrawCenter -> { chart!!.setDrawCenterText(!chart!!.isDrawCenterTextEnabled) - chart!!.invalidate() + chart?.invalidate() } R.id.actionToggleXValues -> { chart!!.setDrawEntryLabels(!chart!!.isDrawEntryLabelsEnabled) - chart!!.invalidate() + chart?.invalidate() } R.id.actionTogglePercent -> { chart!!.setUsePercentValues(!chart!!.isUsePercentValuesEnabled) - chart!!.invalidate() + chart?.invalidate() } R.id.animateX -> { diff --git a/app/src/main/kotlin/info/appdev/chartexample/PieChartRoundedActivity.kt b/app/src/main/kotlin/info/appdev/chartexample/PieChartRoundedActivity.kt index bbf691e50..516cf7812 100644 --- a/app/src/main/kotlin/info/appdev/chartexample/PieChartRoundedActivity.kt +++ b/app/src/main/kotlin/info/appdev/chartexample/PieChartRoundedActivity.kt @@ -77,7 +77,7 @@ class PieChartRoundedActivity : DemoBase(), OnSeekBarChangeListener, OnChartValu chart!!.setDrawCenterText(true) - chart!!.setRotationAngle(0f) + chart!!.rotationAngle = 0f // enable rotation of the chart by touch chart!!.isRotationEnabled = true chart!!.isHighlightPerTapEnabled = true @@ -165,7 +165,7 @@ class PieChartRoundedActivity : DemoBase(), OnSeekBarChangeListener, OnChartValu renderer.roundedCornerRadius = 30f dataSet.sliceSpace = renderer.roundedCornerRadius / 2 - chart!!.invalidate() + chart?.invalidate() } override fun onCreateOptionsMenu(menu: Menu?): Boolean { @@ -185,26 +185,26 @@ class PieChartRoundedActivity : DemoBase(), OnSeekBarChangeListener, OnChartValu chart!!.data!!.dataSets.forEach { it?.isDrawValues = !it.isDrawValues } - chart!!.invalidate() + chart?.invalidate() } R.id.actionToggleIcons -> { for (set in chart!!.data!!.getDataSets()) set?.isDrawIcons = !set.isDrawIcons - chart!!.invalidate() + chart?.invalidate() } R.id.actionToggleHole -> { chart!!.isDrawHoleEnabled = !chart!!.isDrawHoleEnabled - chart!!.invalidate() + chart?.invalidate() } R.id.actionToggleMinAngles -> { if (chart!!.minAngleForSlices == 0f) chart!!.setMinAngleForSlices(36f) else chart!!.setMinAngleForSlices(0f) - chart!!.notifyDataSetChanged() - chart!!.invalidate() + chart?.notifyDataSetChanged() + chart?.invalidate() } R.id.actionToggleCurvedSlices -> { @@ -216,22 +216,22 @@ class PieChartRoundedActivity : DemoBase(), OnSeekBarChangeListener, OnChartValu if (toSet && chart!!.isDrawSlicesUnderHoleEnabled) { chart!!.setDrawSlicesUnderHole(false) } - chart!!.invalidate() + chart?.invalidate() } R.id.actionDrawCenter -> { chart!!.setDrawCenterText(!chart!!.isDrawCenterTextEnabled) - chart!!.invalidate() + chart?.invalidate() } R.id.actionToggleXValues -> { chart!!.setDrawEntryLabels(!chart!!.isDrawEntryLabelsEnabled) - chart!!.invalidate() + chart?.invalidate() } R.id.actionTogglePercent -> { chart!!.setUsePercentValues(!chart!!.isUsePercentValuesEnabled) - chart!!.invalidate() + chart?.invalidate() } R.id.animateX -> { diff --git a/app/src/main/kotlin/info/appdev/chartexample/PiePolylineChartActivity.kt b/app/src/main/kotlin/info/appdev/chartexample/PiePolylineChartActivity.kt index a850c6ea6..94eaba0ad 100644 --- a/app/src/main/kotlin/info/appdev/chartexample/PiePolylineChartActivity.kt +++ b/app/src/main/kotlin/info/appdev/chartexample/PiePolylineChartActivity.kt @@ -79,7 +79,7 @@ class PiePolylineChartActivity : DemoBase(), OnSeekBarChangeListener, OnChartVal chart!!.setDrawCenterText(true) - chart!!.setRotationAngle(0f) + chart!!.rotationAngle = 0f // enable rotation of the chart by touch chart!!.isRotationEnabled = true chart!!.isHighlightPerTapEnabled = true @@ -154,7 +154,7 @@ class PiePolylineChartActivity : DemoBase(), OnSeekBarChangeListener, OnChartVal // undo all highlights chart!!.highlightValues(null) - chart!!.invalidate() + chart?.invalidate() } override fun onCreateOptionsMenu(menu: Menu?): Boolean { @@ -174,19 +174,19 @@ class PiePolylineChartActivity : DemoBase(), OnSeekBarChangeListener, OnChartVal chart!!.data!!.dataSets.forEach { it?.isDrawValues = !it.isDrawValues } - chart!!.invalidate() + chart?.invalidate() } R.id.actionToggleHole -> { chart!!.isDrawHoleEnabled = !chart!!.isDrawHoleEnabled - chart!!.invalidate() + chart?.invalidate() } R.id.actionToggleMinAngles -> { if (chart!!.minAngleForSlices == 0f) chart!!.setMinAngleForSlices(36f) else chart!!.setMinAngleForSlices(0f) - chart!!.notifyDataSetChanged() - chart!!.invalidate() + chart?.notifyDataSetChanged() + chart?.invalidate() } R.id.actionToggleCurvedSlices -> { @@ -198,22 +198,22 @@ class PiePolylineChartActivity : DemoBase(), OnSeekBarChangeListener, OnChartVal if (toSet && chart!!.isDrawSlicesUnderHoleEnabled) { chart!!.setDrawSlicesUnderHole(false) } - chart!!.invalidate() + chart?.invalidate() } R.id.actionDrawCenter -> { chart!!.setDrawCenterText(!chart!!.isDrawCenterTextEnabled) - chart!!.invalidate() + chart?.invalidate() } R.id.actionToggleXValues -> { chart!!.setDrawEntryLabels(!chart!!.isDrawEntryLabelsEnabled) - chart!!.invalidate() + chart?.invalidate() } R.id.actionTogglePercent -> { chart!!.setUsePercentValues(!chart!!.isUsePercentValuesEnabled) - chart!!.invalidate() + chart?.invalidate() } R.id.animateX -> { diff --git a/app/src/main/kotlin/info/appdev/chartexample/RadarChartActivity.kt b/app/src/main/kotlin/info/appdev/chartexample/RadarChartActivity.kt index eccadb908..d72faf595 100644 --- a/app/src/main/kotlin/info/appdev/chartexample/RadarChartActivity.kt +++ b/app/src/main/kotlin/info/appdev/chartexample/RadarChartActivity.kt @@ -139,7 +139,7 @@ class RadarChartActivity : DemoBase() { colorList.add(Color.rgb(240, 255, 240)) colorList.add(Color.rgb(250, 255, 250)) chart!!.setLayerColorList(colorList) - chart!!.invalidate() + chart?.invalidate() } override fun onCreateOptionsMenu(menu: Menu?): Boolean { @@ -160,44 +160,44 @@ class RadarChartActivity : DemoBase() { it?.isDrawValues = !it.isDrawValues } - chart!!.invalidate() + chart?.invalidate() } R.id.actionToggleHighlight -> { if (chart!!.data != null) { chart!!.data!!.isHighlightEnabled = !chart!!.data!!.isHighlightEnabled() - chart!!.invalidate() + chart?.invalidate() } } R.id.actionToggleRotate -> { chart!!.isRotationEnabled = !chart!!.isRotationEnabled - chart!!.invalidate() + chart?.invalidate() } R.id.actionToggleFilled -> { chart!!.data!!.dataSets.forEach { set -> set?.setDrawFilled(!set.isDrawFilledEnabled) } - chart!!.invalidate() + chart?.invalidate() } R.id.actionToggleHighlightCircle -> { chart!!.data!!.dataSets.forEach { set -> set.isDrawHighlightCircleEnabled = !set.isDrawHighlightCircleEnabled } - chart!!.invalidate() + chart?.invalidate() } R.id.actionToggleXLabels -> { chart!!.xAxis.isEnabled = !chart!!.xAxis.isEnabled - chart!!.notifyDataSetChanged() - chart!!.invalidate() + chart?.notifyDataSetChanged() + chart?.invalidate() } R.id.actionToggleYLabels -> { chart!!.yAxis.isEnabled = !chart!!.yAxis.isEnabled - chart!!.invalidate() + chart?.invalidate() } R.id.animateX -> { diff --git a/app/src/main/kotlin/info/appdev/chartexample/RealtimeLineChartActivity.kt b/app/src/main/kotlin/info/appdev/chartexample/RealtimeLineChartActivity.kt index fa6435822..37ab52739 100644 --- a/app/src/main/kotlin/info/appdev/chartexample/RealtimeLineChartActivity.kt +++ b/app/src/main/kotlin/info/appdev/chartexample/RealtimeLineChartActivity.kt @@ -100,7 +100,7 @@ class RealtimeLineChartActivity : DemoBase(), OnChartValueSelectedListener { data.notifyDataChanged() // let the chart know it's data has changed - chart!!.notifyDataSetChanged() + chart?.notifyDataSetChanged() // limit the number of visible entries chart!!.setVisibleXRangeMaximum(120f) diff --git a/app/src/main/kotlin/info/appdev/chartexample/ScatterChartActivity.kt b/app/src/main/kotlin/info/appdev/chartexample/ScatterChartActivity.kt index 9641236e1..74f06d356 100644 --- a/app/src/main/kotlin/info/appdev/chartexample/ScatterChartActivity.kt +++ b/app/src/main/kotlin/info/appdev/chartexample/ScatterChartActivity.kt @@ -133,7 +133,7 @@ class ScatterChartActivity : DemoBase(), OnSeekBarChangeListener, OnChartValueSe data.setValueTypeface(tfLight) chart!!.setData(data) - chart!!.invalidate() + chart?.invalidate() } override fun onCreateOptionsMenu(menu: Menu?): Boolean { @@ -153,25 +153,25 @@ class ScatterChartActivity : DemoBase(), OnSeekBarChangeListener, OnChartValueSe chart!!.data!!.dataSets.forEach { set -> set?.isDrawValues = !set.isDrawValues } - chart!!.invalidate() + chart?.invalidate() } R.id.actionToggleHighlight -> { if (chart!!.data != null) { chart!!.data!!.isHighlightEnabled = !chart!!.data!!.isHighlightEnabled() - chart!!.invalidate() + chart?.invalidate() } } R.id.actionTogglePinch -> { chart!!.setPinchZoom(!chart!!.isPinchZoomEnabled) - chart!!.invalidate() + chart?.invalidate() } R.id.actionToggleAutoScaleMinMax -> { chart!!.isAutoScaleMinMaxEnabled = !chart!!.isAutoScaleMinMaxEnabled - chart!!.notifyDataSetChanged() + chart?.notifyDataSetChanged() } R.id.animateX -> { diff --git a/app/src/main/kotlin/info/appdev/chartexample/ScrollViewActivity.kt b/app/src/main/kotlin/info/appdev/chartexample/ScrollViewActivity.kt index 1c51ba1c5..dfde6c005 100644 --- a/app/src/main/kotlin/info/appdev/chartexample/ScrollViewActivity.kt +++ b/app/src/main/kotlin/info/appdev/chartexample/ScrollViewActivity.kt @@ -59,7 +59,7 @@ class ScrollViewActivity : DemoBase() { val data = BarData(set) chart!!.setData(data) - chart!!.invalidate() + chart?.invalidate() chart!!.animateY(800) } diff --git a/app/src/main/kotlin/info/appdev/chartexample/StackedBarActivity.kt b/app/src/main/kotlin/info/appdev/chartexample/StackedBarActivity.kt index fa05b1968..430a952aa 100644 --- a/app/src/main/kotlin/info/appdev/chartexample/StackedBarActivity.kt +++ b/app/src/main/kotlin/info/appdev/chartexample/StackedBarActivity.kt @@ -125,7 +125,7 @@ class StackedBarActivity : DemoBase(), OnSeekBarChangeListener, OnChartValueSele set1 = chart!!.data!!.getDataSetByIndex(0) as BarDataSet set1.entries = values chart!!.data!!.notifyDataChanged() - chart!!.notifyDataSetChanged() + chart?.notifyDataSetChanged() } else { set1 = BarDataSet(values, "Statistics Vienna 2014") set1.isDrawIcons = false @@ -143,7 +143,7 @@ class StackedBarActivity : DemoBase(), OnSeekBarChangeListener, OnChartValueSele } chart!!.setFitBars(true) - chart!!.invalidate() + chart?.invalidate() } override fun onCreateOptionsMenu(menu: Menu?): Boolean { @@ -163,38 +163,38 @@ class StackedBarActivity : DemoBase(), OnSeekBarChangeListener, OnChartValueSele chart!!.data!!.dataSets.forEach { it?.isDrawValues = !it.isDrawValues } - chart!!.invalidate() + chart?.invalidate() } R.id.actionToggleIcons -> { chart!!.data!!.dataSets.forEach { set -> set?.isDrawIcons = !set.isDrawIcons } - chart!!.invalidate() + chart?.invalidate() } R.id.actionToggleHighlight -> { if (chart!!.data != null) { chart!!.data!!.isHighlightEnabled = !chart!!.data!!.isHighlightEnabled() - chart!!.invalidate() + chart?.invalidate() } } R.id.actionTogglePinch -> { chart!!.setPinchZoom(!chart!!.isPinchZoomEnabled) - chart!!.invalidate() + chart?.invalidate() } R.id.actionToggleAutoScaleMinMax -> { chart!!.isAutoScaleMinMaxEnabled = !chart!!.isAutoScaleMinMaxEnabled - chart!!.notifyDataSetChanged() + chart?.notifyDataSetChanged() } R.id.actionToggleBarBorders -> { for (set in chart!!.data!!.dataSets) (set as BarDataSet).barBorderWidth = if (set.barBorderWidth == 1f) 0f else 1f - chart!!.invalidate() + chart?.invalidate() } R.id.animateX -> { diff --git a/app/src/main/kotlin/info/appdev/chartexample/StackedBarActivityNegative.kt b/app/src/main/kotlin/info/appdev/chartexample/StackedBarActivityNegative.kt index baf8dd1a0..e3d1d74b5 100644 --- a/app/src/main/kotlin/info/appdev/chartexample/StackedBarActivityNegative.kt +++ b/app/src/main/kotlin/info/appdev/chartexample/StackedBarActivityNegative.kt @@ -110,7 +110,7 @@ class StackedBarActivityNegative : DemoBase(), OnChartValueSelectedListener { val data = BarData(set) data.barWidth = 8.5f chart!!.setData(data) - chart!!.invalidate() + chart?.invalidate() } override fun onCreateOptionsMenu(menu: Menu?): Boolean { @@ -131,7 +131,7 @@ class StackedBarActivityNegative : DemoBase(), OnChartValueSelectedListener { chart!!.data!!.dataSets.forEach { it?.isDrawValues = !it.isDrawValues } - chart!!.invalidate() + chart?.invalidate() } R.id.actionToggleIcons -> { @@ -139,13 +139,13 @@ class StackedBarActivityNegative : DemoBase(), OnChartValueSelectedListener { set?.isDrawIcons = !set.isDrawIcons } - chart!!.invalidate() + chart?.invalidate() } R.id.actionToggleHighlight -> { if (chart!!.data != null) { chart!!.data!!.isHighlightEnabled = !chart!!.data!!.isHighlightEnabled() - chart!!.invalidate() + chart?.invalidate() } } @@ -156,12 +156,12 @@ class StackedBarActivityNegative : DemoBase(), OnChartValueSelectedListener { chart!!.setPinchZoom(true) } - chart!!.invalidate() + chart?.invalidate() } R.id.actionToggleAutoScaleMinMax -> { chart!!.isAutoScaleMinMaxEnabled = !chart!!.isAutoScaleMinMaxEnabled - chart!!.notifyDataSetChanged() + chart?.notifyDataSetChanged() } R.id.actionToggleBarBorders -> { @@ -169,7 +169,7 @@ class StackedBarActivityNegative : DemoBase(), OnChartValueSelectedListener { (set as BarDataSet).barBorderWidth = if (set.barBorderWidth == 1f) 0f else 1f } - chart!!.invalidate() + chart?.invalidate() } R.id.animateX -> { diff --git a/app/src/main/kotlin/info/appdev/chartexample/listviewitems/PieChartItem.kt b/app/src/main/kotlin/info/appdev/chartexample/listviewitems/PieChartItem.kt index 3a8fad1fd..2475f049e 100644 --- a/app/src/main/kotlin/info/appdev/chartexample/listviewitems/PieChartItem.kt +++ b/app/src/main/kotlin/info/appdev/chartexample/listviewitems/PieChartItem.kt @@ -64,17 +64,18 @@ class PieChartItem(cd: ChartData<*>, c: Context) : ChartItem(cd) { // set data holder.chart!!.setData(chartData as PieData?) - val l = holder.chart!!.legend - l.verticalAlignment = Legend.LegendVerticalAlignment.TOP - l.horizontalAlignment = Legend.LegendHorizontalAlignment.RIGHT - l.orientation = Legend.LegendOrientation.VERTICAL - l.setDrawInside(false) - l.yEntrySpace = 0f - l.yOffset = 0f + holder.chart?.legend?.apply { + verticalAlignment = Legend.LegendVerticalAlignment.TOP + horizontalAlignment = Legend.LegendHorizontalAlignment.RIGHT + orientation = Legend.LegendOrientation.VERTICAL + setDrawInside(false) + yEntrySpace = 0f + yOffset = 0f + } // do not forget to refresh the chart // holder.chart.invalidate(); - holder.chart!!.animateY(900) + holder.chart?.animateY(900) return convertView }