Search in sources :

Example 36 with Entry

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

the class DataSetTest method testAddRemoveEntry.

@Test
public void testAddRemoveEntry() {
    List<Entry> entries = new ArrayList<Entry>();
    entries.add(new Entry(10, 10));
    entries.add(new Entry(15, 2));
    entries.add(new Entry(21, 5));
    ScatterDataSet set = new ScatterDataSet(entries, "");
    assertEquals(3, set.getEntryCount());
    set.addEntryOrdered(new Entry(5, 1));
    assertEquals(4, set.getEntryCount());
    assertEquals(5, set.getXMin(), 0.01f);
    assertEquals(21, set.getXMax(), 0.01f);
    assertEquals(1f, set.getYMin(), 0.01f);
    assertEquals(10f, set.getYMax(), 0.01f);
    assertEquals(5, set.getEntryForIndex(0).getX(), 0.01f);
    assertEquals(1, set.getEntryForIndex(0).getY(), 0.01f);
    set.addEntryOrdered(new Entry(20, 50));
    assertEquals(5, set.getEntryCount());
    assertEquals(20, set.getEntryForIndex(3).getX(), 0.01f);
    assertEquals(50, set.getEntryForIndex(3).getY(), 0.01f);
    assertTrue(set.removeEntry(3));
    assertEquals(4, set.getEntryCount());
    assertEquals(21, set.getEntryForIndex(3).getX(), 0.01f);
    assertEquals(5, set.getEntryForIndex(3).getY(), 0.01f);
    assertEquals(5, set.getEntryForIndex(0).getX(), 0.01f);
    assertEquals(1, set.getEntryForIndex(0).getY(), 0.01f);
    assertTrue(set.removeFirst());
    assertEquals(3, set.getEntryCount());
    assertEquals(10, set.getEntryForIndex(0).getX(), 0.01f);
    assertEquals(10, set.getEntryForIndex(0).getY(), 0.01f);
    set.addEntryOrdered(new Entry(15, 3));
    assertEquals(4, set.getEntryCount());
    assertEquals(15, set.getEntryForIndex(1).getX(), 0.01f);
    assertEquals(3, set.getEntryForIndex(1).getY(), 0.01f);
    assertEquals(21, set.getEntryForIndex(3).getX(), 0.01f);
    assertEquals(5, set.getEntryForIndex(3).getY(), 0.01f);
    assertTrue(set.removeLast());
    assertEquals(3, set.getEntryCount());
    assertEquals(15, set.getEntryForIndex(2).getX(), 0.01f);
    assertEquals(2, set.getEntryForIndex(2).getY(), 0.01f);
    assertTrue(set.removeLast());
    assertEquals(2, set.getEntryCount());
    assertTrue(set.removeLast());
    assertEquals(1, set.getEntryCount());
    assertEquals(10, set.getEntryForIndex(0).getX(), 0.01f);
    assertEquals(10, set.getEntryForIndex(0).getY(), 0.01f);
    assertTrue(set.removeLast());
    assertEquals(0, set.getEntryCount());
    assertFalse(set.removeLast());
    assertFalse(set.removeFirst());
}
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 37 with Entry

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

the class DataSetTest method testGetEntryForXValue.

