• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 
2 package com.xxmassdeveloper.mpchartexample;
3 
4 import android.Manifest;
5 import android.content.Intent;
6 import android.content.pm.PackageManager;
7 import android.graphics.Color;
8 import android.graphics.Paint;
9 import android.net.Uri;
10 import android.os.Bundle;
11 import androidx.core.content.ContextCompat;
12 import android.view.Menu;
13 import android.view.MenuItem;
14 import android.view.WindowManager;
15 import android.widget.SeekBar;
16 import android.widget.SeekBar.OnSeekBarChangeListener;
17 import android.widget.TextView;
18 
19 import com.github.mikephil.charting.charts.CandleStickChart;
20 import com.github.mikephil.charting.components.XAxis;
21 import com.github.mikephil.charting.components.XAxis.XAxisPosition;
22 import com.github.mikephil.charting.components.YAxis;
23 import com.github.mikephil.charting.components.YAxis.AxisDependency;
24 import com.github.mikephil.charting.data.CandleData;
25 import com.github.mikephil.charting.data.CandleDataSet;
26 import com.github.mikephil.charting.data.CandleEntry;
27 import com.github.mikephil.charting.interfaces.datasets.ICandleDataSet;
28 import com.github.mikephil.charting.interfaces.datasets.IDataSet;
29 import com.xxmassdeveloper.mpchartexample.notimportant.DemoBase;
30 
31 import java.util.ArrayList;
32 
33 public class CandleStickChartActivity extends DemoBase implements OnSeekBarChangeListener {
34 
35     private CandleStickChart chart;
36     private SeekBar seekBarX, seekBarY;
37     private TextView tvX, tvY;
38 
39     @Override
onCreate(Bundle savedInstanceState)40     protected void onCreate(Bundle savedInstanceState) {
41         super.onCreate(savedInstanceState);
42         getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
43                 WindowManager.LayoutParams.FLAG_FULLSCREEN);
44         setContentView(R.layout.activity_candlechart);
45 
46         setTitle("CandleStickChartActivity");
47 
48         tvX = findViewById(R.id.tvXMax);
49         tvY = findViewById(R.id.tvYMax);
50 
51         seekBarX = findViewById(R.id.seekBar1);
52         seekBarX.setOnSeekBarChangeListener(this);
53 
54         seekBarY = findViewById(R.id.seekBar2);
55         seekBarY.setOnSeekBarChangeListener(this);
56 
57         chart = findViewById(R.id.chart1);
58         chart.setBackgroundColor(Color.WHITE);
59 
60         chart.getDescription().setEnabled(false);
61 
62         // if more than 60 entries are displayed in the chart, no values will be
63         // drawn
64         chart.setMaxVisibleValueCount(60);
65 
66         // scaling can now only be done on x- and y-axis separately
67         chart.setPinchZoom(false);
68 
69         chart.setDrawGridBackground(false);
70 
71         XAxis xAxis = chart.getXAxis();
72         xAxis.setPosition(XAxisPosition.BOTTOM);
73         xAxis.setDrawGridLines(false);
74 
75         YAxis leftAxis = chart.getAxisLeft();
76 //        leftAxis.setEnabled(false);
77         leftAxis.setLabelCount(7, false);
78         leftAxis.setDrawGridLines(false);
79         leftAxis.setDrawAxisLine(false);
80 
81         YAxis rightAxis = chart.getAxisRight();
82         rightAxis.setEnabled(false);
83 //        rightAxis.setStartAtZero(false);
84 
85         // setting data
86         seekBarX.setProgress(40);
87         seekBarY.setProgress(100);
88 
89         chart.getLegend().setEnabled(false);
90     }
91 
92     @Override
onProgressChanged(SeekBar seekBar, int progress, boolean fromUser)93     public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
94 
95         progress = (seekBarX.getProgress());
96 
97         tvX.setText(String.valueOf(progress));
98         tvY.setText(String.valueOf(seekBarY.getProgress()));
99 
100         chart.resetTracking();
101 
102         ArrayList<CandleEntry> values = new ArrayList<>();
103 
104         for (int i = 0; i < progress; i++) {
105             float multi = (seekBarY.getProgress() + 1);
106             float val = (float) (Math.random() * 40) + multi;
107 
108             float high = (float) (Math.random() * 9) + 8f;
109             float low = (float) (Math.random() * 9) + 8f;
110 
111             float open = (float) (Math.random() * 6) + 1f;
112             float close = (float) (Math.random() * 6) + 1f;
113 
114             boolean even = i % 2 == 0;
115 
116             values.add(new CandleEntry(
117                     i, val + high,
118                     val - low,
119                     even ? val + open : val - open,
120                     even ? val - close : val + close,
121                     getResources().getDrawable(R.drawable.star)
122             ));
123         }
124 
125         CandleDataSet set1 = new CandleDataSet(values, "Data Set");
126 
127         set1.setDrawIcons(false);
128         set1.setAxisDependency(AxisDependency.LEFT);
129 //        set1.setColor(Color.rgb(80, 80, 80));
130         set1.setShadowColor(Color.DKGRAY);
131         set1.setShadowWidth(0.7f);
132         set1.setDecreasingColor(Color.RED);
133         set1.setDecreasingPaintStyle(Paint.Style.FILL);
134         set1.setIncreasingColor(Color.rgb(122, 242, 84));
135         set1.setIncreasingPaintStyle(Paint.Style.STROKE);
136         set1.setNeutralColor(Color.BLUE);
137         //set1.setHighlightLineWidth(1f);
138 
139         CandleData data = new CandleData(set1);
140 
141         chart.setData(data);
142         chart.invalidate();
143     }
144 
145     @Override
onCreateOptionsMenu(Menu menu)146     public boolean onCreateOptionsMenu(Menu menu) {
147         getMenuInflater().inflate(R.menu.candle, menu);
148         return true;
149     }
150 
151     @Override
onOptionsItemSelected(MenuItem item)152     public boolean onOptionsItemSelected(MenuItem item) {
153 
154         switch (item.getItemId()) {
155             case R.id.viewGithub: {
156                 Intent i = new Intent(Intent.ACTION_VIEW);
157                 i.setData(Uri.parse("https://github.com/PhilJay/MPAndroidChart/blob/master/MPChartExample/src/com/xxmassdeveloper/mpchartexample/CandleStickChartActivity.java"));
158                 startActivity(i);
159                 break;
160             }
161             case R.id.actionToggleValues: {
162                 for (IDataSet set : chart.getData().getDataSets())
163                     set.setDrawValues(!set.isDrawValuesEnabled());
164 
165                 chart.invalidate();
166                 break;
167             }
168             case R.id.actionToggleIcons: {
169                 for (IDataSet set : chart.getData().getDataSets())
170                     set.setDrawIcons(!set.isDrawIconsEnabled());
171 
172                 chart.invalidate();
173                 break;
174             }
175             case R.id.actionToggleHighlight: {
176                 if(chart.getData() != null) {
177                     chart.getData().setHighlightEnabled(!chart.getData().isHighlightEnabled());
178                     chart.invalidate();
179                 }
180                 break;
181             }
182             case R.id.actionTogglePinch: {
183                 if (chart.isPinchZoomEnabled())
184                     chart.setPinchZoom(false);
185                 else
186                     chart.setPinchZoom(true);
187 
188                 chart.invalidate();
189                 break;
190             }
191             case R.id.actionToggleAutoScaleMinMax: {
192                 chart.setAutoScaleMinMaxEnabled(!chart.isAutoScaleMinMaxEnabled());
193                 chart.notifyDataSetChanged();
194                 break;
195             }
196             case R.id.actionToggleMakeShadowSameColorAsCandle: {
197                 for (ICandleDataSet set : chart.getData().getDataSets()) {
198                     ((CandleDataSet) set).setShadowColorSameAsCandle(!set.getShadowColorSameAsCandle());
199                 }
200 
201                 chart.invalidate();
202                 break;
203             }
204             case R.id.animateX: {
205                 chart.animateX(2000);
206                 break;
207             }
208             case R.id.animateY: {
209                 chart.animateY(2000);
210                 break;
211             }
212             case R.id.animateXY: {
213                 chart.animateXY(2000, 2000);
214                 break;
215             }
216             case R.id.actionSave: {
217                 if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) {
218                     saveToGallery();
219                 } else {
220                     requestStoragePermission(chart);
221                 }
222                 break;
223             }
224         }
225         return true;
226     }
227 
228     @Override
saveToGallery()229     protected void saveToGallery() {
230         saveToGallery(chart, "CandleStickChartActivity");
231     }
232 
233     @Override
onStartTrackingTouch(SeekBar seekBar)234     public void onStartTrackingTouch(SeekBar seekBar) {}
235 
236     @Override
onStopTrackingTouch(SeekBar seekBar)237     public void onStopTrackingTouch(SeekBar seekBar) {}
238 }
239