Search in sources :

Example 6 with IBarDataSet

use of com.github.mikephil.charting.interfaces.datasets.IBarDataSet in project MPAndroidChart by PhilJay.

the class BarChart method getBarBounds.

/**
     * The passed outputRect will be assigned the values of the bounding box of the specified Entry in the specified DataSet.
     * The rect will be assigned Float.MIN_VALUE in all locations if the Entry could not be found in the charts data.
     *
     * @param e
     * @return
     */
public void getBarBounds(BarEntry e, RectF outputRect) {
    RectF bounds = outputRect;
    IBarDataSet set = mData.getDataSetForEntry(e);
    if (set == null) {
        bounds.set(Float.MIN_VALUE, Float.MIN_VALUE, Float.MIN_VALUE, Float.MIN_VALUE);
        return;
    }
    float y = e.getY();
    float x = e.getX();
    float barWidth = mData.getBarWidth();
    float left = x - barWidth / 2f;
    float right = x + barWidth / 2f;
    float top = y >= 0 ? y : 0;
    float bottom = y <= 0 ? y : 0;
    bounds.set(left, top, right, bottom);
    getTransformer(set.getAxisDependency()).rectValueToPixel(outputRect);
}
Also used : RectF(android.graphics.RectF) IBarDataSet(com.github.mikephil.charting.interfaces.datasets.IBarDataSet)

Example 7 with IBarDataSet

use of com.github.mikephil.charting.interfaces.datasets.IBarDataSet in project MPAndroidChart by PhilJay.

the class SimpleFragment method generateBarData.

protected BarData generateBarData(int dataSets, float range, int count) {
    ArrayList<IBarDataSet> sets = new ArrayList<IBarDataSet>();
    for (int i = 0; i < dataSets; i++) {
        ArrayList<BarEntry> entries = new ArrayList<BarEntry>();
        for (int j = 0; j < count; j++) {
            entries.add(new BarEntry(j, (float) (Math.random() * range) + range / 4));
        }
        BarDataSet ds = new BarDataSet(entries, getLabel(i));
        ds.setColors(ColorTemplate.VORDIPLOM_COLORS);
        sets.add(ds);
    }
    BarData d = new BarData(sets);
    d.setValueTypeface(tf);
    return d;
}
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)

Example 8 with IBarDataSet

use of com.github.mikephil.charting.interfaces.datasets.IBarDataSet in project MPAndroidChart by PhilJay.

the class LegendRenderer method computeLegend.

/**
     * Prepares the legend and calculates all needed forms, labels and colors.
     *
     * @param data
     */
