Search in sources :

Example 36 with BarDataSet

use of com.github.mikephil.charting.data.BarDataSet in project carat by amplab.

the class BarChart method getBarBounds.

/**
 * Returns the bounding box of the specified Entry in the specified DataSet.
 * Returns null if the Entry could not be found in the charts data.
 *
 * @param e
 * @param dataSetIndex
 * @return
 */
public RectF getBarBounds(BarEntry e) {
    BarDataSet set = mData.getDataSetForEntry(e);
    if (set == null)
        return null;
    float barspace = set.getBarSpace();
    float y = e.getVal();
    float x = e.getXIndex();
    float spaceHalf = barspace / 2f;
    float left = x + spaceHalf;
    float right = x + 1f - spaceHalf;
    float top = y >= 0 ? y : 0;
    float bottom = y <= 0 ? y : 0;
    RectF bounds = new RectF(left, top, right, bottom);
    mTrans.rectValueToPixel(bounds);
    return bounds;
}
Also used : RectF(android.graphics.RectF) BarDataSet(com.github.mikephil.charting.data.BarDataSet)

Example 37 with BarDataSet

use of com.github.mikephil.charting.data.BarDataSet in project carat by amplab.

the class BarChart method drawHighlights.

@Override
protected void drawHighlights() {
    int setCount = mData.getDataSetCount();
    for (int i = 0; i < mIndicesToHightlight.length; i++) {
        Highlight h = mIndicesToHightlight[i];
        int index = h.getXIndex();
        int dataSetIndex = h.getDataSetIndex();
        BarDataSet set = (BarDataSet) mData.getDataSetByIndex(dataSetIndex);
        if (set == null)
            continue;
        mHighlightPaint.setColor(set.getHighLightColor());
        mHighlightPaint.setAlpha(set.getHighLightAlpha());
        // check outofbounds
        if (index < mData.getYValCount() && index >= 0 && index < (mDeltaX * mPhaseX) / mData.getDataSetCount()) {
            Entry e = getEntryByDataSetIndex(index, dataSetIndex);
            if (e == null)
                continue;
            // calculate the correct x-position
            float x = index * setCount + dataSetIndex + mData.getGroupSpace() / 2f + mData.getGroupSpace() * index;
            float y = e.getVal();
            prepareBar(x, y, set.getBarSpace());
            mDrawCanvas.drawRect(mBarRect, mHighlightPaint);
            if (mDrawHighlightArrow) {
                mHighlightPaint.setAlpha(255);
                // distance between highlight arrow and bar
                float offsetY = mDeltaY * 0.07f;
                Path arrow = new Path();
                arrow.moveTo(x + 0.5f, y + offsetY * 0.3f);
                arrow.lineTo(x + 0.2f, y + offsetY);
                arrow.lineTo(x + 0.8f, y + offsetY);
                mTrans.pathValueToPixel(arrow);
                mDrawCanvas.drawPath(arrow, mHighlightPaint);
            }
        }
    }
}
Also used : Path(android.graphics.Path) Entry(com.github.mikephil.charting.data.Entry) BarEntry(com.github.mikephil.charting.data.BarEntry) Highlight(com.github.mikephil.charting.utils.Highlight) BarDataSet(com.github.mikephil.charting.data.BarDataSet) Paint(android.graphics.Paint)

Example 38 with BarDataSet

use of com.github.mikephil.charting.data.BarDataSet in project carat by amplab.

the class BarLineChartBase method getPosition.

/**
 * Returns the position (in pixels) the provided Entry has inside the chart
 * view or null, if the provided Entry is null.
 *
 * @param e
 * @return
 */
public PointF getPosition(Entry e) {
    if (e == null)
        return null;
    float[] vals = new float[] { e.getXIndex(), e.getVal() };
    if (this instanceof BarChart) {
        BarDataSet set = (BarDataSet) mData.getDataSetForEntry(e);
        if (set != null)
            vals[0] += set.getBarSpace() / 2f;
    }
    mTrans.pointValuesToPixel(vals);
    return new PointF(vals[0], vals[1]);
}
Also used : BarDataSet(com.github.mikephil.charting.data.BarDataSet) PointF(android.graphics.PointF)

Example 39 with BarDataSet

use of com.github.mikephil.charting.data.BarDataSet in project carat by amplab.

the class Chart method prepareLegend.

/**
 * Generates an automatically prepared legend depending on the DataSets in
 * the chart and their colors.
 */
