Skip to content

Commit ccced3c

Browse files
committed
Merge pull request PhilJay#1668 from danielgindi/horizontal-cubic-line
Horizontal cubic line
2 parents a0b49fc + 215be8a commit ccced3c

9 files changed

Lines changed: 378 additions & 210 deletions

File tree

MPChartExample/res/menu/line.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@
2121
android:id="@+id/actionToggleStepped"
2222
android:title="Toggle Stepped">
2323
</item>
24+
<item
25+
android:id="@+id/actionToggleHorizontalCubic"
26+
android:title="Toggle Horizontal Cubic">
27+
</item>
2428
<item
2529
android:id="@+id/actionToggleHighlight"
2630
android:title="Toggle Highlight">

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

Lines changed: 71 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -166,10 +166,37 @@ public boolean onOptionsItemSelected(MenuItem item) {
166166
for (ILineDataSet iSet : sets) {
167167

168168
LineDataSet set = (LineDataSet) iSet;
169-
if (set.isDrawCubicEnabled())
170-
set.setDrawCubic(false);
171-
else
172-
set.setDrawCubic(true);
169+
set.setMode(set.getMode() == LineDataSet.Mode.CUBIC_BEZIER
170+
? LineDataSet.Mode.LINEAR
171+
: LineDataSet.Mode.CUBIC_BEZIER);
172+
}
173+
mChart.invalidate();
174+
break;
175+
}
176+
case R.id.actionToggleStepped: {
177+
List<ILineDataSet> sets = mChart.getData()
178+
.getDataSets();
179+
180+
for (ILineDataSet iSet : sets) {
181+
182+
LineDataSet set = (LineDataSet) iSet;
183+
set.setMode(set.getMode() == LineDataSet.Mode.STEPPED
184+
? LineDataSet.Mode.LINEAR
185+
: LineDataSet.Mode.STEPPED);
186+
}
187+
mChart.invalidate();
188+
break;
189+
}
190+
case R.id.actionToggleHorizontalCubic: {
191+
List<ILineDataSet> sets = mChart.getData()
192+
.getDataSets();
193+
194+
for (ILineDataSet iSet : sets) {
195+
196+
LineDataSet set = (LineDataSet) iSet;
197+
set.setMode(set.getMode() == LineDataSet.Mode.HORIZONTAL_BEZIER
198+
? LineDataSet.Mode.LINEAR
199+
: LineDataSet.Mode.HORIZONTAL_BEZIER);
173200
}
174201
mChart.invalidate();
175202
break;
@@ -246,44 +273,54 @@ private void setData(int count, float range) {
246273
xVals.add((1990 +i) + "");
247274
}
248275

249-
ArrayList<Entry> vals1 = new ArrayList<Entry>();
276+
ArrayList<Entry> yVals = new ArrayList<Entry>();
250277

251278
for (int i = 0; i < count; i++) {
252279
float mult = (range + 1);
253280
float val = (float) (Math.random() * mult) + 20;// + (float)
254281
// ((mult *
255282
// 0.1) / 10);
256-
vals1.add(new Entry(val, i));
283+
yVals.add(new Entry(val, i));
257284
}
258-
259-
// create a dataset and give it a type
260-
LineDataSet set1 = new LineDataSet(vals1, "DataSet 1");
261-
set1.setDrawCubic(true);
262-
set1.setCubicIntensity(0.2f);
263-
//set1.setDrawFilled(true);
264-
set1.setDrawCircles(false);
265-
set1.setLineWidth(1.8f);
266-
set1.setCircleRadius(4f);
267-
set1.setCircleColor(Color.WHITE);
268-
set1.setHighLightColor(Color.rgb(244, 117, 117));
269-
set1.setColor(Color.WHITE);
270-
set1.setFillColor(Color.WHITE);
271-
set1.setFillAlpha(100);
272-
set1.setDrawHorizontalHighlightIndicator(false);
273-
set1.setFillFormatter(new FillFormatter() {
274-
@Override
275-
public float getFillLinePosition(ILineDataSet dataSet, LineDataProvider dataProvider) {
276-
return -10;
277-
}
278-
});
279285

280-
// create a data object with the datasets
281-
LineData data = new LineData(xVals, set1);
282-
data.setValueTypeface(tf);
283-
data.setValueTextSize(9f);
284-
data.setDrawValues(false);
286+
LineDataSet set1;
287+
288+
if (mChart.getData() != null &&
289+
mChart.getData().getDataSetCount() > 0) {
290+
set1 = (LineDataSet)mChart.getData().getDataSetByIndex(0);
291+
set1.setYVals(yVals);
292+
mChart.notifyDataSetChanged();
293+
} else {
294+
// create a dataset and give it a type
295+
set1 = new LineDataSet(yVals, "DataSet 1");
296+
297+
set1.setDrawCubic(true);
298+
set1.setCubicIntensity(0.2f);
299+
//set1.setDrawFilled(true);
300+
set1.setDrawCircles(false);
301+
set1.setLineWidth(1.8f);
302+
set1.setCircleRadius(4f);
303+
set1.setCircleColor(Color.WHITE);
304+
set1.setHighLightColor(Color.rgb(244, 117, 117));
305+
set1.setColor(Color.WHITE);
306+
set1.setFillColor(Color.WHITE);
307+
set1.setFillAlpha(100);
308+
set1.setDrawHorizontalHighlightIndicator(false);
309+
set1.setFillFormatter(new FillFormatter() {
310+
@Override
311+
public float getFillLinePosition(ILineDataSet dataSet, LineDataProvider dataProvider) {
312+
return -10;
313+
}
314+
});
315+
316+
// create a data object with the datasets
317+
LineData data = new LineData(xVals, set1);
318+
data.setValueTypeface(tf);
319+
data.setValueTextSize(9f);
320+
data.setDrawValues(false);
285321

286-
// set data
287-
mChart.setData(data);
322+
// set data
323+
mChart.setData(data);
324+
}
288325
}
289326
}

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

