Search in sources :

Example 11 with BarEntry

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

the class HorizontalBarChartRenderer 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(5f);
        float posOffset = 0f;
        float negOffset = 0f;
        final boolean drawValueAboveBar = mChart.isDrawValueAboveBarEnabled();
        for (int i = 0; i < mChart.getBarData().getDataSetCount(); i++) {
            IBarDataSet dataSet = dataSets.get(i);
            if (!shouldDrawValues(dataSet))
                continue;
            boolean isInverted = mChart.isInverted(dataSet.getAxisDependency());
            // apply the text-styling defined by the DataSet
            applyValueTextStyle(dataSet);
            final float halfTextHeight = Utils.calcTextHeight(mValuePaint, "10") / 2f;
            IValueFormatter formatter = dataSet.getValueFormatter();
            // 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 y = (buffer.buffer[j + 1] + buffer.buffer[j + 3]) / 2f;
                    if (!mViewPortHandler.isInBoundsTop(buffer.buffer[j + 1]))
                        break;
                    if (!mViewPortHandler.isInBoundsX(buffer.buffer[j]))
                        continue;
                    if (!mViewPortHandler.isInBoundsBottom(buffer.buffer[j + 1]))
                        continue;
                    BarEntry entry = dataSet.getEntryForIndex(j / 4);
                    float val = entry.getY();
                    String formattedValue = formatter.getFormattedValue(val, entry, i, mViewPortHandler);
                    // calculate the correct offset depending on the draw position of the value
                    float valueTextWidth = Utils.calcTextWidth(mValuePaint, formattedValue);
                    posOffset = (drawValueAboveBar ? valueOffsetPlus : -(valueTextWidth + valueOffsetPlus));
                    negOffset = (drawValueAboveBar ? -(valueTextWidth + valueOffsetPlus) : valueOffsetPlus);
                    if (isInverted) {
                        posOffset = -posOffset - valueTextWidth;
                        negOffset = -negOffset - valueTextWidth;
                    }
                    if (dataSet.isDrawValuesEnabled()) {
                        drawValue(c, formattedValue, buffer.buffer[j + 2] + (val >= 0 ? posOffset : negOffset), y + halfTextHeight, dataSet.getValueTextColor(j / 2));
                    }
                    if (entry.getIcon() != null && dataSet.isDrawIconsEnabled()) {
                        Drawable icon = entry.getIcon();
                        float px = buffer.buffer[j + 2] + (val >= 0 ? posOffset : negOffset);
                        float py = y;
                        px += iconsOffset.x;
                        py += iconsOffset.y;
                        Utils.drawImage(c, icon, (int) px, (int) py, icon.getIntrinsicWidth(), icon.getIntrinsicHeight());
                    }
                }
            // if each value of a potential stack should be drawn
            } else {
                Transformer trans = mChart.getTransformer(dataSet.getAxisDependency());
                int bufferIndex = 0;
                int index = 0;
                while (index < dataSet.getEntryCount() * mAnimator.getPhaseX()) {
                    BarEntry entry = dataSet.getEntryForIndex(index);
                    int color = dataSet.getValueTextColor(index);
                    float[] vals = entry.getYVals();
                    // in between
                    if (vals == null) {
                        if (!mViewPortHandler.isInBoundsTop(buffer.buffer[bufferIndex + 1]))
                            break;
                        if (!mViewPortHandler.isInBoundsX(buffer.buffer[bufferIndex]))
                            continue;
                        if (!mViewPortHandler.isInBoundsBottom(buffer.buffer[bufferIndex + 1]))
                            continue;
                        float val = entry.getY();
                        String formattedValue = formatter.getFormattedValue(val, entry, i, mViewPortHandler);
                        // calculate the correct offset depending on the draw position of the value
                        float valueTextWidth = Utils.calcTextWidth(mValuePaint, formattedValue);
                        posOffset = (drawValueAboveBar ? valueOffsetPlus : -(valueTextWidth + valueOffsetPlus));
                        negOffset = (drawValueAboveBar ? -(valueTextWidth + valueOffsetPlus) : valueOffsetPlus);
                        if (isInverted) {
                            posOffset = -posOffset - valueTextWidth;
                            negOffset = -negOffset - valueTextWidth;
                        }
                        if (dataSet.isDrawValuesEnabled()) {
                            drawValue(c, formattedValue, buffer.buffer[bufferIndex + 2] + (entry.getY() >= 0 ? posOffset : negOffset), buffer.buffer[bufferIndex + 1] + halfTextHeight, color);
                        }
                        if (entry.getIcon() != null && dataSet.isDrawIconsEnabled()) {
                            Drawable icon = entry.getIcon();
                            float px = buffer.buffer[bufferIndex + 2] + (entry.getY() >= 0 ? posOffset : negOffset);
                            float py = buffer.buffer[bufferIndex + 1];
                            px += iconsOffset.x;
                            py += iconsOffset.y;
                            Utils.drawImage(c, icon, (int) px, (int) py, icon.getIntrinsicWidth(), icon.getIntrinsicHeight());
                        }
                    } 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] = y * phaseY;
                        }
                        trans.pointValuesToPixel(transformed);
                        for (int k = 0; k < transformed.length; k += 2) {
                            final float val = vals[k / 2];
                            String formattedValue = formatter.getFormattedValue(val, entry, i, mViewPortHandler);
                            // calculate the correct offset depending on the draw position of the value
                            float valueTextWidth = Utils.calcTextWidth(mValuePaint, formattedValue);
                            posOffset = (drawValueAboveBar ? valueOffsetPlus : -(valueTextWidth + valueOffsetPlus));
                            negOffset = (drawValueAboveBar ? -(valueTextWidth + valueOffsetPlus) : valueOffsetPlus);
                            if (isInverted) {
                                posOffset = -posOffset - valueTextWidth;
                                negOffset = -negOffset - valueTextWidth;
                            }
                            final boolean drawBelow = (val == 0.0f && negY == 0.0f && posY > 0.0f) || val < 0.0f;
                            float x = transformed[k] + (drawBelow ? negOffset : posOffset);
                            float y = (buffer.buffer[bufferIndex + 1] + buffer.buffer[bufferIndex + 3]) / 2f;
                            if (!mViewPortHandler.isInBoundsTop(y))
                                break;
                            if (!mViewPortHandler.isInBoundsX(x))
                                continue;
                            if (!mViewPortHandler.isInBoundsBottom(y))
                                continue;
                            if (dataSet.isDrawValuesEnabled()) {
                                drawValue(c, formattedValue, x, y + halfTextHeight, 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) MPPointF(com.github.mikephil.charting.utils.MPPointF) Drawable(android.graphics.drawable.Drawable) BarEntry(com.github.mikephil.charting.data.BarEntry) BarBuffer(com.github.mikephil.charting.buffer.BarBuffer) HorizontalBarBuffer(com.github.mikephil.charting.buffer.HorizontalBarBuffer) IValueFormatter(com.github.mikephil.charting.formatter.IValueFormatter) IBarDataSet(com.github.mikephil.charting.interfaces.datasets.IBarDataSet)

Example 12 with BarEntry

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

the class StackedValueFormatter method getFormattedValue.

@Override
public String getFormattedValue(float value, Entry entry, int dataSetIndex, ViewPortHandler viewPortHandler) {
    if (!mDrawWholeStack && entry instanceof BarEntry) {
        BarEntry barEntry = (BarEntry) entry;
        float[] vals = barEntry.getYVals();
        if (vals != null) {
            // find out if we are on top of the stack
            if (vals[vals.length - 1] == value) {
                // return the "sum" across all stack values
                return mFormat.format(barEntry.getY()) + mAppendix;
            } else {
                // return empty
                return "";
            }
        }
    }
    // return the "proposed" value
    return mFormat.format(value) + mAppendix;
}
Also used : BarEntry(com.github.mikephil.charting.data.BarEntry)

Example 13 with BarEntry

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

the class BarHighlighter method getStackedHighlight.

/**
     * This method creates the Highlight object that also indicates which value of a stacked BarEntry has been
     * selected.
     *
     * @param high the Highlight to work with looking for stacked values
     * @param set
     * @param xVal
     * @param yVal
     * @return
     */
public Highlight getStackedHighlight(Highlight high, IBarDataSet set, float xVal, float yVal) {
    BarEntry entry = set.getEntryForXValue(xVal, yVal);
    if (entry == null)
        return null;
    // not stacked
    if (entry.getYVals() == null) {
        return high;
    } else {
        Range[] ranges = entry.getRanges();
        if (ranges.length > 0) {
            int stackIndex = getClosestStackIndex(ranges, yVal);
            MPPointD pixels = mChart.getTransformer(set.getAxisDependency()).getPixelForValues(high.getX(), ranges[stackIndex].to);
            Highlight stackedHigh = new Highlight(entry.getX(), entry.getY(), (float) pixels.x, (float) pixels.y, high.getDataSetIndex(), stackIndex, high.getAxis());
            MPPointD.recycleInstance(pixels);
            return stackedHigh;
        }
    }
    return null;
}
Also used : MPPointD(com.github.mikephil.charting.utils.MPPointD) BarEntry(com.github.mikephil.charting.data.BarEntry)

Example 14 with BarEntry

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

the class FileUtils method loadEntriesFromFile.

/**
     * Loads a an Array of Entries from a textfile from the sd-card.
     * 
     * @param path the name of the file on the sd-card (+ path if needed)
     * @return
     */
public static List<Entry> loadEntriesFromFile(String path) {
    File sdcard = Environment.getExternalStorageDirectory();
    // Get the text file
    File file = new File(sdcard, path);
    List<Entry> entries = new ArrayList<Entry>();
    try {
        @SuppressWarnings("resource") BufferedReader br = new BufferedReader(new FileReader(file));
        String line;
        while ((line = br.readLine()) != null) {
            String[] split = line.split("#");
            if (split.length <= 2) {
                entries.add(new Entry(Float.parseFloat(split[0]), Integer.parseInt(split[1])));
            } 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));
            }
        }
    } catch (IOException e) {
        Log.e(LOG, e.toString());
    }
    return entries;
