Skip to content

Commit 43ca046

Browse files
committed
Fixed issues concerning background color (issue PhilJay#9, issue PhilJay#53). Fixed issue concerning zero values. General bugfixes and improvements.
1 parent cb5ca58 commit 43ca046

19 files changed

Lines changed: 360 additions & 226 deletions

MPChartExample/res/layout/activity_colored_lines.xml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
33
android:layout_width="match_parent"
44
android:layout_height="match_parent"
5-
android:background="@android:color/white"
65
android:orientation="vertical" >
76

87
<com.github.mikephil.charting.charts.LineChart

MPChartExample/res/layout/activity_linechart.xml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
33
android:layout_width="match_parent"
4-
android:layout_height="match_parent"
5-
android:background="@android:color/darker_gray" >
4+
android:layout_height="match_parent">
65

76
<com.github.mikephil.charting.charts.LineChart
87
android:id="@+id/chart1"

MPChartExample/res/layout/activity_linechart_noseekbar.xml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
33
android:layout_width="match_parent"
4-
android:layout_height="match_parent"
5-
android:background="@android:color/darker_gray" >
4+
android:layout_height="match_parent" >
65

76
<com.github.mikephil.charting.charts.LineChart
87
android:id="@+id/chart1"

MPChartExample/res/layout/custom_marker_view.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
android:layout_height="wrap_content"
1111
android:layout_centerHorizontal="true"
1212
android:layout_marginTop="7dp"
13+
android:layout_marginLeft="5dp"
14+
android:layout_marginRight="5dp"
1315
android:text=""
1416
android:textSize="12dp"
1517
android:textColor="@android:color/white"

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

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@
3333

3434
import java.util.ArrayList;
3535

36-
public class BarChartActivity extends DemoBase implements OnSeekBarChangeListener, OnChartValueSelectedListener {
36+
public class BarChartActivity extends DemoBase implements OnSeekBarChangeListener,
37+
OnChartValueSelectedListener {
3738

3839
private BarChart mChart;
3940
private SeekBar mSeekBarX, mSeekBarY;
@@ -59,7 +60,7 @@ protected void onCreate(Bundle savedInstanceState) {
5960
mChart.setDrawYValues(true);
6061

6162
mChart.setDescription("");
62-
63+
6364
// if more than 60 entries are displayed in the chart, no values will be
6465
// drawn
6566
mChart.setMaxVisibleValueCount(60);
@@ -71,17 +72,17 @@ protected void onCreate(Bundle savedInstanceState) {
7172
mChart.setPinchZoom(false);
7273

7374
// draw shadows for each bar that show the maximum value
74-
// mChart.setDrawBarShadow(true);
75+
// mChart.setDrawBarShadow(true);
7576

7677
mChart.setUnit(" €");
77-
78+
7879
// mChart.setDrawXLabels(false);
7980

8081
mChart.setDrawGridBackground(false);
8182
mChart.setDrawHorizontalGrid(true);
8283
mChart.setDrawVerticalGrid(false);
8384
// mChart.setDrawYLabels(false);
84-
85+
8586
// sets the text size of the values inside the chart
8687
mChart.setValueTextSize(10f);
8788

@@ -95,20 +96,20 @@ protected void onCreate(Bundle savedInstanceState) {
9596
xl.setPosition(XLabelPosition.BOTTOM);
9697
xl.setCenterXLabelText(true);
9798
xl.setTypeface(tf);
98-
99+
99100
YLabels yl = mChart.getYLabels();
100101
yl.setTypeface(tf);
101102
yl.setLabelCount(8);
102103
yl.setPosition(YLabelPosition.BOTH_SIDED);
103104

104105
mChart.setValueTypeface(tf);
105-
106+
106107
setData(12, 50);
107108

108109
// setting data
109110
mSeekBarY.setProgress(50);
110111
mSeekBarX.setProgress(12);
111-
112+
112113
mSeekBarY.setOnSeekBarChangeListener(this);
113114
mSeekBarX.setOnSeekBarChangeListener(this);
114115

@@ -231,7 +232,7 @@ public boolean onOptionsItemSelected(MenuItem item) {
231232

232233
@Override
233234
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
234-
235+
235236
tvX.setText("" + (mSeekBarX.getProgress() + 1));
236237
tvY.setText("" + (mSeekBarY.getProgress()));
237238

@@ -250,7 +251,7 @@ public void onStopTrackingTouch(SeekBar seekBar) {
250251
// TODO Auto-generated method stub
251252

252253
}
253-
254+
254255
private void setData(int count, float range) {
255256

256257
ArrayList<String> xVals = new ArrayList<String>();
@@ -268,25 +269,29 @@ private void setData(int count, float range) {
268269

269270
BarDataSet set1 = new BarDataSet(yVals1, "DataSet");
270271
set1.setBarSpacePercent(35f);
271-
272+
272273
ArrayList<BarDataSet> dataSets = new ArrayList<BarDataSet>();
273274
dataSets.add(set1);
274275

275276
BarData data = new BarData(xVals, dataSets);
276277

277278
mChart.setData(data);
278279
}
279-
280+
280281
@SuppressLint("NewApi")
281282
@Override
282283
public void onValueSelected(Entry e, int dataSetIndex) {
283-
284+
285+
if (e == null)
286+
return;
287+
284288
RectF bounds = mChart.getBarBounds((BarEntry) e);
285289
PointF position = mChart.getPosition(e);
286-
290+
287291
Log.i("bounds", bounds.toString());
288292
Log.i("position", position.toString());
289293
}
290-
291-
public void onNothingSelected() {};
294+
295+
public void onNothingSelected() {
296+
};
292297
}

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33

44
import android.graphics.Color;
55
import android.os.Bundle;
6-
import android.util.Log;
76
import android.view.Menu;
87
import android.view.MenuItem;
98
import android.view.WindowManager;

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ protected void onCreate(Bundle savedInstanceState) {
5050

5151
mChart = (LineChart) findViewById(R.id.chart1);
5252
mChart.setOnChartValueSelectedListener(this);
53+
54+
mChart.setDrawGridBackground(false);
5355

5456
// mChart.setStartAtZero(true);
5557

@@ -231,6 +233,8 @@ public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
231233

232234
// make the first DataSet dashed
233235
dataSets.get(0).enableDashedLine(10, 10, 0);
236+
dataSets.get(0).setColors(ColorTemplate.VORDIPLOM_COLORS);
237+
dataSets.get(0).setCircleColors(ColorTemplate.VORDIPLOM_COLORS);
234238

235239
LineData data = new LineData(xVals, dataSets);
236240
mChart.setData(data);

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

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

22
package com.xxmassdeveloper.mpchartexample;
33

4+
import android.graphics.Color;
45
import android.graphics.Typeface;
56
import android.os.Bundle;
67
import android.util.Log;
@@ -13,6 +14,8 @@
1314

1415
import com.github.mikephil.charting.charts.PieChart;
1516
import com.github.mikephil.charting.data.Entry;
17+
import com.github.mikephil.charting.data.LineData;
18+
import com.github.mikephil.charting.data.LineDataSet;
1619
import com.github.mikephil.charting.data.PieData;
1720
import com.github.mikephil.charting.data.PieDataSet;
1821
import com.github.mikephil.charting.interfaces.OnChartValueSelectedListener;
@@ -48,6 +51,9 @@ protected void onCreate(Bundle savedInstanceState) {
4851

4952
mChart = (PieChart) findViewById(R.id.chart1);
5053

54+
// change the color of the center-hole
55+
mChart.setHoleColor(Color.rgb(235, 235, 235));
56+
5157
Typeface tf = Typeface.createFromAsset(getAssets(), "OpenSans-Regular.ttf");
5258

5359
mChart.setValueTypeface(tf);
@@ -78,9 +84,9 @@ protected void onCreate(Bundle savedInstanceState) {
7884
// add a selection listener
7985
mChart.setOnChartValueSelectedListener(this);
8086
// mChart.setTouchEnabled(false);
81-
87+
8288
setData(3, 100);
83-
89+
8490
mChart.animateXY(1500, 1500);
8591
// mChart.spin(2000, 0, 360);
8692

@@ -91,6 +97,47 @@ protected void onCreate(Bundle savedInstanceState) {
9197

9298
}
9399

100+
private void removeLastEntry() {
101+
102+
PieData data = mChart.getDataOriginal();
103+
104+
if (data != null) {
105+
106+
PieDataSet set = data.getDataSet();
107+
108+
if (set != null) {
109+
110+
Entry e = set.getEntryForXIndex(set.getEntryCount() - 1);
111+
112+
data.removeEntry(e, 0);
113+
// or remove by index
114+
// mData.removeEntry(xIndex, dataSetIndex);
115+
116+
mChart.notifyDataSetChanged();
117+
mChart.invalidate();
118+
}
119+
}
120+
}
121+
122+
private void addEntry() {
123+
124+
PieData data = mChart.getDataOriginal();
125+
126+
if(data != null) {
127+
128+
PieDataSet set = data.getDataSet();
129+
// set.addEntry(...);
130+
131+
data.addEntry(new Entry((float) (Math.random() * 50) + 50f, set.getEntryCount()), 0);
132+
133+
// let the chart know it's data has changed
134+
mChart.notifyDataSetChanged();
135+
136+
// redraw the chart
137+
mChart.invalidate();
138+
}
139+
}
140+
94141
@Override
95142
public boolean onCreateOptionsMenu(Menu menu) {
96143
getMenuInflater().inflate(R.menu.pie, menu);
@@ -102,19 +149,21 @@ public boolean onOptionsItemSelected(MenuItem item) {
102149

103150
switch (item.getItemId()) {
104151
case R.id.actionToggleValues: {
105-
if (mChart.isDrawYValuesEnabled())
106-
mChart.setDrawYValues(false);
107-
else
108-
mChart.setDrawYValues(true);
109-
mChart.invalidate();
152+
// if (mChart.isDrawYValuesEnabled())
153+
// mChart.setDrawYValues(false);
154+
// else
155+
// mChart.setDrawYValues(true);
156+
// mChart.invalidate();
157+
removeLastEntry();
110158
break;
111159
}
112160
case R.id.actionTogglePercent: {
113-
if (mChart.isUsePercentValuesEnabled())
114-
mChart.setUsePercentValues(false);
115-
else
116-
mChart.setUsePercentValues(true);
117-
mChart.invalidate();
161+
// if (mChart.isUsePercentValuesEnabled())
162+
// mChart.setUsePercentValues(false);
163+
// else
164+
// mChart.setUsePercentValues(true);
165+
// mChart.invalidate();
166+
addEntry();
118167
break;
119168
}
120169
case R.id.actionToggleHole: {
@@ -163,7 +212,7 @@ public boolean onOptionsItemSelected(MenuItem item) {
163212
}
164213

165214
private String[] mParties = new String[] {
166-
"Party A", "Party B", "Party C", "Party D", "Party E"
215+
"Party A", "Party B", "Party C", "Party D", "Party E", "Party F", "Party G"
167216
};
168217

169218
@Override
@@ -174,9 +223,9 @@ public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
174223

175224
setData(mSeekBarX.getProgress(), mSeekBarY.getProgress());
176225
}
177-
226+
178227
private void setData(int count, float range) {
179-
228+
180229
float mult = range;
181230

182231
ArrayList<Entry> yVals1 = new ArrayList<Entry>();
@@ -210,12 +259,16 @@ private void setData(int count, float range) {
210259
mChart.highlightValues(null);
211260

212261
// set a text for the chart center
213-
mChart.setCenterText("Total Value\n" + (int) mChart.getYValueSum() + "\n(all slices)");
262+
mChart.setCenterText("MPAndroid\nChart");
263+
214264
mChart.invalidate();
215265
}
216266

217267
@Override
218268
public void onValueSelected(Entry e, int dataSetIndex) {
269+
270+
if (e == null)
271+
return;
219272
Log.i("VAL SELECTED",
220273
"Value: " + e.getVal() + ", xIndex: " + e.getXIndex()
221274
+ ", DataSet index: " + dataSetIndex);

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

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -123,10 +123,13 @@ protected void drawHighlights() {
123123
int index = h.getXIndex();
124124

125125
int dataSetIndex = h.getDataSetIndex();
126-
BarDataSet ds = (BarDataSet) mCurrentData.getDataSetByIndex(dataSetIndex);
126+
BarDataSet set = (BarDataSet) mCurrentData.getDataSetByIndex(dataSetIndex);
127+
128+
if (set == null)
129+
continue;
127130

128-
mHighlightPaint.setColor(ds.getHighLightColor());
129-
mHighlightPaint.setAlpha(ds.getHighLightAlpha());
131+
mHighlightPaint.setColor(set.getHighLightColor());
132+
mHighlightPaint.setAlpha(set.getHighLightAlpha());
130133

131134
// check outofbounds
132135
if (index < mCurrentData.getYValCount() && index >= 0
@@ -142,7 +145,7 @@ protected void drawHighlights() {
142145
+ mOriginalData.getGroupSpace() * index;
143146
float y = e.getVal();
144147

145-
prepareBar(x, y, ds.getBarSpace());
148+
prepareBar(x, y, set.getBarSpace());
146149

147150
mDrawCanvas.drawRect(mBarRect, mHighlightPaint);
148151

0 commit comments

Comments
 (0)