Search in sources :

Example 26 with BarEntry

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

the class BarBuffer method feed.

@Override
public void feed(IBarDataSet data) {
    float size = data.getEntryCount() * phaseX;
    float barWidthHalf = mBarWidth / 2f;
    for (int i = 0; i < size; i++) {
        BarEntry e = data.getEntryForIndex(i);
        if (e == null)
            continue;
        float x = e.getX();
        float y = e.getY();
        float[] vals = e.getYVals();
        if (!mContainsStacks || vals == null) {
            float left = x - barWidthHalf;
            float right = x + barWidthHalf;
            float bottom, top;
            if (mInverted) {
                bottom = y >= 0 ? y : 0;
                top = y <= 0 ? y : 0;
            } else {
                top = y >= 0 ? y : 0;
                bottom = y <= 0 ? y : 0;
            }
            // multiply the height of the rect with the phase
            if (top > 0)
                top *= phaseY;
            else
                bottom *= phaseY;
            addBar(left, top, right, bottom);
        } else {
            float posY = 0f;
            float negY = -e.getNegativeSum();
            float yStart = 0f;
            // fill the stack
            for (int k = 0; k < vals.length; k++) {
                float value = vals[k];
                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;
                    yStart = y;
                } else if (value >= 0.0f) {
                    y = posY;
                    yStart = posY + value;
                    posY = yStart;
                } else {
                    y = negY;
                    yStart = negY + Math.abs(value);
                    negY += Math.abs(value);
                }
                float left = x - barWidthHalf;
                float right = x + barWidthHalf;
                float bottom, top;
                if (mInverted) {
                    bottom = y >= yStart ? y : yStart;
                    top = y <= yStart ? y : yStart;
                } else {
                    top = y >= yStart ? y : yStart;
                    bottom = y <= yStart ? y : yStart;
                }
                // multiply the height of the rect with the phase
                top *= phaseY;
                bottom *= phaseY;
                addBar(left, top, right, bottom);
            }
        }
    }
    reset();
}
Also used : BarEntry(com.github.mikephil.charting.data.BarEntry)

Example 27 with BarEntry

use of com.github.mikephil.charting.data.BarEntry 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 28 with BarEntry

use of com.github.mikephil.charting.data.BarEntry 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 29 with BarEntry

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

the class FileUtils method loadEntriesFromAssets.

/**
     * Loads an array of Entries from a textfile from the assets folder.
     * 
     * @param am
     * @param path the name of the file in the assets folder (+ path if needed)
     * @return
     */
public static List<Entry> loadEntriesFromAssets(AssetManager am, String path) {
    List<Entry> entries = new ArrayList<Entry>();
    BufferedReader reader = null;
    try {
        reader = new BufferedReader(new InputStreamReader(am.open(path), "UTF-8"));
        String line = reader.readLine();
        while (line != null) {
            // process line
            String[] split = line.split("#");
            if (split.length <= 2) {
                entries.add(new Entry(Float.parseFloat(split[1]), Float.parseFloat(split[0])));
            } else {
                float[] vals = new float[split.length - 1];
                for (int i = 0; i < vals.length; i++) {
                    vals[i] = Float.parseFloat(split[i]);
                }
                entries.add(new BarEntry(Integer.parseInt(split[split.length - 1]), vals));
            }
            line = reader.readLine();
        }
    } catch (IOException e) {
        Log.e(LOG, e.toString());
    } finally {
        if (reader != null) {
            try {
                reader.close();
            } catch (IOException e) {
                Log.e(LOG, e.toString());
            }
        }
    }
    return entries;
// String label = null;
// List<Entry> entries = new ArrayList<Entry>();
//
// BufferedReader reader = null;
// try {
// reader = new BufferedReader(
// new InputStreamReader(am.open(path), "UTF-8"));
//
// // do reading, usually loop until end of file reading
// label = reader.readLine();
// String line = reader.readLine();
//
// while (line != null) {
// // process line
// String[] split = line.split("#");
// entries.add(new Entry(Float.parseFloat(split[0]),
// Integer.parseInt(split[1])));
// line = reader.readLine();
// }
// } catch (IOException e) {
// Log.e(LOG, e.toString());
//
// } finally {
//
// if (reader != null) {
// try {
// reader.close();
// } catch (IOException e) {
// Log.e(LOG, e.toString());
// }
// }
// }
//
// DataSet ds = new DataSet(entries, label);
// return ds;
}
Also used : Entry(com.github.mikephil.charting.data.Entry) BarEntry(com.github.mikephil.charting.data.BarEntry) InputStreamReader(java.io.InputStreamReader) ArrayList(java.util.ArrayList) BufferedReader(java.io.BufferedReader) IOException(java.io.IOException) BarEntry(com.github.mikephil.charting.data.BarEntry)

