Search in sources :

Example 81 with Entry

use of com.github.mikephil.charting.data.Entry in project MPAndroidChart by PhilJay.

the class DataSetTest method testGetEntryForXValueWithDuplicates.

@Test
public void testGetEntryForXValueWithDuplicates() {
    // sorted list of values (by x position)
    List<Entry> values = new ArrayList<Entry>();
    values.add(new Entry(0, 10));
    values.add(new Entry(1, 20));
    values.add(new Entry(2, 30));
    values.add(new Entry(3, 40));
    // duplicate
    values.add(new Entry(3, 50));
    values.add(new Entry(4, 60));
    // duplicate
    values.add(new Entry(4, 70));
    values.add(new Entry(5, 80));
    values.add(new Entry(6, 90));
    values.add(new Entry(7, 100));
    values.add(new Entry(8, 110));
    // duplicate
    values.add(new Entry(8, 120));
    ScatterDataSet set = new ScatterDataSet(values, "");
    Entry closest = set.getEntryForXValue(0, Float.NaN, DataSet.Rounding.CLOSEST);
    assertEquals(0, closest.getX(), 0.01f);
    assertEquals(10, closest.getY(), 0.01f);
    closest = set.getEntryForXValue(5, Float.NaN, DataSet.Rounding.CLOSEST);
    assertEquals(5, closest.getX(), 0.01f);
    assertEquals(80, closest.getY(), 0.01f);
    closest = set.getEntryForXValue(5.4f, Float.NaN, DataSet.Rounding.CLOSEST);
    assertEquals(5, closest.getX(), 0.01f);
    assertEquals(80, closest.getY(), 0.01f);
    closest = set.getEntryForXValue(4.6f, Float.NaN, DataSet.Rounding.CLOSEST);
    assertEquals(5, closest.getX(), 0.01f);
    assertEquals(80, closest.getY(), 0.01f);
    closest = set.getEntryForXValue(7, Float.NaN, DataSet.Rounding.CLOSEST);
    assertEquals(7, closest.getX(), 0.01f);
    assertEquals(100, closest.getY(), 0.01f);
    closest = set.getEntryForXValue(4f, Float.NaN, DataSet.Rounding.CLOSEST);
    assertEquals(4, closest.getX(), 0.01f);
    assertEquals(60, closest.getY(), 0.01f);
    List<Entry> entries = set.getEntriesForXValue(4f);
    assertEquals(2, entries.size());
    assertEquals(60, entries.get(0).getY(), 0.01f);
    assertEquals(70, entries.get(1).getY(), 0.01f);
    entries = set.getEntriesForXValue(3.5f);
    assertEquals(0, entries.size());
    entries = set.getEntriesForXValue(2f);
    assertEquals(1, entries.size());
    assertEquals(30, entries.get(0).getY(), 0.01f);
}
Also used : Entry(com.github.mikephil.charting.data.Entry) ScatterDataSet(com.github.mikephil.charting.data.ScatterDataSet) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Example 82 with Entry

use of com.github.mikephil.charting.data.Entry in project AdMoney by ErnestoGonAr.

