Search in sources :

Example 11 with LineData

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

the class RealmWikiExample method setData.

private void setData() {
    // LINE-CHART
    final RealmResults<Score> results = mRealm.where(Score.class).findAll();
    IAxisValueFormatter formatter = new IAxisValueFormatter() {

        @Override
        public String getFormattedValue(float value, AxisBase axis) {
            return results.get((int) value).getPlayerName();
        }
    };
    lineChart.getXAxis().setValueFormatter(formatter);
    barChart.getXAxis().setValueFormatter(formatter);
    RealmLineDataSet<Score> lineDataSet = new RealmLineDataSet<Score>(results, "scoreNr", "totalScore");
    lineDataSet.setMode(LineDataSet.Mode.CUBIC_BEZIER);
    lineDataSet.setLabel("Result Scores");
    lineDataSet.setDrawCircleHole(false);
    lineDataSet.setColor(ColorTemplate.rgb("#FF5722"));
    lineDataSet.setCircleColor(ColorTemplate.rgb("#FF5722"));
    lineDataSet.setLineWidth(1.8f);
    lineDataSet.setCircleRadius(3.6f);
    ArrayList<ILineDataSet> dataSets = new ArrayList<ILineDataSet>();
    dataSets.add(lineDataSet);
    LineData lineData = new LineData(dataSets);
    styleData(lineData);
    // set data
    lineChart.setData(lineData);
    lineChart.animateY(1400, Easing.EasingOption.EaseInOutQuart);
    // BAR-CHART
    RealmBarDataSet<Score> barDataSet = new RealmBarDataSet<Score>(results, "scoreNr", "totalScore");
    barDataSet.setColors(new int[] { ColorTemplate.rgb("#FF5722"), ColorTemplate.rgb("#03A9F4") });
    barDataSet.setLabel("Realm BarDataSet");
    ArrayList<IBarDataSet> barDataSets = new ArrayList<IBarDataSet>();
    barDataSets.add(barDataSet);
    BarData barData = new BarData(barDataSets);
    styleData(barData);
    barChart.setData(barData);
    barChart.setFitBars(true);
    barChart.animateY(1400, Easing.EasingOption.EaseInOutQuart);
}
Also used : ArrayList(java.util.ArrayList) IAxisValueFormatter(com.github.mikephil.charting.formatter.IAxisValueFormatter) AxisBase(com.github.mikephil.charting.components.AxisBase) RealmLineDataSet(com.github.mikephil.charting.data.realm.implementation.RealmLineDataSet) LineData(com.github.mikephil.charting.data.LineData) ILineDataSet(com.github.mikephil.charting.interfaces.datasets.ILineDataSet) RealmBarDataSet(com.github.mikephil.charting.data.realm.implementation.RealmBarDataSet) BarData(com.github.mikephil.charting.data.BarData) IBarDataSet(com.github.mikephil.charting.interfaces.datasets.IBarDataSet)

Example 12 with LineData

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

the class SimpleFragment method getComplexity.

