Search in sources :

Example 6 with BarEntry

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

the class StackedBarActivityNegative method onCreate.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
    setContentView(R.layout.activity_age_distribution);
    setTitle("Age Distribution Austria");
    mChart = (HorizontalBarChart) findViewById(R.id.chart1);
    mChart.setOnChartValueSelectedListener(this);
    mChart.setDrawGridBackground(false);
    mChart.getDescription().setEnabled(false);
    // scaling can now only be done on x- and y-axis separately
    mChart.setPinchZoom(false);
    mChart.setDrawBarShadow(false);
    mChart.setDrawValueAboveBar(true);
    mChart.setHighlightFullBarEnabled(false);
    mChart.getAxisLeft().setEnabled(false);
    mChart.getAxisRight().setAxisMaximum(25f);
    mChart.getAxisRight().setAxisMinimum(-25f);
    mChart.getAxisRight().setDrawGridLines(false);
    mChart.getAxisRight().setDrawZeroLine(true);
    mChart.getAxisRight().setLabelCount(7, false);
    mChart.getAxisRight().setValueFormatter(new CustomFormatter());
    mChart.getAxisRight().setTextSize(9f);
    XAxis xAxis = mChart.getXAxis();
    xAxis.setPosition(XAxisPosition.BOTH_SIDED);
    xAxis.setDrawGridLines(false);
    xAxis.setDrawAxisLine(false);
    xAxis.setTextSize(9f);
    xAxis.setAxisMinimum(0f);
    xAxis.setAxisMaximum(110f);
    xAxis.setCenterAxisLabels(true);
    xAxis.setLabelCount(12);
    xAxis.setGranularity(10f);
    xAxis.setValueFormatter(new IAxisValueFormatter() {

        private DecimalFormat format = new DecimalFormat("###");

        @Override
        public String getFormattedValue(float value, AxisBase axis) {
            return format.format(value) + "-" + format.format(value + 10);
        }
    });
    Legend l = mChart.getLegend();
    l.setVerticalAlignment(Legend.LegendVerticalAlignment.BOTTOM);
    l.setHorizontalAlignment(Legend.LegendHorizontalAlignment.RIGHT);
    l.setOrientation(Legend.LegendOrientation.HORIZONTAL);
    l.setDrawInside(false);
    l.setFormSize(8f);
    l.setFormToTextSpace(4f);
    l.setXEntrySpace(6f);
    // IMPORTANT: When using negative values in stacked bars, always make sure the negative values are in the array first
    ArrayList<BarEntry> yValues = new ArrayList<BarEntry>();
    yValues.add(new BarEntry(5, new float[] { -10, 10 }));
    yValues.add(new BarEntry(15, new float[] { -12, 13 }));
    yValues.add(new BarEntry(25, new float[] { -15, 15 }));
    yValues.add(new BarEntry(35, new float[] { -17, 17 }));
    yValues.add(new BarEntry(45, new float[] { -19, 20 }));
    yValues.add(new BarEntry(45, new float[] { -19, 20 }, getResources().getDrawable(R.drawable.star)));
    yValues.add(new BarEntry(55, new float[] { -19, 19 }));
    yValues.add(new BarEntry(65, new float[] { -16, 16 }));
    yValues.add(new BarEntry(75, new float[] { -13, 14 }));
    yValues.add(new BarEntry(85, new float[] { -10, 11 }));
    yValues.add(new BarEntry(95, new float[] { -5, 6 }));
    yValues.add(new BarEntry(105, new float[] { -1, 2 }));
    BarDataSet set = new BarDataSet(yValues, "Age Distribution");
    set.setDrawIcons(false);
    set.setValueFormatter(new CustomFormatter());
    set.setValueTextSize(7f);
    set.setAxisDependency(YAxis.AxisDependency.RIGHT);
    set.setColors(new int[] { Color.rgb(67, 67, 72), Color.rgb(124, 181, 236) });
    set.setStackLabels(new String[] { "Men", "Women" });
    String[] xLabels = new String[] { "0-10", "10-20", "20-30", "30-40", "40-50", "50-60", "60-70", "70-80", "80-90", "90-100", "100+" };
    BarData data = new BarData(set);
    data.setBarWidth(8.5f);
    mChart.setData(data);
    mChart.invalidate();
}
Also used : Legend(com.github.mikephil.charting.components.Legend) BarDataSet(com.github.mikephil.charting.data.BarDataSet) IBarDataSet(com.github.mikephil.charting.interfaces.datasets.IBarDataSet) DecimalFormat(java.text.DecimalFormat) ArrayList(java.util.ArrayList) IAxisValueFormatter(com.github.mikephil.charting.formatter.IAxisValueFormatter) AxisBase(com.github.mikephil.charting.components.AxisBase) BarEntry(com.github.mikephil.charting.data.BarEntry) XAxis(com.github.mikephil.charting.components.XAxis) BarData(com.github.mikephil.charting.data.BarData)

Example 7 with BarEntry

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

the class HorizontalBarBuffer 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 bottom = x - barWidthHalf;
            float top = x + barWidthHalf;
            float left, right;
            if (mInverted) {
                left = y >= 0 ? y : 0;
                right = y <= 0 ? y : 0;
            } else {
                right = y >= 0 ? y : 0;
                left = y <= 0 ? y : 0;
            }
            // multiply the height of the rect with the phase
            if (right > 0)
                right *= phaseY;
            else
                left *= 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 >= 0f) {
                    y = posY;
                    yStart = posY + value;
                    posY = yStart;
                } else {
                    y = negY;
                    yStart = negY + Math.abs(value);
                    negY += Math.abs(value);
                }
                float bottom = x - barWidthHalf;
                float top = x + barWidthHalf;
                float left, right;
                if (mInverted) {
                    left = y >= yStart ? y : yStart;
                    right = y <= yStart ? y : yStart;
                } else {
                    right = y >= yStart ? y : yStart;
                    left = y <= yStart ? y : yStart;
                }
                // multiply the height of the rect with the phase
                right *= phaseY;
                left *= phaseY;
                addBar(left, top, right, bottom);
            }
        }
    }
    reset();
}
Also used : BarEntry(com.github.mikephil.charting.data.BarEntry)

Example 8 with BarEntry

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

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

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

the class HorizontalBarChartRenderer method drawDataSet.

@Override
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.top = x - barWidthHalf;
            mBarShadowRectBuffer.bottom = x + barWidthHalf;
            trans.rectValueToPixel(mBarShadowRectBuffer);
            if (!mViewPortHandler.isInBoundsTop(mBarShadowRectBuffer.bottom))
                continue;
            if (!mViewPortHandler.isInBoundsBottom(mBarShadowRectBuffer.top))
                break;
            mBarShadowRectBuffer.left = mViewPortHandler.contentLeft();
            mBarShadowRectBuffer.right = mViewPortHandler.contentRight();
            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.isInBoundsTop(buffer.buffer[j + 3]))
            break;
        if (!mViewPortHandler.isInBoundsBottom(buffer.buffer[j + 1]))
            continue;
        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) BarBuffer(com.github.mikephil.charting.buffer.BarBuffer) HorizontalBarBuffer(com.github.mikephil.charting.buffer.HorizontalBarBuffer)

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