public void computeLegend(ChartData<?> data) {
    if (!mLegend.isLegendCustom()) {
        computedEntries.clear();
        // loop for building up the colors and labels used in the legend
        for (int i = 0; i < data.getDataSetCount(); i++) {
            IDataSet dataSet = data.getDataSetByIndex(i);
            List<Integer> clrs = dataSet.getColors();
            int entryCount = dataSet.getEntryCount();
            // if we have a barchart with stacked bars
            if (dataSet instanceof IBarDataSet && ((IBarDataSet) dataSet).isStacked()) {
                IBarDataSet bds = (IBarDataSet) dataSet;
                String[] sLabels = bds.getStackLabels();
                for (int j = 0; j < clrs.size() && j < bds.getStackSize(); j++) {
                    computedEntries.add(new LegendEntry(sLabels[j % sLabels.length], dataSet.getForm(), dataSet.getFormSize(), dataSet.getFormLineWidth(), dataSet.getFormLineDashEffect(), clrs.get(j)));
                }
                if (bds.getLabel() != null) {
                    // add the legend description label
                    computedEntries.add(new LegendEntry(dataSet.getLabel(), Legend.LegendForm.NONE, Float.NaN, Float.NaN, null, ColorTemplate.COLOR_NONE));
                }
            } else if (dataSet instanceof IPieDataSet) {
                IPieDataSet pds = (IPieDataSet) dataSet;
                for (int j = 0; j < clrs.size() && j < entryCount; j++) {
                    computedEntries.add(new LegendEntry(pds.getEntryForIndex(j).getLabel(), dataSet.getForm(), dataSet.getFormSize(), dataSet.getFormLineWidth(), dataSet.getFormLineDashEffect(), clrs.get(j)));
                }
                if (pds.getLabel() != null) {
                    // add the legend description label
                    computedEntries.add(new LegendEntry(dataSet.getLabel(), Legend.LegendForm.NONE, Float.NaN, Float.NaN, null, ColorTemplate.COLOR_NONE));
                }
            } else if (dataSet instanceof ICandleDataSet && ((ICandleDataSet) dataSet).getDecreasingColor() != ColorTemplate.COLOR_NONE) {
                int decreasingColor = ((ICandleDataSet) dataSet).getDecreasingColor();
                int increasingColor = ((ICandleDataSet) dataSet).getIncreasingColor();
                computedEntries.add(new LegendEntry(null, dataSet.getForm(), dataSet.getFormSize(), dataSet.getFormLineWidth(), dataSet.getFormLineDashEffect(), decreasingColor));
                computedEntries.add(new LegendEntry(dataSet.getLabel(), dataSet.getForm(), dataSet.getFormSize(), dataSet.getFormLineWidth(), dataSet.getFormLineDashEffect(), increasingColor));
            } else {
                for (int j = 0; j < clrs.size() && j < entryCount; j++) {
                    String label;
                    // if multiple colors are set for a DataSet, group them
                    if (j < clrs.size() - 1 && j < entryCount - 1) {
                        label = null;
                    } else {
                        // add label to the last entry
                        label = data.getDataSetByIndex(i).getLabel();
                    }
                    computedEntries.add(new LegendEntry(label, dataSet.getForm(), dataSet.getFormSize(), dataSet.getFormLineWidth(), dataSet.getFormLineDashEffect(), clrs.get(j)));
                }
            }
        }
        if (mLegend.getExtraEntries() != null) {
            Collections.addAll(computedEntries, mLegend.getExtraEntries());
        }
        mLegend.setEntries(computedEntries);
    }
    Typeface tf = mLegend.getTypeface();
    if (tf != null)
        mLegendLabelPaint.setTypeface(tf);
    mLegendLabelPaint.setTextSize(mLegend.getTextSize());
    mLegendLabelPaint.setColor(mLegend.getTextColor());
    // calculate all dimensions of the mLegend
    mLegend.calculateDimensions(mLegendLabelPaint, mViewPortHandler);
}
Also used : ICandleDataSet(com.github.mikephil.charting.interfaces.datasets.ICandleDataSet) LegendEntry(com.github.mikephil.charting.components.LegendEntry) IPieDataSet(com.github.mikephil.charting.interfaces.datasets.IPieDataSet) Typeface(android.graphics.Typeface) IBarDataSet(com.github.mikephil.charting.interfaces.datasets.IBarDataSet) IDataSet(com.github.mikephil.charting.interfaces.datasets.IDataSet) Paint(android.graphics.Paint)

Example 9 with IBarDataSet

use of com.github.mikephil.charting.interfaces.datasets.IBarDataSet in project MPAndroidChart by PhilJay.

the class BarChartRenderer method drawValues.

