|
| 1 | + |
| 2 | +package com.xxmassdeveloper.mpchartexample; |
| 3 | + |
| 4 | +import android.os.Bundle; |
| 5 | +import android.view.Menu; |
| 6 | +import android.view.MenuItem; |
| 7 | +import android.view.WindowManager; |
| 8 | +import android.widget.SeekBar; |
| 9 | +import android.widget.SeekBar.OnSeekBarChangeListener; |
| 10 | +import android.widget.TextView; |
| 11 | +import android.widget.Toast; |
| 12 | + |
| 13 | +import com.github.mikephil.charting.charts.CandleStickChart; |
| 14 | +import com.github.mikephil.charting.data.CandleData; |
| 15 | +import com.github.mikephil.charting.data.CandleDataSet; |
| 16 | +import com.github.mikephil.charting.data.CandleEntry; |
| 17 | +import com.github.mikephil.charting.data.filter.Approximator; |
| 18 | +import com.github.mikephil.charting.data.filter.Approximator.ApproximatorType; |
| 19 | +import com.github.mikephil.charting.utils.ColorTemplate; |
| 20 | +import com.github.mikephil.charting.utils.XLabels; |
| 21 | +import com.github.mikephil.charting.utils.XLabels.XLabelPosition; |
| 22 | +import com.xxmassdeveloper.mpchartexample.notimportant.DemoBase; |
| 23 | + |
| 24 | +import java.util.ArrayList; |
| 25 | + |
| 26 | +public class CandleStickChartActivity extends DemoBase implements OnSeekBarChangeListener { |
| 27 | + |
| 28 | + private CandleStickChart mChart; |
| 29 | + private SeekBar mSeekBarX, mSeekBarY; |
| 30 | + private TextView tvX, tvY; |
| 31 | + |
| 32 | + @Override |
| 33 | + protected void onCreate(Bundle savedInstanceState) { |
| 34 | + super.onCreate(savedInstanceState); |
| 35 | + getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, |
| 36 | + WindowManager.LayoutParams.FLAG_FULLSCREEN); |
| 37 | + setContentView(R.layout.activity_candlechart); |
| 38 | + |
| 39 | + tvX = (TextView) findViewById(R.id.tvXMax); |
| 40 | + tvY = (TextView) findViewById(R.id.tvYMax); |
| 41 | + |
| 42 | + mSeekBarX = (SeekBar) findViewById(R.id.seekBar1); |
| 43 | + mSeekBarX.setOnSeekBarChangeListener(this); |
| 44 | + |
| 45 | + mSeekBarY = (SeekBar) findViewById(R.id.seekBar2); |
| 46 | + mSeekBarY.setOnSeekBarChangeListener(this); |
| 47 | + |
| 48 | + mChart = (CandleStickChart) findViewById(R.id.chart1); |
| 49 | + |
| 50 | + mChart.setDrawYValues(false); |
| 51 | + |
| 52 | + mChart.setUnit(" €"); |
| 53 | + mChart.setDescription(""); |
| 54 | + |
| 55 | + // if more than 60 entries are displayed in the chart, no values will be |
| 56 | + // drawn |
| 57 | + mChart.setMaxVisibleValueCount(60); |
| 58 | + |
| 59 | + // scaling can now only be done on x- and y-axis separately |
| 60 | + mChart.setPinchZoom(false); |
| 61 | + |
| 62 | + mChart.setDrawVerticalGrid(false); |
| 63 | + mChart.setDrawHorizontalGrid(false); |
| 64 | + mChart.setDrawGridBackground(false); |
| 65 | + |
| 66 | + XLabels xLabels = mChart.getXLabels(); |
| 67 | + xLabels.setPosition(XLabelPosition.BOTTOM); |
| 68 | + xLabels.setCenterXLabelText(true); |
| 69 | + xLabels.setSpaceBetweenLabels(0); |
| 70 | + |
| 71 | + mChart.setDrawYLabels(false); |
| 72 | + mChart.setDrawLegend(false); |
| 73 | + |
| 74 | + // setting data |
| 75 | + mSeekBarX.setProgress(10); |
| 76 | + mSeekBarY.setProgress(100); |
| 77 | + |
| 78 | +// Legend l = mChart.getLegend(); |
| 79 | +// l.setPosition(LegendPosition.BELOW_CHART_CENTER); |
| 80 | +// l.setFormSize(8f); |
| 81 | +// l.setFormToTextSpace(4f); |
| 82 | +// l.setXEntrySpace(6f); |
| 83 | + |
| 84 | + // mChart.setDrawLegend(false); |
| 85 | + } |
| 86 | + |
| 87 | + @Override |
| 88 | + public boolean onCreateOptionsMenu(Menu menu) { |
| 89 | + getMenuInflater().inflate(R.menu.bar, menu); |
| 90 | + return true; |
| 91 | + } |
| 92 | + |
| 93 | + @Override |
| 94 | + public boolean onOptionsItemSelected(MenuItem item) { |
| 95 | + |
| 96 | + switch (item.getItemId()) { |
| 97 | + case R.id.actionToggleValues: { |
| 98 | + if (mChart.isDrawYValuesEnabled()) |
| 99 | + mChart.setDrawYValues(false); |
| 100 | + else |
| 101 | + mChart.setDrawYValues(true); |
| 102 | + mChart.invalidate(); |
| 103 | + break; |
| 104 | + } |
| 105 | + case R.id.actionToggleHighlight: { |
| 106 | + if (mChart.isHighlightEnabled()) |
| 107 | + mChart.setHighlightEnabled(false); |
| 108 | + else |
| 109 | + mChart.setHighlightEnabled(true); |
| 110 | + mChart.invalidate(); |
| 111 | + break; |
| 112 | + } |
| 113 | + case R.id.actionTogglePinch: { |
| 114 | + if (mChart.isPinchZoomEnabled()) |
| 115 | + mChart.setPinchZoom(false); |
| 116 | + else |
| 117 | + mChart.setPinchZoom(true); |
| 118 | + |
| 119 | + mChart.invalidate(); |
| 120 | + break; |
| 121 | + } |
| 122 | + case R.id.actionToggleStartzero: { |
| 123 | + if (mChart.isStartAtZeroEnabled()) |
| 124 | + mChart.setStartAtZero(false); |
| 125 | + else |
| 126 | + mChart.setStartAtZero(true); |
| 127 | + |
| 128 | + mChart.invalidate(); |
| 129 | + break; |
| 130 | + } |
| 131 | + case R.id.actionToggleAdjustXLegend: { |
| 132 | + XLabels xLabels = mChart.getXLabels(); |
| 133 | + |
| 134 | + if (xLabels.isAdjustXLabelsEnabled()) |
| 135 | + xLabels.setAdjustXLabels(false); |
| 136 | + else |
| 137 | + xLabels.setAdjustXLabels(true); |
| 138 | + |
| 139 | + mChart.invalidate(); |
| 140 | + break; |
| 141 | + } |
| 142 | + case R.id.animateX: { |
| 143 | + mChart.animateX(3000); |
| 144 | + break; |
| 145 | + } |
| 146 | + case R.id.animateY: { |
| 147 | + mChart.animateY(3000); |
| 148 | + break; |
| 149 | + } |
| 150 | + case R.id.animateXY: { |
| 151 | + |
| 152 | + mChart.animateXY(3000, 3000); |
| 153 | + break; |
| 154 | + } |
| 155 | + case R.id.actionToggleFilter: { |
| 156 | + |
| 157 | + Approximator a = new Approximator(ApproximatorType.DOUGLAS_PEUCKER, 25); |
| 158 | + |
| 159 | + if (!mChart.isFilteringEnabled()) { |
| 160 | + mChart.enableFiltering(a); |
| 161 | + } else { |
| 162 | + mChart.disableFiltering(); |
| 163 | + } |
| 164 | + mChart.invalidate(); |
| 165 | + break; |
| 166 | + } |
| 167 | + case R.id.actionSave: { |
| 168 | + if (mChart.saveToGallery("title" + System.currentTimeMillis(), 50)) { |
| 169 | + Toast.makeText(getApplicationContext(), "Saving SUCCESSFUL!", |
| 170 | + Toast.LENGTH_SHORT).show(); |
| 171 | + } else |
| 172 | + Toast.makeText(getApplicationContext(), "Saving FAILED!", Toast.LENGTH_SHORT) |
| 173 | + .show(); |
| 174 | + break; |
| 175 | + } |
| 176 | + } |
| 177 | + return true; |
| 178 | + } |
| 179 | + |
| 180 | + @Override |
| 181 | + public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { |
| 182 | + |
| 183 | + tvX.setText("" + (mSeekBarX.getProgress() + 1)); |
| 184 | + tvY.setText("" + (mSeekBarY.getProgress())); |
| 185 | + |
| 186 | + ArrayList<CandleEntry> yVals1 = new ArrayList<CandleEntry>(); |
| 187 | + |
| 188 | + for (int i = 0; i < mSeekBarX.getProgress()+1; i++) { |
| 189 | + float mult = (mSeekBarY.getProgress() + 1); |
| 190 | + float val = (float) (Math.random() * mult) + mult / 3; |
| 191 | + yVals1.add(new CandleEntry(val, i, val+5, val-5, val+7, val-7)); |
| 192 | + } |
| 193 | + |
| 194 | + ArrayList<String> xVals = new ArrayList<String>(); |
| 195 | + for (int i = 0; i < mSeekBarX.getProgress()+1; i++) { |
| 196 | + xVals.add((int) yVals1.get(i).getVal() + " " + mChart.getUnit()); |
| 197 | + } |
| 198 | + |
| 199 | + CandleDataSet set1 = new CandleDataSet(yVals1, "Data Set"); |
| 200 | + set1.setColors(ColorTemplate.createColors(getApplicationContext(), |
| 201 | + ColorTemplate.VORDIPLOM_COLORS)); |
| 202 | + |
| 203 | + CandleData data = new CandleData(xVals, set1); |
| 204 | + |
| 205 | + mChart.setData(data); |
| 206 | + mChart.invalidate(); |
| 207 | + } |
| 208 | + |
| 209 | + @Override |
| 210 | + public void onStartTrackingTouch(SeekBar seekBar) { |
| 211 | + // TODO Auto-generated method stub |
| 212 | + |
| 213 | + } |
| 214 | + |
| 215 | + @Override |
| 216 | + public void onStopTrackingTouch(SeekBar seekBar) { |
| 217 | + // TODO Auto-generated method stub |
| 218 | + |
| 219 | + } |
| 220 | +} |
0 commit comments