Skip to content

Commit 71a5a1e

Browse files
committed
Worked on RadarChart, implemented highlighting.
1 parent c22b47c commit 71a5a1e

14 files changed

Lines changed: 347 additions & 175 deletions

File tree

‎MPChartExample/AndroidManifest.xml‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
33
package="com.xxmassdeveloper.mpchartexample"
4-
android:versionCode="15"
5-
android:versionName="1.3.3" >
4+
android:versionCode="16"
5+
android:versionName="1.4.1" >
66

77
<uses-sdk
88
android:minSdkVersion="8"

‎MPChartExample/build.gradle‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ android {
77
applicationId 'com.xxmassdeveloper.mpchartexample'
88
minSdkVersion 16
99
targetSdkVersion 19
10-
versionCode 15
11-
versionName '1.3.3'
10+
versionCode 16
11+
versionName '1.4.1'
1212

1313
sourceSets {
1414
main {

‎MPChartExample/res/menu/radar.xml‎

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
3+
4+
<item
5+
android:id="@+id/actionToggleValues"
6+
android:title="Toggle Values">
7+
</item>
8+
<item
9+
android:id="@+id/actionToggleFilled"
10+
android:title="Toggle Filled">
11+
</item>
12+
<item
13+
android:id="@+id/actionToggleHighlight"
14+
android:title="Toggle Highlight">
15+
</item>
16+
<item
17+
android:id="@+id/actionSave"
18+
android:title="Save to SD">
19+
</item>
20+
<item
21+
android:id="@+id/actionToggleRotate"
22+
android:title="Toggle Rotation">
23+
</item>
24+
<item
25+
android:id="@+id/actionToggleYLabels"
26+
android:title="Toggle Y-Labels">
27+
</item>
28+
<item
29+
android:id="@+id/actionToggleXLabels"
30+
android:title="Toggle X-Labels">
31+
</item>
32+
33+
</menu>

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

Lines changed: 96 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,22 @@
33

44
import android.graphics.Typeface;
55
import android.os.Bundle;
6+
import android.view.Menu;
7+
import android.view.MenuItem;
68
import android.view.WindowManager;
7-
import android.widget.SeekBar;
8-
import android.widget.SeekBar.OnSeekBarChangeListener;
9-
import android.widget.TextView;
9+
import android.widget.Toast;
1010

1111
import com.github.mikephil.charting.charts.RadarChart;
1212
import com.github.mikephil.charting.data.Entry;
13+
import com.github.mikephil.charting.data.LineDataSet;
1314
import com.github.mikephil.charting.data.RadarData;
1415
import com.github.mikephil.charting.data.RadarDataSet;
15-
import com.github.mikephil.charting.utils.ColorTemplate;
16+
import com.github.mikephil.charting.data.filter.Approximator;
17+
import com.github.mikephil.charting.data.filter.Approximator.ApproximatorType;
1618
import com.github.mikephil.charting.utils.Legend;
1719
import com.github.mikephil.charting.utils.Legend.LegendPosition;
20+
import com.github.mikephil.charting.utils.XLabels;
1821
import com.github.mikephil.charting.utils.YLabels;
19-
import com.github.mikephil.charting.utils.YLabels.YLabelPosition;
2022
import com.xxmassdeveloper.mpchartexample.notimportant.DemoBase;
2123

2224
import java.util.ArrayList;
@@ -42,12 +44,18 @@ protected void onCreate(Bundle savedInstanceState) {
4244

4345
mChart.setWebLineWidth(2f);
4446
mChart.setWebLineWidthInner(0.75f);
47+
mChart.setWebAlpha(140);
4548

4649
setData();
50+
51+
XLabels xl = mChart.getXLabels();
52+
xl.setTypeface(tf);
53+
xl.setTextSize(9f);
4754

4855
YLabels yl = mChart.getYLabels();
4956
yl.setTypeface(tf);
5057
yl.setLabelCount(5);
58+
yl.setTextSize(9f);
5159

5260
// mChart.animateXY(1500, 1500);
5361

@@ -57,6 +65,83 @@ protected void onCreate(Bundle savedInstanceState) {
5765
l.setXEntrySpace(7f);
5866
l.setYEntrySpace(5f);
5967
}
68+
69+
@Override
70+
public boolean onCreateOptionsMenu(Menu menu) {
71+
getMenuInflater().inflate(R.menu.radar, menu);
72+
return true;
73+
}
74+
75+
@Override
76+
public boolean onOptionsItemSelected(MenuItem item) {
77+
78+
switch (item.getItemId()) {
79+
case R.id.actionToggleValues: {
80+
if (mChart.isDrawYValuesEnabled())
81+
mChart.setDrawYValues(false);
82+
else
83+
mChart.setDrawYValues(true);
84+
mChart.invalidate();
85+
break;
86+
}
87+
case R.id.actionToggleHighlight: {
88+
if (mChart.isHighlightEnabled())
89+
mChart.setHighlightEnabled(false);
90+
else
91+
mChart.setHighlightEnabled(true);
92+
mChart.invalidate();
93+
break;
94+
}
95+
case R.id.actionToggleRotate: {
96+
if (mChart.isRotationEnabled())
97+
mChart.setRotationEnabled(false);
98+
else
99+
mChart.setRotationEnabled(true);
100+
mChart.invalidate();
101+
break;
102+
}
103+
case R.id.actionToggleFilled: {
104+
105+
ArrayList<RadarDataSet> sets = (ArrayList<RadarDataSet>) mChart.getDataCurrent()
106+
.getDataSets();
107+
108+
for (RadarDataSet set : sets) {
109+
if (set.isDrawFilledEnabled())
110+
set.setDrawFilled(false);
111+
else
112+
set.setDrawFilled(true);
113+
}
114+
mChart.invalidate();
115+
break;
116+
}
117+
case R.id.actionSave: {
118+
if (mChart.saveToPath("title" + System.currentTimeMillis(), "")) {
119+
Toast.makeText(getApplicationContext(), "Saving SUCCESSFUL!",
120+
Toast.LENGTH_SHORT).show();
121+
} else
122+
Toast.makeText(getApplicationContext(), "Saving FAILED!", Toast.LENGTH_SHORT)
123+
.show();
124+
break;
125+
}
126+
case R.id.actionToggleXLabels: {
127+
if (mChart.isDrawXLabelsEnabled())
128+
mChart.setDrawXLabels(false);
129+
else
130+
mChart.setDrawXLabels(true);
131+
mChart.invalidate();
132+
break;
133+
}
134+
case R.id.actionToggleYLabels: {
135+
if (mChart.isDrawYLabelsEnabled())
136+
mChart.setDrawYLabels(false);
137+
else
138+
mChart.setDrawYLabels(true);
139+
mChart.invalidate();
140+
break;
141+
}
142+
}
143+
return true;
144+
}
60145

61146
private String[] mParties = new String[] {
62147
"Party A", "Party B", "Party C", "Party D", "Party E", "Party F"
@@ -65,34 +150,35 @@ protected void onCreate(Bundle savedInstanceState) {
65150
public void setData() {
66151

67152
float mult = 150;
153+
int cnt = 9;
68154

69155
ArrayList<Entry> yVals1 = new ArrayList<Entry>();
70156
ArrayList<Entry> yVals2 = new ArrayList<Entry>();
71157

72158
// IMPORTANT: In a PieChart, no values (Entry) should have the same
73159
// xIndex (even if from different DataSets), since no values can be
74160
// drawn above each other.
75-
for (int i = 0; i < 6; i++) {
161+
for (int i = 0; i < cnt; i++) {
76162
yVals1.add(new Entry((float) (Math.random() * mult) + mult / 2, i));
77163
}
78164

79-
for (int i = 0; i < 6; i++) {
165+
for (int i = 0; i < cnt; i++) {
80166
yVals2.add(new Entry((float) (Math.random() * mult) + mult / 2, i));
81167
}
82168

83169
ArrayList<String> xVals = new ArrayList<String>();
84170

85-
for (int i = 0; i < 6; i++)
171+
for (int i = 0; i < cnt; i++)
86172
xVals.add(mParties[i % mParties.length]);
87173

88174
RadarDataSet set1 = new RadarDataSet(yVals1, "Set 1");
89175
set1.setColor(getResources().getColor(R.color.vordiplom_1));
90-
// set1.setDrawFilled(true);
176+
set1.setDrawFilled(true);
91177
set1.setLineWidth(2f);
92178

93179
RadarDataSet set2 = new RadarDataSet(yVals2, "Set 2");
94180
set2.setColor(getResources().getColor(R.color.vordiplom_5));
95-
// set2.setDrawFilled(true);
181+
set2.setDrawFilled(true);
96182
set2.setLineWidth(2f);
97183

98184
ArrayList<RadarDataSet> sets = new ArrayList<RadarDataSet>();

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

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -840,31 +840,6 @@ public boolean isDrawHighlightArrowEnabled() {
840840
return mDrawHighlightArrow;
841841
}
842842

843-
@Override
844-
public void setPaint(Paint p, int which) {
845-
super.setPaint(p, which);
846-
847-
switch (which) {
848-
case PAINT_HIGHLIGHT_BAR:
849-
mHighlightPaint = p;
850-
break;
851-
}
852-
}
853-
854-
@Override
855-
public Paint getPaint(int which) {
856-
Paint p = super.getPaint(which);
857-
if (p != null)
858-
return p;
859-
860-
switch (which) {
861-
case PAINT_HIGHLIGHT_BAR:
862-
return mHighlightPaint;
863-
}
864-
865-
return null;
866-
}
867-
868843
/**
869844
* If set to true, all values are drawn above their bars, instead of below
870845
* their top.

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

Lines changed: 19 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -94,12 +94,6 @@ public abstract class BarLineChartBase extends Chart {
9494
/** paint for the line surrounding the chart */
9595
protected Paint mBorderPaint;
9696

97-
/** paint for the x-label values */
98-
protected Paint mXLabelPaint;
99-
100-
/** paint for the y-label values */
101-
protected Paint mYLabelPaint;
102-
10397
/** paint used for the limit lines */
10498
protected Paint mLimitLinePaint;
10599

@@ -1526,6 +1520,24 @@ public void setDrawXLabels(boolean enabled) {
15261520
public void setDrawYLabels(boolean enabled) {
15271521
mDrawYLabels = enabled;
15281522
}
1523+
1524+
/**
1525+
* Returns true if drawing y-labels is enabled, false if not.
1526+
*
1527+
* @return
1528+
*/
1529+
public boolean isDrawYLabelsEnabled() {
1530+
return mDrawYLabels;
1531+
}
1532+
1533+
/**
1534+
* Returns true if drawing x-labels is enabled, false if not.
1535+
*
1536+
* @return
1537+
*/
1538+
public boolean isDrawXLabelsEnabled() {
1539+
return mDrawXLabels;
1540+
}
15291541

15301542
/**
15311543
* Sets an array of positions where to draw the chart border lines (e.g. new
@@ -1615,7 +1627,7 @@ public Highlight getHighlightByTouchPoint(float x, float y) {
16151627

16161628
ArrayList<SelInfo> valsAtIndex = getYValsAtIndex(xIndex);
16171629

1618-
dataSetIndex = getClosestDataSetIndex(valsAtIndex, (float) yTouchVal);
1630+
dataSetIndex = Utils.getClosestDataSetIndex(valsAtIndex, (float) yTouchVal);
16191631

16201632
if (dataSetIndex == -1)
16211633
return null;
@@ -1627,31 +1639,6 @@ public Highlight getHighlightByTouchPoint(float x, float y) {
16271639
return new Highlight(xIndex, dataSetIndex);
16281640
}
16291641

1630-
/**
1631-
* Returns the index of the DataSet that contains the closest value on the
1632-
* y-axis.
1633-
*
1634-
* @param valsAtIndex all the values at a specific index
1635-
* @return
1636-
*/
1637-
private int getClosestDataSetIndex(ArrayList<SelInfo> valsAtIndex, float val) {
1638-
1639-
int index = -1;
1640-
float distance = Float.MAX_VALUE;
1641-
1642-
for (int i = 0; i < valsAtIndex.size(); i++) {
1643-
1644-
float cdistance = Math.abs((float) valsAtIndex.get(i).val - val);
1645-
if (cdistance < distance) {
1646-
index = valsAtIndex.get(i).dataSetIndex;
1647-
distance = cdistance;
1648-
}
1649-
}
1650-
1651-
// Log.i(LOG_TAG, "Closest DataSet index: " + index);
1652-
1653-
return index;
1654-
}
16551642

16561643
/**
16571644
* Returns the x and y values in the chart at the given touch point
@@ -1916,12 +1903,6 @@ public void setPaint(Paint p, int which) {
19161903
case PAINT_BORDER:
19171904
mBorderPaint = p;
19181905
break;
1919-
case PAINT_XLABEL:
1920-
mXLabelPaint = p;
1921-
break;
1922-
case PAINT_YLABEL:
1923-
mYLabelPaint = p;
1924-
break;
19251906
case PAINT_LIMIT_LINE:
19261907
mLimitLinePaint = p;
19271908
break;
@@ -1941,10 +1922,6 @@ public Paint getPaint(int which) {
19411922
return mGridBackgroundPaint;
19421923
case PAINT_BORDER:
19431924
return mBorderPaint;
1944-
case PAINT_XLABEL:
1945-
return mXLabelPaint;
1946-
case PAINT_YLABEL:
1947-
return mYLabelPaint;
19481925
case PAINT_LIMIT_LINE:
19491926
return mLimitLinePaint;
19501927
}

0 commit comments

Comments
 (0)