@Override
public void drawValues(Canvas c) {
    // if values are drawn
    if (isDrawingValuesAllowed(mChart)) {
        List<IBarDataSet> dataSets = mChart.getBarData().getDataSets();
        final float valueOffsetPlus = Utils.convertDpToPixel(4.5f);
        float posOffset = 0f;
        float negOffset = 0f;
        boolean drawValueAboveBar = mChart.isDrawValueAboveBarEnabled();
        for (int i = 0; i < mChart.getBarData().getDataSetCount(); i++) {
            IBarDataSet dataSet = dataSets.get(i);
            if (!shouldDrawValues(dataSet))
                continue;
            // apply the text-styling defined by the DataSet
            applyValueTextStyle(dataSet);
            boolean isInverted = mChart.isInverted(dataSet.getAxisDependency());
            // calculate the correct offset depending on the draw position of
            // the value
            float valueTextHeight = Utils.calcTextHeight(mValuePaint, "8");
            posOffset = (drawValueAboveBar ? -valueOffsetPlus : valueTextHeight + valueOffsetPlus);
            negOffset = (drawValueAboveBar ? valueTextHeight + valueOffsetPlus : -valueOffsetPlus);
            if (isInverted) {
                posOffset = -posOffset - valueTextHeight;
                negOffset = -negOffset - valueTextHeight;
            }
            // get the buffer
            BarBuffer buffer = mBarBuffers[i];
            final float phaseY = mAnimator.getPhaseY();
            MPPointF iconsOffset = MPPointF.getInstance(dataSet.getIconsOffset());
            iconsOffset.x = Utils.convertDpToPixel(iconsOffset.x);
            iconsOffset.y = Utils.convertDpToPixel(iconsOffset.y);
            // if only single values are drawn (sum)
            if (!dataSet.isStacked()) {
                for (int j = 0; j < buffer.buffer.length * mAnimator.getPhaseX(); j += 4) {
                    float x = (buffer.buffer[j] + buffer.buffer[j + 2]) / 2f;
                    if (!mViewPortHandler.isInBoundsRight(x))
                        break;
                    if (!mViewPortHandler.isInBoundsY(buffer.buffer[j + 1]) || !mViewPortHandler.isInBoundsLeft(x))
                        continue;
                    BarEntry entry = dataSet.getEntryForIndex(j / 4);
                    float val = entry.getY();
                    if (dataSet.isDrawValuesEnabled()) {
                        drawValue(c, dataSet.getValueFormatter(), val, entry, i, x, val >= 0 ? (buffer.buffer[j + 1] + posOffset) : (buffer.buffer[j + 3] + negOffset), dataSet.getValueTextColor(j / 4));
                    }
                    if (entry.getIcon() != null && dataSet.isDrawIconsEnabled()) {
                        Drawable icon = entry.getIcon();
                        float px = x;
                        float py = val >= 0 ? (buffer.buffer[j + 1] + posOffset) : (buffer.buffer[j + 3] + negOffset);
                        px += iconsOffset.x;
                        py += iconsOffset.y;
                        Utils.drawImage(c, icon, (int) px, (int) py, icon.getIntrinsicWidth(), icon.getIntrinsicHeight());
                    }
                }
            // if we have stacks
            } else {
                Transformer trans = mChart.getTransformer(dataSet.getAxisDependency());
                int bufferIndex = 0;
                int index = 0;
                while (index < dataSet.getEntryCount() * mAnimator.getPhaseX()) {
                    BarEntry entry = dataSet.getEntryForIndex(index);
                    float[] vals = entry.getYVals();
                    float x = (buffer.buffer[bufferIndex] + buffer.buffer[bufferIndex + 2]) / 2f;
                    int color = dataSet.getValueTextColor(index);
                    // in between
                    if (vals == null) {
                        if (!mViewPortHandler.isInBoundsRight(x))
                            break;
                        if (!mViewPortHandler.isInBoundsY(buffer.buffer[bufferIndex + 1]) || !mViewPortHandler.isInBoundsLeft(x))
                            continue;
                        if (dataSet.isDrawValuesEnabled()) {
                            drawValue(c, dataSet.getValueFormatter(), entry.getY(), entry, i, x, buffer.buffer[bufferIndex + 1] + (entry.getY() >= 0 ? posOffset : negOffset), color);
                        }
                        if (entry.getIcon() != null && dataSet.isDrawIconsEnabled()) {
                            Drawable icon = entry.getIcon();
                            float px = x;
                            float py = buffer.buffer[bufferIndex + 1] + (entry.getY() >= 0 ? posOffset : negOffset);
                            px += iconsOffset.x;
                            py += iconsOffset.y;
                            Utils.drawImage(c, icon, (int) px, (int) py, icon.getIntrinsicWidth(), icon.getIntrinsicHeight());
                        }
                    // draw stack values
                    } else {
                        float[] transformed = new float[vals.length * 2];
                        float posY = 0f;
                        float negY = -entry.getNegativeSum();
                        for (int k = 0, idx = 0; k < transformed.length; k += 2, idx++) {
                            float value = vals[idx];
                            float y;
                            if (value == 0.0f && (posY == 0.0f || negY == 0.0f)) {
                                // Take care of the situation of a 0.0 value, which overlaps a non-zero bar
                                y = value;
                            } else if (value >= 0.0f) {
                                posY += value;
                                y = posY;
                            } else {
                                y = negY;
                                negY -= value;
                            }
                            transformed[k + 1] = y * phaseY;
                        }
                        trans.pointValuesToPixel(transformed);
                        for (int k = 0; k < transformed.length; k += 2) {
                            final float val = vals[k / 2];
                            final boolean drawBelow = (val == 0.0f && negY == 0.0f && posY > 0.0f) || val < 0.0f;
                            float y = transformed[k + 1] + (drawBelow ? negOffset : posOffset);
                            if (!mViewPortHandler.isInBoundsRight(x))
                                break;
                            if (!mViewPortHandler.isInBoundsY(y) || !mViewPortHandler.isInBoundsLeft(x))
                                continue;
                            if (dataSet.isDrawValuesEnabled()) {
                                drawValue(c, dataSet.getValueFormatter(), vals[k / 2], entry, i, x, y, color);
                            }
                            if (entry.getIcon() != null && dataSet.isDrawIconsEnabled()) {
                                Drawable icon = entry.getIcon();
                                Utils.drawImage(c, icon, (int) (x + iconsOffset.x), (int) (y + iconsOffset.y), icon.getIntrinsicWidth(), icon.getIntrinsicHeight());
                            }
                        }
                    }
                    bufferIndex = vals == null ? bufferIndex + 4 : bufferIndex + 4 * vals.length;
                    index++;
                }
            }
            MPPointF.recycleInstance(iconsOffset);
        }
    }
}
Also used : Transformer(com.github.mikephil.charting.utils.Transformer) IBarDataSet(com.github.mikephil.charting.interfaces.datasets.IBarDataSet) MPPointF(com.github.mikephil.charting.utils.MPPointF) Drawable(android.graphics.drawable.Drawable) BarEntry(com.github.mikephil.charting.data.BarEntry) Paint(android.graphics.Paint) BarBuffer(com.github.mikephil.charting.buffer.BarBuffer)

