/** * Constructor taking single or multiple DataSet objects. * * @param dataSets */ public ChartData(T... dataSets) { mDataSets = arrayToList(dataSets); notifyDataChanged(); }
data.calcMinMax(); List<IBarLineScatterCandleBubbleDataSet<? extends Entry>> sets = data.getDataSets(); mDataSets.addAll(sets); if (data.getYMax() > mYMax) mYMax = data.getYMax(); if (data.getYMin() < mYMin) mYMin = data.getYMin(); if (data.getXMax() > mXMax) mXMax = data.getXMax(); if (data.getXMin() < mXMin) mXMin = data.getXMin();
/** * Removes all DataSets (and thereby Entries) from the chart. Does not set the data object to null. Also refreshes the * chart by calling invalidate(). */ public void clearValues() { mData.clearValues(); invalidate(); }
/** * Sets a new data object for the chart. The data object contains all values * and information needed for displaying. * * @param data */ public void setData(T data) { mData = data; mOffsetsCalculated = false; if (data == null) { return; } // calculate how many digits are needed setupDefaultFormatter(data.getYMin(), data.getYMax()); for (IDataSet set : mData.getDataSets()) { if (set.needsFormatter() || set.getValueFormatter() == mDefaultValueFormatter) set.setValueFormatter(mDefaultValueFormatter); } // let the chart know there is new data notifyDataSetChanged(); if (mLogEnabled) Log.i(LOG_TAG, "Data is set."); }
calcMinMax(set); T firstLeft = getFirstLeft(mDataSets); T firstRight = getFirstRight(mDataSets);
/** * Get the Entry for a corresponding highlight object * * @param highlight * @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<Entry> 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; }
@Override public int getMaxVisibleCount() { return mData.getEntryCount(); }
/** * draws all MarkerViews on the highlighted positions */ protected void drawMarkers(Canvas canvas) { // if there is no marker view or drawing marker is disabled if (mMarker == null || !isDrawMarkersEnabled() || !valuesToHighlight()) return; for (int i = 0; i < mIndicesToHighlight.length; i++) { Highlight highlight = mIndicesToHighlight[i]; IDataSet set = mData.getDataSetByIndex(highlight.getDataSetIndex()); Entry e = mData.getEntryForHighlight(mIndicesToHighlight[i]); int entryIndex = set.getEntryIndex(e); // make sure entry not null if (e == null || entryIndex > set.getEntryCount() * mAnimator.getPhaseX()) continue; float[] pos = getMarkerPosition(highlight); // check bounds if (!mViewPortHandler.isInBounds(pos[0], pos[1])) continue; // callbacks to update the content mMarker.refreshContent(e, highlight); // draw the marker mMarker.draw(canvas, pos[0], pos[1]); } }
/** * Call this method to let the ChartData know that the underlying data has * changed. Calling this performs all necessary recalculations needed when * the contained data has changed. */ public void notifyDataChanged() { calcMinMax(); }
/** * Highlights any y-value at the given x-value in the given DataSet. * Provide -1 as the dataSetIndex to undo all highlighting. * @param x The x-value to highlight * @param y The y-value to highlight. Supply `NaN` for "any" * @param dataSetIndex The dataset index to search in * @param callListener Should the listener be called for this change */ public void highlightValue(float x, float y, int dataSetIndex, boolean callListener) { if (dataSetIndex < 0 || dataSetIndex >= mData.getDataSetCount()) { highlightValue(null, callListener); } else { highlightValue(new Highlight(x, y, dataSetIndex), callListener); } }
/** * Returns the DataSet object with the given label. Search can be case * sensitive or not. IMPORTANT: This method does calculations at runtime. * Use with care in performance critical situations. * * @param label * @param ignorecase * @return */ public T getDataSetByLabel(String label, boolean ignorecase) { int index = getDataSetIndexByLabel(mDataSets, label, ignorecase); if (index < 0 || index >= mDataSets.size()) return null; else return mDataSets.get(index); }
Log.i(LOG_TAG, "Highlighted: " + high.toString()); e = mData.getEntryForHighlight(high); if (e == null) { mIndicesToHighlight = null;
/** * Sets a new data object for the chart. The data object contains all values * and information needed for displaying. * * @param data */ public void setData(T data) { mData = data; mOffsetsCalculated = false; if (data == null) { return; } // calculate how many digits are needed setupDefaultFormatter(data.getYMin(), data.getYMax()); for (IDataSet set : mData.getDataSets()) { if (set.needsFormatter() || set.getValueFormatter() == mDefaultValueFormatter) set.setValueFormatter(mDefaultValueFormatter); } // let the chart know there is new data notifyDataSetChanged(); if (mLogEnabled) Log.i(LOG_TAG, "Data is set."); }
calcMinMax(set); T firstLeft = getFirstLeft(mDataSets); T firstRight = getFirstRight(mDataSets);
for (int i = 0; i < data.getDataSetCount(); i++) { IDataSet dataSet = data.getDataSetByIndex(i); label = null; } else { // add label to the last entry label = data.getDataSetByIndex(i).getLabel();
/** * Returns true if the chart is empty (meaning it's data object is either * null or contains no entries). * * @return */ public boolean isEmpty() { if (mData == null) return true; else { if (mData.getEntryCount() <= 0) return true; else return false; } }
/** * draws all MarkerViews on the highlighted positions */ protected void drawMarkers(Canvas canvas) { // if there is no marker view or drawing marker is disabled if (mMarker == null || !isDrawMarkersEnabled() || !valuesToHighlight()) return; for (int i = 0; i < mIndicesToHighlight.length; i++) { Highlight highlight = mIndicesToHighlight[i]; IDataSet set = mData.getDataSetByIndex(highlight.getDataSetIndex()); Entry e = mData.getEntryForHighlight(mIndicesToHighlight[i]); int entryIndex = set.getEntryIndex(e); // make sure entry not null if (e == null || entryIndex > set.getEntryCount() * mAnimator.getPhaseX()) continue; float[] pos = getMarkerPosition(highlight); // check bounds if (!mViewPortHandler.isInBounds(pos[0], pos[1])) continue; // callbacks to update the content mMarker.refreshContent(e, highlight); // draw the marker mMarker.draw(canvas, pos[0], pos[1]); } }
/** * Adds a DataSet dynamically. * * @param d */ public void addDataSet(T d) { if (d == null) return; calcMinMax(d); mDataSets.add(d); }
@Override protected List<Highlight> getHighlightsAtXValue(float xVal, float x, float y) { mHighlightBuffer.clear(); List<BarLineScatterCandleBubbleData> dataObjects = mChart.getCombinedData().getAllData(); for (int i = 0; i < dataObjects.size(); i++) { ChartData dataObject = dataObjects.get(i); // in case of BarData, let the BarHighlighter take over if (barHighlighter != null && dataObject instanceof BarData) { Highlight high = barHighlighter.getHighlight(x, y); if (high != null) { high.setDataIndex(i); mHighlightBuffer.add(high); } } else { for (int j = 0, dataSetCount = dataObject.getDataSetCount(); j < dataSetCount; j++) { IDataSet dataSet = dataObjects.get(i).getDataSetByIndex(j); // don't include datasets that cannot be highlighted if (!dataSet.isHighlightEnabled()) continue; List<Highlight> highs = buildHighlights(dataSet, j, xVal, DataSet.Rounding.CLOSEST); for (Highlight high : highs) { high.setDataIndex(i); mHighlightBuffer.add(high); } } } } return mHighlightBuffer; }
/** * Returns the DataSet object with the given label. Search can be case * sensitive or not. IMPORTANT: This method does calculations at runtime. * Use with care in performance critical situations. * * @param label * @param ignorecase * @return */ public T getDataSetByLabel(String label, boolean ignorecase) { int index = getDataSetIndexByLabel(mDataSets, label, ignorecase); if (index < 0 || index >= mDataSets.size()) return null; else return mDataSets.get(index); }