Search in sources :

Example 31 with LineData

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

the class LineChartActivity1 method setData.

private void setData(int count, float range) {
    ArrayList<Entry> values = new ArrayList<Entry>();
    for (int i = 0; i < count; i++) {
        float val = (float) (Math.random() * range) + 3;
        values.add(new Entry(i, val, getResources().getDrawable(R.drawable.star)));
    }
    LineDataSet set1;
    if (mChart.getData() != null && mChart.getData().getDataSetCount() > 0) {
        set1 = (LineDataSet) mChart.getData().getDataSetByIndex(0);
        set1.setValues(values);
        mChart.getData().notifyDataChanged();
        mChart.notifyDataSetChanged();
    } else {
        // create a dataset and give it a type
        set1 = new LineDataSet(values, "DataSet 1");
        set1.setDrawIcons(false);
        // set the line to be drawn like this "- - - - - -"
        set1.enableDashedLine(10f, 5f, 0f);
        set1.enableDashedHighlightLine(10f, 5f, 0f);
        set1.setColor(Color.BLACK);
        set1.setCircleColor(Color.BLACK);
        set1.setLineWidth(1f);
        set1.setCircleRadius(3f);
        set1.setDrawCircleHole(false);
        set1.setValueTextSize(9f);
        set1.setDrawFilled(true);
        set1.setFormLineWidth(1f);
        set1.setFormLineDashEffect(new DashPathEffect(new float[] { 10f, 5f }, 0f));
        set1.setFormSize(15.f);
        if (Utils.getSDKInt() >= 18) {
            // fill drawable only supported on api level 18 and above
            Drawable drawable = ContextCompat.getDrawable(this, R.drawable.fade_red);
            set1.setFillDrawable(drawable);
        } else {
            set1.setFillColor(Color.BLACK);
        }
        ArrayList<ILineDataSet> dataSets = new ArrayList<ILineDataSet>();
        // add the datasets
        dataSets.add(set1);
        // create a data object with the datasets
        LineData data = new LineData(dataSets);
        // set data
        mChart.setData(data);
    }
}
Also used : Entry(com.github.mikephil.charting.data.Entry) 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) Drawable(android.graphics.drawable.Drawable) DashPathEffect(android.graphics.DashPathEffect)

Example 32 with LineData

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

the class LineChartActivity2 method setData.

private void setData(int count, float range) {
    ArrayList<Entry> yVals1 = new ArrayList<Entry>();
    for (int i = 0; i < count; i++) {
        float mult = range / 2f;
        float val = (float) (Math.random() * mult) + 50;
        yVals1.add(new Entry(i, val));
    }
    ArrayList<Entry> yVals2 = new ArrayList<Entry>();
    for (int i = 0; i < count - 1; i++) {
        float mult = range;
        float val = (float) (Math.random() * mult) + 450;
        yVals2.add(new Entry(i, val));
    //            if(i == 10) {
    //                yVals2.add(new Entry(i, val + 50));
    //            }
    }
    ArrayList<Entry> yVals3 = new ArrayList<Entry>();
    for (int i = 0; i < count; i++) {
        float mult = range;
        float val = (float) (Math.random() * mult) + 500;
        yVals3.add(new Entry(i, val));
    }
    LineDataSet set1, set2, set3;
    if (mChart.getData() != null && mChart.getData().getDataSetCount() > 0) {
        set1 = (LineDataSet) mChart.getData().getDataSetByIndex(0);
        set2 = (LineDataSet) mChart.getData().getDataSetByIndex(1);
        set3 = (LineDataSet) mChart.getData().getDataSetByIndex(2);
        set1.setValues(yVals1);
        set2.setValues(yVals2);
        set3.setValues(yVals3);
        mChart.getData().notifyDataChanged();
        mChart.notifyDataSetChanged();
    } else {
        // create a dataset and give it a type
        set1 = new LineDataSet(yVals1, "DataSet 1");
        set1.setAxisDependency(AxisDependency.LEFT);
        set1.setColor(ColorTemplate.getHoloBlue());
        set1.setCircleColor(Color.WHITE);
        set1.setLineWidth(2f);
        set1.setCircleRadius(3f);
        set1.setFillAlpha(65);
        set1.setFillColor(ColorTemplate.getHoloBlue());
        set1.setHighLightColor(Color.rgb(244, 117, 117));
        set1.setDrawCircleHole(false);
        //set1.setFillFormatter(new MyFillFormatter(0f));
        //set1.setDrawHorizontalHighlightIndicator(false);
        //set1.setVisible(false);
        //set1.setCircleHoleColor(Color.WHITE);
        // create a dataset and give it a type
        set2 = new LineDataSet(yVals2, "DataSet 2");
        set2.setAxisDependency(AxisDependency.RIGHT);
        set2.setColor(Color.RED);
        set2.setCircleColor(Color.WHITE);
        set2.setLineWidth(2f);
        set2.setCircleRadius(3f);
        set2.setFillAlpha(65);
        set2.setFillColor(Color.RED);
        set2.setDrawCircleHole(false);
        set2.setHighLightColor(Color.rgb(244, 117, 117));
        //set2.setFillFormatter(new MyFillFormatter(900f));
        set3 = new LineDataSet(yVals3, "DataSet 3");
        set3.setAxisDependency(AxisDependency.RIGHT);
        set3.setColor(Color.YELLOW);
        set3.setCircleColor(Color.WHITE);
        set3.setLineWidth(2f);
        set3.setCircleRadius(3f);
        set3.setFillAlpha(65);
        set3.setFillColor(ColorTemplate.colorWithAlpha(Color.YELLOW, 200));
        set3.setDrawCircleHole(false);
        set3.setHighLightColor(Color.rgb(244, 117, 117));
        // create a data object with the datasets
        LineData data = new LineData(set1, set2, set3);
        data.setValueTextColor(Color.WHITE);
        data.setValueTextSize(9f);
        // set data
        mChart.setData(data);
    }
}
Also used : Entry(com.github.mikephil.charting.data.Entry) LineData(com.github.mikephil.charting.data.LineData) ILineDataSet(com.github.mikephil.charting.interfaces.datasets.ILineDataSet) LineDataSet(com.github.mikephil.charting.data.LineDataSet) ArrayList(java.util.ArrayList)

