aboutsummaryrefslogtreecommitdiff
path: root/MPChartLib
diff options
context:
space:
mode:
authorDaniel Cohen Gindi <danielgindi@gmail.com>2016-08-14 16:40:55 +0300
committerDaniel Cohen Gindi <danielgindi@gmail.com>2016-08-14 16:53:20 +0300
commit7a10c05e3a2a1f2d7549b1ff42c565e1ce291937 (patch)
treeff4d22a999dc504549165ba8c652087266796db5 /MPChartLib
parentea6b0e8e1c3f2567ad5724807a31493b7b88e629 (diff)
downloadMPAndroidChart-7a10c05e3a2a1f2d7549b1ff42c565e1ce291937.tar.gz
Added feature for dashing legend line forms (Closes #1843)
Diffstat (limited to 'MPChartLib')
-rw-r--r--MPChartLib/src/main/java/com/github/mikephil/charting/components/Legend.java22
-rw-r--r--MPChartLib/src/main/java/com/github/mikephil/charting/components/LegendEntry.java18
-rw-r--r--MPChartLib/src/main/java/com/github/mikephil/charting/data/BaseDataSet.java11
-rw-r--r--MPChartLib/src/main/java/com/github/mikephil/charting/interfaces/datasets/IDataSet.java8
-rw-r--r--MPChartLib/src/main/java/com/github/mikephil/charting/renderer/LegendRenderer.java28
5 files changed, 82 insertions, 5 deletions
diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/components/Legend.java b/MPChartLib/src/main/java/com/github/mikephil/charting/components/Legend.java
index 8e521f5a..bdcc024f 100644
--- a/MPChartLib/src/main/java/com/github/mikephil/charting/components/Legend.java
+++ b/MPChartLib/src/main/java/com/github/mikephil/charting/components/Legend.java
@@ -1,6 +1,7 @@
package com.github.mikephil.charting.components;
+import android.graphics.DashPathEffect;
import android.graphics.Paint;
import com.github.mikephil.charting.data.Entry;
@@ -124,6 +125,11 @@ public class Legend extends ComponentBase {
private float mFormLineWidth = 3f;
/**
+ * Line dash path effect used for shapes that consist of lines.
+ */
+ private DashPathEffect mFormLineDashEffect = null;
+
+ /**
* the space between the legend entries on a horizontal axis, default 6f
*/
private float mXEntrySpace = 6f;
@@ -668,6 +674,22 @@ public class Legend extends ComponentBase {
}
/**
+ * Sets the line dash path effect used for shapes that consist of lines.
+ *
+ * @param dashPathEffect
+ */
+ public void setFormLineDashEffect(DashPathEffect dashPathEffect) {
+ mFormLineDashEffect = dashPathEffect;
+ }
+
+ /**
+ * @return The line dash path effect used for shapes that consist of lines.
+ */
+ public DashPathEffect getFormLineDashEffect() {
+ return mFormLineDashEffect;
+ }
+
+ /**
* returns the space between the legend entries on a horizontal axis in
* pixels
*
diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/components/LegendEntry.java b/MPChartLib/src/main/java/com/github/mikephil/charting/components/LegendEntry.java
index 50300b6d..3acec0f4 100644
--- a/MPChartLib/src/main/java/com/github/mikephil/charting/components/LegendEntry.java
+++ b/MPChartLib/src/main/java/com/github/mikephil/charting/components/LegendEntry.java
@@ -1,6 +1,8 @@
package com.github.mikephil.charting.components;
+import android.graphics.DashPathEffect;
+
import com.github.mikephil.charting.utils.ColorTemplate;
public class LegendEntry {
@@ -12,20 +14,23 @@ public class LegendEntry {
*
* @param label The legend entry text. A `null` label will start a group.
* @param form The form to draw for this entry.
- * @param formSize Set as NaN to use the legend's default
- * @param formLineWidth Set as NaN to use the legend's default
- * @param formColor The color for drawing the form
+ * @param formSize Set to NaN to use the legend's default.
+ * @param formLineWidth Set to NaN to use the legend's default.
+ * @param formLineDashEffect Set to nil to use the legend's default.
+ * @param formColor The color for drawing the form.
*/
public LegendEntry(String label,
Legend.LegendForm form,
float formSize,
float formLineWidth,
+ DashPathEffect formLineDashEffect,
int formColor)
{
this.label = label;
this.form = form;
this.formSize = formSize;
this.formLineWidth = formLineWidth;
+ this.formLineDashEffect = formLineDashEffect;
this.formColor = formColor;
}
@@ -59,6 +64,13 @@ public class LegendEntry {
public float formLineWidth = Float.NaN;
/**
+ * Line dash path effect used for shapes that consist of lines.
+ *
+ * Set to null to use the legend's default
+ */
+ public DashPathEffect formLineDashEffect = null;
+
+ /**
* The color for drawing the form
*/
public int formColor = ColorTemplate.COLOR_NONE;
diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/data/BaseDataSet.java b/MPChartLib/src/main/java/com/github/mikephil/charting/data/BaseDataSet.java
index 42212f44..f3107ebe 100644
--- a/MPChartLib/src/main/java/com/github/mikephil/charting/data/BaseDataSet.java
+++ b/MPChartLib/src/main/java/com/github/mikephil/charting/data/BaseDataSet.java
@@ -2,6 +2,7 @@ package com.github.mikephil.charting.data;
import android.content.Context;
import android.graphics.Color;
+import android.graphics.DashPathEffect;
import android.graphics.Typeface;
import com.github.mikephil.charting.components.Legend;
@@ -60,6 +61,7 @@ public abstract class BaseDataSet<T extends Entry> implements IDataSet<T> {
private Legend.LegendForm mForm = Legend.LegendForm.DEFAULT;
private float mFormSize = Float.NaN;
private float mFormLineWidth = Float.NaN;
+ private DashPathEffect mFormLineDashEffect = null;
/**
* if true, y-values are drawn on the chart
@@ -350,6 +352,15 @@ public abstract class BaseDataSet<T extends Entry> implements IDataSet<T> {
return mFormLineWidth;
}
+ public void setFormLineDashEffect(DashPathEffect dashPathEffect) {
+ mFormLineDashEffect = dashPathEffect;
+ }
+
+ @Override
+ public DashPathEffect getFormLineDashEffect() {
+ return mFormLineDashEffect;
+ }
+
@Override
public void setDrawValues(boolean enabled) {
this.mDrawValues = enabled;
diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/interfaces/datasets/IDataSet.java b/MPChartLib/src/main/java/com/github/mikephil/charting/interfaces/datasets/IDataSet.java
index ad432377..62c10b3f 100644
--- a/MPChartLib/src/main/java/com/github/mikephil/charting/interfaces/datasets/IDataSet.java
+++ b/MPChartLib/src/main/java/com/github/mikephil/charting/interfaces/datasets/IDataSet.java
@@ -1,5 +1,6 @@
package com.github.mikephil.charting.interfaces.datasets;
+import android.graphics.DashPathEffect;
import android.graphics.Typeface;
import com.github.mikephil.charting.components.Legend;
@@ -391,6 +392,13 @@ public interface IDataSet<T extends Entry> {
float getFormLineWidth();
/**
+ * The line dash path effect used for shapes that consist of lines.
+ *
+ * Return `null` to use the default legend form line dash effect.
+ */
+ DashPathEffect getFormLineDashEffect();
+
+ /**
* set this to true to draw y-values on the chart NOTE (for bar and
* linechart): if "maxvisiblecount" is reached, no values will be drawn even
* if this is enabled
diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/LegendRenderer.java b/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/LegendRenderer.java
index 14cf91ec..06dc0b60 100644
--- a/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/LegendRenderer.java
+++ b/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/LegendRenderer.java
@@ -2,8 +2,10 @@
package com.github.mikephil.charting.renderer;
import android.graphics.Canvas;
+import android.graphics.DashPathEffect;
import android.graphics.Paint;
import android.graphics.Paint.Align;
+import android.graphics.Path;
import android.graphics.Typeface;
import com.github.mikephil.charting.components.Legend;
@@ -50,7 +52,6 @@ public class LegendRenderer extends Renderer {
mLegendFormPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
mLegendFormPaint.setStyle(Paint.Style.FILL);
- mLegendFormPaint.setStrokeWidth(3f);
}
/**
@@ -106,6 +107,7 @@ public class LegendRenderer extends Renderer {
dataSet.getForm(),
dataSet.getFormSize(),
dataSet.getFormLineWidth(),
+ dataSet.getFormLineDashEffect(),
clrs.get(j)
));
}
@@ -117,6 +119,7 @@ public class LegendRenderer extends Renderer {
Legend.LegendForm.NONE,
Float.NaN,
Float.NaN,
+ null,
ColorTemplate.COLOR_NONE
));
}
@@ -132,6 +135,7 @@ public class LegendRenderer extends Renderer {
dataSet.getForm(),
dataSet.getFormSize(),
dataSet.getFormLineWidth(),
+ dataSet.getFormLineDashEffect(),
clrs.get(j)
));
}
@@ -143,6 +147,7 @@ public class LegendRenderer extends Renderer {
Legend.LegendForm.NONE,
Float.NaN,
Float.NaN,
+ null,
ColorTemplate.COLOR_NONE
));
}
@@ -158,6 +163,7 @@ public class LegendRenderer extends Renderer {
dataSet.getForm(),
dataSet.getFormSize(),
dataSet.getFormLineWidth(),
+ dataSet.getFormLineDashEffect(),
decreasingColor
));
@@ -166,6 +172,7 @@ public class LegendRenderer extends Renderer {
dataSet.getForm(),
dataSet.getFormSize(),
dataSet.getFormLineWidth(),
+ dataSet.getFormLineDashEffect(),
increasingColor
));
@@ -187,6 +194,7 @@ public class LegendRenderer extends Renderer {
dataSet.getForm(),
dataSet.getFormSize(),
dataSet.getFormLineWidth(),
+ dataSet.getFormLineDashEffect(),
clrs.get(j)
));
}
@@ -456,6 +464,8 @@ public class LegendRenderer extends Renderer {
}
}
+ private Path mLineFormPath = new Path();
+
/**
* Draws the Legend-form at the given position with the color at the given
* index.
@@ -477,6 +487,8 @@ public class LegendRenderer extends Renderer {
entry.formColor == 0)
return;
+ int restoreCount = c.save();
+
Legend.LegendForm form = entry.form;
if (form == Legend.LegendForm.DEFAULT)
form = legend.getForm();
@@ -497,10 +509,12 @@ public class LegendRenderer extends Renderer {
case DEFAULT:
case CIRCLE:
+ mLegendFormPaint.setStyle(Paint.Style.FILL);
c.drawCircle(x + half, y, half, mLegendFormPaint);
break;
case SQUARE:
+ mLegendFormPaint.setStyle(Paint.Style.FILL);
c.drawRect(x, y - half, x + formSize, y + half, mLegendFormPaint);
break;
@@ -509,12 +523,22 @@ public class LegendRenderer extends Renderer {
final float formLineWidth = Float.isNaN(entry.formLineWidth)
? legend.getFormLineWidth()
: entry.formLineWidth;
+ final DashPathEffect formLineDashEffect = entry.formLineDashEffect == null
+ ? legend.getFormLineDashEffect()
+ : entry.formLineDashEffect;
+ mLegendFormPaint.setStyle(Paint.Style.STROKE);
mLegendFormPaint.setStrokeWidth(formLineWidth);
+ mLegendFormPaint.setPathEffect(formLineDashEffect);
- c.drawLine(x, y, x + formSize, y, mLegendFormPaint);
+ mLineFormPath.reset();
+ mLineFormPath.moveTo(x, y);
+ mLineFormPath.lineTo(x + formSize, y);
+ c.drawPath(mLineFormPath, mLegendFormPaint);
}
break;
}
+
+ c.restoreToCount(restoreCount);
}
/**