|
25 | 25 | import com.github.mikephil.charting.utils.ColorTemplate; |
26 | 26 | import com.xxmassdeveloper.mpchartexample.notimportant.DemoBase; |
27 | 27 |
|
| 28 | +import java.sql.Time; |
28 | 29 | import java.text.SimpleDateFormat; |
29 | 30 | import java.util.ArrayList; |
30 | 31 | import java.util.Date; |
31 | 32 | import java.util.List; |
| 33 | +import java.util.concurrent.TimeUnit; |
32 | 34 |
|
33 | 35 | public class LineChartTime extends DemoBase implements OnSeekBarChangeListener { |
34 | 36 |
|
@@ -87,14 +89,16 @@ protected void onCreate(Bundle savedInstanceState) { |
87 | 89 | xAxis.setDrawGridLines(true); |
88 | 90 | xAxis.setTextColor(Color.rgb(255, 192, 56)); |
89 | 91 | xAxis.setCenterAxisLabels(true); |
90 | | - xAxis.setGranularity(60000L); // one minute in millis |
| 92 | + xAxis.setGranularity(1f); // one hour |
91 | 93 | xAxis.setValueFormatter(new IAxisValueFormatter() { |
92 | 94 |
|
93 | 95 | private SimpleDateFormat mFormat = new SimpleDateFormat("dd MMM HH:mm"); |
94 | 96 |
|
95 | 97 | @Override |
96 | 98 | 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)); |
98 | 102 | } |
99 | 103 |
|
100 | 104 | @Override |
@@ -264,15 +268,18 @@ public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { |
264 | 268 |
|
265 | 269 | private void setData(int count, float range) { |
266 | 270 |
|
267 | | - long now = System.currentTimeMillis(); |
268 | | - long hourMillis = 3600000L; |
| 271 | + // now in hours |
| 272 | + long now = TimeUnit.MILLISECONDS.toHours(System.currentTimeMillis()); |
269 | 273 |
|
270 | 274 | ArrayList<Entry> values = new ArrayList<Entry>(); |
271 | 275 |
|
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; |
274 | 280 |
|
275 | | - for (float x = from; x < to; x += hourMillis) { |
| 281 | + // increment by 1 hour |
| 282 | + for (float x = from; x < to; x++) { |
276 | 283 |
|
277 | 284 | float y = getRandom(range, 50); |
278 | 285 | values.add(new Entry(x, y)); // add one entry per hour |
|
0 commit comments