Skip to content

Commit 1f1f8c4

Browse files
committed
Extract setData to Kotlin
1 parent 1fcf6ea commit 1f1f8c4

File tree

2 files changed

+109
-81
lines changed

2 files changed

+109
-81
lines changed
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
package com.xxmassdeveloper.mpchartexample
2+
3+
import android.content.Context
4+
import android.graphics.Color
5+
import android.graphics.DashPathEffect
6+
import android.util.Log
7+
import androidx.core.content.ContextCompat
8+
import com.github.mikephil.charting.charts.LineChart
9+
import com.github.mikephil.charting.data.Entry
10+
import com.github.mikephil.charting.data.LineData
11+
import com.github.mikephil.charting.data.LineDataSet
12+
import com.github.mikephil.charting.formatter.IFillFormatter
13+
import com.github.mikephil.charting.interfaces.dataprovider.LineDataProvider
14+
import com.github.mikephil.charting.interfaces.datasets.ILineDataSet
15+
import com.github.mikephil.charting.utils.Utils
16+
17+
class DataTools {
18+
companion object {
19+
private const val VAL_COUNT = 45
20+
private const val VAL_RANGE = 180f
21+
22+
private val VAL_FIX = arrayOf(
23+
94.84043, -19.610321, 34.980606, 137.20502, 3.3113098, 18.506958,
24+
72.16055, 36.291832, 135.97142, 122.0381, 85.873055, 68.582016, -13.099461, 85.85466,
25+
85.45009, 97.90119, 132.87332, 125.11917, 147.57323, -2.4140186, 28.75475, -17.208168,
26+
70.36055, 10.715485, 53.00132, 49.197624, 48.25173, 117.765854, 15.96653, 114.79433,
27+
127.782455, 14.049572, 44.2753, 106.75516, 108.96782, 75.33596, 128.22353, -12.423063,
28+
44.768654, -25.790316, 5.9754066, 99.64748, 141.99321, -17.990795, 38.272446
29+
)
30+
31+
fun setData(context: Context, lineChart: LineChart, count: Int = VAL_COUNT, range: Float = VAL_RANGE) {
32+
Log.d("setData", "$count= range=$range")
33+
val values = ArrayList<Entry>()
34+
if (count == VAL_COUNT) {
35+
VAL_FIX.forEachIndexed { index, d ->
36+
values.add(Entry(index.toFloat(), d.toFloat(), ContextCompat.getDrawable(context, R.drawable.star)))
37+
}
38+
} else {
39+
for (i in 0 until count) {
40+
val value = (Math.random() * range).toFloat() - 30
41+
values.add(Entry(i.toFloat(), value, ContextCompat.getDrawable(context, R.drawable.star)))
42+
}
43+
}
44+
val lineDataSet0: LineDataSet
45+
if (lineChart.data != null && lineChart.data.dataSetCount > 0) {
46+
lineDataSet0 = lineChart.data.getDataSetByIndex(0) as LineDataSet
47+
lineDataSet0.entries = values
48+
lineDataSet0.notifyDataSetChanged()
49+
lineChart.data.notifyDataChanged()
50+
lineChart.notifyDataSetChanged()
51+
} else {
52+
// create a dataset and give it a type
53+
lineDataSet0 = LineDataSet(values, "DataSet 1")
54+
lineDataSet0.setDrawIcons(false)
55+
56+
// draw dashed line
57+
lineDataSet0.enableDashedLine(10f, 5f, 0f)
58+
59+
// black lines and points
60+
lineDataSet0.color = Color.BLACK
61+
lineDataSet0.setCircleColor(Color.BLACK)
62+
63+
// line thickness and point size
64+
lineDataSet0.lineWidth = 1f
65+
lineDataSet0.circleRadius = 3f
66+
67+
// draw points as solid circles
68+
lineDataSet0.setDrawCircleHole(false)
69+
70+
// customize legend entry
71+
lineDataSet0.formLineWidth = 1f
72+
lineDataSet0.formLineDashEffect = DashPathEffect(floatArrayOf(10f, 5f), 0f)
73+
lineDataSet0.formSize = 15f
74+
75+
// text size of values
76+
lineDataSet0.valueTextSize = 9f
77+
78+
// draw selection line as dashed
79+
lineDataSet0.enableDashedHighlightLine(10f, 5f, 0f)
80+
81+
// set the filled area
82+
lineDataSet0.setDrawFilled(true)
83+
lineDataSet0.fillFormatter = IFillFormatter { _: ILineDataSet?, _: LineDataProvider? ->
84+
lineChart.axisLeft.axisMinimum
85+
}
86+
87+
// set color of filled area
88+
if (Utils.getSDKInt() >= 18) {
89+
// drawables only supported on api level 18 and above
90+
val drawable = ContextCompat.getDrawable(context, R.drawable.fade_red)
91+
lineDataSet0.fillDrawable = drawable
92+
} else {
93+
lineDataSet0.fillColor = Color.BLACK
94+
}
95+
val dataSets = ArrayList<ILineDataSet>()
96+
dataSets.add(lineDataSet0) // add the data sets
97+
98+
// create a data object with the data sets
99+
val data = LineData(dataSets)
100+
101+
// set data
102+
lineChart.data = data
103+
}
104+
}
105+
}
106+
}

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

Lines changed: 3 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@
44
import android.content.Intent;
55
import android.content.pm.PackageManager;
66
import android.graphics.Color;
7-
import android.graphics.DashPathEffect;
8-
import android.graphics.drawable.Drawable;
97
import android.net.Uri;
108
import android.os.Bundle;
119