Example 10 with IBarDataSet

use of com.github.mikephil.charting.interfaces.datasets.IBarDataSet in project MPAndroidChart by PhilJay.

the class BarChartRenderer method initBuffers.

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

Aggregations

IBarDataSet (com.github.mikephil.charting.interfaces.datasets.IBarDataSet)22 BarData (com.github.mikephil.charting.data.BarData)16 BarEntry (com.github.mikephil.charting.data.BarEntry)11 ArrayList (java.util.ArrayList)11 BarDataSet (com.github.mikephil.charting.data.BarDataSet)8 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 RectF (android.graphics.RectF)2 Drawable (android.graphics.drawable.Drawable)2 HorizontalBarBuffer (com.github.mikephil.charting.buffer.HorizontalBarBuffer)2 LineData (com.github.mikephil.charting.data.LineData)2 IAxisValueFormatter (com.github.mikephil.charting.formatter.IAxisValueFormatter)2 MPPointD (com.github.mikephil.charting.utils.MPPointD)2 MPPointF (com.github.mikephil.charting.utils.MPPointF)2 RealmDemoData (com.xxmassdeveloper.mpchartexample.custom.RealmDemoData)2 Typeface (android.graphics.Typeface)1 BarChart (com.github.mikephil.charting.charts.BarChart)1