the class Reporte_Egresos method onCreate.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    try {
        initData();
    } catch (NullPointerException e) {
        Toast toast = Toast.makeText(this, "No hay Egresos", Toast.LENGTH_SHORT);
        toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
        toast.show();
        return;
    }
    setContentView(R.layout.activity_reporte__egresos);
    pieChart = (PieChart) findViewById(R.id.grafico_egresos);
    pieChart.setRotationEnabled(true);
    pieChart.setHoleRadius(30f);
    pieChart.setCenterTextSize(10);
    pieChart.setEntryLabelTextSize(4);
    pieChart.setTransparentCircleAlpha(0);
    Description d = pieChart.getDescription();
    d.setText("");
    pieChart.setDescription(d);
    pieChart.getLegend().setOrientation(Legend.LegendOrientation.VERTICAL);
    pieChart.getLegend().setTextSize(12);
    pieChart.getLegend().setPosition(Legend.LegendPosition.LEFT_OF_CHART);
    if (xData.length == 0)
        return;
    addData();
    pieChart.setOnChartValueSelectedListener(new OnChartValueSelectedListener() {

        @Override
        public void onValueSelected(Entry e, Highlight h) {
            Log.d(TAG, "onValueSelected: Value selected from chart");
            Log.d(TAG, e.toString());
            Log.d(TAG, h.toString());
            int pos1 = e.toString().indexOf("Entry, x: 0.0 y: ");
            String res = e.toString().substring(pos1 + 17);
            for (int i = 0; i < yData.length; i++) {
                if (yData[i] == Float.parseFloat(res)) {
                    pos1 = i;
                    break;
                }
            }
            String categoria = xData[pos1];
            Toast.makeText(Reporte_Egresos.this, "Ha gastado $" + res + " en " + categoria, Toast.LENGTH_LONG).show();
        }

        @Override
        public void onNothingSelected() {
        }
    });
    spinner = (Spinner) findViewById(R.id.spinner_r_egresos);
    spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
            calendar.setText("Selecciona una fecha");
        }

        @Override
        public void onNothingSelected(AdapterView<?> parent) {
        }
    });
    año = 2017;
    mes = 5;
    diad = 17;
    calendar = (EditText) findViewById(R.id.fecha_r_egresos);
    calendar.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            // To show current date in the datepicker
            final Calendar mcurrentDate = Calendar.getInstance();
            mYear = mcurrentDate.get(Calendar.YEAR);
            mMonth = mcurrentDate.get(Calendar.MONTH);
            mDay = mcurrentDate.get(Calendar.DAY_OF_MONTH);
            DatePickerDialog mDatePicker = new DatePickerDialog(Reporte_Egresos.this, new DatePickerDialog.OnDateSetListener() {

                public void onDateSet(DatePicker datepicker, int selectedyear, int selectedmonth, int selectedday) {
                    // TODO Auto-generated method stub
                    año = selectedyear;
                    mes = selectedmonth + 1;
                    diad = selectedday;
                    calendar.setText(selectedyear + "-" + (selectedmonth + 1) + "-" + selectedday);
                }
            }, mYear, mMonth, mDay);
            mDatePicker.setTitle("Selecciona fecha");
            mDatePicker.show();
        }
    });
    consultar = (Button) findViewById(R.id.consultar_e);
    consultar.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            if (calendar.getText().toString().equals("Selecciona una fecha")) {
                calendar.requestFocus();
                calendar.callOnClick();
                return;
            }
            String[][] data;
            switch(spinner.getSelectedItemPosition()) {
                case 1:
                    String dia = calendar.getText().toString();
                    data = bd.obtenerEgresos(diad + "", mes + "", año + "");
                    if (data == null) {
                        Toast toast = Toast.makeText(getApplicationContext(), "No hay registros para esta fecha", Toast.LENGTH_SHORT);
                        toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
                        toast.show();
                        pieChart.setVisibility(View.INVISIBLE);
                        break;
                    }
                    pieChart.clear();
                    initData(data);
                    addData();
                    pieChart.setVisibility(View.VISIBLE);
                    break;
                case 2:
                    data = bd.obtenerEgresos(mes + "", año + "");
                    if (data == null) {
                        Toast toast = Toast.makeText(getApplicationContext(), "No hay registros para esta fecha", Toast.LENGTH_SHORT);
                        toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
                        toast.show();
                        pieChart.setVisibility(View.INVISIBLE);
                        break;
                    }
                    pieChart.clear();
                    initData(data);
                    addData();
                    pieChart.setVisibility(View.VISIBLE);
                    break;
                case 3:
                    data = bd.obtenerEgresos(año + "");
                    if (data == null) {
                        Toast toast = Toast.makeText(getApplicationContext(), "No hay registros para esta fecha", Toast.LENGTH_SHORT);
                        toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
                        toast.show();
                        pieChart.setVisibility(View.INVISIBLE);
                        break;
                    }
                    pieChart.clear();
                    initData(data);
                    addData();
                    pieChart.setVisibility(View.VISIBLE);
                    break;
                default:
                    pieChart.clear();
                    initData();
                    addData();
                    pieChart.setVisibility(View.VISIBLE);
                    break;
            }
        }
    });
}
Also used : OnChartValueSelectedListener(com.github.mikephil.charting.listener.OnChartValueSelectedListener) Description(com.github.mikephil.charting.components.Description) Highlight(com.github.mikephil.charting.highlight.Highlight) DatePickerDialog(android.app.DatePickerDialog) Calendar(java.util.Calendar) View(android.view.View) AdapterView(android.widget.AdapterView) Entry(com.github.mikephil.charting.data.Entry) PieEntry(com.github.mikephil.charting.data.PieEntry) Toast(android.widget.Toast) AdapterView(android.widget.AdapterView) DatePicker(android.widget.DatePicker)