protected LineData getComplexity() {
    ArrayList<ILineDataSet> sets = new ArrayList<ILineDataSet>();
    LineDataSet ds1 = new LineDataSet(FileUtils.loadEntriesFromAssets(getActivity().getAssets(), "n.txt"), "O(n)");
    LineDataSet ds2 = new LineDataSet(FileUtils.loadEntriesFromAssets(getActivity().getAssets(), "nlogn.txt"), "O(nlogn)");
    LineDataSet ds3 = new LineDataSet(FileUtils.loadEntriesFromAssets(getActivity().getAssets(), "square.txt"), "O(n²)");
    LineDataSet ds4 = new LineDataSet(FileUtils.loadEntriesFromAssets(getActivity().getAssets(), "three.txt"), "O(n³)");
    ds1.setColor(ColorTemplate.VORDIPLOM_COLORS[0]);
    ds2.setColor(ColorTemplate.VORDIPLOM_COLORS[1]);
    ds3.setColor(ColorTemplate.VORDIPLOM_COLORS[2]);
    ds4.setColor(ColorTemplate.VORDIPLOM_COLORS[3]);
    ds1.setCircleColor(ColorTemplate.VORDIPLOM_COLORS[0]);
    ds2.setCircleColor(ColorTemplate.VORDIPLOM_COLORS[1]);
    ds3.setCircleColor(ColorTemplate.VORDIPLOM_COLORS[2]);
    ds4.setCircleColor(ColorTemplate.VORDIPLOM_COLORS[3]);
    ds1.setLineWidth(2.5f);
    ds1.setCircleRadius(3f);
    ds2.setLineWidth(2.5f);
    ds2.setCircleRadius(3f);
    ds3.setLineWidth(2.5f);
    ds3.setCircleRadius(3f);
    ds4.setLineWidth(2.5f);
    ds4.setCircleRadius(3f);
    // load DataSets from textfiles in assets folders
    sets.add(ds1);
    sets.add(ds2);
    sets.add(ds3);
    sets.add(ds4);
    LineData d = new LineData(sets);
    d.setValueTypeface(tf);
    return d;
}
Also used : LineData(com.github.mikephil.charting.data.LineData) ILineDataSet(com.github.mikephil.charting.interfaces.datasets.ILineDataSet) ILineDataSet(com.github.mikephil.charting.interfaces.datasets.ILineDataSet) LineDataSet(com.github.mikephil.charting.data.LineDataSet) ArrayList(java.util.ArrayList)

Example 13 with LineData

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

the class DefaultFillFormatter method getFillLinePosition.

@Override
public float getFillLinePosition(ILineDataSet dataSet, LineDataProvider dataProvider) {
    float fillMin = 0f;
    float chartMaxY = dataProvider.getYChartMax();
    float chartMinY = dataProvider.getYChartMin();
    LineData data = dataProvider.getLineData();
    if (dataSet.getYMax() > 0 && dataSet.getYMin() < 0) {
        fillMin = 0f;
    } else {
        float max, min;
        if (data.getYMax() > 0)
            max = 0f;
        else
            max = chartMaxY;
        if (data.getYMin() < 0)
            min = 0f;
        else
            min = chartMinY;
        fillMin = dataSet.getYMin() >= 0 ? min : max;
    }
    return fillMin;
}
Also used : LineData(com.github.mikephil.charting.data.LineData)

Example 14 with LineData

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

the class ChartDataTest method testDynamicChartData.

