Worked on dynamically adding and removing Entries.
diff --git a/MPChartExample/src/com/xxmassdeveloper/mpchartexample/DynamicalAddingActivity.java b/MPChartExample/src/com/xxmassdeveloper/mpchartexample/DynamicalAddingActivity.java
index d18814e..7569e47 100644
--- a/MPChartExample/src/com/xxmassdeveloper/mpchartexample/DynamicalAddingActivity.java
+++ b/MPChartExample/src/com/xxmassdeveloper/mpchartexample/DynamicalAddingActivity.java
@@ -63,7 +63,7 @@
private void addEntry() {
- DataSet set = mData.getDataSetByIndex(0);
+ LineDataSet set = mData.getDataSetByIndex(0);
// set.addEntry();
mData.addEntry(new Entry((float) (Math.random() * 100) + 500f, set.getEntryCount()), 0);
@@ -74,7 +74,7 @@
private void removeLastEntry() {
- DataSet set = mData.getDataSetByIndex(0);
+ LineDataSet set = mData.getDataSetByIndex(0);
Entry e = set.getEntryForXIndex(set.getEntryCount() - 1);
diff --git a/MPChartLib/src/com/github/mikephil/charting/charts/BarChart.java b/MPChartLib/src/com/github/mikephil/charting/charts/BarChart.java
index ef43242..3afa7de 100644
--- a/MPChartLib/src/com/github/mikephil/charting/charts/BarChart.java
+++ b/MPChartLib/src/com/github/mikephil/charting/charts/BarChart.java
@@ -247,7 +247,7 @@
BarData bd = (BarData) mCurrentData;
- ArrayList<BarDataSet> dataSets = (ArrayList<BarDataSet>) bd.getDataSets();
+ ArrayList<BarDataSet> dataSets = bd.getDataSets();
int setCount = bd.getDataSetCount();
// the space between bar-groups
@@ -259,7 +259,7 @@
BarDataSet dataSet = dataSets.get(i);
boolean noStacks = dataSet.getStackSize() == 1 ? true : false;
- ArrayList<BarEntry> entries = (ArrayList<BarEntry>) dataSet.getYVals();
+ ArrayList<BarEntry> entries = dataSet.getYVals();
// do the drawing
for (int j = 0; j < dataSet.getEntryCount() * mPhaseX; j++) {
@@ -595,7 +595,7 @@
// if values are drawn
if (mDrawYValues && mCurrentData.getYValCount() < mMaxVisibleCount * mScaleX) {
- ArrayList<BarDataSet> dataSets = (ArrayList<BarDataSet>) mCurrentData.getDataSets();
+ ArrayList<BarDataSet> dataSets = ((BarData) mCurrentData).getDataSets();
float offset = 0f;
@@ -609,7 +609,7 @@
for (int i = 0; i < mCurrentData.getDataSetCount(); i++) {
BarDataSet dataSet = dataSets.get(i);
- ArrayList<BarEntry> entries = (ArrayList<BarEntry>) dataSet.getYVals();
+ ArrayList<BarEntry> entries = dataSet.getYVals();
float[] valuePoints = generateTransformedValuesBarChart(entries, i);
diff --git a/MPChartLib/src/com/github/mikephil/charting/charts/CandleStickChart.java b/MPChartLib/src/com/github/mikephil/charting/charts/CandleStickChart.java
index 4032628..e0ed3ee 100644
--- a/MPChartLib/src/com/github/mikephil/charting/charts/CandleStickChart.java
+++ b/MPChartLib/src/com/github/mikephil/charting/charts/CandleStickChart.java
@@ -51,7 +51,7 @@
@Override
protected void drawData() {
- ArrayList<CandleDataSet> dataSets = (ArrayList<CandleDataSet>) mCurrentData.getDataSets();
+ ArrayList<CandleDataSet> dataSets = ((CandleData) mCurrentData).getDataSets();
// pre allocate
float[] shadowPoints = new float[4];
@@ -60,7 +60,7 @@
for (int i = 0; i < mCurrentData.getDataSetCount(); i++) {
CandleDataSet dataSet = dataSets.get(i);
- ArrayList<CandleEntry> entries = (ArrayList<CandleEntry>) dataSet.getYVals();
+ ArrayList<CandleEntry> entries = dataSet.getYVals();
mRenderPaint.setStrokeWidth(dataSet.getShadowWidth());
@@ -165,15 +165,17 @@
@Override
protected void drawHighlights() {
+ CandleData cd = (CandleData) mCurrentData;
+
for (int i = 0; i < mIndicesToHightlight.length; i++) {
int xIndex = mIndicesToHightlight[i].getXIndex(); // get the
// x-position
- CandleDataSet set = (CandleDataSet) getDataSetByIndex(mIndicesToHightlight[i].getDataSetIndex());
+ CandleDataSet set = cd.getDataSetByIndex(mIndicesToHightlight[i].getDataSetIndex());
mHighlightPaint.setColor(set.getHighLightColor());
- CandleEntry e = (CandleEntry) set.getEntryForXIndex(xIndex);
+ CandleEntry e = set.getEntryForXIndex(xIndex);
if(e == null) continue;
diff --git a/MPChartLib/src/com/github/mikephil/charting/charts/LineChart.java b/MPChartLib/src/com/github/mikephil/charting/charts/LineChart.java
index e73e721..2c1bd47 100644
--- a/MPChartLib/src/com/github/mikephil/charting/charts/LineChart.java
+++ b/MPChartLib/src/com/github/mikephil/charting/charts/LineChart.java
@@ -121,12 +121,12 @@
@Override
protected void drawData() {
- ArrayList<LineDataSet> dataSets = (ArrayList<LineDataSet>) mCurrentData.getDataSets();
+ ArrayList<LineDataSet> dataSets = ((LineData) mCurrentData).getDataSets();
for (int i = 0; i < mCurrentData.getDataSetCount(); i++) {
LineDataSet dataSet = dataSets.get(i);
- ArrayList<? extends Entry> entries = dataSet.getYVals();
+ ArrayList<Entry> entries = dataSet.getYVals();
mRenderPaint.setStrokeWidth(dataSet.getLineWidth());
mRenderPaint.setPathEffect(dataSet.getDashPathEffect());
@@ -281,29 +281,13 @@
return filled;
}
- /**
- * Calculates the middle point between two points and multiplies its
- * coordinates with the given smoothness _Mulitplier.
- *
- * @param p1 First point
- * @param p2 Second point
- * @param _Result Resulting point
- * @param mult Smoothness multiplier
- */
- private void calculatePointDiff(PointF p1, PointF p2, PointF _Result, float mult) {
- float diffX = p2.x - p1.x;
- float diffY = p2.y - p1.y;
- _Result.x = (p1.x + (diffX * mult));
- _Result.y = (p1.y + (diffY * mult));
- }
-
@Override
protected void drawValues() {
// if values are drawn
if (mDrawYValues && mCurrentData.getYValCount() < mMaxVisibleCount * mScaleX) {
- ArrayList<LineDataSet> dataSets = (ArrayList<LineDataSet>) mCurrentData.getDataSets();
+ ArrayList<LineDataSet> dataSets = ((LineData) mCurrentData).getDataSets();
for (int i = 0; i < mCurrentData.getDataSetCount(); i++) {
@@ -315,7 +299,7 @@
if (!dataSet.isDrawCirclesEnabled())
valOffset = valOffset / 2;
- ArrayList<? extends Entry> entries = dataSet.getYVals();
+ ArrayList<Entry> entries = dataSet.getYVals();
float[] positions = generateTransformedValuesLineScatter(entries);
@@ -354,7 +338,7 @@
mRenderPaint.setStyle(Paint.Style.FILL);
- ArrayList<LineDataSet> dataSets = (ArrayList<LineDataSet>) mCurrentData.getDataSets();
+ ArrayList<LineDataSet> dataSets = ((LineData) mCurrentData).getDataSets();
for (int i = 0; i < mCurrentData.getDataSetCount(); i++) {
@@ -363,7 +347,7 @@
// if drawing circles is enabled for this dataset
if (dataSet.isDrawCirclesEnabled()) {
- ArrayList<? extends Entry> entries = dataSet.getYVals();
+ ArrayList<Entry> entries = dataSet.getYVals();
float[] positions = generateTransformedValuesLineScatter(entries);
diff --git a/MPChartLib/src/com/github/mikephil/charting/charts/PieChart.java b/MPChartLib/src/com/github/mikephil/charting/charts/PieChart.java
index 27a7479..dd0d9fb 100644
--- a/MPChartLib/src/com/github/mikephil/charting/charts/PieChart.java
+++ b/MPChartLib/src/com/github/mikephil/charting/charts/PieChart.java
@@ -6,20 +6,18 @@
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Align;
-import android.graphics.Paint.Style;
import android.graphics.PointF;
import android.graphics.RectF;
import android.graphics.Typeface;
import android.util.AttributeSet;
import android.util.Log;
-import android.widget.Toast;
import com.github.mikephil.charting.data.DataSet;
import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.data.PieData;
import com.github.mikephil.charting.data.PieDataSet;
-import com.github.mikephil.charting.utils.Utils;
import com.github.mikephil.charting.utils.Legend.LegendPosition;
+import com.github.mikephil.charting.utils.Utils;
import java.util.ArrayList;
@@ -241,14 +239,14 @@
mDrawAngles = new float[mCurrentData.getYValCount()];
mAbsoluteAngles = new float[mCurrentData.getYValCount()];
- ArrayList<? extends DataSet> dataSets = mCurrentData.getDataSets();
+ ArrayList<PieDataSet> dataSets = ((PieData) mCurrentData).getDataSets();
int cnt = 0;
for (int i = 0; i < mCurrentData.getDataSetCount(); i++) {
- DataSet set = dataSets.get(i);
- ArrayList<? extends Entry> entries = set.getYVals();
+ PieDataSet set = dataSets.get(i);
+ ArrayList<Entry> entries = set.getYVals();
for (int j = 0; j < entries.size(); j++) {
@@ -271,6 +269,8 @@
// if there are values to highlight and highlighnting is enabled, do it
if (mHighlightEnabled && valuesToHighlight()) {
+
+ PieData pd = (PieData) mCurrentData;
float angle = 0f;
@@ -292,7 +292,7 @@
float shiftangle = (float) Math.toRadians(angle + sliceDegrees / 2f);
- PieDataSet set = (PieDataSet) mCurrentData
+ PieDataSet set = pd
.getDataSetByIndex(mIndicesToHightlight[i]
.getDataSetIndex());
@@ -319,14 +319,14 @@
float angle = mRotationAngle;
- ArrayList<PieDataSet> dataSets = (ArrayList<PieDataSet>) mCurrentData.getDataSets();
+ ArrayList<PieDataSet> dataSets = ((PieData) mCurrentData).getDataSets();
int cnt = 0;
for (int i = 0; i < mCurrentData.getDataSetCount(); i++) {
PieDataSet dataSet = dataSets.get(i);
- ArrayList<? extends Entry> entries = dataSet.getYVals();
+ ArrayList<Entry> entries = dataSet.getYVals();
for (int j = 0; j < entries.size(); j++) {
@@ -431,14 +431,14 @@
r -= off; // offset to keep things inside the chart
- ArrayList<? extends DataSet> dataSets = mCurrentData.getDataSets();
+ ArrayList<PieDataSet> dataSets = ((PieData) mCurrentData).getDataSets();
int cnt = 0;
for (int i = 0; i < mCurrentData.getDataSetCount(); i++) {
- DataSet dataSet = dataSets.get(i);
- ArrayList<? extends Entry> entries = dataSet.getYVals();
+ PieDataSet dataSet = dataSets.get(i);
+ ArrayList<Entry> entries = dataSet.getYVals();
for (int j = 0; j < entries.size() * mPhaseX; j++) {
diff --git a/MPChartLib/src/com/github/mikephil/charting/charts/RadarChart.java b/MPChartLib/src/com/github/mikephil/charting/charts/RadarChart.java
index 2de7590..3187ad5 100644
--- a/MPChartLib/src/com/github/mikephil/charting/charts/RadarChart.java
+++ b/MPChartLib/src/com/github/mikephil/charting/charts/RadarChart.java
@@ -110,7 +110,7 @@
prepareXLabels();
}
-
+
@Override
protected void calculateOffsets() {
@@ -129,21 +129,21 @@
mLegend.setOffsetBottom(mLegendLabelPaint.getTextSize() * 5.5f);
}
-
+
mLegend.setOffsetTop(mOffsetTop);
mLegend.setOffsetLeft(mOffsetLeft);
if (mDrawLegend) {
-
+
mOffsetBottom = Math.max(mXLabels.mLabelWidth, mOffsetBottom);
mOffsetTop = Math.max(mXLabels.mLabelWidth, mOffsetTop);
mOffsetRight = Math.max(mXLabels.mLabelWidth, mOffsetRight);
mOffsetLeft = Math.max(mXLabels.mLabelWidth, mOffsetLeft);
-
+
mOffsetBottom = Math.max(mOffsetBottom, mLegend.getOffsetBottom());
mOffsetRight = Math.max(mOffsetRight, mLegend.getOffsetRight() / 3 * 2);
}
-
+
applyCalculatedOffsets();
}
@@ -232,7 +232,7 @@
@Override
protected void drawData() {
- ArrayList<RadarDataSet> dataSets = (ArrayList<RadarDataSet>) mCurrentData.getDataSets();
+ ArrayList<RadarDataSet> dataSets = ((RadarData) mCurrentData).getDataSets();
float sliceangle = getSliceAngle();
@@ -245,7 +245,7 @@
for (int i = 0; i < mCurrentData.getDataSetCount(); i++) {
RadarDataSet dataSet = dataSets.get(i);
- ArrayList<? extends Entry> entries = dataSet.getYVals();
+ ArrayList<Entry> entries = dataSet.getYVals();
Path surface = new Path();
@@ -361,8 +361,6 @@
mXLabels.mLabelWidth = Utils.calcTextWidth(mXLabelPaint, a.toString());
mXLabels.mLabelHeight = Utils.calcTextWidth(mXLabelPaint, "Q");
-
- Log.i(LOG_TAG, "xlabels prepared, width: " + mXLabels.mLabelWidth);
}
/**
@@ -403,6 +401,8 @@
// if values are drawn
if (mDrawYValues) {
+ RadarData rd = (RadarData) mCurrentData;
+
float sliceangle = getSliceAngle();
// calculate the factor that is needed for transforming the value to
@@ -415,8 +415,8 @@
for (int i = 0; i < mCurrentData.getDataSetCount(); i++) {
- DataSet dataSet = mCurrentData.getDataSetByIndex(i);
- ArrayList<? extends Entry> entries = dataSet.getYVals();
+ RadarDataSet dataSet = rd.getDataSetByIndex(i);
+ ArrayList<Entry> entries = dataSet.getYVals();
for (int j = 0; j < entries.size(); j++) {
@@ -438,6 +438,8 @@
// if there are values to highlight and highlighnting is enabled, do it
if (mHighlightEnabled && valuesToHighlight()) {
+ RadarData rd = (RadarData) mCurrentData;
+
float sliceangle = getSliceAngle();
float factor = getFactor();
@@ -445,7 +447,7 @@
for (int i = 0; i < mIndicesToHightlight.length; i++) {
- RadarDataSet set = (RadarDataSet) mCurrentData
+ RadarDataSet set = rd
.getDataSetByIndex(mIndicesToHightlight[i]
.getDataSetIndex());
diff --git a/MPChartLib/src/com/github/mikephil/charting/charts/ScatterChart.java b/MPChartLib/src/com/github/mikephil/charting/charts/ScatterChart.java
index 709973f..192d9a8 100644
--- a/MPChartLib/src/com/github/mikephil/charting/charts/ScatterChart.java
+++ b/MPChartLib/src/com/github/mikephil/charting/charts/ScatterChart.java
@@ -49,12 +49,12 @@
@Override
protected void drawData() {
- ArrayList<ScatterDataSet> dataSets = (ArrayList<ScatterDataSet>) mCurrentData.getDataSets();
+ ArrayList<ScatterDataSet> dataSets = ((ScatterData) mCurrentData).getDataSets();
for (int i = 0; i < mCurrentData.getDataSetCount(); i++) {
ScatterDataSet dataSet = dataSets.get(i);
- ArrayList<? extends Entry> entries = dataSet.getYVals();
+ ArrayList<Entry> entries = dataSet.getYVals();
float shapeHalf = dataSet.getScatterShapeSize() / 2f;
@@ -129,13 +129,13 @@
// if values are drawn
if (mDrawYValues && mCurrentData.getYValCount() < mMaxVisibleCount * mScaleX) {
- ArrayList<ScatterDataSet> dataSets = (ArrayList<ScatterDataSet>) mCurrentData
+ ArrayList<ScatterDataSet> dataSets = ((ScatterData) mCurrentData)
.getDataSets();
for (int i = 0; i < mCurrentData.getDataSetCount(); i++) {
ScatterDataSet dataSet = dataSets.get(i);
- ArrayList<? extends Entry> entries = dataSet.getYVals();
+ ArrayList<Entry> entries = dataSet.getYVals();
float[] positions = generateTransformedValuesLineScatter(entries);
diff --git a/MPChartLib/src/com/github/mikephil/charting/data/ChartData.java b/MPChartLib/src/com/github/mikephil/charting/data/ChartData.java
index ddb4ef1..d5dbc04 100644
--- a/MPChartLib/src/com/github/mikephil/charting/data/ChartData.java
+++ b/MPChartLib/src/com/github/mikephil/charting/data/ChartData.java
@@ -296,7 +296,7 @@
* @param ignorecase if true, the search is not case-sensitive
* @return
*/
- protected int getDataSetIndexByLabel(ArrayList<? extends DataSet> dataSets, String label,
+ protected int getDataSetIndexByLabel(ArrayList<T> dataSets, String label,
boolean ignorecase) {
if (ignorecase) {
@@ -398,7 +398,8 @@
}
/**
- * Adds an Entry to the DataSet at the specified index.
+ * Adds an Entry to the DataSet at the specified index. Entries are added to
+ * the end of the list.
*
* @param e
* @param dataSetIndex
@@ -414,18 +415,50 @@
mYMax = val;
if (mYMin > val)
mYMin = val;
-
+
// add the entry to the dataset
- mDataSets.get(dataSetIndex).addEntry(e);
+ mDataSets.get(dataSetIndex).addEntry((Entry) e);
}
+ /**
+ * Removes the given Entry object from the DataSet at the specified index.
+ *
+ * @param e
+ * @param dataSetIndex
+ */
public void removeEntry(Entry e, int dataSetIndex) {
+ // entry null, outofbounds
+ if (e == null || dataSetIndex >= mDataSets.size())
+ return;
+
float val = e.getVal();
mYValCount -= 1;
mYValueSum -= val;
+ // remove the entry from the dataset
+ mDataSets.get(dataSetIndex).removeEntry(e.getXIndex());
+
+ calcMinMax(mDataSets);
+ }
+
+ /**
+ * Removes the Entry object at the given xIndex from the DataSet at the
+ * specified index.
+ *
+ * @param xIndex
+ * @param dataSetIndex
+ */
+ public void removeEntry(int xIndex, int dataSetIndex) {
+
+ if (dataSetIndex >= mDataSets.size())
+ return;
+
+ T dataSet = mDataSets.get(dataSetIndex);
+ Entry e = dataSet.getEntryForXIndex(xIndex);
+
+ removeEntry(e, dataSetIndex);
}
/**
diff --git a/MPChartLib/src/com/github/mikephil/charting/data/DataSet.java b/MPChartLib/src/com/github/mikephil/charting/data/DataSet.java
index 7890bdc..ebcf438 100644
--- a/MPChartLib/src/com/github/mikephil/charting/data/DataSet.java
+++ b/MPChartLib/src/com/github/mikephil/charting/data/DataSet.java
@@ -144,7 +144,7 @@
* @param xIndex
* @return
*/
- public Entry getEntryForXIndex(int x) {
+ public T getEntryForXIndex(int x) {
int low = 0;
int high = mYVals.size() - 1;
@@ -173,9 +173,9 @@
* @param xIndex
* @return
*/
- public ArrayList<Entry> getEntriesForXIndex(int x) {
+ public ArrayList<T> getEntriesForXIndex(int x) {
- ArrayList<Entry> entries = new ArrayList<Entry>();
+ ArrayList<T> entries = new ArrayList<T>();
int low = 0;
int high = mYVals.size();
@@ -306,6 +306,9 @@
*/
public void addEntry(Entry e) {
+ if (e == null)
+ return;
+
float val = e.getVal();
if (mYVals == null || mYVals.size() <= 0) {
@@ -322,11 +325,44 @@
}
mYValueSum += val;
-
+
// add the entry
mYVals.add((T) e);
}
+ /**
+ * Removes an Entry from the DataSets entries array. This will also
+ * recalculate the current minimum and maximum values of the DataSet and the
+ * value-sum.
+ *
+ * @param e
+ */
+ public void removeEntry(T e) {
+
+ if (e == null)
+ return;
+
+ // remove the entry
+ mYVals.remove(e);
+
+ float val = e.getVal();
+
+ calcMinMax();
+
+ mYValueSum -= val;
+ }
+
+ /**
+ * Removes the Entry object that has the given xIndex from the DataSet.
+ *
+ * @param xIndex
+ */
+ public void removeEntry(int xIndex) {
+
+ T e = getEntryForXIndex(xIndex);
+ removeEntry(e);
+ }
+
/** BELOW THIS COLOR HANDLING */
/**
diff --git a/MPChartLib/src/com/github/mikephil/charting/listener/BarLineChartTouchListener.java b/MPChartLib/src/com/github/mikephil/charting/listener/BarLineChartTouchListener.java
index f47a487..af07556 100644
--- a/MPChartLib/src/com/github/mikephil/charting/listener/BarLineChartTouchListener.java
+++ b/MPChartLib/src/com/github/mikephil/charting/listener/BarLineChartTouchListener.java
@@ -415,6 +415,8 @@
public void onLongPress(MotionEvent e) {
mChart.fitScreen();
+
+ Log.i("BarlineChartTouch", "Longpress, resetting zoom and drag, adjusting chart bounds to screen.");
// PointF trans = getTrans(e.getX(), e.getY());
//