Example 83 with Entry

use of com.github.mikephil.charting.data.Entry in project run-wallet-android by runplay.

the class ChooseSeedFragment method drawChart.

private void drawChart() {
    long displayBalance = 0L;
    if (Store.getCurrentWallet() != null) {
        seedName.setText(Store.getCurrentSeed().name);
        displayBalance = Store.getCurrentWallet().getBalanceDisplay();
    }
    Currency cur = Store.getDefaultCurrency(getActivity());
    seedValue.setText(IotaToText.convertRawIotaAmountToDisplayText(displayBalance, true));
    Ticker ticker = Store.getTicker("IOTA:" + cur.getCurrencyCode());
    if (ticker != null) {
        seedCurrency.setText(ticker.getIotaValString(displayBalance) + "\n" + cur.getSymbol());
        DecimalFormat udf = df;
        if (ticker.getLast() < 0.01)
            udf = dfs;
        xchangeLast.setText(udf.format(ticker.getLast()));
        xchangeHigh.setText(udf.format(ticker.getHigh()));
        xchangeLow.setText(udf.format(ticker.getLow()));
    }
    // Log.e("THIST","go 3");
    chart.setNoDataText(getString(R.string.messages_no_chart_data));
    chart.setNoDataTextColor(ContextCompat.getColor(getActivity(), R.color.white));
    chart.setGridBackgroundColor(ContextCompat.getColor(getActivity(), R.color.colorLight));
    Paint p = chart.getPaint(Chart.PAINT_INFO);
    p.setColor(ContextCompat.getColor(getActivity(), AppTheme.getPrimaryDark()));
    chart.getDescription().setEnabled(false);
    chart.setDragDecelerationFrictionCoef(0.95f);
    // chart.setExtraOffsets(15.f, 15.f, 15.f, 15.f);
    chart.setHighlightPerTapEnabled(false);
    chart.setEnabled(false);
    chart.setTouchEnabled(false);
    chart.animateX(1000);
    chart.setPadding(0, 0, 0, 0);
    chart.getAxisLeft().setEnabled(false);
    chart.getAxisRight().setEnabled(false);
    chart.getXAxis().setEnabled(false);
    // chart.setZ(0.5f);
    chart.zoomOut();
    chart.setPinchZoom(false);
    chart.setPinchZoom(false);
    chart.setFocusable(false);
    Legend leg = chart.getLegend();
    leg.setVerticalAlignment(Legend.LegendVerticalAlignment.BOTTOM);
    leg.setHorizontalAlignment(Legend.LegendHorizontalAlignment.LEFT);
    leg.setOrientation(Legend.LegendOrientation.HORIZONTAL);
    leg.setDrawInside(false);
    leg.setEnabled(false);
    List<Entry> entries = new ArrayList<Entry>();
    List<Entry> entriesvol = new ArrayList<Entry>();
    // Log
    history = Store.getTickerHist(getActivity(), Store.getDefaultCurrency(getActivity()).getCurrencyCode(), currentStep);
    double avg = 0D;
    if (history != null) {
        // Log.e("THIST","go 4");
        List<Tick> tickers = history.getTicks();
        Collections.reverse(tickers);
        // double min=tickers.get(0).getLast();
        double max = tickers.get(0).getLast();
        double lastvol = 0;
        double maxvol = 0;
        double minvol = 0D;
        for (int i = 0; i < tickers.size(); i++) {
            double last = tickers.get(i).getLast();
            double tmpvol = tickers.get(i).getVol();
            if (avg == 0D) {
                avg = last;
            } else {
                avg = (avg + last) / 2;
            }
            if (last > max)
                max = last;
            if (tmpvol > maxvol)
                maxvol = tmpvol;
            if (minvol == 0)
                minvol = tmpvol;
            else if (tmpvol < minvol)
                minvol = tmpvol;
            Entry e = new Entry(i * 10, Double.valueOf(last).floatValue());
            entries.add(e);
        }
        max = Math.abs(max);
        double voldivide = (100 / maxvol) / 70;
        for (int i = tickers.size() - 1; i >= 0; i--) {
            double tmpvol = tickers.get(i).getVol() * voldivide;
            // Log.e("YUP", "vol: "+tickers.get(i).getVol()+" = " + tmpvol);
            Entry ev = new Entry(i * 10, Double.valueOf(tmpvol + (0.015 * history.getStep())).floatValue());
            entriesvol.add(ev);
        }
        Collections.reverse(entriesvol);
        // add entries to dataset
        LineDataSet dataSet = new LineDataSet(entries, "IOTA:" + Store.getDefaultCurrency(getActivity()).getCurrencyCode());
        dataSet.setDrawCircleHole(false);
        dataSet.setDrawCircles(false);
        dataSet.setDrawFilled(true);
        dataSet.setFillColor(getResources().getColor(AppTheme.getPrimaryDark()));
        dataSet.setFillAlpha(100);
        dataSet.setColor(B.getColor(getActivity(), AppTheme.getPrimary()));
        // add entries to dataset
        LineDataSet idataSet = new LineDataSet(entriesvol, "Vol");
        idataSet.setDrawCircleHole(false);
        idataSet.setDrawCircles(false);
        idataSet.setDrawFilled(true);
        idataSet.setFillColor(getResources().getColor(R.color.colorMiddle));
        idataSet.setColor(B.getColor(getActivity(), AppTheme.getPrimaryDark()));
        idataSet.setFillAlpha(100);
        ArrayList<ILineDataSet> dataSets = new ArrayList<ILineDataSet>();
        dataSets.add(dataSet);
        dataSets.add(idataSet);
        LineData lineData = new LineData(dataSets);
        chart.setData(lineData);
        float gomin = Double.valueOf(0).floatValue();
        float gomax = +Double.valueOf(max * 1.4).floatValue();
        chart.getAxisLeft().setAxisMinimum(gomin);
        chart.getAxisLeft().setAxisMaximum(gomax);
        chart.getAxisRight().setAxisMinimum(gomin);
        chart.getAxisRight().setAxisMaximum(gomax);
    }
    // refresh
    chart.invalidate();
}
Also used : Legend(com.github.mikephil.charting.components.Legend) LineDataSet(com.github.mikephil.charting.data.LineDataSet) ILineDataSet(com.github.mikephil.charting.interfaces.datasets.ILineDataSet) Ticker(run.wallet.iota.model.Ticker) DecimalFormat(java.text.DecimalFormat) ArrayList(java.util.ArrayList) Paint(android.graphics.Paint) Paint(android.graphics.Paint) Entry(com.github.mikephil.charting.data.Entry) LineData(com.github.mikephil.charting.data.LineData) ILineDataSet(com.github.mikephil.charting.interfaces.datasets.ILineDataSet) Currency(run.wallet.common.Currency) Tick(run.wallet.iota.model.Tick)

Aggregations

Entry (com.github.mikephil.charting.data.Entry)83 ArrayList (java.util.ArrayList)34 Paint (android.graphics.Paint)27 LineData (com.github.mikephil.charting.data.LineData)20 LineDataSet (com.github.mikephil.charting.data.LineDataSet)19 ILineDataSet (com.github.mikephil.charting.interfaces.datasets.ILineDataSet)17 BarEntry (com.github.mikephil.charting.data.BarEntry)13 ScatterDataSet (com.github.mikephil.charting.data.ScatterDataSet)10 Path (android.graphics.Path)7 PieEntry (com.github.mikephil.charting.data.PieEntry)7 IOException (java.io.IOException)6 PointF (android.graphics.PointF)5 CandleEntry (com.github.mikephil.charting.data.CandleEntry)5 PieDataSet (com.github.mikephil.charting.data.PieDataSet)5 ScatterData (com.github.mikephil.charting.data.ScatterData)5 Highlight (com.github.mikephil.charting.highlight.Highlight)5 Transformer (com.github.mikephil.charting.utils.Transformer)5 Test (org.junit.Test)5 MPPointF (com.github.mikephil.charting.utils.MPPointF)4 SuppressLint (android.annotation.SuppressLint)3