From 8c048cea619dbc38edba06dec9f5423ea8d50497 Mon Sep 17 00:00:00 2001 From: Hannes Achleitner Date: Tue, 23 Dec 2025 10:41:05 +0100 Subject: [PATCH] Kotlin CombinedData --- .../mikephil/charting/data/CombinedData.java | 268 ------------------ .../mikephil/charting/data/CombinedData.kt | 213 ++++++++++++++ .../charting/highlight/CombinedHighlighter.kt | 2 +- 3 files changed, 214 insertions(+), 269 deletions(-) delete mode 100644 MPChartLib/src/main/java/com/github/mikephil/charting/data/CombinedData.java create mode 100644 MPChartLib/src/main/java/com/github/mikephil/charting/data/CombinedData.kt diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/data/CombinedData.java b/MPChartLib/src/main/java/com/github/mikephil/charting/data/CombinedData.java deleted file mode 100644 index c059c47c0..000000000 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/data/CombinedData.java +++ /dev/null @@ -1,268 +0,0 @@ - -package com.github.mikephil.charting.data; - -import android.util.Log; - -import com.github.mikephil.charting.components.YAxis; -import com.github.mikephil.charting.highlight.Highlight; -import com.github.mikephil.charting.interfaces.datasets.IBarLineScatterCandleBubbleDataSet; - -import java.util.ArrayList; -import java.util.List; - -/** - * Data object that allows the combination of Line-, Bar-, Scatter-, Bubble- and - * CandleData. Used in the CombinedChart class. - * - * @author Philipp Jahoda - */ -public class CombinedData extends BarLineScatterCandleBubbleData> { - - private LineData mLineData; - private BarData mBarData; - private ScatterData mScatterData; - private CandleData mCandleData; - private BubbleData mBubbleData; - - public CombinedData() { - super(); - } - - public void setData(LineData data) { - mLineData = data; - notifyDataChanged(); - } - - public void setData(BarData data) { - mBarData = data; - notifyDataChanged(); - } - - public void setData(ScatterData data) { - mScatterData = data; - notifyDataChanged(); - } - - public void setData(CandleData data) { - mCandleData = data; - notifyDataChanged(); - } - - public void setData(BubbleData data) { - mBubbleData = data; - notifyDataChanged(); - } - - @Override - public void calcMinMax() { - - if(getDataSets() == null){ - setDataSets(new ArrayList<>()); - } - getDataSets().clear(); - - setYMax(-Float.MAX_VALUE); - setYMin(Float.MAX_VALUE); - setXMax(-Float.MAX_VALUE); - setXMin(Float.MAX_VALUE); - - mLeftAxisMax = -Float.MAX_VALUE; - mLeftAxisMin = Float.MAX_VALUE; - mRightAxisMax = -Float.MAX_VALUE; - mRightAxisMin = Float.MAX_VALUE; - - List allData = getAllData(); - - for (ChartData data : allData) { - - data.calcMinMax(); - - List> sets = data.getDataSets(); - getDataSets().addAll(sets); - - if (data.getYMax() > getYMax()) - setYMax(data.getYMax()); - - if (data.getYMin() < getYMin()) - setYMin(data.getYMin()); - - if (data.getXMax() > getXMax()) - setXMax(data.getXMax()); - - if (data.getXMin() < getXMin()) - setXMin(data.getXMin()); - - for (IBarLineScatterCandleBubbleDataSet dataset : sets) { - if (dataset.getAxisDependency() == YAxis.AxisDependency.LEFT) { - if (dataset.getYMax() > mLeftAxisMax) { - mLeftAxisMax = dataset.getYMax(); - } - - if (dataset.getYMin() < mLeftAxisMin) { - mLeftAxisMin = dataset.getYMin(); - } - } - else { - if (dataset.getYMax() > mRightAxisMax) { - mRightAxisMax = dataset.getYMax(); - } - - if (dataset.getYMin() < mRightAxisMin) { - mRightAxisMin = dataset.getYMin(); - } - } - } - } - } - - public BubbleData getBubbleData() { - return mBubbleData; - } - - public LineData getLineData() { - return mLineData; - } - - public BarData getBarData() { - return mBarData; - } - - public ScatterData getScatterData() { - return mScatterData; - } - - public CandleData getCandleData() { - return mCandleData; - } - - /** - * Returns all data objects in row: line-bar-scatter-candle-bubble if not null. - */ - public List getAllData() { - - List data = new ArrayList<>(); - if (mLineData != null) - data.add(mLineData); - if (mBarData != null) - data.add(mBarData); - if (mScatterData != null) - data.add(mScatterData); - if (mCandleData != null) - data.add(mCandleData); - if (mBubbleData != null) - data.add(mBubbleData); - - return data; - } - - public BarLineScatterCandleBubbleData getDataByIndex(int index) { - return getAllData().get(index); - } - - @Override - public void notifyDataChanged() { - if (mLineData != null) - mLineData.notifyDataChanged(); - if (mBarData != null) - mBarData.notifyDataChanged(); - if (mCandleData != null) - mCandleData.notifyDataChanged(); - if (mScatterData != null) - mScatterData.notifyDataChanged(); - if (mBubbleData != null) - mBubbleData.notifyDataChanged(); - - calcMinMax(); // recalculate everything - } - - /** - * Get the Entry for a corresponding highlight object - * @return the entry that is highlighted - */ - @Override - public Entry getEntryForHighlight(Highlight highlight) { - - if (highlight.getDataIndex() >= getAllData().size()) - return null; - - ChartData data = getDataByIndex(highlight.getDataIndex()); - - if (highlight.getDataSetIndex() >= data.getDataSetCount()) - return null; - - // The value of the highlighted entry could be NaN - - // if we are not interested in highlighting a specific value. - - List entries = data.getDataSetByIndex(highlight.getDataSetIndex()) - .getEntriesForXValue(highlight.getX()); - for (Entry entry : entries) - if (entry.getY() == highlight.getY() || - Float.isNaN(highlight.getY())) - return entry; - - return null; - } - - /** - * Get dataset for highlight - * - * @param highlight current highlight - * @return dataset related to highlight - */ - public IBarLineScatterCandleBubbleDataSet getDataSetByHighlight(Highlight highlight) { - if (highlight.getDataIndex() >= getAllData().size()) - return null; - - BarLineScatterCandleBubbleData data = getDataByIndex(highlight.getDataIndex()); - - if (highlight.getDataSetIndex() >= data.getDataSetCount()) - return null; - - return (IBarLineScatterCandleBubbleDataSet) - data.getDataSets().get(highlight.getDataSetIndex()); - } - - public int getDataIndex(ChartData data) { - return getAllData().indexOf(data); - } - - @Override - public boolean removeDataSet(IBarLineScatterCandleBubbleDataSet d) { - - List datas = getAllData(); - - boolean success = false; - - for (ChartData data : datas) { - - success = data.removeDataSet(d); - - if (success) { - break; - } - } - - return success; - } - - @Deprecated - @Override - public boolean removeDataSet(int index) { - Log.e("AndroidChart", "removeDataSet(int index) not supported for CombinedData"); - return false; - } - - @Deprecated - @Override - public boolean removeEntry(Entry entry, int dataSetIndex) { - Log.e("AndroidChart", "removeEntry(...) not supported for CombinedData"); - return false; - } - - @Deprecated - @Override - public boolean removeEntry(float xValue, int dataSetIndex) { - Log.e("AndroidChart", "removeEntry(...) not supported for CombinedData"); - return false; - } -} diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/data/CombinedData.kt b/MPChartLib/src/main/java/com/github/mikephil/charting/data/CombinedData.kt new file mode 100644 index 000000000..6892ea07a --- /dev/null +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/data/CombinedData.kt @@ -0,0 +1,213 @@ +package com.github.mikephil.charting.data + +import android.util.Log +import com.github.mikephil.charting.components.YAxis +import com.github.mikephil.charting.highlight.Highlight +import com.github.mikephil.charting.interfaces.datasets.IBarLineScatterCandleBubbleDataSet +import java.lang.Float +import kotlin.Boolean +import kotlin.Deprecated +import kotlin.Int + +/** + * Data object that allows the combination of Line-, Bar-, Scatter-, Bubble- and + * CandleData. Used in the CombinedChart class. + * + * @author Philipp Jahoda + */ +class CombinedData : BarLineScatterCandleBubbleData>() { + var lineData: LineData? = null + private set + var barData: BarData? = null + private set + var scatterData: ScatterData? = null + private set + var candleData: CandleData? = null + private set + var bubbleData: BubbleData? = null + private set + + fun setData(data: LineData?) { + this.lineData = data + notifyDataChanged() + } + + fun setData(data: BarData?) { + this.barData = data + notifyDataChanged() + } + + fun setData(data: ScatterData?) { + this.scatterData = data + notifyDataChanged() + } + + fun setData(data: CandleData?) { + this.candleData = data + notifyDataChanged() + } + + fun setData(data: BubbleData?) { + this.bubbleData = data + notifyDataChanged() + } + + override fun calcMinMax() { + if (dataSets == null) { + dataSets = ArrayList>() + } + dataSets!!.clear() + + yMax = -Float.MAX_VALUE + yMin = Float.MAX_VALUE + xMax = -Float.MAX_VALUE + xMin = Float.MAX_VALUE + + mLeftAxisMax = -Float.MAX_VALUE + mLeftAxisMin = Float.MAX_VALUE + mRightAxisMax = -Float.MAX_VALUE + mRightAxisMin = Float.MAX_VALUE + + val allData = this.allData + + for (data in allData) { + data.calcMinMax() + + val sets = data.dataSets + dataSets!!.addAll(sets!!) + + if (data.yMax > yMax) yMax = data.yMax + + if (data.yMin < yMin) yMin = data.yMin + + if (data.xMax > xMax) xMax = data.xMax + + if (data.xMin < xMin) xMin = data.xMin + + for (dataset in sets) { + if (dataset.axisDependency == YAxis.AxisDependency.LEFT) { + if (dataset.yMax > mLeftAxisMax) { + mLeftAxisMax = dataset.yMax + } + + if (dataset.yMin < mLeftAxisMin) { + mLeftAxisMin = dataset.yMin + } + } else { + if (dataset.yMax > mRightAxisMax) { + mRightAxisMax = dataset.yMax + } + + if (dataset.yMin < mRightAxisMin) { + mRightAxisMin = dataset.yMin + } + } + } + } + } + + val allData: MutableList> + /** + * Returns all data objects in row: line-bar-scatter-candle-bubble if not null. + */ + get() { + val data: MutableList> = ArrayList>() + if (this.lineData != null) data.add(this.lineData!!) + if (this.barData != null) data.add(this.barData!!) + if (this.scatterData != null) data.add(this.scatterData!!) + if (this.candleData != null) data.add(this.candleData!!) + if (this.bubbleData != null) data.add(this.bubbleData!!) + + return data + } + + fun getDataByIndex(index: Int): BarLineScatterCandleBubbleData<*> { + return this.allData.get(index) + } + + override fun notifyDataChanged() { + if (this.lineData != null) lineData!!.notifyDataChanged() + if (this.barData != null) barData!!.notifyDataChanged() + if (this.candleData != null) candleData!!.notifyDataChanged() + if (this.scatterData != null) scatterData!!.notifyDataChanged() + if (this.bubbleData != null) bubbleData!!.notifyDataChanged() + + calcMinMax() // recalculate everything + } + + /** + * Get the Entry for a corresponding highlight object + * @return the entry that is highlighted + */ + override fun getEntryForHighlight(highlight: Highlight): Entry? { + if (highlight.dataIndex >= this.allData.size) return null + + val data: ChartData<*> = getDataByIndex(highlight.dataIndex) + + if (highlight.dataSetIndex >= data.dataSetCount) return null + + // The value of the highlighted entry could be NaN - + // if we are not interested in highlighting a specific value. + val entries = data.getDataSetByIndex(highlight.dataSetIndex)!! + .getEntriesForXValue(highlight.x) + for (entry in entries!!) if (entry.y == highlight.y || + Float.isNaN(highlight.y) + ) return entry + + return null + } + + /** + * Get dataset for highlight + * + * @param highlight current highlight + * @return dataset related to highlight + */ + fun getDataSetByHighlight(highlight: Highlight): IBarLineScatterCandleBubbleDataSet? { + if (highlight.dataIndex >= this.allData.size) return null + + val data = getDataByIndex(highlight.dataIndex) + + if (highlight.dataSetIndex >= data.dataSetCount) return null + + return data.dataSets!!.get(highlight.dataSetIndex) as IBarLineScatterCandleBubbleDataSet? + } + + fun getDataIndex(data: ChartData<*>?): Int { + return this.allData.indexOf(data) + } + + override fun removeDataSet(d: IBarLineScatterCandleBubbleDataSet?): Boolean { + val datas = this.allData + + var success = false + + for (data in datas) { + success = data.removeDataSet(d) + + if (success) { + break + } + } + + return success + } + + @Deprecated("") + override fun removeDataSet(index: Int): Boolean { + Log.e("AndroidChart", "removeDataSet(int index) not supported for CombinedData") + return false + } + + @Deprecated("") + override fun removeEntry(entry: Entry?, dataSetIndex: Int): Boolean { + Log.e("AndroidChart", "removeEntry(...) not supported for CombinedData") + return false + } + + @Deprecated("") + override fun removeEntry(xValue: kotlin.Float, dataSetIndex: Int): Boolean { + Log.e("AndroidChart", "removeEntry(...) not supported for CombinedData") + return false + } +} diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/highlight/CombinedHighlighter.kt b/MPChartLib/src/main/java/com/github/mikephil/charting/highlight/CombinedHighlighter.kt index d8b425356..ee62fa89e 100644 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/highlight/CombinedHighlighter.kt +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/highlight/CombinedHighlighter.kt @@ -21,7 +21,7 @@ open class CombinedHighlighter(dataProvider: CombinedDataProvider, barChart: Bar override fun getHighlightsAtXValue(xVal: Float, x: Float, y: Float): MutableList? { highlightBuffer.clear() - val dataObjects = provider.combinedData!!.getAllData() + val dataObjects = provider.combinedData!!.allData for (i in dataObjects.indices) { val dataObject: ChartData<*> = dataObjects[i]