@Test
public void testDynamicChartData() {
    List<Entry> entries1 = new ArrayList<Entry>();
    entries1.add(new Entry(10, 10));
    entries1.add(new Entry(15, -2));
    entries1.add(new Entry(21, 50));
    ScatterDataSet set1 = new ScatterDataSet(entries1, "");
    List<Entry> entries2 = new ArrayList<Entry>();
    entries2.add(new Entry(-1, 10));
    entries2.add(new Entry(10, 2));
    entries2.add(new Entry(20, 5));
    ScatterDataSet set2 = new ScatterDataSet(entries2, "");
    ScatterData data = new ScatterData(set1, set2);
    assertEquals(-2, data.getYMin(YAxis.AxisDependency.LEFT), 0.01f);
    assertEquals(50f, data.getYMax(YAxis.AxisDependency.LEFT), 0.01f);
    assertEquals(6, data.getEntryCount());
    assertEquals(-1f, data.getXMin(), 0.01f);
    assertEquals(21f, data.getXMax(), 0.01f);
    assertEquals(-2f, data.getYMin(), 0.01f);
    assertEquals(50f, data.getYMax(), 0.01f);
    assertEquals(3, data.getMaxEntryCountSet().getEntryCount());
    // now add and remove values
    data.addEntry(new Entry(-10, -10), 0);
    assertEquals(set1, data.getMaxEntryCountSet());
    assertEquals(4, data.getMaxEntryCountSet().getEntryCount());
    assertEquals(-10f, data.getYMin(YAxis.AxisDependency.LEFT), 0.01f);
    assertEquals(50f, data.getYMax(YAxis.AxisDependency.LEFT), 0.01f);
    assertEquals(-10f, data.getXMin(), 0.01f);
    assertEquals(21f, data.getXMax(), 0.01f);
    assertEquals(-10f, data.getYMin(), 0.01f);
    assertEquals(50f, data.getYMax(), 0.01f);
    data.addEntry(new Entry(-100, 100), 0);
    data.addEntry(new Entry(0, -100), 0);
    assertEquals(-100f, data.getYMin(YAxis.AxisDependency.LEFT), 0.01f);
    assertEquals(100f, data.getYMax(YAxis.AxisDependency.LEFT), 0.01f);
    // right axis will adapt left axis values if no right axis values are present
    assertEquals(-100, data.getYMin(YAxis.AxisDependency.RIGHT), 0.01f);
    assertEquals(100f, data.getYMax(YAxis.AxisDependency.RIGHT), 0.01f);
    List<Entry> entries3 = new ArrayList<Entry>();
    entries3.add(new Entry(0, 200));
    entries3.add(new Entry(0, -50));
    ScatterDataSet set3 = new ScatterDataSet(entries3, "");
    set3.setAxisDependency(YAxis.AxisDependency.RIGHT);
    data.addDataSet(set3);
    assertEquals(3, data.getDataSetCount());
    assertEquals(-100f, data.getYMin(YAxis.AxisDependency.LEFT), 0.01f);
    assertEquals(100f, data.getYMax(YAxis.AxisDependency.LEFT), 0.01f);
    assertEquals(-50f, data.getYMin(YAxis.AxisDependency.RIGHT), 0.01f);
    assertEquals(200f, data.getYMax(YAxis.AxisDependency.RIGHT), 0.01f);
    LineData lineData = new LineData();
    assertEquals(Float.MAX_VALUE, lineData.getYMin(), 0.01f);
    assertEquals(-Float.MAX_VALUE, lineData.getYMax(), 0.01f);
    assertEquals(Float.MAX_VALUE, lineData.getYMin(YAxis.AxisDependency.LEFT), 0.01f);
    assertEquals(-Float.MAX_VALUE, lineData.getYMax(YAxis.AxisDependency.LEFT), 0.01f);
    assertEquals(Float.MAX_VALUE, lineData.getYMin(YAxis.AxisDependency.RIGHT), 0.01f);
    assertEquals(-Float.MAX_VALUE, lineData.getYMax(YAxis.AxisDependency.RIGHT), 0.01f);
    assertEquals(0, lineData.getDataSetCount());
    List<Entry> lineEntries1 = new ArrayList<Entry>();
    lineEntries1.add(new Entry(10, 90));
    lineEntries1.add(new Entry(1000, 1000));
    LineDataSet lineSet1 = new LineDataSet(lineEntries1, "");
    lineData.addDataSet(lineSet1);
    assertEquals(1, lineData.getDataSetCount());
    assertEquals(2, lineSet1.getEntryCount());
    assertEquals(2, lineData.getEntryCount());
    assertEquals(10, lineData.getXMin(), 0.01f);
    assertEquals(1000f, lineData.getXMax(), 0.01f);
    assertEquals(90, lineData.getYMin(), 0.01f);
    assertEquals(1000, lineData.getYMax(), 0.01f);
    assertEquals(90, lineData.getYMin(YAxis.AxisDependency.LEFT), 0.01f);
    assertEquals(1000f, lineData.getYMax(YAxis.AxisDependency.LEFT), 0.01f);
    assertEquals(90, lineData.getYMin(YAxis.AxisDependency.RIGHT), 0.01f);
    assertEquals(1000, lineData.getYMax(YAxis.AxisDependency.RIGHT), 0.01f);
    List<Entry> lineEntries2 = new ArrayList<Entry>();
    lineEntries2.add(new Entry(-1000, 2000));
    lineEntries2.add(new Entry(2000, -3000));
    Entry e = new Entry(-1000, 2500);
    lineEntries2.add(e);
    LineDataSet lineSet2 = new LineDataSet(lineEntries2, "");
    lineSet2.setAxisDependency(YAxis.AxisDependency.RIGHT);
    lineData.addDataSet(lineSet2);
    assertEquals(2, lineData.getDataSetCount());
    assertEquals(3, lineSet2.getEntryCount());
    assertEquals(5, lineData.getEntryCount());
    assertEquals(-1000, lineData.getXMin(), 0.01f);
    assertEquals(2000, lineData.getXMax(), 0.01f);
    assertEquals(-3000, lineData.getYMin(), 0.01f);
    assertEquals(2500, lineData.getYMax(), 0.01f);
    assertEquals(90, lineData.getYMin(YAxis.AxisDependency.LEFT), 0.01f);
    assertEquals(1000f, lineData.getYMax(YAxis.AxisDependency.LEFT), 0.01f);
    assertEquals(-3000, lineData.getYMin(YAxis.AxisDependency.RIGHT), 0.01f);
    assertEquals(2500, lineData.getYMax(YAxis.AxisDependency.RIGHT), 0.01f);
    assertTrue(lineData.removeEntry(e, 1));
    assertEquals(-1000, lineData.getXMin(), 0.01f);
    assertEquals(2000, lineData.getXMax(), 0.01f);
    assertEquals(-3000, lineData.getYMin(), 0.01f);
    assertEquals(2000, lineData.getYMax(), 0.01f);
    assertEquals(90, lineData.getYMin(YAxis.AxisDependency.LEFT), 0.01f);
    assertEquals(1000f, lineData.getYMax(YAxis.AxisDependency.LEFT), 0.01f);
    assertEquals(-3000, lineData.getYMin(YAxis.AxisDependency.RIGHT), 0.01f);
    assertEquals(2000, lineData.getYMax(YAxis.AxisDependency.RIGHT), 0.01f);
    assertEquals(2, lineData.getDataSetCount());
    assertTrue(lineData.removeDataSet(lineSet2));
    assertEquals(1, lineData.getDataSetCount());
    assertEquals(10, lineData.getXMin(), 0.01f);
    assertEquals(1000, lineData.getXMax(), 0.01f);
    assertEquals(90, lineData.getYMin(), 0.01f);
    assertEquals(1000, lineData.getYMax(), 0.01f);
    assertEquals(90, lineData.getYMin(YAxis.AxisDependency.LEFT), 0.01f);
    assertEquals(1000f, lineData.getYMax(YAxis.AxisDependency.LEFT), 0.01f);
    assertEquals(90, lineData.getYMin(YAxis.AxisDependency.RIGHT), 0.01f);
    assertEquals(1000, lineData.getYMax(YAxis.AxisDependency.RIGHT), 0.01f);
    assertTrue(lineData.removeDataSet(lineSet1));
    assertEquals(0, lineData.getDataSetCount());
    assertEquals(Float.MAX_VALUE, lineData.getXMin(), 0.01f);
    assertEquals(-Float.MAX_VALUE, lineData.getXMax(), 0.01f);
    assertEquals(Float.MAX_VALUE, lineData.getYMin(), 0.01f);
    assertEquals(-Float.MAX_VALUE, lineData.getYMax(), 0.01f);
    assertEquals(Float.MAX_VALUE, lineData.getYMin(YAxis.AxisDependency.LEFT), 0.01f);
    assertEquals(-Float.MAX_VALUE, lineData.getYMax(YAxis.AxisDependency.LEFT), 0.01f);
    assertEquals(Float.MAX_VALUE, lineData.getYMin(YAxis.AxisDependency.RIGHT), 0.01f);
    assertEquals(-Float.MAX_VALUE, lineData.getYMax(YAxis.AxisDependency.RIGHT), 0.01f);
    assertFalse(lineData.removeDataSet(lineSet1));
    assertFalse(lineData.removeDataSet(lineSet2));
}
Also used : Entry(com.github.mikephil.charting.data.Entry) LineData(com.github.mikephil.charting.data.LineData) LineDataSet(com.github.mikephil.charting.data.LineDataSet) ScatterDataSet(com.github.mikephil.charting.data.ScatterDataSet) ArrayList(java.util.ArrayList) ScatterData(com.github.mikephil.charting.data.ScatterData) Test(org.junit.Test)

