@Override public void calcMinMaxY(float fromX, float toX) { if (mValues == null || mValues.isEmpty()) return; mYMax = -Float.MAX_VALUE; mYMin = Float.MAX_VALUE; int indexFrom = getEntryIndex(fromX, Float.NaN, DataSet.Rounding.DOWN); int indexTo = getEntryIndex(toX, Float.NaN, DataSet.Rounding.UP); for (int i = indexFrom; i <= indexTo; i++) { // only recalculate y calcMinMaxY(mValues.get(i)); } }
@Override public void addEntryOrdered(S e) { if (e == null) return; if (mValues == null) { mValues = new ArrayList<S>(); } calcMinMax(e); if (mValues.size() > 0 && mValues.get(mValues.size() - 1).getX() > e.getX()) { int closestIndex = getEntryIndex(e.getX(), e.getY(), DataSet.Rounding.UP); mValues.add(closestIndex, e); } else { mValues.add(e); } }
@Override protected void calcMinMax(boolean fixedValues) { super.calcMinMax(fixedValues); // increase deltax by 1 because the bars have a width of 1 mDeltaX++; // extend xDelta to make space for multiple datasets (if ther are one) mDeltaX *= mData.getDataSetCount(); int maxEntry = 0; for (int i = 0; i < mData.getDataSetCount(); i++) { DataSet<? extends Entry> set = mData.getDataSetByIndex(i); if (maxEntry < set.getEntryCount()) maxEntry = set.getEntryCount(); } float groupSpace = mData.getGroupSpace(); mDeltaX += maxEntry * groupSpace; }
@Override protected void calcMinMax(boolean fixedValues) { super.calcMinMax(fixedValues); // increase deltax by 1 because the bars have a width of 1 mDeltaX++; // extend xDelta to make space for multiple datasets (if ther are one) mDeltaX *= mOriginalData.getDataSetCount(); int maxEntry = 0; for (int i = 0; i < mOriginalData.getDataSetCount(); i++) { DataSet<? extends Entry> set = mOriginalData.getDataSetByIndex(i); if (maxEntry < set.getEntryCount()) maxEntry = set.getEntryCount(); } float groupSpace = mOriginalData.getGroupSpace(); mDeltaX += maxEntry * groupSpace; }
/** * Calculates the minimum and maximum x values as well as the range between them. * * @param chart * @param dataSet */ public void set(BarLineScatterCandleBubbleDataProvider chart, IBarLineScatterCandleBubbleDataSet dataSet) { float phaseX = Math.max(0.f, Math.min(1.f, mAnimator.getPhaseX())); float low = chart.getLowestVisibleX(); float high = chart.getHighestVisibleX(); Entry entryFrom = dataSet.getEntryForXValue(low, Float.NaN, DataSet.Rounding.DOWN); Entry entryTo = dataSet.getEntryForXValue(high, Float.NaN, DataSet.Rounding.UP); min = entryFrom == null ? 0 : dataSet.getEntryIndex(entryFrom); max = entryTo == null ? 0 : dataSet.getEntryIndex(entryTo); range = (int) ((max - min) * phaseX); }
/** * An array of `Highlight` objects corresponding to the selected xValue and dataSetIndex. * * @param set * @param dataSetIndex * @param xVal * @param rounding * @return */ protected List<Highlight> buildHighlights(IDataSet set, int dataSetIndex, float xVal, DataSet.Rounding rounding) { ArrayList<Highlight> highlights = new ArrayList<>(); //noinspection unchecked List<Entry> entries = set.getEntriesForXValue(xVal); if (entries.size() == 0) { // Try to find closest x-value and take all entries for that x-value final Entry closest = set.getEntryForXValue(xVal, Float.NaN, rounding); if (closest != null) { //noinspection unchecked entries = set.getEntriesForXValue(closest.getX()); } } if (entries.size() == 0) return highlights; for (Entry e : entries) { MPPointD pixels = mChart.getTransformer( set.getAxisDependency()).getPixelForValues(e.getX(), e.getY()); highlights.add(new Highlight( e.getX(), e.getY(), (float) pixels.x, (float) pixels.y, dataSetIndex, set.getAxisDependency())); } return highlights; }
@Override protected List<Highlight> buildHighlights(IDataSet set, int dataSetIndex, float xVal, DataSet.Rounding rounding) { ArrayList<Highlight> highlights = new ArrayList<>(); //noinspection unchecked List<Entry> entries = set.getEntriesForXValue(xVal); if (entries.size() == 0) { // Try to find closest x-value and take all entries for that x-value final Entry closest = set.getEntryForXValue(xVal, Float.NaN, rounding); if (closest != null) { //noinspection unchecked entries = set.getEntriesForXValue(closest.getX()); } } if (entries.size() == 0) return highlights; for (Entry e : entries) { MPPointD pixels = mChart.getTransformer( set.getAxisDependency()).getPixelForValues(e.getY(), e.getX()); highlights.add(new Highlight( e.getX(), e.getY(), (float) pixels.x, (float) pixels.y, dataSetIndex, set.getAxisDependency())); } return highlights; }
@Test public void testGetEntryForXValue() { List<Entry> entries = new ArrayList<Entry>(); entries.add(new Entry(10, 10)); entries.add(new Entry(15, 5)); entries.add(new Entry(21, 5)); ScatterDataSet set = new ScatterDataSet(entries, ""); Entry closest = set.getEntryForXValue(17, Float.NaN, DataSet.Rounding.CLOSEST); assertEquals(15, closest.getX(), 0.01f); assertEquals(5, closest.getY(), 0.01f); closest = set.getEntryForXValue(17, Float.NaN, DataSet.Rounding.DOWN); assertEquals(15, closest.getX(), 0.01f); assertEquals(5, closest.getY(), 0.01f); closest = set.getEntryForXValue(15, Float.NaN, DataSet.Rounding.DOWN); assertEquals(15, closest.getX(), 0.01f); assertEquals(5, closest.getY(), 0.01f); closest = set.getEntryForXValue(14, Float.NaN, DataSet.Rounding.DOWN); assertEquals(10, closest.getX(), 0.01f); assertEquals(10, closest.getY(), 0.01f); closest = set.getEntryForXValue(17, Float.NaN, DataSet.Rounding.UP); assertEquals(21, closest.getX(), 0.01f); assertEquals(5, closest.getY(), 0.01f); closest = set.getEntryForXValue(21, Float.NaN, DataSet.Rounding.UP); assertEquals(21, closest.getX(), 0.01f); assertEquals(5, closest.getY(), 0.01f); closest = set.getEntryForXValue(21, Float.NaN, DataSet.Rounding.CLOSEST); assertEquals(21, closest.getX(), 0.01f); assertEquals(5, closest.getY(), 0.01f); }
/** callback when a DataSet has been drawn (when lifting the finger) */ @Override public void onDrawFinished(DataSet<?> dataSet) { Log.i(Chart.LOG_TAG, "DataSet drawn. " + dataSet.toSimpleString()); // prepare the legend again mChart.getLegendRenderer().computeLegend(mChart.getData()); }
@Override public S getEntryForXIndex(int xIndex, DataSet.Rounding rounding) { int index = getEntryIndex(xIndex, rounding); if (index > -1) return mValues.get(index); return null; }
@Override public int getEntryIndex(int x, DataSet.Rounding rounding) { int low = 0; int high = mValues.size() - 1; int closest = -1; while (low <= high) { int m = (high + low) / 2; if (x == mValues.get(m).getXIndex()) { while (m > 0 && mValues.get(m - 1).getXIndex() == x) m--; return m; } if (x > mValues.get(m).getXIndex()) low = m + 1; else high = m - 1; closest = m; } if (closest != -1) { int closestXIndex = mValues.get(closest).getXIndex(); if (rounding == DataSet.Rounding.UP) { if (closestXIndex < x && closest < mValues.size() - 1) { ++closest; } } else if (rounding == DataSet.Rounding.DOWN) { if (closestXIndex > x && closest > 0) { --closest; } } } return closest; }
@Override public void addEntryOrdered(S e) { if (e == null) return; float val = e.getVal(); if (mValues == null) { mValues = new ArrayList<S>(); } if (mValues.size() == 0) { mYMax = val; mYMin = val; } else { if (mYMax < val) mYMax = val; if (mYMin > val) mYMin = val; } if (mValues.size() > 0 && mValues.get(mValues.size() - 1).getXIndex() > e.getXIndex()) { int closestIndex = getEntryIndex(e.getXIndex(), DataSet.Rounding.UP); mValues.add(closestIndex, e); return; } mValues.add(e); }
/** * Returns the index of the DataSet this x-index belongs to. * * @param xIndex * @return */ public int getDataSetIndexForIndex(int xIndex) { List<? extends DataSet<? extends Entry>> dataSets = mData.getDataSets(); for (int i = 0; i < dataSets.size(); i++) { if (dataSets.get(i).getEntryForXIndex(xIndex) != null) return i; } return -1; }
/** * 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) { if (data == null) { Log.e(LOG_TAG, "Cannot set data for chart. Provided data object is null."); return; } // LET THE CHART KNOW THERE IS DATA mDataNotSet = false; mOffsetsCalculated = false; mData = data; // calculate how many digits are needed calculateFormatter(data.getYMin(), data.getYMax()); for (DataSet<?> set : mData.getDataSets()) { if (set.needsDefaultFormatter()) set.setValueFormatter(mDefaultFormatter); } // let the chart know there is new data notifyDataSetChanged(); if (mLogEnabled) Log.i(LOG_TAG, "Data is set."); }
/** * Applies the required styling (provided by the DataSet) to the value-paint * object. * * @param set */ protected void applyValueTextStyle(DataSet<?> set) { mValuePaint.setColor(set.getValueTextColor()); mValuePaint.setTypeface(set.getValueTypeface()); mValuePaint.setTextSize(set.getValueTextSize()); }
public static void commonConfig(DataSet dataSet, ReadableMap config) { // Setting main color if (BridgeUtils.validate(config, ReadableType.String, "color")) { dataSet.setColor(Color.parseColor(config.getString("color"))); } if (BridgeUtils.validate(config, ReadableType.Array, "colors")) { dataSet.setColors(BridgeUtils.parseColors(config.getArray("colors"))); } // TODO more config to add: https://github.com/PhilJay/MPAndroidChart/wiki/The-DataSet-class if (BridgeUtils.validate(config, ReadableType.Boolean, "drawValues")) { dataSet.setDrawValues(config.getBoolean("drawValues")); } }
/** * Calculates the minimum and maximum x values as well as the range between them. * * @param chart * @param dataSet */ public void set(BarLineScatterCandleBubbleDataProvider chart, IBarLineScatterCandleBubbleDataSet dataSet) { float phaseX = Math.max(0.f, Math.min(1.f, mAnimator.getPhaseX())); float low = chart.getLowestVisibleX(); float high = chart.getHighestVisibleX(); Entry entryFrom = dataSet.getEntryForXPos(low, DataSet.Rounding.DOWN); Entry entryTo = dataSet.getEntryForXPos(high, DataSet.Rounding.UP); min = dataSet.getEntryIndex(entryFrom); max = dataSet.getEntryIndex(entryTo); range = (int) ((max - min) * phaseX); }
@Test public void testGetEntryForXPos() { List<Entry> entries = new ArrayList<Entry>(); entries.add(new Entry(10, 10)); entries.add(new Entry(15, 5)); entries.add(new Entry(21, 5)); ScatterDataSet set = new ScatterDataSet(entries, ""); Entry closest = set.getEntryForXPos(17, DataSet.Rounding.CLOSEST); assertEquals(15, closest.getX(), 0.01f); assertEquals(5, closest.getY(), 0.01f); closest = set.getEntryForXPos(17, DataSet.Rounding.DOWN); assertEquals(15, closest.getX(), 0.01f); assertEquals(5, closest.getY(), 0.01f); closest = set.getEntryForXPos(15, DataSet.Rounding.DOWN); assertEquals(15, closest.getX(), 0.01f); assertEquals(5, closest.getY(), 0.01f); closest = set.getEntryForXPos(14, DataSet.Rounding.DOWN); assertEquals(10, closest.getX(), 0.01f); assertEquals(10, closest.getY(), 0.01f); closest = set.getEntryForXPos(17, DataSet.Rounding.UP); assertEquals(21, closest.getX(), 0.01f); assertEquals(5, closest.getY(), 0.01f); closest = set.getEntryForXPos(21, DataSet.Rounding.UP); assertEquals(21, closest.getX(), 0.01f); assertEquals(5, closest.getY(), 0.01f); closest = set.getEntryForXPos(21, DataSet.Rounding.CLOSEST); assertEquals(21, closest.getX(), 0.01f); assertEquals(5, closest.getY(), 0.01f); }
@Override public S getEntryForXValue(float xValue, float closestToY, DataSet.Rounding rounding) { int index = getEntryIndex(xValue, closestToY, rounding); if (index > -1) return mValues.get(index); return null; }
/** * Returns the index of the DataSet this x-index belongs to. * * @param xIndex * @return */ public int getDataSetIndexForIndex(int xIndex) { ArrayList<? extends DataSet<? extends Entry>> dataSets = mData.getDataSets(); for (int i = 0; i < dataSets.size(); i++) { if (dataSets.get(i).getEntryForXIndex(xIndex) != null) return i; } return -1; }
/** * returns the average value for a specific DataSet (with a specific label) * in the chart * * @param dataSetLabel * @return */ public float getAverage(String dataSetLabel) { DataSet<? extends Entry> ds = mData.getDataSetByLabel(dataSetLabel, true); return ds.getYValueSum() / ds.getEntryCount(); }