Example 33 with LineData

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

the class LineChartRenderer method drawHighlighted.

@Override
public void drawHighlighted(Canvas c, Highlight[] indices) {
    LineData lineData = mChart.getLineData();
    for (Highlight high : indices) {
        ILineDataSet set = lineData.getDataSetByIndex(high.getDataSetIndex());
        if (set == null || !set.isHighlightEnabled())
            continue;
        Entry e = set.getEntryForXValue(high.getX(), high.getY());
        if (!isInBoundsX(e, set))
            continue;
        MPPointD pix = mChart.getTransformer(set.getAxisDependency()).getPixelForValues(e.getX(), e.getY() * mAnimator.getPhaseY());
        high.setDraw((float) pix.x, (float) pix.y);
        // draw the lines
        drawHighlightLines(c, (float) pix.x, (float) pix.y, set);
    }
}
Also used : LineData(com.github.mikephil.charting.data.LineData) Entry(com.github.mikephil.charting.data.Entry) Highlight(com.github.mikephil.charting.highlight.Highlight) ILineDataSet(com.github.mikephil.charting.interfaces.datasets.ILineDataSet) MPPointD(com.github.mikephil.charting.utils.MPPointD)

Example 34 with LineData

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

the class LineChartRenderer method drawData.

@Override
public void drawData(Canvas c) {
    int width = (int) mViewPortHandler.getChartWidth();
    int height = (int) mViewPortHandler.getChartHeight();
    if (mDrawBitmap == null || (mDrawBitmap.get().getWidth() != width) || (mDrawBitmap.get().getHeight() != height)) {
        if (width > 0 && height > 0) {
            mDrawBitmap = new WeakReference<Bitmap>(Bitmap.createBitmap(width, height, mBitmapConfig));
            mBitmapCanvas = new Canvas(mDrawBitmap.get());
        } else
            return;
    }
    mDrawBitmap.get().eraseColor(Color.TRANSPARENT);
    LineData lineData = mChart.getLineData();
    for (ILineDataSet set : lineData.getDataSets()) {
        if (set.isVisible())
            drawDataSet(c, set);
    }
    c.drawBitmap(mDrawBitmap.get(), 0, 0, mRenderPaint);
}
Also used : Bitmap(android.graphics.Bitmap) LineData(com.github.mikephil.charting.data.LineData) ILineDataSet(com.github.mikephil.charting.interfaces.datasets.ILineDataSet) Canvas(android.graphics.Canvas) Paint(android.graphics.Paint)

Example 35 with LineData

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

the class GraphsFragment method loadSnowChart.

public void loadSnowChart() {
    snowChart.setDrawGridBackground(false);
    snowChart.setBackgroundColor(Color.BLACK);
    snowChart.setTouchEnabled(true);
    snowChart.setDragEnabled(true);
    snowChart.setMaxHighlightDistance(300);
    snowChart.setPinchZoom(true);
    snowChart.setPadding(2, 2, 2, 2);
    snowChart.getLegend().setEnabled(true);
    snowChart.getLegend().setTextColor(Color.WHITE);
    YAxis yAxisRight = snowChart.getAxisRight();
    yAxisRight.setDrawGridLines(false);
    yAxisRight.setDrawAxisLine(false);
    yAxisRight.setDrawLabels(false);
    yAxisRight.setTextColor(Color.WHITE);
    yAxisRight.enableAxisLineDashedLine(2f, 4f, 2f);
    YAxis yAxisLeft = snowChart.getAxisLeft();
    yAxisLeft.setTextColor(Color.WHITE);
    XAxis x = snowChart.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 (snowChart.getData() != null) {
        snowChart.getData().removeDataSet(snowChart.getData().getDataSetByIndex(snowChart.getData().getDataSetCount() - 1));
        snowChart.getLegend().setTextColor(Color.parseColor("#FFFFFF"));
    }
    set = new LineDataSet(snowEntries, getString(R.string.g_snow));
    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.YELLOW);
    set.setHighlightEnabled(false);
    set.setValueFormatter(mValueFormatter);
    LineData data = new LineData(set);
    snowChart.setData(data);
    snowChart.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)38 LineDataSet (com.github.mikephil.charting.data.LineDataSet)23 Entry (com.github.mikephil.charting.data.Entry)20 ILineDataSet (com.github.mikephil.charting.interfaces.datasets.ILineDataSet)20 ArrayList (java.util.ArrayList)20 XAxis (com.github.mikephil.charting.components.XAxis)6 YAxis (com.github.mikephil.charting.components.YAxis)6 XFormatter (com.a5corp.weather.utils.XFormatter)5 Legend (com.github.mikephil.charting.components.Legend)3 BarEntry (com.github.mikephil.charting.data.BarEntry)3 Paint (android.graphics.Paint)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