Example 15 with LineData

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

the class GraphsFragment method loadRainChart.

public void loadRainChart() {
    rainChart.setDrawGridBackground(false);
    rainChart.setBackgroundColor(Color.BLACK);
    rainChart.setTouchEnabled(true);
    rainChart.setDragEnabled(true);
    rainChart.setMaxHighlightDistance(300);
    rainChart.setPinchZoom(true);
    rainChart.setPadding(2, 2, 2, 2);
    rainChart.getLegend().setEnabled(true);
    rainChart.getLegend().setTextColor(Color.WHITE);
    YAxis yAxisRight = rainChart.getAxisRight();
    yAxisRight.setDrawGridLines(false);
    yAxisRight.setDrawAxisLine(false);
    yAxisRight.setDrawLabels(false);
    yAxisRight.setTextColor(Color.WHITE);
    yAxisRight.enableAxisLineDashedLine(2f, 4f, 2f);
    YAxis yAxisLeft = rainChart.getAxisLeft();
    yAxisLeft.setTextColor(Color.WHITE);
    XAxis x = rainChart.getXAxis();
    x.setEnabled(true);
    x.setPosition(XAxis.XAxisPosition.BOTTOM);
    x.setDrawGridLines(false);
    x.setTextColor(Color.parseColor("#FFFFFF"));
    x.setValueFormatter(new XFormatter(dates));
    LineDataSet set;
    if (rainChart.getData() != null) {
        rainChart.getData().removeDataSet(rainChart.getData().getDataSetByIndex(rainChart.getData().getDataSetCount() - 1));
        rainChart.getLegend().setTextColor(Color.parseColor("#FFFFFF"));
    }
    set = new LineDataSet(rainEntries, getString(R.string.g_rain));
    set.setMode(LineDataSet.Mode.HORIZONTAL_BEZIER);
    set.setCubicIntensity(0.2f);
    set.setDrawCircles(false);
    set.setLineWidth(2f);
    set.setDrawValues(false);
    set.setValueTextSize(10f);
    set.setColor(Color.GREEN);
    set.setHighlightEnabled(false);
    set.setValueFormatter(mValueFormatter);
    LineData data = new LineData(set);
    rainChart.setData(data);
    rainChart.invalidate();
}
Also used : LineData(com.github.mikephil.charting.data.LineData) LineDataSet(com.github.mikephil.charting.data.LineDataSet) XFormatter(com.a5corp.weather.utils.XFormatter) XAxis(com.github.mikephil.charting.components.XAxis) YAxis(com.github.mikephil.charting.components.YAxis)