Lines changed: 59 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -235,10 +235,9 @@ public boolean onOptionsItemSelected(MenuItem item) {
235235
for (ILineDataSet iSet : sets) {
236236

237237
LineDataSet set = (LineDataSet) iSet;
238-
if (set.isDrawCubicEnabled())
239-
set.setDrawCubic(false);
240-
else
241-
set.setDrawCubic(true);
238+
set.setMode(set.getMode() == LineDataSet.Mode.CUBIC_BEZIER
239+
? LineDataSet.Mode.LINEAR
240+
: LineDataSet.Mode.CUBIC_BEZIER);
242241
}
243242
mChart.invalidate();
244243
break;
@@ -250,10 +249,23 @@ public boolean onOptionsItemSelected(MenuItem item) {
250249
for (ILineDataSet iSet : sets) {
251250

252251
LineDataSet set = (LineDataSet) iSet;
253-
if (set.isDrawSteppedEnabled())
254-
set.setDrawStepped(false);
255-
else
256-
set.setDrawStepped(true);
252+
set.setMode(set.getMode() == LineDataSet.Mode.STEPPED
253+
? LineDataSet.Mode.LINEAR
254+
: LineDataSet.Mode.STEPPED);
255+
}
256+
mChart.invalidate();
257+
break;
258+
}
259+
case R.id.actionToggleHorizontalCubic: {
260+
List<ILineDataSet> sets = mChart.getData()
261+
.getDataSets();
262+
263+
for (ILineDataSet iSet : sets) {
264+
265+
LineDataSet set = (LineDataSet) iSet;
266+
set.setMode(set.getMode() == LineDataSet.Mode.HORIZONTAL_BEZIER
267+
? LineDataSet.Mode.LINEAR
268+
: LineDataSet.Mode.HORIZONTAL_BEZIER);
257269
}
258270
mChart.invalidate();
259271
break;
@@ -341,38 +353,49 @@ private void setData(int count, float range) {
341353
yVals.add(new Entry(val, i));
342354
}
343355

344-
// create a dataset and give it a type
345-
LineDataSet set1 = new LineDataSet(yVals, "DataSet 1");
346-
// set1.setFillAlpha(110);
347-
// set1.setFillColor(Color.RED);
348-
349-
// set the line to be drawn like this "- - - - - -"
350-
set1.enableDashedLine(10f, 5f, 0f);
351-
set1.enableDashedHighlightLine(10f, 5f, 0f);
352-
set1.setColor(Color.BLACK);
353-
set1.setCircleColor(Color.BLACK);
354-
set1.setLineWidth(1f);
355-
set1.setCircleRadius(3f);
356-
set1.setDrawCircleHole(false);
357-
set1.setValueTextSize(9f);
358-
set1.setDrawFilled(true);
359-
360-
if(Utils.getSDKInt() >= 18) {
361-
// fill drawable only supported on api level 18 and above
362-
Drawable drawable = ContextCompat.getDrawable(this, R.drawable.fade_red);
363-
set1.setFillDrawable(drawable);
356+
LineDataSet set1;
357+
358+
if (mChart.getData() != null &&
359+
mChart.getData().getDataSetCount() > 0) {
360+
set1 = (LineDataSet)mChart.getData().getDataSetByIndex(0);
361+
set1.setYVals(yVals);
362+
mChart.notifyDataSetChanged();
364363
} else {
365-
set1.setFillColor(Color.BLACK);
366-
}
364+
// create a dataset and give it a type
365+
set1 = new LineDataSet(yVals, "DataSet 1");
366+
367+
// set1.setFillAlpha(110);
368+
// set1.setFillColor(Color.RED);
369+
370+
// set the line to be drawn like this "- - - - - -"
371+
set1.enableDashedLine(10f, 5f, 0f);
372+
set1.enableDashedHighlightLine(10f, 5f, 0f);
373+
set1.setColor(Color.BLACK);
374+
set1.setCircleColor(Color.BLACK);
375+
set1.setLineWidth(1f);
376+
set1.setCircleRadius(3f);
377+
set1.setDrawCircleHole(false);
378+
set1.setValueTextSize(9f);
379+
set1.setDrawFilled(true);
380+
381+
if (Utils.getSDKInt() >= 18) {
382+
// fill drawable only supported on api level 18 and above
383+
Drawable drawable = ContextCompat.getDrawable(this, R.drawable.fade_red);
384+
set1.setFillDrawable(drawable);
385+
}
386+
else {
387+
set1.setFillColor(Color.BLACK);
388+
}
367389

368-
ArrayList<ILineDataSet> dataSets = new ArrayList<ILineDataSet>();
369-
dataSets.add(set1); // add the datasets
390+
ArrayList<ILineDataSet> dataSets = new ArrayList<ILineDataSet>();
391+
dataSets.add(set1); // add the datasets
370392

371-
// create a data object with the datasets
372-
LineData data = new LineData(xVals, dataSets);
393+
// create a data object with the datasets
394+
LineData data = new LineData(xVals, dataSets);
373395

374-
// set data
375-
mChart.setData(data);
396+
// set data
397+
mChart.setData(data);
398+
}
376399
}
377400

378401
@Override

0 commit comments

Comments
 (0)