11
22package com .xxmassdeveloper .mpchartexample ;
33
4+ import android .Manifest ;
45import android .annotation .SuppressLint ;
6+ import android .content .Intent ;
7+ import android .content .pm .PackageManager ;
58import android .graphics .RectF ;
9+ import android .net .Uri ;
610import android .os .Bundle ;
711import android .util .Log ;
812import android .view .Menu ;
1317import android .widget .TextView ;
1418import android .widget .Toast ;
1519
20+ import androidx .core .content .ContextCompat ;
21+
1622import com .github .mikephil .charting .charts .HorizontalBarChart ;
1723import com .github .mikephil .charting .components .Legend ;
1824import com .github .mikephil .charting .components .XAxis ;
3440public class HorizontalBarNegativeChartActivity extends DemoBase implements OnSeekBarChangeListener ,
3541 OnChartValueSelectedListener {
3642
37- protected HorizontalBarChart mChart ;
38- private SeekBar mSeekBarX , mSeekBarY ;
43+ private HorizontalBarChart chart ;
44+ private SeekBar seekBarX , seekBarY ;
3945 private TextView tvX , tvY ;
4046
4147 @ Override
@@ -45,66 +51,66 @@ protected void onCreate(Bundle savedInstanceState) {
4551 WindowManager .LayoutParams .FLAG_FULLSCREEN );
4652 setContentView (R .layout .activity_horizontalbarchart );
4753
54+ setTitle ("HorizontalBarChartActivity" );
55+
4856 tvX = findViewById (R .id .tvXMax );
49- tvY = ( TextView ) findViewById (R .id .tvYMax );
57+ tvY = findViewById (R .id .tvYMax );
5058
51- mSeekBarX = ( SeekBar ) findViewById (R .id .seekBar1 );
52- mSeekBarY = ( SeekBar ) findViewById (R .id .seekBar2 );
59+ seekBarX = findViewById (R .id .seekBar1 );
60+ seekBarY = findViewById (R .id .seekBar2 );
5361
54- mChart = (HorizontalBarChart ) findViewById (R .id .chart1 );
55- mChart .setOnChartValueSelectedListener (this );
56- // mChart.setHighlightEnabled(false);
62+ seekBarY .setOnSeekBarChangeListener (this );
63+ seekBarX .setOnSeekBarChangeListener (this );
5764
58- mChart .setDrawBarShadow (false );
65+ chart = findViewById (R .id .chart1 );
66+ chart .setOnChartValueSelectedListener (this );
67+ // chart.setHighlightEnabled(false);
5968
60- mChart . setDrawValueAboveBar ( true );
69+ chart . setDrawBarShadow ( false );
6170
62- mChart .getDescription ().setEnabled (false );
71+ chart .setDrawValueAboveBar (true );
72+
73+ chart .getDescription ().setEnabled (false );
6374
6475 // if more than 60 entries are displayed in the chart, no values will be
6576 // drawn
66- mChart .setMaxVisibleValueCount (60 );
77+ chart .setMaxVisibleValueCount (60 );
6778
6879 // scaling can now only be done on x- and y-axis separately
69- mChart .setPinchZoom (false );
80+ chart .setPinchZoom (false );
7081
7182 // draw shadows for each bar that show the maximum value
72- // mChart .setDrawBarShadow(true);
83+ // chart .setDrawBarShadow(true);
7384
74- mChart .setDrawGridBackground (false );
85+ chart .setDrawGridBackground (false );
7586
76- XAxis xl = mChart .getXAxis ();
87+ XAxis xl = chart .getXAxis ();
7788 xl .setPosition (XAxisPosition .BOTTOM );
78- xl .setTypeface (mTfLight );
89+ xl .setTypeface (tfLight );
7990 xl .setDrawAxisLine (true );
8091 xl .setDrawGridLines (false );
8192 xl .setGranularity (10f );
8293
83- YAxis yl = mChart .getAxisLeft ();
84- yl .setTypeface (mTfLight );
94+ YAxis yl = chart .getAxisLeft ();
95+ yl .setTypeface (tfLight );
8596 yl .setDrawAxisLine (true );
8697 yl .setDrawGridLines (true );
87- yl .setDrawZeroLine (true ); // draw a zero line
8898// yl.setInverted(true);
8999
90- YAxis yr = mChart .getAxisRight ();
91- yr .setTypeface (mTfLight );
100+ YAxis yr = chart .getAxisRight ();
101+ yr .setTypeface (tfLight );
92102 yr .setDrawAxisLine (true );
93103 yr .setDrawGridLines (false );
94104// yr.setInverted(true);
95105
96- setData (12 , 50 );
97- mChart .setFitBars (true );
98- mChart .animateY (2500 );
106+ chart .setFitBars (true );
107+ chart .animateY (2500 );
99108
100109 // setting data
101- mSeekBarY .setProgress (50 );
102- mSeekBarX .setProgress (12 );
103-
104- mSeekBarY .setOnSeekBarChangeListener (this );
105- mSeekBarX .setOnSeekBarChangeListener (this );
110+ seekBarY .setProgress (50 );
111+ seekBarX .setProgress (12 );
106112
107- Legend l = mChart .getLegend ();
113+ Legend l = chart .getLegend ();
108114 l .setVerticalAlignment (Legend .LegendVerticalAlignment .BOTTOM );
109115 l .setHorizontalAlignment (Legend .LegendHorizontalAlignment .LEFT );
110116 l .setOrientation (Legend .LegendOrientation .HORIZONTAL );
@@ -113,6 +119,42 @@ protected void onCreate(Bundle savedInstanceState) {
113119 l .setXEntrySpace (4f );
114120 }
115121
122+ private void setData (int count , float range ) {
123+
124+ float barWidth = 9f ;
125+ float spaceForBar = 10f ;
126+ ArrayList <BarEntry > values = new ArrayList <>();
127+
128+ for (int i = 0 ; i < count ; i ++) {
129+ float val = (float ) (Math .random () * range - range / 2 );
130+ values .add (new BarEntry (i * spaceForBar , val ,
131+ getResources ().getDrawable (R .drawable .star )));
132+ }
133+
134+ BarDataSet set1 ;
135+
136+ if (chart .getData () != null &&
137+ chart .getData ().getDataSetCount () > 0 ) {
138+ set1 = (BarDataSet ) chart .getData ().getDataSetByIndex (0 );
139+ set1 .setValues (values );
140+ chart .getData ().notifyDataChanged ();
141+ chart .notifyDataSetChanged ();
142+ } else {
143+ set1 = new BarDataSet (values , "DataSet 1" );
144+
145+ set1 .setDrawIcons (false );
146+
147+ ArrayList <IBarDataSet > dataSets = new ArrayList <>();
148+ dataSets .add (set1 );
149+
150+ BarData data = new BarData (dataSets );
151+ data .setValueTextSize (10f );
152+ data .setValueTypeface (tfLight );
153+ data .setBarWidth (barWidth );
154+ chart .setData (data );
155+ }
156+ }
157+
116158 @ Override
117159 public boolean onCreateOptionsMenu (Menu menu ) {
118160 getMenuInflater ().inflate (R .menu .bar , menu );
@@ -123,80 +165,80 @@ public boolean onCreateOptionsMenu(Menu menu) {
123165 public boolean onOptionsItemSelected (MenuItem item ) {
124166
125167 switch (item .getItemId ()) {
168+ case R .id .viewGithub : {
169+ Intent i = new Intent (Intent .ACTION_VIEW );
170+ i .setData (Uri .parse ("https://github.com/PhilJay/MPAndroidChart/blob/master/MPChartExample/src/com/xxmassdeveloper/mpchartexample/HorizontalBarChartActivity.java" ));
171+ startActivity (i );
172+ break ;
173+ }
126174 case R .id .actionToggleValues : {
127- List <IBarDataSet > sets = mChart .getData ()
175+ List <IBarDataSet > sets = chart .getData ()
128176 .getDataSets ();
129177
130178 for (IBarDataSet iSet : sets ) {
131-
132- IBarDataSet set = (BarDataSet ) iSet ;
133- set .setDrawValues (!set .isDrawValuesEnabled ());
179+ iSet .setDrawValues (!iSet .isDrawValuesEnabled ());
134180 }
135181
136- mChart .invalidate ();
182+ chart .invalidate ();
137183 break ;
138184 }
139185 case R .id .actionToggleIcons : {
140- List <IBarDataSet > sets = mChart .getData ()
186+ List <IBarDataSet > sets = chart .getData ()
141187 .getDataSets ();
142188
143189 for (IBarDataSet iSet : sets ) {
144-
145- IBarDataSet set = (BarDataSet ) iSet ;
146- set .setDrawIcons (!set .isDrawIconsEnabled ());
190+ iSet .setDrawIcons (!iSet .isDrawIconsEnabled ());
147191 }
148192
149- mChart .invalidate ();
193+ chart .invalidate ();
150194 break ;
151195 }
152196 case R .id .actionToggleHighlight : {
153- if (mChart .getData () != null ) {
154- mChart .getData ().setHighlightEnabled (!mChart .getData ().isHighlightEnabled ());
155- mChart .invalidate ();
197+ if (chart .getData () != null ) {
198+ chart .getData ().setHighlightEnabled (!chart .getData ().isHighlightEnabled ());
199+ chart .invalidate ();
156200 }
157201 break ;
158202 }
159203 case R .id .actionTogglePinch : {
160- if (mChart .isPinchZoomEnabled ())
161- mChart .setPinchZoom (false );
204+ if (chart .isPinchZoomEnabled ())
205+ chart .setPinchZoom (false );
162206 else
163- mChart .setPinchZoom (true );
207+ chart .setPinchZoom (true );
164208
165- mChart .invalidate ();
209+ chart .invalidate ();
166210 break ;
167211 }
168212 case R .id .actionToggleAutoScaleMinMax : {
169- mChart .setAutoScaleMinMaxEnabled (!mChart .isAutoScaleMinMaxEnabled ());
170- mChart .notifyDataSetChanged ();
213+ chart .setAutoScaleMinMaxEnabled (!chart .isAutoScaleMinMaxEnabled ());
214+ chart .notifyDataSetChanged ();
171215 break ;
172216 }
173217 case R .id .actionToggleBarBorders : {
174- for (IBarDataSet set : mChart .getData ().getDataSets ())
218+ for (IBarDataSet set : chart .getData ().getDataSets ())
175219 ((BarDataSet )set ).setBarBorderWidth (set .getBarBorderWidth () == 1.f ? 0.f : 1.f );
176220
177- mChart .invalidate ();
221+ chart .invalidate ();
178222 break ;
179223 }
180224 case R .id .animateX : {
181- mChart .animateX (3000 );
225+ chart .animateX (2000 );
182226 break ;
183227 }
184228 case R .id .animateY : {
185- mChart .animateY (3000 );
229+ chart .animateY (2000 );
186230 break ;
187231 }
188232 case R .id .animateXY : {
189-
190- mChart .animateXY (3000 , 3000 );
233+ chart .animateXY (2000 , 2000 );
191234 break ;
192235 }
193236 case R .id .actionSave : {
194- if (mChart .saveToGallery ("title" + System .currentTimeMillis (), 50 )) {
195- Toast .makeText (getApplicationContext (), "Saving SUCCESSFUL!" ,
196- Toast .LENGTH_SHORT ).show ();
197- } else
198- Toast .makeText (getApplicationContext (), "Saving FAILED!" , Toast .LENGTH_SHORT )
199- .show ();
237+ if (ContextCompat .checkSelfPermission (this , Manifest .permission .WRITE_EXTERNAL_STORAGE ) == PackageManager .PERMISSION_GRANTED ) {
238+ saveToGallery ();
239+ } else {
240+ requestStoragePermission (chart );
241+ }
200242 break ;
201243 }
202244 }
@@ -206,74 +248,37 @@ public boolean onOptionsItemSelected(MenuItem item) {
206248 @ Override
207249 public void onProgressChanged (SeekBar seekBar , int progress , boolean fromUser ) {
208250
209- tvX .setText ("" + ( mSeekBarX .getProgress () + 1 ));
210- tvY .setText ("" + ( mSeekBarY .getProgress ()));
251+ tvX .setText (String . valueOf ( seekBarX .getProgress ()));
252+ tvY .setText (String . valueOf ( seekBarY .getProgress ()));
211253
212- setData (mSeekBarX .getProgress () + 1 , mSeekBarY .getProgress ());
213- mChart .setFitBars (true );
214- mChart .invalidate ();
254+ setData (seekBarX .getProgress (), seekBarY .getProgress ());
255+ chart .setFitBars (true );
256+ chart .invalidate ();
215257 }
216258
217259 @ Override
218- public void onStartTrackingTouch (SeekBar seekBar ) {
219- // TODO Auto-generated method stub
220-
260+ protected void saveToGallery () {
261+ saveToGallery (chart , "HorizontalBarChartActivity" );
221262 }
222263
223264 @ Override
224- public void onStopTrackingTouch (SeekBar seekBar ) {
225- // TODO Auto-generated method stub
226-
227- }
228-
229- private void setData (int count , float range ) {
230-
231- float barWidth = 9f ;
232- float spaceForBar = 10f ;
233- ArrayList <BarEntry > yVals1 = new ArrayList <BarEntry >();
234-
235- for (int i = 0 ; i < count ; i ++) {
236- float val = (float ) (Math .random () * range - range / 2 );
237- yVals1 .add (new BarEntry (i * spaceForBar , val ,
238- getResources ().getDrawable (R .drawable .star )));
239- }
240-
241- BarDataSet set1 ;
242-
243- if (mChart .getData () != null &&
244- mChart .getData ().getDataSetCount () > 0 ) {
245- set1 = (BarDataSet )mChart .getData ().getDataSetByIndex (0 );
246- set1 .setValues (yVals1 );
247- mChart .getData ().notifyDataChanged ();
248- mChart .notifyDataSetChanged ();
249- } else {
250- set1 = new BarDataSet (yVals1 , "DataSet 1" );
251-
252- set1 .setDrawIcons (false );
265+ public void onStartTrackingTouch (SeekBar seekBar ) {}
253266
254- ArrayList < IBarDataSet > dataSets = new ArrayList < IBarDataSet >();
255- dataSets . add ( set1 );
267+ @ Override
268+ public void onStopTrackingTouch ( SeekBar seekBar ) {}
256269
257- BarData data = new BarData (dataSets );
258- data .setValueTextSize (10f );
259- data .setValueTypeface (mTfLight );
260- data .setBarWidth (barWidth );
261- mChart .setData (data );
262- }
263- }
270+ private final RectF mOnValueSelectedRectF = new RectF ();
264271
265- protected RectF mOnValueSelectedRectF = new RectF ();
266- @ SuppressLint ("NewApi" )
267272 @ Override
268273 public void onValueSelected (Entry e , Highlight h ) {
269274
270275 if (e == null )
271276 return ;
272277
273278 RectF bounds = mOnValueSelectedRectF ;
274- mChart .getBarBounds ((BarEntry ) e , bounds );
279+ chart .getBarBounds ((BarEntry ) e , bounds );
275280
276- MPPointF position = mChart .getPosition (e , mChart .getData ().getDataSetByIndex (h .getDataSetIndex ())
281+ MPPointF position = chart .getPosition (e , chart .getData ().getDataSetByIndex (h .getDataSetIndex ())
277282 .getAxisDependency ());
278283
279284 Log .i ("bounds" , bounds .toString ());
@@ -283,6 +288,5 @@ public void onValueSelected(Entry e, Highlight h) {
283288 }
284289
285290 @ Override
286- public void onNothingSelected () {
287- };
291+ public void onNothingSelected () {}
288292}
0 commit comments