Aggregations

LineData (com.github.mikephil.charting.data.LineData)37 LineDataSet (com.github.mikephil.charting.data.LineDataSet)22 Entry (com.github.mikephil.charting.data.Entry)19 ILineDataSet (com.github.mikephil.charting.interfaces.datasets.ILineDataSet)19 ArrayList (java.util.ArrayList)19 XAxis (com.github.mikephil.charting.components.XAxis)6 YAxis (com.github.mikephil.charting.components.YAxis)6 XFormatter (com.a5corp.weather.utils.XFormatter)5 BarEntry (com.github.mikephil.charting.data.BarEntry)3 Legend (com.github.mikephil.charting.components.Legend)2 BarData (com.github.mikephil.charting.data.BarData)2 RealmLineDataSet (com.github.mikephil.charting.data.realm.implementation.RealmLineDataSet)2 IAxisValueFormatter (com.github.mikephil.charting.formatter.IAxisValueFormatter)2 IFillFormatter (com.github.mikephil.charting.formatter.IFillFormatter)2 LineDataProvider (com.github.mikephil.charting.interfaces.dataprovider.LineDataProvider)2 IBarDataSet (com.github.mikephil.charting.interfaces.datasets.IBarDataSet)2 Bitmap (android.graphics.Bitmap)1 Canvas (android.graphics.Canvas)1 DashPathEffect (android.graphics.DashPathEffect)1 Paint (android.graphics.Paint)1