• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 
2 package com.github.mikephil.charting.data;
3 
4 import android.util.Log;
5 
6 import com.github.mikephil.charting.components.YAxis;
7 import com.github.mikephil.charting.highlight.Highlight;
8 import com.github.mikephil.charting.interfaces.datasets.IBarLineScatterCandleBubbleDataSet;
9 
10 import java.util.ArrayList;
11 import java.util.List;
12 
13 /**
14  * Data object that allows the combination of Line-, Bar-, Scatter-, Bubble- and
15  * CandleData. Used in the CombinedChart class.
16  *
17  * @author Philipp Jahoda
18  */
19 public class CombinedData extends BarLineScatterCandleBubbleData<IBarLineScatterCandleBubbleDataSet<? extends Entry>> {
20 
21     private LineData mLineData;
22     private BarData mBarData;
23     private ScatterData mScatterData;
24     private CandleData mCandleData;
25     private BubbleData mBubbleData;
26 
CombinedData()27     public CombinedData() {
28         super();
29     }
30 
setData(LineData data)31     public void setData(LineData data) {
32         mLineData = data;
33         notifyDataChanged();
34     }
35 
setData(BarData data)36     public void setData(BarData data) {
37         mBarData = data;
38         notifyDataChanged();
39     }
40 
setData(ScatterData data)41     public void setData(ScatterData data) {
42         mScatterData = data;
43         notifyDataChanged();
44     }
45 
setData(CandleData data)46     public void setData(CandleData data) {
47         mCandleData = data;
48         notifyDataChanged();
49     }
50 
setData(BubbleData data)51     public void setData(BubbleData data) {
52         mBubbleData = data;
53         notifyDataChanged();
54     }
55 
56     @Override
calcMinMax()57     public void calcMinMax() {
58 
59         if(mDataSets == null){
60             mDataSets = new ArrayList<>();
61         }
62         mDataSets.clear();
63 
64         mYMax = -Float.MAX_VALUE;
65         mYMin = Float.MAX_VALUE;
66         mXMax = -Float.MAX_VALUE;
67         mXMin = Float.MAX_VALUE;
68 
69         mLeftAxisMax = -Float.MAX_VALUE;
70         mLeftAxisMin = Float.MAX_VALUE;
71         mRightAxisMax = -Float.MAX_VALUE;
72         mRightAxisMin = Float.MAX_VALUE;
73 
74         List<BarLineScatterCandleBubbleData> allData = getAllData();
75 
76         for (ChartData data : allData) {
77 
78             data.calcMinMax();
79 
80             List<IBarLineScatterCandleBubbleDataSet<? extends Entry>> sets = data.getDataSets();
81             mDataSets.addAll(sets);
82 
83             if (data.getYMax() > mYMax)
84                 mYMax = data.getYMax();
85 
86             if (data.getYMin() < mYMin)
87                 mYMin = data.getYMin();
88 
89             if (data.getXMax() > mXMax)
90                 mXMax = data.getXMax();
91 
92             if (data.getXMin() < mXMin)
93                 mXMin = data.getXMin();
94 
95             for (IBarLineScatterCandleBubbleDataSet<? extends Entry> dataset : sets) {
96                 if (dataset.getAxisDependency() == YAxis.AxisDependency.LEFT)  {
97                     if (dataset.getYMax() > mLeftAxisMax) {
98                         mLeftAxisMax = dataset.getYMax();
99                     }
100 
101                     if (dataset.getYMin() < mLeftAxisMin) {
102                         mLeftAxisMin = dataset.getYMin();
103                     }
104                 }
105                 else {
106                     if (dataset.getYMax() > mRightAxisMax) {
107                         mRightAxisMax = dataset.getYMax();
108                     }
109 
110                     if (dataset.getYMin() < mRightAxisMin) {
111                         mRightAxisMin = dataset.getYMin();
112                     }
113                 }
114             }
115         }
116     }
117 
getBubbleData()118     public BubbleData getBubbleData() {
119         return mBubbleData;
120     }
121 
getLineData()122     public LineData getLineData() {
123         return mLineData;
124     }
125 
getBarData()126     public BarData getBarData() {
127         return mBarData;
128     }
129 
getScatterData()130     public ScatterData getScatterData() {
131         return mScatterData;
132     }
133 
getCandleData()134     public CandleData getCandleData() {
135         return mCandleData;
136     }
137 
138     /**
139      * Returns all data objects in row: line-bar-scatter-candle-bubble if not null.
140      *
141      * @return
142      */
getAllData()143     public List<BarLineScatterCandleBubbleData> getAllData() {
144 
145         List<BarLineScatterCandleBubbleData> data = new ArrayList<BarLineScatterCandleBubbleData>();
146         if (mLineData != null)
147             data.add(mLineData);
148         if (mBarData != null)
149             data.add(mBarData);
150         if (mScatterData != null)
151             data.add(mScatterData);
152         if (mCandleData != null)
153             data.add(mCandleData);
154         if (mBubbleData != null)
155             data.add(mBubbleData);
156 
157         return data;
158     }
159 
getDataByIndex(int index)160     public BarLineScatterCandleBubbleData getDataByIndex(int index) {
161         return getAllData().get(index);
162     }
163 
164     @Override
notifyDataChanged()165     public void notifyDataChanged() {
166         if (mLineData != null)
167             mLineData.notifyDataChanged();
168         if (mBarData != null)
169             mBarData.notifyDataChanged();
170         if (mCandleData != null)
171             mCandleData.notifyDataChanged();
172         if (mScatterData != null)
173             mScatterData.notifyDataChanged();
174         if (mBubbleData != null)
175             mBubbleData.notifyDataChanged();
176 
177         calcMinMax(); // recalculate everything
178     }
179 
180     /**
181      * Get the Entry for a corresponding highlight object
182      *
183      * @param highlight
184      * @return the entry that is highlighted
185      */
186     @Override
getEntryForHighlight(Highlight highlight)187     public Entry getEntryForHighlight(Highlight highlight) {
188 
189         if (highlight.getDataIndex() >= getAllData().size())
190             return null;
191 
192         ChartData data = getDataByIndex(highlight.getDataIndex());
193 
194         if (highlight.getDataSetIndex() >= data.getDataSetCount())
195             return null;
196 
197         // The value of the highlighted entry could be NaN -
198         //   if we are not interested in highlighting a specific value.
199 
200         List<Entry> entries = data.getDataSetByIndex(highlight.getDataSetIndex())
201                 .getEntriesForXValue(highlight.getX());
202         for (Entry entry : entries)
203             if (entry.getY() == highlight.getY() ||
204                     Float.isNaN(highlight.getY()))
205                 return entry;
206 
207         return null;
208     }
209 
210     /**
211      * Get dataset for highlight
212      *
213      * @param highlight current highlight
214      * @return dataset related to highlight
215      */
getDataSetByHighlight(Highlight highlight)216     public IBarLineScatterCandleBubbleDataSet<? extends Entry> getDataSetByHighlight(Highlight highlight) {
217         if (highlight.getDataIndex() >= getAllData().size())
218             return null;
219 
220         BarLineScatterCandleBubbleData data = getDataByIndex(highlight.getDataIndex());
221 
222         if (highlight.getDataSetIndex() >= data.getDataSetCount())
223             return null;
224 
225         return (IBarLineScatterCandleBubbleDataSet<? extends Entry>)
226                 data.getDataSets().get(highlight.getDataSetIndex());
227     }
228 
getDataIndex(ChartData data)229     public int getDataIndex(ChartData data) {
230         return getAllData().indexOf(data);
231     }
232 
233     @Override
removeDataSet(IBarLineScatterCandleBubbleDataSet<? extends Entry> d)234     public boolean removeDataSet(IBarLineScatterCandleBubbleDataSet<? extends Entry> d) {
235 
236         List<BarLineScatterCandleBubbleData> datas = getAllData();
237 
238         boolean success = false;
239 
240         for (ChartData data : datas) {
241 
242             success = data.removeDataSet(d);
243 
244             if (success) {
245                 break;
246             }
247         }
248 
249         return success;
250     }
251 
252     @Deprecated
253     @Override
removeDataSet(int index)254     public boolean removeDataSet(int index) {
255         Log.e("MPAndroidChart", "removeDataSet(int index) not supported for CombinedData");
256         return false;
257     }
258 
259     @Deprecated
260     @Override
removeEntry(Entry e, int dataSetIndex)261     public boolean removeEntry(Entry e, int dataSetIndex) {
262         Log.e("MPAndroidChart", "removeEntry(...) not supported for CombinedData");
263         return false;
264     }
265 
266     @Deprecated
267     @Override
removeEntry(float xValue, int dataSetIndex)268     public boolean removeEntry(float xValue, int dataSetIndex) {
269         Log.e("MPAndroidChart", "removeEntry(...) not supported for CombinedData");
270         return false;
271     }
272 }
273