Skip to content

Commit 5b9bd6e

Browse files
committed
Fix time chart example
1 parent 0aeddb0 commit 5b9bd6e

File tree

1 file changed

+14
-7
lines changed

1 file changed

+14
-7
lines changed

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

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,12 @@
2525
import com.github.mikephil.charting.utils.ColorTemplate;
2626
import com.xxmassdeveloper.mpchartexample.notimportant.DemoBase;
2727

28+
import java.sql.Time;
2829
import java.text.SimpleDateFormat;
2930
import java.util.ArrayList;
3031
import java.util.Date;
3132
import java.util.List;
33+
import java.util.concurrent.TimeUnit;
3234

3335
public class LineChartTime extends DemoBase implements OnSeekBarChangeListener {
3436

@@ -87,14 +89,16 @@ protected void onCreate(Bundle savedInstanceState) {
8789
xAxis.setDrawGridLines(true);
8890
xAxis.setTextColor(Color.rgb(255, 192, 56));
8991
xAxis.setCenterAxisLabels(true);
90-
xAxis.setGranularity(60000L); // one minute in millis
92+
xAxis.setGranularity(1f); // one hour
9193
xAxis.setValueFormatter(new IAxisValueFormatter() {
9294

9395
private SimpleDateFormat mFormat = new SimpleDateFormat("dd MMM HH:mm");
9496

9597
@Override
9698
public String getFormattedValue(float value, AxisBase axis) {
97-
return mFormat.format(new Date((long) value));
99+
100+
long millis = TimeUnit.HOURS.toMillis((long) value);
101+
return mFormat.format(new Date(millis));
98102
}
99103

100104
@Override
@@ -264,15 +268,18 @@ public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
264268

265269
private void setData(int count, float range) {
266270

267-
long now = System.currentTimeMillis();
268-
long hourMillis = 3600000L;
271+
// now in hours
272+
long now = TimeUnit.MILLISECONDS.toHours(System.currentTimeMillis());
269273

270274
ArrayList<Entry> values = new ArrayList<Entry>();
271275

272-
float from = now - (count / 2) * hourMillis;
273-
float to = now + (count / 2) * hourMillis;
276+
float from = now;
277+
278+
// count = hours
279+
float to = now + count;
274280

275-
for (float x = from; x < to; x += hourMillis) {
281+
// increment by 1 hour
282+
for (float x = from; x < to; x++) {
276283

277284
float y = getRandom(range, 50);
278285
values.add(new Entry(x, y)); // add one entry per hour

0 commit comments

Comments
 (0)