@Test
public void testGetEntryForXValue() {
    List<Entry> entries = new ArrayList<Entry>();
    entries.add(new Entry(10, 10));
    entries.add(new Entry(15, 5));
    entries.add(new Entry(21, 5));
    ScatterDataSet set = new ScatterDataSet(entries, "");
    Entry closest = set.getEntryForXValue(17, Float.NaN, DataSet.Rounding.CLOSEST);
    assertEquals(15, closest.getX(), 0.01f);
    assertEquals(5, closest.getY(), 0.01f);
    closest = set.getEntryForXValue(17, Float.NaN, DataSet.Rounding.DOWN);
    assertEquals(15, closest.getX(), 0.01f);
    assertEquals(5, closest.getY(), 0.01f);
    closest = set.getEntryForXValue(15, Float.NaN, DataSet.Rounding.DOWN);
    assertEquals(15, closest.getX(), 0.01f);
    assertEquals(5, closest.getY(), 0.01f);
    closest = set.getEntryForXValue(14, Float.NaN, DataSet.Rounding.DOWN);
    assertEquals(10, closest.getX(), 0.01f);
    assertEquals(10, closest.getY(), 0.01f);
    closest = set.getEntryForXValue(17, Float.NaN, DataSet.Rounding.UP);
    assertEquals(21, closest.getX(), 0.01f);
    assertEquals(5, closest.getY(), 0.01f);
    closest = set.getEntryForXValue(21, Float.NaN, DataSet.Rounding.UP);
    assertEquals(21, closest.getX(), 0.01f);
    assertEquals(5, closest.getY(), 0.01f);
    closest = set.getEntryForXValue(21, Float.NaN, DataSet.Rounding.CLOSEST);
    assertEquals(21, closest.getX(), 0.01f);
    assertEquals(5, closest.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 38 with Entry

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

the class Transformer method generateTransformedValuesLine.

/**
     * Transforms an List of Entry into a float array containing the x and
     * y values transformed with all matrices for the LINECHART.
     *
     * @param data
     * @return
     */
public float[] generateTransformedValuesLine(ILineDataSet data, float phaseX, float phaseY, int min, int max) {
    final int count = ((int) ((max - min) * phaseX) + 1) * 2;
    if (valuePointsForGenerateTransformedValuesLine.length != count) {
        valuePointsForGenerateTransformedValuesLine = new float[count];
    }
    float[] valuePoints = valuePointsForGenerateTransformedValuesLine;
    for (int j = 0; j < count; j += 2) {
        Entry e = data.getEntryForIndex(j / 2 + min);
        if (e != null) {
            valuePoints[j] = e.getX();
            valuePoints[j + 1] = e.getY() * phaseY;
        } else {
            valuePoints[j] = 0;
            valuePoints[j + 1] = 0;
        }
    }
    getValueToPixelMatrix().mapPoints(valuePoints);
    return valuePoints;
}
Also used : Entry(com.github.mikephil.charting.data.Entry) CandleEntry(com.github.mikephil.charting.data.CandleEntry)

Example 39 with Entry

use of com.github.mikephil.charting.data.Entry in project Weather by Sparker0i.

the class GraphsFragment method createEntries.

public void createEntries() {
    ArrayList<WeatherFort.WeatherList> list;
    try {
        list = mDescribable;
        for (int i = 0; i < 10; ++i) {
            long day = list.get(i).getDt();
            long temp = list.get(i).getTemp().getDay();
            long pressure = (long) list.get(i).getPressure();
            long rain;
            try {
                rain = (long) list.get(i).getRain();
            } catch (Exception ex) {
                rain = 0;
            }
            long snow;
            try {
                snow = (long) list.get(i).getSnow();
            } catch (Exception ex) {
                snow = 0;
            }
            long wind = (long) list.get(i).getSpeed();
            tempEntries.add(new Entry(i, temp));
            rainEntries.add(new Entry(i, rain));
            pressureEntries.add(new Entry(i, pressure));
            snowEntries.add(new Entry(i, snow));
            windEntries.add(new Entry(i, wind));
            Log.i("Added", "Entry : " + i + " " + temp);
            dates[i] = getDay(day);
            Log.i("Added", "Day : " + dates[i]);
        }
    } catch (Exception ex) {
        ex.printStackTrace();
        Log.i("Caught", "JSON Ex");
    }
}
Also used : Entry(com.github.mikephil.charting.data.Entry) SuppressLint(android.annotation.SuppressLint) ExecutionException(java.util.concurrent.ExecutionException)

Example 40 with Entry

use of com.github.mikephil.charting.data.Entry in project Gadgetbridge by Freeyourgadget.

the class AbstractChartFragment method refresh.

protected DefaultChartsData<CombinedData> refresh(GBDevice gbDevice, List<? extends ActivitySample> samples) {
    //        Calendar cal = GregorianCalendar.getInstance();
    //        cal.clear();
    TimestampTranslation tsTranslation = new TimestampTranslation();
    //        Date date;
    //        String dateStringFrom = "";
    //        String dateStringTo = "";
    //        ArrayList<String> xLabels = null;
    LOG.info("" + getTitle() + ": number of samples:" + samples.size());
    CombinedData combinedData;
    if (samples.size() > 1) {
        boolean annotate = true;
        boolean use_steps_as_movement;
        int last_type = ActivityKind.TYPE_UNKNOWN;
        int numEntries = samples.size();
        List<BarEntry> activityEntries = new ArrayList<>(numEntries);
        boolean hr = supportsHeartrate(gbDevice);
        List<Entry> heartrateEntries = hr ? new ArrayList<Entry>(numEntries) : null;
        // this is kinda inefficient...
        List<Integer> colors = new ArrayList<>(numEntries);
        int lastHrSampleIndex = -1;
        for (int i = 0; i < numEntries; i++) {
            ActivitySample sample = samples.get(i);
            int type = sample.getKind();
            int ts = tsTranslation.shorten(sample.getTimestamp());
            //                System.out.println(ts);
            //                ts = i;
            // determine start and end dates
            //                if (i == 0) {
            //                    cal.setTimeInMillis(ts * 1000L); // make sure it's converted to long
            //                    date = cal.getTime();
            //                    dateStringFrom = dateFormat.format(date);
            //                } else if (i == samples.size() - 1) {
            //                    cal.setTimeInMillis(ts * 1000L); // same here
            //                    date = cal.getTime();
            //                    dateStringTo = dateFormat.format(date);
            //                }
            float movement = sample.getIntensity();
            float value = movement;
            switch(type) {
                case ActivityKind.TYPE_DEEP_SLEEP:
                    value += SleepUtils.Y_VALUE_DEEP_SLEEP;
                    colors.add(akDeepSleep.color);
                    break;
                case ActivityKind.TYPE_LIGHT_SLEEP:
                    colors.add(akLightSleep.color);
                    break;
                case ActivityKind.TYPE_NOT_WORN:
                    //a small value, just to show something on the graphs
                    value = SleepUtils.Y_VALUE_DEEP_SLEEP;
                    colors.add(akNotWorn.color);
                    break;
                default:
                    //                        short steps = sample.getSteps();
                    //                        if (use_steps_as_movement && steps != 0) {
                    //                            // I'm not sure using steps for this is actually a good idea
                    //                            movement = steps;
                    //                        }
                    //                        value = ((float) movement) / movement_divisor;
                    colors.add(akActivity.color);
            }
            activityEntries.add(createBarEntry(value, ts));
            if (hr && isValidHeartRateValue(sample.getHeartRate())) {
                if (lastHrSampleIndex > -1 && ts - lastHrSampleIndex > 60 * HeartRateUtils.MAX_HR_MEASUREMENTS_GAP_MINUTES) {
                    heartrateEntries.add(createLineEntry(0, lastHrSampleIndex + 1));
                    heartrateEntries.add(createLineEntry(0, ts - 1));
                }
                heartrateEntries.add(createLineEntry(sample.getHeartRate(), ts));
                lastHrSampleIndex = ts;
            }
            String xLabel = "";
            if (annotate) {
            //                    cal.setTimeInMillis((ts + tsOffset) * 1000L);
            //                    date = cal.getTime();
            //                    String dateString = annotationDateFormat.format(date);
            //                    xLabel = dateString;
            //                    if (last_type != type) {
            //                        if (isSleep(last_type) && !isSleep(type)) {
            //                            // woken up
            //                            LimitLine line = new LimitLine(i, dateString);
            //                            line.enableDashedLine(8, 8, 0);
            //                            line.setTextColor(Color.WHITE);
            //                            line.setTextSize(15);
            //                            chart.getXAxis().addLimitLine(line);
            //                        } else if (!isSleep(last_type) && isSleep(type)) {
            //                            // fallen asleep
            //                            LimitLine line = new LimitLine(i, dateString);
            //                            line.enableDashedLine(8, 8, 0);
            //                            line.setTextSize(15);
            //                            line.setTextColor(Color.WHITE);
            //                            chart.getXAxis().addLimitLine(line);
            //                        }
            //                    }
            //                    last_type = type;
            }
        }
        BarDataSet activitySet = createActivitySet(activityEntries, colors, "Activity");
        // create a data object with the datasets
        //            combinedData = new CombinedData(xLabels);
        combinedData = new CombinedData();
        List<IBarDataSet> list = new ArrayList<>();
        list.add(activitySet);
        BarData barData = new BarData(list);
        barData.setBarWidth(100f);
        //            barData.setGroupSpace(0);
        combinedData.setData(barData);
        if (hr && heartrateEntries.size() > 0) {
            LineDataSet heartrateSet = createHeartrateSet(heartrateEntries, "Heart Rate");
            LineData lineData = new LineData(heartrateSet);
            combinedData.setData(lineData);
        }
    //            chart.setDescription(getString(R.string.sleep_activity_date_range, dateStringFrom, dateStringTo));
    //            chart.setDescriptionPosition(?, ?);
    } else {
        combinedData = new CombinedData();
    }
    IAxisValueFormatter xValueFormatter = new SampleXLabelFormatter(tsTranslation);
    return new DefaultChartsData(combinedData, xValueFormatter);
}
Also used : AbstractActivitySample(nodomain.freeyourgadget.gadgetbridge.entities.AbstractActivitySample) ActivitySample(nodomain.freeyourgadget.gadgetbridge.model.ActivitySample) BarDataSet(com.github.mikephil.charting.data.BarDataSet) IBarDataSet(com.github.mikephil.charting.interfaces.datasets.IBarDataSet) LineDataSet(com.github.mikephil.charting.data.LineDataSet) ArrayList(java.util.ArrayList) IAxisValueFormatter(com.github.mikephil.charting.formatter.IAxisValueFormatter) CombinedData(com.github.mikephil.charting.data.CombinedData) BarEntry(com.github.mikephil.charting.data.BarEntry) Entry(com.github.mikephil.charting.data.Entry) BarEntry(com.github.mikephil.charting.data.BarEntry) LineData(com.github.mikephil.charting.data.LineData) BarData(com.github.mikephil.charting.data.BarData) IBarDataSet(com.github.mikephil.charting.interfaces.datasets.IBarDataSet)

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