@@ -25,17 +23,14 @@
2523
import com.github.mikephil.charting.components.LimitLine;
2624
import com.github.mikephil.charting.components.LimitLine.LimitLabelPosition;
2725
import com.github.mikephil.charting.data.Entry;
28-
import com.github.mikephil.charting.data.LineData;
2926
import com.github.mikephil.charting.data.LineDataSet;
3027
import com.github.mikephil.charting.highlight.Highlight;
3128
import com.github.mikephil.charting.interfaces.datasets.ILineDataSet;
3229
import com.github.mikephil.charting.listener.OnChartValueSelectedListener;
33-
import com.github.mikephil.charting.utils.Utils;
3430
import com.xxmassdeveloper.mpchartexample.custom.MyMarkerView;
3531
import com.xxmassdeveloper.mpchartexample.databinding.ActivityLinechartBinding;
3632
import com.xxmassdeveloper.mpchartexample.notimportant.DemoBase;
3733

38-
import java.util.ArrayList;
3934
import java.util.List;
4035

4136
/**
@@ -133,7 +128,8 @@ protected void onCreate(Bundle savedInstanceState) {
133128
// add data
134129
binding.seekBarX.setProgress(45);
135130
binding.seekBarY.setProgress(180);
136-
setData(45, 180);
131+
Log.d("setDataCreate", "$count=45 range=180f");
132+
DataTools.Companion.setData(this, binding.chart1, 45, 180f);
137133

138134
// draw points over time
139135
binding.chart1.animateX(1500);
@@ -143,79 +139,6 @@ protected void onCreate(Bundle savedInstanceState) {
143139
legend.setForm(LegendForm.LINE);
144140
}
145141

146-
private void setData(int count, float range) {
147-
Log.d("setData", count + "= range=" + range);
148-
ArrayList<Entry> values = new ArrayList<>();
149-
150-
for (int i = 0; i < count; i++) {
151-
float val = (float) (Math.random() * range) - 30;
152-
Log.v("setData", i + "=" + val);
153-
values.add(new Entry(i, val, ContextCompat.getDrawable(this, R.drawable.star)));
154-
}
155-
156-
LineDataSet lineDataSet0;
157-
158-
if (binding.chart1.getData() != null && binding.chart1.getData().getDataSetCount() > 0) {
159-
lineDataSet0 = (LineDataSet) binding.chart1.getData().getDataSetByIndex(0);
160-
lineDataSet0.setEntries(values);
161-
lineDataSet0.notifyDataSetChanged();
162-
binding.chart1.getData().notifyDataChanged();
163-
binding.chart1.notifyDataSetChanged();
164-
} else {
165-
// create a dataset and give it a type
166-
lineDataSet0 = new LineDataSet(values, "DataSet 1");
167-
168-
lineDataSet0.setDrawIcons(false);
169-
170-
// draw dashed line
171-
lineDataSet0.enableDashedLine(10f, 5f, 0f);
172-
173-
// black lines and points
174-
lineDataSet0.setColor(Color.BLACK);
175-
lineDataSet0.setCircleColor(Color.BLACK);
176-
177-
// line thickness and point size
178-
lineDataSet0.setLineWidth(1f);
179-
lineDataSet0.setCircleRadius(3f);
180-
181-
// draw points as solid circles
182-
lineDataSet0.setDrawCircleHole(false);
183-
184-
// customize legend entry
185-
lineDataSet0.setFormLineWidth(1f);
186-
lineDataSet0.setFormLineDashEffect(new DashPathEffect(new float[]{10f, 5f}, 0f));
187-
lineDataSet0.setFormSize(15.f);
188-
189-
// text size of values
190-
lineDataSet0.setValueTextSize(9f);
191-
192-
// draw selection line as dashed
193-
lineDataSet0.enableDashedHighlightLine(10f, 5f, 0f);
194-
195-
// set the filled area
196-
lineDataSet0.setDrawFilled(true);
197-
lineDataSet0.setFillFormatter((dataSet, dataProvider) -> binding.chart1.getAxisLeft().getAxisMinimum());
198-
199-
// set color of filled area
200-
if (Utils.getSDKInt() >= 18) {
201-
// drawables only supported on api level 18 and above
202-
Drawable drawable = ContextCompat.getDrawable(this, R.drawable.fade_red);
203-
lineDataSet0.setFillDrawable(drawable);
204-
} else {
205-
lineDataSet0.setFillColor(Color.BLACK);
206-
}
207-
208-
ArrayList<ILineDataSet> dataSets = new ArrayList<>();
209-
dataSets.add(lineDataSet0); // add the data sets
210-
211-
// create a data object with the data sets
212-
LineData data = new LineData(dataSets);
213-
214-
// set data
215-
binding.chart1.setData(data);
216-
}
217-
}
218-
219142
@Override
220143
public boolean onCreateOptionsMenu(Menu menu) {
221144
getMenuInflater().inflate(R.menu.line, menu);
@@ -325,11 +248,10 @@ public boolean onOptionsItemSelected(MenuItem item) {
325248

326249
@Override
327250
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
328-
329251
binding.tvXMax.setText(String.valueOf(binding.seekBarX.getProgress()));
330252
binding.tvYMax.setText(String.valueOf(binding.seekBarY.getProgress()));
331253

332-
setData(binding.seekBarX.getProgress(), binding.seekBarY.getProgress());
254+
DataTools.Companion.setData(this, binding.chart1, binding.seekBarX.getProgress(), binding.seekBarY.getProgress());
333255

334256
// redraw
335257
binding.chart1.invalidate();

0 commit comments

Comments
 (0)