Search in sources :

Example 26 with BarData

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

the class ScrollViewActivity method setData.

private void setData(int count) {
    ArrayList<BarEntry> yVals = new ArrayList<BarEntry>();
    for (int i = 0; i < count; i++) {
        float val = (float) (Math.random() * count) + 15;
        yVals.add(new BarEntry(i, (int) val));
    }
    BarDataSet set = new BarDataSet(yVals, "Data Set");
    set.setColors(ColorTemplate.VORDIPLOM_COLORS);
    set.setDrawValues(false);
    BarData data = new BarData(set);
    mChart.setData(data);
    mChart.invalidate();
    mChart.animateY(800);
}
Also used : BarDataSet(com.github.mikephil.charting.data.BarDataSet) BarData(com.github.mikephil.charting.data.BarData) ArrayList(java.util.ArrayList) BarEntry(com.github.mikephil.charting.data.BarEntry)

Example 27 with BarData

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

the class StackedBarActivity method onProgressChanged.

@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
    tvX.setText("" + (mSeekBarX.getProgress() + 1));
    tvY.setText("" + (mSeekBarY.getProgress()));
    ArrayList<BarEntry> yVals1 = new ArrayList<BarEntry>();
    for (int i = 0; i < mSeekBarX.getProgress() + 1; i++) {
        float mult = (mSeekBarY.getProgress() + 1);
        float val1 = (float) (Math.random() * mult) + mult / 3;
        float val2 = (float) (Math.random() * mult) + mult / 3;
        float val3 = (float) (Math.random() * mult) + mult / 3;
        yVals1.add(new BarEntry(i, new float[] { val1, val2, val3 }, getResources().getDrawable(R.drawable.star)));
    }
    BarDataSet set1;
    if (mChart.getData() != null && mChart.getData().getDataSetCount() > 0) {
        set1 = (BarDataSet) mChart.getData().getDataSetByIndex(0);
        set1.setValues(yVals1);
        mChart.getData().notifyDataChanged();
        mChart.notifyDataSetChanged();
    } else {
        set1 = new BarDataSet(yVals1, "Statistics Vienna 2014");
        set1.setDrawIcons(false);
        set1.setColors(getColors());
        set1.setStackLabels(new String[] { "Births", "Divorces", "Marriages" });
        ArrayList<IBarDataSet> dataSets = new ArrayList<IBarDataSet>();
        dataSets.add(set1);
        BarData data = new BarData(dataSets);
        data.setValueFormatter(new MyValueFormatter());
        data.setValueTextColor(Color.WHITE);
        mChart.setData(data);
    }
    mChart.setFitBars(true);
    mChart.invalidate();
}
Also used : BarDataSet(com.github.mikephil.charting.data.BarDataSet) IBarDataSet(com.github.mikephil.charting.interfaces.datasets.IBarDataSet) BarData(com.github.mikephil.charting.data.BarData) IBarDataSet(com.github.mikephil.charting.interfaces.datasets.IBarDataSet) ArrayList(java.util.ArrayList) BarEntry(com.github.mikephil.charting.data.BarEntry) MyValueFormatter(com.xxmassdeveloper.mpchartexample.custom.MyValueFormatter)

Example 28 with BarData

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

the class BarChartRenderer method drawHighlighted.

@Override
public void drawHighlighted(Canvas c, Highlight[] indices) {
    BarData barData = mChart.getBarData();
    for (Highlight high : indices) {
        IBarDataSet set = barData.getDataSetByIndex(high.getDataSetIndex());
        if (set == null || !set.isHighlightEnabled())
            continue;
        BarEntry e = set.getEntryForXValue(high.getX(), high.getY());
        if (!isInBoundsX(e, set))
            continue;
        Transformer trans = mChart.getTransformer(set.getAxisDependency());
        mHighlightPaint.setColor(set.getHighLightColor());
        mHighlightPaint.setAlpha(set.getHighLightAlpha());
        boolean isStack = (high.getStackIndex() >= 0 && e.isStacked()) ? true : false;
        final float y1;
        final float y2;
        if (isStack) {
            if (mChart.isHighlightFullBarEnabled()) {
                y1 = e.getPositiveSum();
                y2 = -e.getNegativeSum();
            } else {
                Range range = e.getRanges()[high.getStackIndex()];
                y1 = range.from;
                y2 = range.to;
            }
        } else {
            y1 = e.getY();
            y2 = 0.f;
        }
        prepareBarHighlight(e.getX(), y1, y2, barData.getBarWidth() / 2f, trans);
        setHighlightDrawPos(high, mBarRect);
        c.drawRect(mBarRect, mHighlightPaint);
    }
}
Also used : Highlight(com.github.mikephil.charting.highlight.Highlight) Transformer(com.github.mikephil.charting.utils.Transformer) BarData(com.github.mikephil.charting.data.BarData) IBarDataSet(com.github.mikephil.charting.interfaces.datasets.IBarDataSet) Range(com.github.mikephil.charting.highlight.Range) BarEntry(com.github.mikephil.charting.data.BarEntry)

Example 29 with BarData

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

the class BarChartRenderer method drawDataSet.