// File sdcard = Environment.getExternalStorageDirectory();
//
// // Get the text file
// File file = new File(sdcard, path);
//
// List<Entry> entries = new ArrayList<Entry>();
// String label = "";
//
// try {
// @SuppressWarnings("resource")
// BufferedReader br = new BufferedReader(new FileReader(file));
// String line = br.readLine();
//
// // firstline is the label
// label = line;
//
// while ((line = br.readLine()) != null) {
// String[] split = line.split("#");
// entries.add(new Entry(Float.parseFloat(split[0]),
// Integer.parseInt(split[1])));
// }
// } catch (IOException e) {
// Log.e(LOG, e.toString());
// }
//
// DataSet ds = new DataSet(entries, label);
// return ds;
}
Also used : ArrayList(java.util.ArrayList) IOException(java.io.IOException) BarEntry(com.github.mikephil.charting.data.BarEntry) Entry(com.github.mikephil.charting.data.Entry) BarEntry(com.github.mikephil.charting.data.BarEntry) BufferedReader(java.io.BufferedReader) FileReader(java.io.FileReader) File(java.io.File)

Example 15 with BarEntry

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

the class BarDataTest method testGroupBars.

@Test
public void testGroupBars() {
    float groupSpace = 5f;
    float barSpace = 1f;
    List<BarEntry> values1 = new ArrayList<>();
    List<BarEntry> values2 = new ArrayList<>();
    for (int i = 0; i < 5; i++) {
        values1.add(new BarEntry(i, 50));
        values2.add(new BarEntry(i, 60));
    }
    BarDataSet barDataSet1 = new BarDataSet(values1, "Set1");
    BarDataSet barDataSet2 = new BarDataSet(values2, "Set2");
    BarData data = new BarData(barDataSet1, barDataSet2);
    data.setBarWidth(10f);
    float groupWidth = data.getGroupWidth(groupSpace, barSpace);
    assertEquals(27f, groupWidth, 0.01f);
    assertEquals(0f, values1.get(0).getX(), 0.01f);
    assertEquals(1f, values1.get(1).getX(), 0.01f);
    data.groupBars(1000, groupSpace, barSpace);
    // 1000 + 2.5 + 0.5 + 5
    assertEquals(1008f, values1.get(0).getX(), 0.01f);
    assertEquals(1019f, values2.get(0).getX(), 0.01f);
    assertEquals(1035f, values1.get(1).getX(), 0.01f);
    assertEquals(1046f, values2.get(1).getX(), 0.01f);
    data.groupBars(-1000, groupSpace, barSpace);
    assertEquals(-992f, values1.get(0).getX(), 0.01f);
    assertEquals(-981f, values2.get(0).getX(), 0.01f);
    assertEquals(-965f, values1.get(1).getX(), 0.01f);
    assertEquals(-954f, values2.get(1).getX(), 0.01f);
    data.setBarWidth(20f);
    groupWidth = data.getGroupWidth(groupSpace, barSpace);
    assertEquals(47f, groupWidth, 0.01f);
    data.setBarWidth(10f);
    data.groupBars(-20, groupSpace, barSpace);
    assertEquals(-12f, values1.get(0).getX(), 0.01f);
    assertEquals(-1f, values2.get(0).getX(), 0.01f);
    assertEquals(15f, values1.get(1).getX(), 0.01f);
    assertEquals(26f, values2.get(1).getX(), 0.01f);
}
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) Test(org.junit.Test)

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