66import android .os .Bundle ;
77import android .view .Menu ;
88import android .view .MenuItem ;
9+ import android .view .View ;
910import android .view .WindowManager ;
1011import android .widget .SeekBar ;
1112import android .widget .SeekBar .OnSeekBarChangeListener ;
1213import android .widget .TextView ;
1314import android .widget .Toast ;
1415
1516import com .github .mikephil .charting .charts .LineChart ;
17+ import com .github .mikephil .charting .components .AxisBase ;
1618import com .github .mikephil .charting .components .Legend ;
1719import com .github .mikephil .charting .components .Legend .LegendForm ;
1820import com .github .mikephil .charting .components .Legend .LegendPosition ;
2224import com .github .mikephil .charting .data .Entry ;
2325import com .github .mikephil .charting .data .LineData ;
2426import com .github .mikephil .charting .data .LineDataSet ;
27+ import com .github .mikephil .charting .formatter .AxisValueFormatter ;
2528import com .github .mikephil .charting .interfaces .datasets .ILineDataSet ;
2629import com .github .mikephil .charting .utils .ColorTemplate ;
2730import com .xxmassdeveloper .mpchartexample .notimportant .DemoBase ;
2831
32+ import java .text .SimpleDateFormat ;
2933import java .util .ArrayList ;
34+ import java .util .Date ;
3035import java .util .List ;
3136
3237public class LineChartTime extends DemoBase implements OnSeekBarChangeListener {
3338
3439 private LineChart mChart ;
35- private SeekBar mSeekBarX , mSeekBarY ;
36- private TextView tvX , tvY ;
37- private long curTime = System .currentTimeMillis ();
40+ private SeekBar mSeekBarX ;
41+ private TextView tvX ;
3842
3943 @ Override
4044 protected void onCreate (Bundle savedInstanceState ) {
4145 super .onCreate (savedInstanceState );
4246 getWindow ().setFlags (WindowManager .LayoutParams .FLAG_FULLSCREEN ,
4347 WindowManager .LayoutParams .FLAG_FULLSCREEN );
44- setContentView (R .layout .activity_linechart );
48+ setContentView (R .layout .activity_linechart_time );
4549
4650 tvX = (TextView ) findViewById (R .id .tvXMax );
47- tvY = (TextView ) findViewById (R .id .tvYMax );
4851 mSeekBarX = (SeekBar ) findViewById (R .id .seekBar1 );
49- mSeekBarY = (SeekBar ) findViewById (R .id .seekBar2 );
52+ mSeekBarX .setProgress (100 );
53+ tvX .setText ("100" );
5054
51- mSeekBarX .setProgress (45 );
52- mSeekBarY .setProgress (100 );
53-
54- mSeekBarY .setOnSeekBarChangeListener (this );
5555 mSeekBarX .setOnSeekBarChangeListener (this );
5656
5757 mChart = (LineChart ) findViewById (R .id .chart1 );
58- mChart .setLogEnabled (true );
58+ mChart .setHardwareAccelerationEnabled (true );
5959
6060 // no description text
6161 mChart .setDescription ("" );
@@ -72,47 +72,55 @@ protected void onCreate(Bundle savedInstanceState) {
7272 mChart .setDrawGridBackground (false );
7373 mChart .setHighlightPerDragEnabled (true );
7474
75- // if disabled, scaling can be done on x- and y-axis separately
76- mChart .setPinchZoom (true );
77-
7875 // set an alternative background color
79- mChart .setBackgroundColor (Color .LTGRAY );
76+ mChart .setBackgroundColor (Color .WHITE );
77+ mChart .setViewPortOffsets (0f , 0f , 0f , 0f );
8078
8179 // add data
82- setData (20 , 30 );
80+ setData (100 , 30 );
8381 mChart .invalidate ();
8482
85- Typeface tf = Typeface .createFromAsset (getAssets (), "OpenSans-Regular .ttf" );
83+ Typeface tf = Typeface .createFromAsset (getAssets (), "OpenSans-Light .ttf" );
8684
8785 // get the legend (only possible after setting data)
8886 Legend l = mChart .getLegend ();
89-
90- // modify the legend ...
91- // l.setPosition(LegendPosition.LEFT_OF_CHART);
92- l .setForm (LegendForm .LINE );
93- l .setTypeface (tf );
94- l .setTextSize (11f );
95- l .setTextColor (Color .WHITE );
96- l .setPosition (LegendPosition .BELOW_CHART_LEFT );
97- // l.setYOffset(11f);
87+ l .setEnabled (false );
9888
9989 XAxis xAxis = mChart .getXAxis ();
90+ xAxis .setPosition (XAxis .XAxisPosition .TOP_INSIDE );
10091 xAxis .setTypeface (tf );
101- xAxis .setTextSize (12f );
92+ xAxis .setTextSize (10f );
10293 xAxis .setTextColor (Color .WHITE );
10394 xAxis .setDrawAxisLine (false );
95+ xAxis .setDrawGridLines (true );
96+ xAxis .setTextColor (Color .rgb (255 , 192 , 56 ));
97+ xAxis .setCenterAxisLabels (true );
98+ xAxis .setGranularity (60000L ); // one minute in millis
99+ xAxis .setValueFormatter (new AxisValueFormatter () {
100+
101+ private SimpleDateFormat mFormat = new SimpleDateFormat ("dd MMM HH:mm" );
104102
105- // custom x-axis min / max
106- xAxis .setAxisMinValue (5000 );
107- xAxis .setAxisMaxValue (30000 );
103+ @ Override
104+ public String getFormattedValue (float value , AxisBase axis ) {
105+ return mFormat .format (new Date ((long ) value ));
106+ }
107+
108+ @ Override
109+ public int getDecimalDigits () {
110+ return 0 ;
111+ }
112+ });
108113
109114 YAxis leftAxis = mChart .getAxisLeft ();
115+ leftAxis .setPosition (YAxis .YAxisLabelPosition .INSIDE_CHART );
110116 leftAxis .setTypeface (tf );
111117 leftAxis .setTextColor (ColorTemplate .getHoloBlue ());
112- leftAxis .setAxisMaxValue (200f );
113- leftAxis .setAxisMinValue (0f );
114118 leftAxis .setDrawGridLines (true );
115119 leftAxis .setGranularityEnabled (true );
120+ leftAxis .setAxisMinValue (0f );
121+ leftAxis .setAxisMaxValue (170f );
122+ leftAxis .setYOffset (-9f );
123+ leftAxis .setTextColor (Color .rgb (255 , 192 , 56 ));
116124
117125 YAxis rightAxis = mChart .getAxisRight ();
118126 rightAxis .setEnabled (false );
@@ -254,43 +262,38 @@ public boolean onOptionsItemSelected(MenuItem item) {
254262 @ Override
255263 public void onProgressChanged (SeekBar seekBar , int progress , boolean fromUser ) {
256264
257- tvX .setText ("" + (mSeekBarX .getProgress () + 1 ));
258- tvY .setText ("" + (mSeekBarY .getProgress ()));
265+ tvX .setText ("" + (mSeekBarX .getProgress ()));
259266
260- setData (mSeekBarX .getProgress () + 1 , mSeekBarY . getProgress () );
267+ setData (mSeekBarX .getProgress (), 50 );
261268
262269 // redraw
263270 mChart .invalidate ();
264-
265- System .out .println ("xmin: " + mChart .getXAxis ().getAxisMinimum ());
266- System .out .println ("xmax: " + mChart .getXAxis ().getAxisMaximum ());
267- System .out .println ("lvx: " + mChart .getLowestVisibleX ());
268- System .out .println ("hvx: " + mChart .getHighestVisibleX ());
269271 }
270272
271273 private void setData (int count , float range ) {
272274
273- ArrayList <Entry > yVals1 = new ArrayList <Entry >();
275+ long now = System .currentTimeMillis ();
276+ long hourMillis = 3600000L ;
274277
275- for (int i = 0 ; i < count ; i ++) {
276- float mult = range / 2f ;
277- float val = (float ) (Math .random () * mult ) + 50 ;// + (float)
278- // ((mult *
279- // 0.1) / 10);
280- //yVals1.add(new Entry(val, 10000 + i * 1000));
281- }
278+ ArrayList <Entry > values = new ArrayList <Entry >();
282279
283- yVals1 .add (new Entry (100 , 10000 ));
284- yVals1 .add (new Entry (130 , 15000 ));
285- yVals1 .add (new Entry (120 , 20000 ));
280+ float from = now - (count / 2 ) * hourMillis ;
281+ float to = now + (count / 2 ) * hourMillis ;
282+
283+ for (float x = from ; x < to ; x += hourMillis ) {
284+
285+ float y = getRandom (range , 50 );
286+ values .add (new Entry (x , y )); // add one entry per hour
287+ }
286288
287289 // create a dataset and give it a type
288- LineDataSet set1 = new LineDataSet (yVals1 , "DataSet 1" );
290+ LineDataSet set1 = new LineDataSet (values , "DataSet 1" );
289291 set1 .setAxisDependency (AxisDependency .LEFT );
290292 set1 .setColor (ColorTemplate .getHoloBlue ());
291- set1 .setCircleColor (Color .WHITE );
292- set1 .setLineWidth (2f );
293- set1 .setCircleRadius (3f );
293+ set1 .setValueTextColor (ColorTemplate .getHoloBlue ());
294+ set1 .setLineWidth (1.5f );
295+ set1 .setDrawCircles (false );
296+ set1 .setDrawValues (false );
294297 set1 .setFillAlpha (65 );
295298 set1 .setFillColor (ColorTemplate .getHoloBlue ());
296299 set1 .setHighLightColor (Color .rgb (244 , 117 , 117 ));
0 commit comments