protected void drawDataSet(Canvas c, IBarDataSet dataSet, int index) {
    Transformer trans = mChart.getTransformer(dataSet.getAxisDependency());
    mBarBorderPaint.setColor(dataSet.getBarBorderColor());
    mBarBorderPaint.setStrokeWidth(Utils.convertDpToPixel(dataSet.getBarBorderWidth()));
    final boolean drawBorder = dataSet.getBarBorderWidth() > 0.f;
    float phaseX = mAnimator.getPhaseX();
    float phaseY = mAnimator.getPhaseY();
    // draw the bar shadow before the values
    if (mChart.isDrawBarShadowEnabled()) {
        mShadowPaint.setColor(dataSet.getBarShadowColor());
        BarData barData = mChart.getBarData();
        final float barWidth = barData.getBarWidth();
        final float barWidthHalf = barWidth / 2.0f;
        float x;
        for (int i = 0, count = Math.min((int) (Math.ceil((float) (dataSet.getEntryCount()) * phaseX)), dataSet.getEntryCount()); i < count; i++) {
            BarEntry e = dataSet.getEntryForIndex(i);
            x = e.getX();
            mBarShadowRectBuffer.left = x - barWidthHalf;
            mBarShadowRectBuffer.right = x + barWidthHalf;
            trans.rectValueToPixel(mBarShadowRectBuffer);
            if (!mViewPortHandler.isInBoundsLeft(mBarShadowRectBuffer.right))
                continue;
            if (!mViewPortHandler.isInBoundsRight(mBarShadowRectBuffer.left))
                break;
            mBarShadowRectBuffer.top = mViewPortHandler.contentTop();
            mBarShadowRectBuffer.bottom = mViewPortHandler.contentBottom();
            c.drawRect(mBarShadowRectBuffer, mShadowPaint);
        }
    }
    // initialize the buffer
    BarBuffer buffer = mBarBuffers[index];
    buffer.setPhases(phaseX, phaseY);
    buffer.setDataSet(index);
    buffer.setInverted(mChart.isInverted(dataSet.getAxisDependency()));
    buffer.setBarWidth(mChart.getBarData().getBarWidth());
    buffer.feed(dataSet);
    trans.pointValuesToPixel(buffer.buffer);
    final boolean isSingleColor = dataSet.getColors().size() == 1;
    if (isSingleColor) {
        mRenderPaint.setColor(dataSet.getColor());
    }
    for (int j = 0; j < buffer.size(); j += 4) {
        if (!mViewPortHandler.isInBoundsLeft(buffer.buffer[j + 2]))
            continue;
        if (!mViewPortHandler.isInBoundsRight(buffer.buffer[j]))
            break;
        if (!isSingleColor) {
            // Set the color for the currently drawn value. If the index
            // is out of bounds, reuse colors.
            mRenderPaint.setColor(dataSet.getColor(j / 4));
        }
        c.drawRect(buffer.buffer[j], buffer.buffer[j + 1], buffer.buffer[j + 2], buffer.buffer[j + 3], mRenderPaint);
        if (drawBorder) {
            c.drawRect(buffer.buffer[j], buffer.buffer[j + 1], buffer.buffer[j + 2], buffer.buffer[j + 3], mBarBorderPaint);
        }
    }
}
Also used : Transformer(com.github.mikephil.charting.utils.Transformer) BarData(com.github.mikephil.charting.data.BarData) BarEntry(com.github.mikephil.charting.data.BarEntry) Paint(android.graphics.Paint) BarBuffer(com.github.mikephil.charting.buffer.BarBuffer)

Example 30 with BarData

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

the class HorizontalBarChartRenderer method initBuffers.

@Override
public void initBuffers() {
    BarData barData = mChart.getBarData();
    mBarBuffers = new HorizontalBarBuffer[barData.getDataSetCount()];
    for (int i = 0; i < mBarBuffers.length; i++) {
        IBarDataSet set = barData.getDataSetByIndex(i);
        mBarBuffers[i] = new HorizontalBarBuffer(set.getEntryCount() * 4 * (set.isStacked() ? set.getStackSize() : 1), barData.getDataSetCount(), set.isStacked());
    }
}
Also used : HorizontalBarBuffer(com.github.mikephil.charting.buffer.HorizontalBarBuffer) BarData(com.github.mikephil.charting.data.BarData) IBarDataSet(com.github.mikephil.charting.interfaces.datasets.IBarDataSet)

Aggregations

BarData (com.github.mikephil.charting.data.BarData)32 BarEntry (com.github.mikephil.charting.data.BarEntry)22 ArrayList (java.util.ArrayList)22 BarDataSet (com.github.mikephil.charting.data.BarDataSet)19 IBarDataSet (com.github.mikephil.charting.interfaces.datasets.IBarDataSet)19 IAxisValueFormatter (com.github.mikephil.charting.formatter.IAxisValueFormatter)4 Paint (android.graphics.Paint)3 BarBuffer (com.github.mikephil.charting.buffer.BarBuffer)3 RealmBarDataSet (com.github.mikephil.charting.data.realm.implementation.RealmBarDataSet)3 Transformer (com.github.mikephil.charting.utils.Transformer)3 SuppressLint (android.annotation.SuppressLint)2 HorizontalBarBuffer (com.github.mikephil.charting.buffer.HorizontalBarBuffer)2 AxisBase (com.github.mikephil.charting.components.AxisBase)2 LineData (com.github.mikephil.charting.data.LineData)2 MPPointD (com.github.mikephil.charting.utils.MPPointD)2 RealmDemoData (com.xxmassdeveloper.mpchartexample.custom.RealmDemoData)2 PointF (android.graphics.PointF)1 ListView (android.widget.ListView)1 BarChart (com.github.mikephil.charting.charts.BarChart)1 Legend (com.github.mikephil.charting.components.Legend)1