Example 30 with BarEntry

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

the class FileUtils method loadBarEntriesFromAssets.

public static List<BarEntry> loadBarEntriesFromAssets(AssetManager am, String path) {
    List<BarEntry> entries = new ArrayList<BarEntry>();
    BufferedReader reader = null;
    try {
        reader = new BufferedReader(new InputStreamReader(am.open(path), "UTF-8"));
        String line = reader.readLine();
        while (line != null) {
            // process line
            String[] split = line.split("#");
            entries.add(new BarEntry(Float.parseFloat(split[1]), Float.parseFloat(split[0])));
            line = reader.readLine();
        }
    } catch (IOException e) {
        Log.e(LOG, e.toString());
    } finally {
        if (reader != null) {
            try {
                reader.close();
            } catch (IOException e) {
                Log.e(LOG, e.toString());
            }
        }
    }
    return entries;
// String label = null;
// ArrayList<Entry> entries = new ArrayList<Entry>();
//
// BufferedReader reader = null;
// try {
// reader = new BufferedReader(
// new InputStreamReader(am.open(path), "UTF-8"));
//
// // do reading, usually loop until end of file reading
// label = reader.readLine();
// String line = reader.readLine();
//
// while (line != null) {
// // process line
// String[] split = line.split("#");
// entries.add(new Entry(Float.parseFloat(split[0]),
// Integer.parseInt(split[1])));
// line = reader.readLine();
// }
// } catch (IOException e) {
// Log.e(LOG, e.toString());
//
// } finally {
//
// if (reader != null) {
// try {
// reader.close();
// } catch (IOException e) {
// Log.e(LOG, e.toString());
// }
// }
// }
//
// DataSet ds = new DataSet(entries, label);
// return ds;
}
Also used : InputStreamReader(java.io.InputStreamReader) ArrayList(java.util.ArrayList) BufferedReader(java.io.BufferedReader) IOException(java.io.IOException) BarEntry(com.github.mikephil.charting.data.BarEntry)

Aggregations

BarEntry (com.github.mikephil.charting.data.BarEntry)36 ArrayList (java.util.ArrayList)23 BarData (com.github.mikephil.charting.data.BarData)22 BarDataSet (com.github.mikephil.charting.data.BarDataSet)20 IBarDataSet (com.github.mikephil.charting.interfaces.datasets.IBarDataSet)14 Entry (com.github.mikephil.charting.data.Entry)5 Transformer (com.github.mikephil.charting.utils.Transformer)5 BufferedReader (java.io.BufferedReader)5 IOException (java.io.IOException)5 Paint (android.graphics.Paint)4 BarBuffer (com.github.mikephil.charting.buffer.BarBuffer)4 IAxisValueFormatter (com.github.mikephil.charting.formatter.IAxisValueFormatter)3 InputStreamReader (java.io.InputStreamReader)3 SuppressLint (android.annotation.SuppressLint)2 Drawable (android.graphics.drawable.Drawable)2 HorizontalBarBuffer (com.github.mikephil.charting.buffer.HorizontalBarBuffer)2 IValueFormatter (com.github.mikephil.charting.formatter.IValueFormatter)2 MPPointF (com.github.mikephil.charting.utils.MPPointF)2 File (java.io.File)2 FileReader (java.io.FileReader)2