public void prepareLegend() {
    ArrayList<String> labels = new ArrayList<String>();
    ArrayList<Integer> colors = new ArrayList<Integer>();
    // loop for building up the colors and labels used in the legend
    for (int i = 0; i < mData.getDataSetCount(); i++) {
        DataSet<? extends Entry> dataSet = mData.getDataSetByIndex(i);
        ArrayList<Integer> clrs = dataSet.getColors();
        int entryCount = dataSet.getEntryCount();
        // if we have a barchart with stacked bars
        if (dataSet instanceof BarDataSet && ((BarDataSet) dataSet).getStackSize() > 1) {
            BarDataSet bds = (BarDataSet) dataSet;
            String[] sLabels = bds.getStackLabels();
            for (int j = 0; j < clrs.size() && j < entryCount && j < bds.getStackSize(); j++) {
                labels.add(sLabels[j % sLabels.length]);
                colors.add(clrs.get(j));
            }
            // add the legend description label
            colors.add(-2);
            labels.add(bds.getLabel());
        } else if (dataSet instanceof PieDataSet) {
            ArrayList<String> xVals = mData.getXVals();
            PieDataSet pds = (PieDataSet) dataSet;
            for (int j = 0; j < clrs.size() && j < entryCount && j < xVals.size(); j++) {
                labels.add(xVals.get(j));
                colors.add(clrs.get(j));
            }
            // add the legend description label
            colors.add(-2);
            labels.add(pds.getLabel());
        } else {
            for (int j = 0; j < clrs.size() && j < entryCount; j++) {
                // if multiple colors are set for a DataSet, group them
                if (j < clrs.size() - 1 && j < entryCount - 1) {
                    labels.add(null);
                } else {
                    // add label to the last entry
                    String label = mData.getDataSetByIndex(i).getLabel();
                    labels.add(label);
                }
                colors.add(clrs.get(j));
            }
        }
    }
    Legend l = new Legend(colors, labels);
    if (mLegend != null) {
        // apply the old legend settings to a potential new legend
        l.apply(mLegend);
    }
    mLegend = l;
}
Also used : Legend(com.github.mikephil.charting.utils.Legend) BarDataSet(com.github.mikephil.charting.data.BarDataSet) PieDataSet(com.github.mikephil.charting.data.PieDataSet) ArrayList(java.util.ArrayList) Paint(android.graphics.Paint)

Example 40 with BarDataSet

use of com.github.mikephil.charting.data.BarDataSet in project anitrend-app by AniTrend.

the class MediaStatsFragment method showScoreDistribution.

private void showScoreDistribution() {
    if (model.getStats() != null && model.getStats().getScoreDistribution() != null) {
        configureScoreDistribution(model.getStats().getScoreDistribution());
        BarDataSet barDataSet = new BarDataSet(getPresenter().getMediaScoreDistribution(model.getStats().getScoreDistribution()), getString(R.string.title_score_distribution));
        barDataSet.setColor(CompatUtil.getColorFromAttr(getContext(), R.attr.colorAccent), 253);
        barDataSet.setValueTextColor(CompatUtil.getColorFromAttr(getContext(), R.attr.titleColor));
        BarData barData = new BarData(barDataSet);
        barData.setBarWidth(0.9f);
        binding.seriesScoreDist.setData(barData);
        binding.seriesScoreDist.setFitBars(true);
        binding.seriesScoreDist.invalidate();
    }
}
Also used : BarDataSet(com.github.mikephil.charting.data.BarDataSet) BarData(com.github.mikephil.charting.data.BarData)

Aggregations

BarDataSet (com.github.mikephil.charting.data.BarDataSet)41 ArrayList (java.util.ArrayList)27 BarEntry (com.github.mikephil.charting.data.BarEntry)25 BarData (com.github.mikephil.charting.data.BarData)24 IBarDataSet (com.github.mikephil.charting.interfaces.datasets.IBarDataSet)22 Intent (android.content.Intent)8 Paint (android.graphics.Paint)5 List (java.util.List)4 SuppressLint (android.annotation.SuppressLint)2 XAxis (com.github.mikephil.charting.components.XAxis)2 IAxisValueFormatter (com.github.mikephil.charting.formatter.IAxisValueFormatter)2 IDataSet (com.github.mikephil.charting.interfaces.datasets.IDataSet)2 Context (android.content.Context)1 Path (android.graphics.Path)1 PointF (android.graphics.PointF)1 RectF (android.graphics.RectF)1 BarChart (com.github.mikephil.charting.charts.BarChart)1 AxisBase (com.github.mikephil.charting.components.AxisBase)1 Legend (com.github.mikephil.charting.components.Legend)1 LimitLine (com.github.mikephil.charting.components.LimitLine)1