Skip to content

Commit 71bd55b

Browse files
committed
Finished implementation of RadarChart (issue #65).
1 parent c7e4c27 commit 71bd55b

File tree

5 files changed

+109
-59
lines changed

5 files changed

+109
-59
lines changed

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

+12-9
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,9 @@ protected void onCreate(Bundle savedInstanceState) {
4242
mChart.setWebLineWidth(1.5f);
4343
mChart.setWebLineWidthInner(0.75f);
4444
mChart.setWebAlpha(100);
45-
45+
46+
mChart.setDrawYValues(false);
47+
4648
// create a custom MarkerView (extend MarkerView) and specify the layout
4749
// to use for it
4850
MyMarkerView mv = new MyMarkerView(this, R.layout.custom_marker_view);
@@ -55,7 +57,7 @@ protected void onCreate(Bundle savedInstanceState) {
5557
mChart.setMarkerView(mv);
5658

5759
setData();
58-
60+
5961
XLabels xl = mChart.getXLabels();
6062
xl.setTypeface(tf);
6163
xl.setTextSize(9f);
@@ -68,18 +70,18 @@ protected void onCreate(Bundle savedInstanceState) {
6870
// mChart.animateXY(1500, 1500);
6971

7072
Legend l = mChart.getLegend();
71-
l.setPosition(LegendPosition.BELOW_CHART_CENTER);
73+
l.setPosition(LegendPosition.RIGHT_OF_CHART);
7274
l.setTypeface(tf);
7375
l.setXEntrySpace(7f);
7476
l.setYEntrySpace(5f);
7577
}
76-
78+
7779
@Override
7880
public boolean onCreateOptionsMenu(Menu menu) {
7981
getMenuInflater().inflate(R.menu.radar, menu);
8082
return true;
8183
}
82-
84+
8385
@Override
8486
public boolean onOptionsItemSelected(MenuItem item) {
8587

@@ -147,7 +149,7 @@ public boolean onOptionsItemSelected(MenuItem item) {
147149
mChart.invalidate();
148150
break;
149151
}
150-
case R.id.actionToggleSpin: {
152+
case R.id.actionToggleSpin: {
151153
mChart.spin(2000, mChart.getRotationAngle(), mChart.getRotationAngle() + 360);
152154
break;
153155
}
@@ -156,7 +158,8 @@ public boolean onOptionsItemSelected(MenuItem item) {
156158
}
157159

158160
private String[] mParties = new String[] {
159-
"Party A", "Party B", "Party C", "Party D", "Party E", "Party F"
161+
"Party A", "Party B", "Party C", "Party D", "Party E", "Party F", "Party G", "Party H",
162+
"Party I"
160163
};
161164

162165
public void setData() {
@@ -185,12 +188,12 @@ public void setData() {
185188

186189
RadarDataSet set1 = new RadarDataSet(yVals1, "Set 1");
187190
set1.setColor(getResources().getColor(R.color.vordiplom_1));
188-
set1.setDrawFilled(true);
191+
set1.setDrawFilled(true);
189192
set1.setLineWidth(2f);
190193

191194
RadarDataSet set2 = new RadarDataSet(yVals2, "Set 2");
192195
set2.setColor(getResources().getColor(R.color.vordiplom_5));
193-
set2.setDrawFilled(true);
196+
set2.setDrawFilled(true);
194197
set2.setLineWidth(2f);
195198

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

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

+33
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import com.github.mikephil.charting.data.PieData;
2020
import com.github.mikephil.charting.data.PieDataSet;
2121
import com.github.mikephil.charting.utils.Utils;
22+
import com.github.mikephil.charting.utils.Legend.LegendPosition;
2223

2324
import java.util.ArrayList;
2425

@@ -199,6 +200,38 @@ protected void calcMinMax(boolean fixedValues) {
199200

200201
calcAngles();
201202
}
203+
204+
205+
@Override
206+
protected void calculateOffsets() {
207+
208+
if (mLegend == null)
209+
return;
210+
211+
// setup offsets for legend
212+
if (mLegend.getPosition() == LegendPosition.RIGHT_OF_CHART) {
213+
214+
mLegend.setOffsetRight(mLegend.getMaximumEntryLength(mLegendLabelPaint));
215+
mLegendLabelPaint.setTextAlign(Align.LEFT);
216+
217+
} else if (mLegend.getPosition() == LegendPosition.BELOW_CHART_LEFT
218+
|| mLegend.getPosition() == LegendPosition.BELOW_CHART_RIGHT
219+
|| mLegend.getPosition() == LegendPosition.BELOW_CHART_CENTER) {
220+
221+
mLegend.setOffsetBottom(mLegendLabelPaint.getTextSize() * 4f);
222+
}
223+
224+
if (mDrawLegend) {
225+
226+
mOffsetBottom = Math.max(mOffsetBottom, mLegend.getOffsetBottom());
227+
mOffsetRight = Math.max(mOffsetRight, mLegend.getOffsetRight() / 3 * 2);
228+
}
229+
230+
mLegend.setOffsetTop(mOffsetTop);
231+
mLegend.setOffsetLeft(mOffsetLeft);
232+
233+
applyCalculatedOffsets();
234+
}
202235

203236
/**
204237
* calculates the needed angles for the chart slices

MPChartLib/src/com/github/mikephil/charting/charts/PieRadarChartBase.java

+6-30
Original file line numberDiff line numberDiff line change
@@ -99,38 +99,14 @@ public void notifyDataSetChanged() {
9999
protected void drawAdditional() {
100100
// TODO Auto-generated method stub
101101
}
102-
103-
@Override
104-
protected void calculateOffsets() {
105-
106-
if (mLegend == null)
107-
return;
108-
109-
// setup offsets for legend
110-
if (mLegend.getPosition() == LegendPosition.RIGHT_OF_CHART) {
111-
112-
mLegend.setOffsetRight(mLegend.getMaximumEntryLength(mLegendLabelPaint));
113-
mLegendLabelPaint.setTextAlign(Align.LEFT);
114-
115-
} else if (mLegend.getPosition() == LegendPosition.BELOW_CHART_LEFT
116-
|| mLegend.getPosition() == LegendPosition.BELOW_CHART_RIGHT
117-
|| mLegend.getPosition() == LegendPosition.BELOW_CHART_CENTER) {
118-
119-
if(this instanceof RadarChart) mLegend.setOffsetBottom(mLegendLabelPaint.getTextSize() * 5.5f);
120-
else mLegend.setOffsetBottom(mLegendLabelPaint.getTextSize() * 4f);
121-
}
122-
123-
if (mDrawLegend) {
124-
125-
mOffsetBottom = Math.max(mOffsetBottom, mLegend.getOffsetBottom());
126-
mOffsetRight = Math.max(mOffsetRight, mLegend.getOffsetRight() / 3 * 2);
127-
}
128-
129-
mLegend.setOffsetTop(mOffsetTop);
130-
mLegend.setOffsetLeft(mOffsetLeft);
102+
103+
/**
104+
* Applys the newly calculated offsets to the matrices.
105+
*/
106+
protected void applyCalculatedOffsets() {
131107

132108
prepareContentRect();
133-
109+
134110
float scaleX = (float) ((getWidth() - mOffsetLeft - mOffsetRight) / mDeltaX);
135111
float scaleY = (float) ((getHeight() - mOffsetBottom - mOffsetTop) / mDeltaY);
136112

MPChartLib/src/com/github/mikephil/charting/charts/RadarChart.java

+58-19
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import android.graphics.Paint;
88
import android.graphics.Path;
99
import android.graphics.PointF;
10+
import android.graphics.Paint.Align;
1011
import android.util.AttributeSet;
1112
import android.util.Log;
1213

@@ -17,12 +18,13 @@
1718
import com.github.mikephil.charting.utils.Utils;
1819
import com.github.mikephil.charting.utils.XLabels;
1920
import com.github.mikephil.charting.utils.YLabels;
21+
import com.github.mikephil.charting.utils.Legend.LegendPosition;
2022

2123
import java.util.ArrayList;
2224

2325
/**
24-
* Implementation of the RadarChart, a "spidernet"-like chart. It works best when
25-
* displaying 5-10 entries per DataSet.
26+
* Implementation of the RadarChart, a "spidernet"-like chart. It works best
27+
* when displaying 5-10 entries per DataSet.
2628
*
2729
* @author Philipp Jahoda
2830
*/
@@ -101,13 +103,49 @@ protected void calcMinMax(boolean fixedValues) {
101103

102104
mYChartMin = 0;
103105
}
104-
106+
105107
@Override
106108
public void prepare() {
107109
super.prepare();
108-
110+
109111
prepareXLabels();
110112
}
113+
114+
@Override
115+
protected void calculateOffsets() {
116+
117+
if (mLegend == null)
118+
return;
119+
120+
// setup offsets for legend
121+
if (mLegend.getPosition() == LegendPosition.RIGHT_OF_CHART) {
122+
123+
mLegend.setOffsetRight(mLegend.getMaximumEntryLength(mLegendLabelPaint));
124+
mLegendLabelPaint.setTextAlign(Align.LEFT);
125+
126+
} else if (mLegend.getPosition() == LegendPosition.BELOW_CHART_LEFT
127+
|| mLegend.getPosition() == LegendPosition.BELOW_CHART_RIGHT
128+
|| mLegend.getPosition() == LegendPosition.BELOW_CHART_CENTER) {
129+
130+
mLegend.setOffsetBottom(mLegendLabelPaint.getTextSize() * 5.5f);
131+
}
132+
133+
mLegend.setOffsetTop(mOffsetTop);
134+
mLegend.setOffsetLeft(mOffsetLeft);
135+
136+
if (mDrawLegend) {
137+
138+
mOffsetBottom = Math.max(mXLabels.mLabelWidth, mOffsetBottom);
139+
mOffsetTop = Math.max(mXLabels.mLabelWidth, mOffsetTop);
140+
mOffsetRight = Math.max(mXLabels.mLabelWidth, mOffsetRight);
141+
mOffsetLeft = Math.max(mXLabels.mLabelWidth, mOffsetLeft);
142+
143+
mOffsetBottom = Math.max(mOffsetBottom, mLegend.getOffsetBottom());
144+
mOffsetRight = Math.max(mOffsetRight, mLegend.getOffsetRight() / 3 * 2);
145+
}
146+
147+
applyCalculatedOffsets();
148+
}
111149

112150
@Override
113151
protected void onDraw(Canvas canvas) {
@@ -119,7 +157,7 @@ protected void onDraw(Canvas canvas) {
119157
long starttime = System.currentTimeMillis();
120158

121159
prepareYLabels();
122-
160+
123161
drawXLabels();
124162

125163
drawWeb();
@@ -137,9 +175,9 @@ protected void onDraw(Canvas canvas) {
137175
drawLegend();
138176

139177
drawDescription();
140-
178+
141179
drawMarkers();
142-
180+
143181
canvas.drawBitmap(mDrawBitmap, 0, 0, mDrawPaint);
144182

145183
Log.i(LOG_TAG, "RadarChart DrawTime: " + (System.currentTimeMillis() - starttime) + " ms");
@@ -243,7 +281,6 @@ protected void drawData() {
243281
mDrawCanvas.drawPath(surface, mRenderPaint);
244282
}
245283
}
246-
247284

248285
/**
249286
* Calculates the required maximum y-value in order to be able to provide
@@ -308,7 +345,7 @@ private void drawYLabels() {
308345
mSeparateTousands), p.x + 10, p.y - 5, mYLabelPaint);
309346
}
310347
}
311-
348+
312349
/**
313350
* setup the x-axis labels
314351
*/
@@ -324,7 +361,7 @@ private void prepareXLabels() {
324361

325362
mXLabels.mLabelWidth = Utils.calcTextWidth(mXLabelPaint, a.toString());
326363
mXLabels.mLabelHeight = Utils.calcTextWidth(mXLabelPaint, "Q");
327-
364+
328365
Log.i(LOG_TAG, "xlabels prepared, width: " + mXLabels.mLabelWidth);
329366
}
330367

@@ -335,26 +372,28 @@ private void drawXLabels() {
335372

336373
if (!mDrawXLabels)
337374
return;
338-
375+
339376
mXLabelPaint.setTypeface(mXLabels.getTypeface());
340377
mXLabelPaint.setTextSize(mXLabels.getTextSize());
341378
mXLabelPaint.setColor(mXLabels.getTextColor());
342-
379+
343380
float sliceangle = getSliceAngle();
344381

345382
// calculate the factor that is needed for transforming the value to
346383
// pixels
347384
float factor = getFactor();
348385

349386
PointF c = getCenterOffsets();
350-
351-
for(int i = 0; i < mCurrentData.getXValCount(); i++) {
352-
387+
388+
for (int i = 0; i < mCurrentData.getXValCount(); i++) {
389+
353390
String text = mCurrentData.getXVals().get(i);
354-
355-
PointF p = getPosition(c, mYChartMax * factor, sliceangle * i + mRotationAngle);
356-
357-
mDrawCanvas.drawText(text, p.x, p.y, mXLabelPaint);
391+
392+
float angle = (sliceangle * i + mRotationAngle) % 360f;
393+
394+
PointF p = getPosition(c, mYChartMax * factor + mXLabels.mLabelWidth / 2f, angle);
395+
396+
mDrawCanvas.drawText(text, p.x, p.y + mXLabels.mLabelHeight / 2f, mXLabelPaint);
358397
}
359398
}
360399

MPChartLib/src/com/github/mikephil/charting/data/LineRadarDataSet.java

-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ public abstract class LineRadarDataSet extends BarLineScatterCandleRadarDataSet
2323
/** the width of the drawn data lines */
2424
private float mLineWidth = 1f;
2525

26-
2726
/** if true, the data will also be drawn filled */
2827
private boolean mDrawFilled = false;
2928

0 commit comments

Comments
 (0)