Skip to content

Commit bedcc71

Browse files
committed
Fixed minor legend issues, improved example.
1 parent 13fdeda commit bedcc71

9 files changed

Lines changed: 72 additions & 37 deletions

File tree

‎MPChartExample/res/layout/list_item_piechart.xml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,6 @@
77
<com.github.mikephil.charting.charts.PieChart
88
android:id="@+id/chart"
99
android:layout_width="match_parent"
10-
android:layout_height="330dp" />
10+
android:layout_height="345dp" />
1111

1212
</LinearLayout>

‎MPChartExample/src/com/xxmassdeveloper/mpchartexample/ListViewMultiChartActivity.java‎

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -93,18 +93,35 @@ public int getViewTypeCount() {
9393
*/
9494
private ChartData generateDataLine(int cnt) {
9595

96-
ArrayList<Entry> entries = new ArrayList<Entry>();
96+
ArrayList<Entry> e1 = new ArrayList<Entry>();
9797

9898
for (int i = 0; i < 12; i++) {
99-
entries.add(new Entry((int) (Math.random() * 70) + 30, i));
99+
e1.add(new Entry((int) (Math.random() * 65) + 40, i));
100100
}
101101

102-
LineDataSet d = new LineDataSet(entries, "New DataSet " + cnt);
103-
d.setLineWidth(3f);
104-
d.setCircleSize(5f);
102+
LineDataSet d1 = new LineDataSet(e1, "New DataSet " + cnt + ", (1)");
103+
d1.setLineWidth(3f);
104+
d1.setCircleSize(5f);
105+
106+
ArrayList<Entry> e2 = new ArrayList<Entry>();
107+
108+
for (int i = 0; i < 12; i++) {
109+
e2.add(new Entry(e1.get(i).getVal() - 30, i));
110+
}
111+
112+
LineDataSet d2 = new LineDataSet(e2, "New DataSet " + cnt + ", (2)");
113+
d2.setLineWidth(3f);
114+
d2.setCircleSize(5f);
115+
d2.setColor(getResources().getColor(R.color.vordiplom_1));
116+
d2.setCircleColor(getResources().getColor(R.color.vordiplom_1));
117+
105118
// d.setColors(ColorTemplate.VORDIPLOM_COLORS, getApplicationContext());
106119

107-
LineData cd = new LineData(getMonths(), d);
120+
ArrayList<LineDataSet> sets = new ArrayList<LineDataSet>();
121+
sets.add(d1);
122+
sets.add(d2);
123+
124+
LineData cd = new LineData(getMonths(), sets);
108125
return cd;
109126
}
110127

@@ -141,7 +158,7 @@ private ChartData generateDataPie(int cnt) {
141158
entries.add(new Entry((int) (Math.random() * 70) + 30, i));
142159
}
143160

144-
PieDataSet d = new PieDataSet(entries, "New DataSet " + cnt);
161+
PieDataSet d = new PieDataSet(entries, "");
145162

146163
// space between slices
147164
d.setSliceSpace(5f);

‎MPChartExample/src/com/xxmassdeveloper/mpchartexample/notimportant/MainActivity.java‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import android.widget.ListView;
1919
import android.widget.TextView;
2020

21+
import com.github.mikephil.charting.utils.Utils;
2122
import com.xxmassdeveloper.mpchartexample.BarChartActivity;
2223
import com.xxmassdeveloper.mpchartexample.BarChartActivityMultiDataset;
2324
import com.xxmassdeveloper.mpchartexample.DrawChartActivity;
@@ -43,6 +44,9 @@ protected void onCreate(Bundle savedInstanceState) {
4344
WindowManager.LayoutParams.FLAG_FULLSCREEN);
4445
setContentView(R.layout.activity_main);
4546

47+
// initialize the utilities
48+
Utils.init(getResources());
49+
4650
ArrayList<ContentItem> objects = new ArrayList<ContentItem>();
4751

4852
objects.add(new ContentItem("Line Chart", "A simple demonstration of the linechart."));

‎MPChartLib/src/com/github/mikephil/charting/charts/PieChart.java‎

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,7 @@ protected void calculateOffsets() {
220220
if (mDrawLegend) {
221221

222222
mOffsetBottom = Math.max(mOffsetBottom, mLegend.getOffsetBottom());
223+
mOffsetRight = Math.max(mOffsetRight, mLegend.getOffsetRight() / 3 * 2);
223224
}
224225

225226
mLegend.setOffsetTop(mOffsetTop);
@@ -903,15 +904,7 @@ public void setCenterTextSize(float size) {
903904
* @param size
904905
*/
905906
public void setHoleRadius(final float percent) {
906-
907-
Handler h = new Handler();
908-
h.post(new Runnable() {
909-
910-
@Override
911-
public void run() {
912-
mHoleRadiusPercent = percent;
913-
}
914-
});
907+
mHoleRadiusPercent = percent;
915908
}
916909

917910
/**
@@ -923,14 +916,7 @@ public void run() {
923916
* @param percent
924917
*/
925918
public void setTransparentCircleRadius(final float percent) {
926-
Handler h = new Handler();
927-
h.post(new Runnable() {
928-
929-
@Override
930-
public void run() {
931-
mTransparentCircleRadius = percent;
932-
}
933-
});
919+
mTransparentCircleRadius = percent;
934920
}
935921

936922
@Override

‎MPChartLib/src/com/github/mikephil/charting/data/BarDataSet.java‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,11 @@ public DataSet copy() {
4747
}
4848

4949
BarDataSet copied = new BarDataSet(yVals, getLabel());
50+
copied.mColors = mColors;
5051
copied.mStackSize = mStackSize;
5152
copied.mBarSpace = mBarSpace;
52-
copied.mEntryCountStacks = mEntryCountStacks;
53+
copied.mBarShadowColor = mBarShadowColor;
54+
copied.mStackLabels = mStackLabels;
5355

5456
return copied;
5557
}

‎MPChartLib/src/com/github/mikephil/charting/data/LineDataSet.java‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@ public class LineDataSet extends DataSet {
3333
public LineDataSet(ArrayList<Entry> yVals, String label) {
3434
super(yVals, label);
3535

36-
mCircleSize = Utils.convertDpToPixel(4f);
36+
// mCircleSize = Utils.convertDpToPixel(4f);
37+
// mLineWidth = Utils.convertDpToPixel(1f);
3738

3839
mCircleColors = new ArrayList<Integer>();
3940

@@ -53,6 +54,13 @@ public DataSet copy() {
5354
}
5455

5556
LineDataSet copied = new LineDataSet(yVals, getLabel());
57+
copied.mColors = mColors;
58+
copied.mLineWidth = mLineWidth;
59+
copied.mCircleSize = mCircleSize;
60+
copied.mCircleColors = mCircleColors;
61+
copied.mDashPathEffect = mDashPathEffect;
62+
copied.mDrawCircles = mDrawCircles;
63+
copied.mDrawFilled = mDrawFilled;
5664
return copied;
5765
}
5866

‎MPChartLib/src/com/github/mikephil/charting/data/PieDataSet.java‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,12 @@ public class PieDataSet extends DataSet {
1111
private float mSliceSpace = 0f;
1212

1313
/** indicates the selection distance of a pie slice */
14-
private float mShift = 0f;
14+
private float mShift = 18f;
1515

1616
public PieDataSet(ArrayList<Entry> yVals, String label) {
1717
super(yVals, label);
1818

19-
mShift = Utils.convertDpToPixel(15f);
19+
// mShift = Utils.convertDpToPixel(12f);
2020
}
2121

2222
@Override
@@ -29,7 +29,9 @@ public DataSet copy() {
2929
}
3030

3131
PieDataSet copied = new PieDataSet(yVals, getLabel());
32+
copied.mColors = mColors;
3233
copied.mSliceSpace = mSliceSpace;
34+
copied.mShift = mShift;
3335
return copied;
3436
}
3537

@@ -61,7 +63,7 @@ public float getSliceSpace() {
6163

6264
/**
6365
* sets the distance the highlighted piechart-slice of this DataSet is
64-
* "shifted" away from the center of the chart, default 18f
66+
* "shifted" away from the center of the chart, default 12f
6567
*
6668
* @param shift
6769
*/

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
public class ScatterDataSet extends DataSet {
1212

1313
/** the size the scattershape will have, in screen pixels */
14-
private float mShapeSize = 8f;
14+
private float mShapeSize = 12f;
1515

1616
/**
1717
* the type of shape that is set to be drawn where the values are at,
@@ -28,7 +28,7 @@ public class ScatterDataSet extends DataSet {
2828
public ScatterDataSet(ArrayList<Entry> yVals, String label) {
2929
super(yVals, label);
3030

31-
mShapeSize = Utils.convertDpToPixel(8f);
31+
// mShapeSize = Utils.convertDpToPixel(8f);
3232
}
3333

3434
@Override
@@ -41,7 +41,10 @@ public DataSet copy() {
4141
}
4242

4343
ScatterDataSet copied = new ScatterDataSet(yVals, getLabel());
44+
copied.mColors = mColors;
4445
copied.mShapeSize = mShapeSize;
46+
copied.mScatterShape = mScatterShape;
47+
copied.mCustomScatterPath = mCustomScatterPath;
4548
return copied;
4649
}
4750

‎MPChartLib/src/com/github/mikephil/charting/utils/Utils.java‎

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,10 @@
1010
import java.util.ArrayList;
1111

1212
/**
13-
* utilities class that has some helper methods
13+
* Utilities class that has some helper methods. Needs to be initialized by
14+
* calling Utils.init(...) before usage. Inside the Chart.init() method, this is
15+
* done, if the Utils are used before that, Utils.init(...) needs to be called
16+
* manually.
1417
*
1518
* @author Philipp Jahoda
1619
*/
@@ -19,7 +22,7 @@ public abstract class Utils {
1922
private static Resources mRes;
2023

2124
/**
22-
* initialize method, called inside the Char.init() method.
25+
* initialize method, called inside the Chart.init() method.
2326
*
2427
* @param res
2528
*/
@@ -51,27 +54,37 @@ public static String formatDecimal(double number, int digits) {
5154

5255
/**
5356
* This method converts dp unit to equivalent pixels, depending on device
54-
* density.
57+
* density. NEEDS UTILS TO BE INITIALIZED BEFORE USAGE.
5558
*
5659
* @param dp A value in dp (density independent pixels) unit. Which we need
5760
* to convert into pixels
5861
* @return A float value to represent px equivalent to dp depending on
5962
* device density
6063
*/
6164
public static float convertDpToPixel(float dp) {
65+
66+
if (mRes == null)
67+
throw new IllegalStateException(
68+
"Utils NOT INITIALIZED. You need to call Utils.init(...) at least once before calling Utils.convertDpToPixel(...).");
69+
6270
DisplayMetrics metrics = mRes.getDisplayMetrics();
6371
float px = dp * (metrics.densityDpi / 160f);
6472
return px;
6573
}
6674

6775
/**
6876
* This method converts device specific pixels to density independent
69-
* pixels.
77+
* pixels. NEEDS UTILS TO BE INITIALIZED BEFORE USAGE.
7078
*
7179
* @param px A value in px (pixels) unit. Which we need to convert into db
7280
* @return A float value to represent dp equivalent to px value
7381
*/
7482
public static float convertPixelsToDp(float px) {
83+
84+
if (mRes == null)
85+
throw new IllegalStateException(
86+
"Utils NOT INITIALIZED. You need to call Utils.init(...) at least once before calling Utils.convertPixelsToDp(...).");
87+
7588
DisplayMetrics metrics = mRes.getDisplayMetrics();
7689
float dp = px / (metrics.densityDpi / 160f);
7790
return dp;
@@ -88,7 +101,7 @@ public static float convertPixelsToDp(float px) {
88101
public static int calcTextWidth(Paint paint, String demoText) {
89102
return (int) paint.measureText(demoText);
90103
}
91-
104+
92105
/**
93106
* calculates the approximate height of a text, depending on a demo text
94107
* avoid repeated calls (e.g. inside drawing methods)

0 commit comments

Comments
 (0)