Skip to content

Commit af114d1

Browse files
committed
Merge master
2 parents f01b906 + 0f3f4b7 commit af114d1

20 files changed

Lines changed: 710 additions & 262 deletions

MPChartExample/src/com/xxmassdeveloper/mpchartexample/ScatterChartActivity.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
import android.widget.TextView;
1313

1414
import com.github.mikephil.charting.charts.ScatterChart;
15-
import com.github.mikephil.charting.charts.ScatterChart.ScatterShape;
1615
import com.github.mikephil.charting.components.Legend;
1716
import com.github.mikephil.charting.components.Legend.LegendPosition;
1817
import com.github.mikephil.charting.components.XAxis;
@@ -24,6 +23,7 @@
2423
import com.github.mikephil.charting.interfaces.datasets.IScatterDataSet;
2524
import com.github.mikephil.charting.listener.OnChartValueSelectedListener;
2625
import com.github.mikephil.charting.utils.ColorTemplate;
26+
import com.xxmassdeveloper.mpchartexample.custom.CustomScatterShapeRenderer;
2727
import com.xxmassdeveloper.mpchartexample.notimportant.DemoBase;
2828

2929
import java.util.ArrayList;
@@ -54,6 +54,7 @@ protected void onCreate(Bundle savedInstanceState) {
5454

5555
mChart = (ScatterChart) findViewById(R.id.chart1);
5656
mChart.setDescription("");
57+
mChart.addShapeRenderer(new CustomScatterShapeRenderer(), CustomScatterShapeRenderer.IDENTIFIER);
5758

5859
mChart.setOnChartValueSelectedListener(this);
5960

@@ -179,15 +180,15 @@ public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
179180

180181
// create a dataset and give it a type
181182
ScatterDataSet set1 = new ScatterDataSet(yVals1, "DS 1");
182-
set1.setScatterShape(ScatterShape.SQUARE);
183+
set1.setScatterShape(ScatterChart.ScatterShape.SQUARE);
183184
set1.setColor(ColorTemplate.COLORFUL_COLORS[0]);
184185
ScatterDataSet set2 = new ScatterDataSet(yVals2, "DS 2");
185-
set2.setScatterShape(ScatterShape.CIRCLE);
186+
set2.setScatterShape(ScatterChart.ScatterShape.CIRCLE);
186187
set2.setScatterShapeHoleColor(ColorTemplate.COLORFUL_COLORS[3]);
187188
set2.setScatterShapeHoleRadius(3f);
188189
set2.setColor(ColorTemplate.COLORFUL_COLORS[1]);
189190
ScatterDataSet set3 = new ScatterDataSet(yVals3, "DS 3");
190-
set3.setScatterShape(ScatterShape.CROSS);
191+
set3.setScatterShape(CustomScatterShapeRenderer.IDENTIFIER);
191192
set3.setColor(ColorTemplate.COLORFUL_COLORS[2]);
192193

193194
set1.setScatterShapeSize(8f);
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package com.xxmassdeveloper.mpchartexample.custom;
2+
3+
import android.graphics.Canvas;
4+
import android.graphics.Paint;
5+
6+
import com.github.mikephil.charting.buffer.ScatterBuffer;
7+
import com.github.mikephil.charting.interfaces.datasets.IScatterDataSet;
8+
import com.github.mikephil.charting.renderer.scatter.ShapeRenderer;
9+
import com.github.mikephil.charting.utils.ViewPortHandler;
10+
11+
/**
12+
* Custom shape renderer that draws a single line.
13+
* Created by philipp on 26/06/16.
14+
*/
15+
public class CustomScatterShapeRenderer implements ShapeRenderer {
16+
17+
public static final String IDENTIFIER = "single_line";
18+
19+
@Override
20+
public void renderShape(Canvas c, IScatterDataSet dataSet, ViewPortHandler viewPortHandler, ScatterBuffer buffer, Paint
21+
renderPaint, float shapeSize) {
22+
23+
final float shapeHalf = shapeSize / 2f;
24+
25+
for (int i = 0; i < buffer.size(); i += 2) {
26+
27+
if (!viewPortHandler.isInBoundsRight(buffer.buffer[i]))
28+
break;
29+
30+
if (!viewPortHandler.isInBoundsLeft(buffer.buffer[i])
31+
|| !viewPortHandler.isInBoundsY(buffer.buffer[i + 1]))
32+
continue;
33+
34+
renderPaint.setColor(dataSet.getColor(i / 2));
35+
36+
c.drawLine(
37+
buffer.buffer[i] - shapeHalf,
38+
buffer.buffer[i + 1] - shapeHalf,
39+
buffer.buffer[i] + shapeHalf,
40+
buffer.buffer[i + 1] + shapeHalf,
41+
renderPaint);
42+
}
43+
}
44+
}

MPChartExample/src/com/xxmassdeveloper/mpchartexample/fragments/SimpleFragment.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
import android.view.ViewGroup;
1010

1111
import com.github.mikephil.charting.charts.ScatterChart;
12-
import com.github.mikephil.charting.charts.ScatterChart.ScatterShape;
1312
import com.github.mikephil.charting.data.BarData;
1413
import com.github.mikephil.charting.data.BarDataSet;
1514
import com.github.mikephil.charting.data.BarEntry;
@@ -71,7 +70,7 @@ protected ScatterData generateScatterData(int dataSets, float range, int count)
7170

7271
ArrayList<IScatterDataSet> sets = new ArrayList<IScatterDataSet>();
7372

74-
ScatterShape[] shapes = ScatterChart.getAllPossibleShapes();
73+
ScatterChart.ScatterShape[] shapes = ScatterChart.ScatterShape.getAllDefaultShapes();
7574

7675
for(int i = 0; i < dataSets; i++) {
7776

MPChartLib/src/main/java/com/github/mikephil/charting/charts/CombinedChart.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import com.github.mikephil.charting.highlight.CombinedHighlighter;
1414
import com.github.mikephil.charting.interfaces.dataprovider.CombinedDataProvider;
1515
import com.github.mikephil.charting.renderer.CombinedChartRenderer;
16+
import com.github.mikephil.charting.renderer.scatter.ShapeRenderer;
1617

1718
/**
1819
* This chart class allows the combination of lines, bars, scatter and candle
@@ -22,6 +23,8 @@
2223
*/
2324
public class CombinedChart extends BarLineChartBase<CombinedData> implements CombinedDataProvider {
2425

26+
private ScatterChart.ShapeRendererHandler mShapeRendererHandler;
27+
2528
/**
2629
* if set to true, all values are drawn above their bars, instead of below
2730
* their top
@@ -68,6 +71,8 @@ public CombinedChart(Context context, AttributeSet attrs, int defStyle) {
6871
protected void init() {
6972
super.init();
7073

74+
mShapeRendererHandler = new ScatterChart.ShapeRendererHandler();
75+
7176
setHighlighter(new CombinedHighlighter(this, this));
7277

7378
// Old default behaviour
@@ -124,6 +129,16 @@ public BubbleData getBubbleData() {
124129
return mData.getBubbleData();
125130
}
126131

132+
@Override
133+
public void addShapeRenderer(ShapeRenderer shapeRenderer, String shapeIdentifier) {
134+
mShapeRendererHandler.addShapeRenderer(shapeRenderer, shapeIdentifier);
135+
}
136+
137+
@Override
138+
public ShapeRenderer getShapeRenderer(String shapeIdentifier) {
139+
return mShapeRendererHandler.getShapeRenderer(shapeIdentifier);
140+
}
141+
127142
@Override
128143
public boolean isDrawBarShadowEnabled() {
129144
return mDrawBarShadow;

MPChartLib/src/main/java/com/github/mikephil/charting/charts/ScatterChart.java

Lines changed: 93 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,16 @@
77
import com.github.mikephil.charting.data.ScatterData;
88
import com.github.mikephil.charting.interfaces.dataprovider.ScatterDataProvider;
99
import com.github.mikephil.charting.renderer.ScatterChartRenderer;
10+
import com.github.mikephil.charting.renderer.scatter.ChevronDownShapeRenderer;
11+
import com.github.mikephil.charting.renderer.scatter.ChevronUpShapeRenderer;
12+
import com.github.mikephil.charting.renderer.scatter.CircleShapeRenderer;
13+
import com.github.mikephil.charting.renderer.scatter.CrossShapeRenderer;
14+
import com.github.mikephil.charting.renderer.scatter.ShapeRenderer;
15+
import com.github.mikephil.charting.renderer.scatter.SquareShapeRenderer;
16+
import com.github.mikephil.charting.renderer.scatter.TriangleShapeRenderer;
17+
import com.github.mikephil.charting.renderer.scatter.XShapeRenderer;
18+
19+
import java.util.HashMap;
1020

1121
/**
1222
* The ScatterChart. Draws dots, triangles, squares and custom shapes into the
@@ -17,12 +27,8 @@
1727
*/
1828
public class ScatterChart extends BarLineChartBase<ScatterData> implements ScatterDataProvider {
1929

20-
/**
21-
* enum that defines the shape that is drawn where the values are
22-
*/
23-
public enum ScatterShape {
24-
SQUARE, CIRCLE, TRIANGLE, CROSS, X,
25-
}
30+
protected ShapeRendererHandler mShapeRendererHandler;
31+
2632

2733
public ScatterChart(Context context) {
2834
super(context);
@@ -36,25 +42,96 @@ public ScatterChart(Context context, AttributeSet attrs, int defStyle) {
3642
super(context, attrs, defStyle);
3743
}
3844

45+
3946
@Override
4047
protected void init() {
4148
super.init();
4249

50+
mShapeRendererHandler = new ShapeRendererHandler();
51+
4352
mRenderer = new ScatterChartRenderer(this, mAnimator, mViewPortHandler);
4453
}
4554

55+
@Override
56+
public ScatterData getScatterData() {
57+
return mData;
58+
}
59+
60+
@Override
61+
public void addShapeRenderer(ShapeRenderer shapeRenderer, String shapeIdentifier) {
62+
mShapeRendererHandler.addShapeRenderer(shapeRenderer, shapeIdentifier);
63+
}
64+
65+
@Override
66+
public ShapeRenderer getShapeRenderer(String shapeIdentifier) {
67+
return mShapeRendererHandler.getShapeRenderer(shapeIdentifier);
68+
}
69+
70+
public enum ScatterShape {
71+
SQUARE("SQUARE"), CIRCLE("CIRCLE"), TRIANGLE("TRIANGLE"), CROSS("CROSS"), X("X"), CHEVRON_UP("CHEVRON_UP"),
72+
CHEVRON_DOWN("CHEVRON_DOWN");
73+
74+
private final String shapeIdentifier;
75+
76+
ScatterShape(final String shapeIdentifier) {
77+
this.shapeIdentifier = shapeIdentifier;
78+
}
79+
80+
@Override
81+
public String toString() {
82+
return shapeIdentifier;
83+
}
84+
85+
public static ScatterShape[] getAllDefaultShapes() {
86+
return new ScatterShape[]{SQUARE, CIRCLE, TRIANGLE, CROSS, X, CHEVRON_UP, CHEVRON_DOWN};
87+
}
88+
}
89+
90+
4691
/**
47-
* Returns all possible predefined ScatterShapes.
48-
*
49-
* @return
92+
* Handler class for all different ShapeRenderers.
5093
*/
51-
public static ScatterShape[] getAllPossibleShapes() {
52-
return new ScatterShape[]{
53-
ScatterShape.SQUARE, ScatterShape.CIRCLE, ScatterShape.TRIANGLE, ScatterShape.CROSS
54-
};
55-
}
94+
protected static class ShapeRendererHandler {
5695

57-
public ScatterData getScatterData() {
58-
return mData;
96+
/**
97+
* Dictionary of ShapeRenderer which are responsible for drawing custom shapes.
98+
* Can add to it your custom shapes.
99+
* CustomShapeRenderer Implements ShapeRenderer{}
100+
*/
101+
protected HashMap<String, ShapeRenderer> shapeRendererList;
102+
103+
public ShapeRendererHandler() {
104+
initShapeRenderers();
105+
}
106+
107+
/**
108+
* Adds a new ShapeRenderer and the shapeIdentifier it is responsible for drawing.
109+
* This shapeIdentifier should correspond to a DataSet with the same identifier.
110+
*
111+
* @param shapeRenderer
112+
* @param shapeIdentifier
113+
*/
114+
public void addShapeRenderer(ShapeRenderer shapeRenderer, String shapeIdentifier) {
115+
shapeRendererList.put(shapeIdentifier, shapeRenderer);
116+
}
117+
118+
public ShapeRenderer getShapeRenderer(String shapeIdentifier) {
119+
return shapeRendererList.get(shapeIdentifier);
120+
}
121+
122+
/**
123+
* Init default ShapeRenderers.
124+
*/
125+
protected void initShapeRenderers() {
126+
shapeRendererList = new HashMap<>();
127+
128+
shapeRendererList.put(ScatterShape.SQUARE.toString(), new SquareShapeRenderer());
129+
shapeRendererList.put(ScatterShape.CIRCLE.toString(), new CircleShapeRenderer());
130+
shapeRendererList.put(ScatterShape.TRIANGLE.toString(), new TriangleShapeRenderer());
131+
shapeRendererList.put(ScatterShape.CROSS.toString(), new CrossShapeRenderer());
132+
shapeRendererList.put(ScatterShape.X.toString(), new XShapeRenderer());
133+
shapeRendererList.put(ScatterShape.CHEVRON_UP.toString(), new ChevronUpShapeRenderer());
134+
shapeRendererList.put(ScatterShape.CHEVRON_DOWN.toString(), new ChevronDownShapeRenderer());
135+
}
59136
}
60137
}

MPChartLib/src/main/java/com/github/mikephil/charting/components/ComponentBase.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ public abstract class ComponentBase {
4343
*/
4444
protected int mTextColor = Color.BLACK;
4545

46+
4647
public ComponentBase() {
4748

4849
}
@@ -130,6 +131,7 @@ public float getTextSize() {
130131
return mTextSize;
131132
}
132133

134+
133135
/**
134136
* Sets the text color to use for the labels. Make sure to use
135137
* getResources().getColor(...) when using a color from the resources.

MPChartLib/src/main/java/com/github/mikephil/charting/data/ScatterDataSet.java

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11

22
package com.github.mikephil.charting.data;
33

4-
import com.github.mikephil.charting.charts.ScatterChart.ScatterShape;
4+
import com.github.mikephil.charting.charts.ScatterChart;
55
import com.github.mikephil.charting.interfaces.datasets.IScatterDataSet;
66
import com.github.mikephil.charting.utils.ColorTemplate;
77

@@ -19,7 +19,7 @@ public class ScatterDataSet extends LineScatterCandleRadarDataSet<Entry> impleme
1919
* the type of shape that is set to be drawn where the values are at,
2020
* default ScatterShape.SQUARE
2121
*/
22-
private ScatterShape mScatterShape = ScatterShape.SQUARE;
22+
private String mScatterShape = ScatterChart.ScatterShape.SQUARE.toString();
2323

2424
/**
2525
* The radius of the hole in the shape (applies to Square, Circle and Triangle)
@@ -82,17 +82,29 @@ public float getScatterShapeSize() {
8282
return mShapeSize;
8383
}
8484

85+
8586
/**
86-
* Sets the shape that is drawn on the position where the values are at.
87+
* Sets the shapeIdentifier that this DataSet should be drawn with.
88+
* Make sure the ScatterChart has a renderer capable of rendering the provided identifier.
8789
*
8890
* @param shape
8991
*/
90-
public void setScatterShape(ScatterShape shape) {
91-
mScatterShape = shape;
92+
public void setScatterShape(ScatterChart.ScatterShape shape) {
93+
mScatterShape = shape.toString();
94+
}
95+
96+
/**
97+
* Sets the shapeIdentifier that this DataSet should be drawn with.
98+
* Make sure the ScatterChart has a renderer capable of rendering the provided identifier.
99+
*
100+
* @param shapeIdentifier
101+
*/
102+
public void setScatterShape(String shapeIdentifier) {
103+
mScatterShape = shapeIdentifier;
92104
}
93105

94106
@Override
95-
public ScatterShape getScatterShape() {
107+
public String getScatterShape() {
96108
return mScatterShape;
97109
}
98110

0 commit comments

Comments
 (0)