com.github.mikephil.charting.interfaces.datasets.IBarDataSet Java Examples

The following examples show how to use com.github.mikephil.charting.interfaces.datasets.IBarDataSet. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: BarHighlighter.java    From Stayfit with Apache License 2.0 6 votes vote down vote up
@Override
public Highlight getHighlight(float x, float y) {

	Highlight h = super.getHighlight(x, y);

	if (h == null)
		return h;
	else {

		IBarDataSet set = mChart.getBarData().getDataSetByIndex(h.getDataSetIndex());

		if (set.isStacked()) {

			// create an array of the touch-point
			float[] pts = new float[2];
			pts[1] = y;

			// take any transformer to determine the x-axis value
			mChart.getTransformer(set.getAxisDependency()).pixelsToValue(pts);

			return getStackedHighlight(h, set, h.getXIndex(), h.getDataSetIndex(), pts[1]);
		} else
			return h;
	}
}
 
Example #2
Source File: OneDayChart.java    From StockChart-MPAndroidChart with MIT License 6 votes vote down vote up
/**
 * 动态增加一个点数据
 * @param timeDatamodel
 * @param length
 */
public void dynamicsAddOne(TimeDataModel timeDatamodel, int length) {
    int index = length - 1;
    LineData lineData = lineChart.getData();
    ILineDataSet d1 = lineData.getDataSetByIndex(0);
    d1.addEntry(new Entry(index, (float) timeDatamodel.getNowPrice()));
    ILineDataSet d2 = lineData.getDataSetByIndex(1);
    d2.addEntry(new Entry(index, (float) timeDatamodel.getAveragePrice()));

    BarData barData = barChart.getData();
    IBarDataSet barDataSet = barData.getDataSetByIndex(0);
    float color = timeDatamodel.getNowPrice() == d1.getEntryForIndex(index - 1).getY() ? 0f : timeDatamodel.getNowPrice() > d1.getEntryForIndex(index - 1).getY() ? 1f : -1f;
    barDataSet.addEntry(new BarEntry(index, timeDatamodel.getVolume(),color));
    lineData.notifyDataChanged();
    lineChart.notifyDataSetChanged();
    barData.notifyDataChanged();
    barChart.notifyDataSetChanged();
    lineChart.setVisibleXRange(maxCount, maxCount);
    barChart.setVisibleXRange(maxCount, maxCount);
    //动态添加或移除数据后, 调用invalidate()刷新图表之前 必须调用 notifyDataSetChanged() .
    lineChart.moveViewToX(index);
    barChart.moveViewToX(index);
}
 
Example #3
Source File: HistogramChart.java    From walt with Apache License 2.0 6 votes vote down vote up
void recalculateDataSet(final BarData barData) {
    minBin = Math.floor(min / binWidth) * binWidth;
    maxBin = Math.floor(max / binWidth) * binWidth;

    int[][] bins = new int[rawData.size()][getNumBins()];

    for (int setNum = 0; setNum < rawData.size(); setNum++) {
        for (Double d : rawData.get(setNum)) {
            ++bins[setNum][(int) (Math.floor((d - minBin) / binWidth))];
        }
    }

    for (int setNum = 0; setNum < barData.getDataSetCount(); setNum++) {
        final IBarDataSet dataSet = barData.getDataSetByIndex(setNum);
        dataSet.clear();
        for (int i = 0; i < bins[setNum].length; i++) {
            dataSet.addEntry(new BarEntry(i, bins[setNum][i]));
        }
    }
    groupBars(barData);
    barData.notifyDataChanged();
}
 
Example #4
Source File: BarChart.java    From StockChart-MPAndroidChart with MIT License 6 votes vote down vote up
/**
 * 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);
}
 
Example #5
Source File: BarChart.java    From android-kline with Apache License 2.0 6 votes vote down vote up
/**
 * 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);
}
 
Example #6
Source File: BarHighlighter.java    From StockChart-MPAndroidChart with MIT License 6 votes vote down vote up
@Override
public Highlight getHighlight(float x, float y) {
    Highlight high = super.getHighlight(x, y);

    if(high == null) {
        return null;
    }

    MPPointD pos = getValsForTouch(x, y);

    BarData barData = mChart.getBarData();

    IBarDataSet set = barData.getDataSetByIndex(high.getDataSetIndex());
    if (set.isStacked()) {

        return getStackedHighlight(high,
                set,
                (float) pos.x,
                (float) pos.y);
    }

    MPPointD.recycleInstance(pos);

    return high;
}
 
Example #7
Source File: HorizontalBarHighlighter.java    From StockChart-MPAndroidChart with MIT License 6 votes vote down vote up
@Override
public Highlight getHighlight(float x, float y) {

    BarData barData = mChart.getBarData();

    MPPointD pos = getValsForTouch(y, x);

    Highlight high = getHighlightForX((float) pos.y, y, x);
    if (high == null) {
        return null;
    }

    IBarDataSet set = barData.getDataSetByIndex(high.getDataSetIndex());
    if (set.isStacked()) {

        return getStackedHighlight(high,
                set,
                (float) pos.y,
                (float) pos.x);
    }

    MPPointD.recycleInstance(pos);

    return high;
}
 
Example #8
Source File: SimpleFragment.java    From StockChart-MPAndroidChart with MIT License 6 votes vote down vote up
protected BarData generateBarData(int dataSets, float range, int count) {

        ArrayList<IBarDataSet> sets = new ArrayList<>();

        for(int i = 0; i < dataSets; i++) {

            ArrayList<BarEntry> entries = new ArrayList<>();

            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;
    }
 
Example #9
Source File: BarChart.java    From Ticket-Analysis with MIT License 6 votes vote down vote up
/**
 * 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);
}
 
Example #10
Source File: HorizontalBarChart.java    From Ticket-Analysis with MIT License 6 votes vote down vote up
@Override
public void getBarBounds(BarEntry e, RectF outputRect) {

    RectF bounds = outputRect;
    IBarDataSet set = mData.getDataSetForEntry(e);

    if (set == null) {
        outputRect.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 top = x - barWidth / 2f;
    float bottom = x + barWidth / 2f;
    float left = y >= 0 ? y : 0;
    float right = y <= 0 ? y : 0;

    bounds.set(left, top, right, bottom);

    getTransformer(set.getAxisDependency()).rectValueToPixel(bounds);

}
 
Example #11
Source File: BarHighlighter.java    From Ticket-Analysis with MIT License 6 votes vote down vote up
@Override
public Highlight getHighlight(float x, float y) {
    Highlight high = super.getHighlight(x, y);

    if(high == null) {
        return null;
    }

    MPPointD pos = getValsForTouch(x, y);

    BarData barData = mChart.getBarData();

    IBarDataSet set = barData.getDataSetByIndex(high.getDataSetIndex());
    if (set.isStacked()) {

        return getStackedHighlight(high,
                set,
                (float) pos.x,
                (float) pos.y);
    }

    MPPointD.recycleInstance(pos);

    return high;
}
 
Example #12
Source File: BarHighlighter.java    From android-kline with Apache License 2.0 6 votes vote down vote up
@Override
public Highlight getHighlight(float x, float y) {
    Highlight high = super.getHighlight(x, y);

    if(high == null) {
        return null;
    }

    MPPointD pos = getValsForTouch(x, y);

    BarData barData = mChart.getBarData();

    IBarDataSet set = barData.getDataSetByIndex(high.getDataSetIndex());
    if (set.isStacked()) {

        return getStackedHighlight(high,
                set,
                (float) pos.x,
                (float) pos.y);
    }

    MPPointD.recycleInstance(pos);

    return high;
}
 
Example #13
Source File: HorizontalBarHighlighter.java    From android-kline with Apache License 2.0 6 votes vote down vote up
@Override
public Highlight getHighlight(float x, float y) {

	BarData barData = mChart.getBarData();

	MPPointD pos = getValsForTouch(y, x);

	Highlight high = getHighlightForX((float) pos.y, y, x);
	if (high == null)
		return null;

	IBarDataSet set = barData.getDataSetByIndex(high.getDataSetIndex());
	if (set.isStacked()) {

		return getStackedHighlight(high,
				set,
				(float) pos.y,
				(float) pos.x);
	}

	MPPointD.recycleInstance(pos);

	return high;
}
 
Example #14
Source File: RealmDatabaseActivityHorizontalBar.java    From Stayfit with Apache License 2.0 6 votes vote down vote up
private void setData() {

        RealmResults<RealmDemoData> result = mRealm.allObjects(RealmDemoData.class);

        //RealmBarDataSet<RealmDemoData> set = new RealmBarDataSet<RealmDemoData>(result, "stackValues", "xIndex"); // normal entries
        RealmBarDataSet<RealmDemoData> set = new RealmBarDataSet<RealmDemoData>(result, "stackValues", "xIndex", "floatValue"); // stacked entries
        set.setColors(new int[]{ColorTemplate.rgb("#8BC34A"), ColorTemplate.rgb("#FFC107"), ColorTemplate.rgb("#9E9E9E")});
        set.setLabel("Mobile OS distribution");
        set.setStackLabels(new String[]{"iOS", "Android", "Other"});

        ArrayList<IBarDataSet> dataSets = new ArrayList<IBarDataSet>();
        dataSets.add(set); // add the dataset

        // create a data object with the dataset list
        RealmBarData data = new RealmBarData(result, "xValue", dataSets);
        styleData(data);
        data.setValueTextColor(Color.WHITE);

        // set data
        mChart.setData(data);
        mChart.animateY(1400, Easing.EasingOption.EaseInOutQuart);
    }
 
Example #15
Source File: BarChart.java    From Stayfit with Apache License 2.0 6 votes vote down vote up
/**
 * 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
 * @return
 */
public RectF getBarBounds(BarEntry e) {

	IBarDataSet set = mData.getDataSetForEntry(e);

	if (set == null)
		return null;

	float barspace = set.getBarSpace();
	float y = e.getVal();
	float x = e.getXIndex();

	float barWidth = 0.5f;

	float spaceHalf = barspace / 2f;
	float left = x - barWidth + spaceHalf;
	float right = x + barWidth - spaceHalf;
	float top = y >= 0 ? y : 0;
	float bottom = y <= 0 ? y : 0;

	RectF bounds = new RectF(left, top, right, bottom);

	getTransformer(set.getAxisDependency()).rectValueToPixel(bounds);

	return bounds;
}
 
Example #16
Source File: BarChart.java    From NetKnight with Apache License 2.0 6 votes vote down vote up
/**
 * 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
 * @return
 */
public RectF getBarBounds(BarEntry e) {

	IBarDataSet set = mData.getDataSetForEntry(e);

	if (set == null)
		return null;

	float barspace = set.getBarSpace();
	float y = e.getVal();
	float x = e.getXIndex();

	float barWidth = 0.5f;

	float spaceHalf = barspace / 2f;
	float left = x - barWidth + spaceHalf;
	float right = x + barWidth - spaceHalf;
	float top = y >= 0 ? y : 0;
	float bottom = y <= 0 ? y : 0;

	RectF bounds = new RectF(left, top, right, bottom);

	getTransformer(set.getAxisDependency()).rectValueToPixel(bounds);

	return bounds;
}
 
Example #17
Source File: ProductDetailActivity.java    From FaceT with Mozilla Public License 2.0 6 votes vote down vote up
public BarData getBarData() {

        ArrayList<BarEntry> entries = new ArrayList<>();
        float overall_people = 100f;

        Log.d(TAG + "barData", barRatingCount[3] + "");
        entries.add(new BarEntry(4, barRatingCount[4]));
        entries.add(new BarEntry(3, barRatingCount[3]));
        entries.add(new BarEntry(2, barRatingCount[2]));
        entries.add(new BarEntry(1, barRatingCount[1]));
        entries.add(new BarEntry(0, barRatingCount[0]));

        BarDataSet dataset = new BarDataSet(entries, "");
        dataset.setColors(CUSTOM_COLOR);
        dataset.setDrawValues(false);

        ArrayList<IBarDataSet> dataSets = new ArrayList<IBarDataSet>();
        dataSets.add(dataset);

        BarData data = new BarData(dataSets);
//        data.setValueTextSize(10f);
//        data.setValueTypeface(fontType);
        data.setBarWidth(1f);

        return data;
    }
 
Example #18
Source File: BarChartRenderer.java    From Stayfit with Apache License 2.0 5 votes vote down vote up
@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.getGroupSpace(),
                barData.getDataSetCount(), set.isStacked());
    }
}
 
Example #19
Source File: BarChartRenderer.java    From android-kline with Apache License 2.0 5 votes vote down vote up
@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());
    }
}
 
Example #20
Source File: BarHighlighter.java    From android-kline with Apache License 2.0 5 votes vote down vote up
/**
 * 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;
}
 
Example #21
Source File: MyBarChartRenderer.java    From shinny-futures-android with GNU General Public License v3.0 5 votes vote down vote up
@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;

        mHighlightPaint.setColor(set.getHighLightColor());
        //保持和顶层图的十字光标一致
        //mHighlightPaint.setAlpha(set.getHighLightAlpha());

        float barWidth = barData.getBarWidth();
        Transformer trans = mChart.getTransformer(set.getAxisDependency());
        prepareBarHighlight(e.getX(), 0, 0, barWidth / 2, trans);

        //画竖线
        float xp = mBarRect.centerX();
        c.drawLine(xp, mViewPortHandler.getContentRect().bottom, xp, 0, mHighlightPaint);

        //判断是否画横线
        float y = high.getDrawY();
        float yMax = mChart.getHeight();
        float xMax = mChart.getWidth();
        if (y >= 0 && y <= yMax) {//在区域内即绘制横线
            //绘制横线
            c.drawLine(0, y, xMax, y, mHighlightPaint);
        }
    }
}
 
Example #22
Source File: Transformer.java    From NetKnight with Apache License 2.0 5 votes vote down vote up
/**
 * Transforms an List of Entry into a float array containing the x and
 * y values transformed with all matrices for the BARCHART.
 *
 * @param data
 * @param dataSet the dataset index
 * @return
 */
public float[] generateTransformedValuesHorizontalBarChart(IBarDataSet data,
                                                           int dataSet, BarData bd, float phaseY) {

    float[] valuePoints = new float[data.getEntryCount() * 2];

    int setCount = bd.getDataSetCount();
    float space = bd.getGroupSpace();

    for (int j = 0; j < valuePoints.length; j += 2) {

        Entry e = data.getEntryForIndex(j / 2);
        int i = e.getXIndex();

        // calculate the x-position, depending on datasetcount
        float x = i + i * (setCount - 1) + dataSet + space * i
                + space / 2f;
        float y = e.getVal();

        valuePoints[j] = y * phaseY;
        valuePoints[j + 1] = x;
    }

    getValueToPixelMatrix().mapPoints(valuePoints);

    return valuePoints;
}
 
Example #23
Source File: AnalysisFragment.java    From outlay with Apache License 2.0 5 votes vote down vote up
@Override
public void showAnalysis(Report report) {
    List<BarEntry> barEntries = new ArrayList<>();
    List<Expense> expenses = report.getExpenses();

    for (int i = 0; i < expenses.size(); i++) {
        Expense expense = expenses.get(i);
        barEntries.add(new BarEntry(
                i,
                expense.getAmount().floatValue())
        );
    }
    BarDataSet barSet = new BarDataSet(barEntries, "");
    barSet.setColor(selectedCategory.getColor());

    List<IBarDataSet> dataSets = new ArrayList<>();
    dataSets.add(barSet);

    BarData data = new BarData(dataSets);
    data.setValueTextSize(10f);
    data.setValueTextColor(getOutlayTheme().secondaryTextColor);
    data.setBarWidth(0.9f);

    dayAxisValueFormatter.setExpenses(expenses);
    barChart.setData(data);
    barChart.invalidate();
    barChart.animateY(500);
}
 
Example #24
Source File: BarChartRenderer.java    From android-kline with Apache License 2.0 5 votes vote down vote up
@Override
public void drawData(Canvas c) {

    BarData barData = mChart.getBarData();

    for (int i = 0; i < barData.getDataSetCount(); i++) {

        IBarDataSet set = barData.getDataSetByIndex(i);

        if (set.isVisible()) {
            drawDataSet(c, set, i);
        }
    }
}
 
Example #25
Source File: BarHighlighter.java    From Ticket-Analysis with MIT License 5 votes vote down vote up
/**
 * 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;
}
 
Example #26
Source File: BarChartRenderer.java    From StockChart-MPAndroidChart with MIT License 5 votes vote down vote up
@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());
    }
}
 
Example #27
Source File: HistogramChart.java    From walt with Apache License 2.0 5 votes vote down vote up
/**
 * Re-implementation of BarData.groupBars(), but allows grouping with only 1 BarDataSet
 * This adjusts the x-coordinates of entries, which centers the bars between axis labels
 */
static void groupBars(final BarData barData) {
    IBarDataSet max = barData.getMaxEntryCountSet();
    int maxEntryCount = max.getEntryCount();
    float groupSpaceWidthHalf = GROUP_SPACE / 2f;
    float barWidthHalf = barData.getBarWidth() / 2f;
    float interval = barData.getGroupWidth(GROUP_SPACE, 0);
    float fromX = 0;

    for (int i = 0; i < maxEntryCount; i++) {
        float start = fromX;
        fromX += groupSpaceWidthHalf;

        for (IBarDataSet set : barData.getDataSets()) {
            fromX += barWidthHalf;
            if (i < set.getEntryCount()) {
                BarEntry entry = set.getEntryForIndex(i);
                if (entry != null) {
                    entry.setX(fromX);
                }
            }
            fromX += barWidthHalf;
        }

        fromX += groupSpaceWidthHalf;
        float end = fromX;
        float innerInterval = end - start;
        float diff = interval - innerInterval;

        // correct rounding errors
        if (diff > 0 || diff < 0) {
            fromX += diff;
        }
    }
    barData.notifyDataChanged();
}
 
Example #28
Source File: BarHighlighter.java    From Stayfit with Apache License 2.0 5 votes vote down vote up
/**
 * This method creates the Highlight object that also indicates which value of a stacked BarEntry has been selected.
 *
 * @param old
 *            the old highlight object before looking for stacked values
 * @param set
 * @param xIndex
 * @param dataSetIndex
 * @param yValue
 * @return
 */
protected Highlight getStackedHighlight(Highlight old, IBarDataSet set, int xIndex, int dataSetIndex, double yValue) {

	BarEntry entry = set.getEntryForXIndex(xIndex);

	if (entry == null || entry.getVals() == null)
		return old;

	Range[] ranges = getRanges(entry);
	int stackIndex = getClosestStackIndex(ranges, (float) yValue);

	Highlight h = new Highlight(xIndex, dataSetIndex, stackIndex, ranges[stackIndex]);
	return h;
}
 
Example #29
Source File: BarChartRenderer.java    From NetKnight with Apache License 2.0 5 votes vote down vote up
@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.getGroupSpace(),
                barData.getDataSetCount(), set.isStacked());
    }
}
 
Example #30
Source File: StackedBarActivity.java    From Stayfit with Apache License 2.0 5 votes vote down vote up
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {

	tvX.setText("" + (mSeekBarX.getProgress() + 1));
	tvY.setText("" + (mSeekBarY.getProgress()));

	ArrayList<String> xVals = new ArrayList<String>();
	for (int i = 0; i < mSeekBarX.getProgress() + 1; i++) {
		xVals.add(mMonths[i % mMonths.length]);
	}

	ArrayList<BarEntry> yVals1 = new ArrayList<BarEntry>();

	for (int i = 0; i < mSeekBarX.getProgress() + 1; i++) {
		float mult = (mSeekBarY.getProgress() + 1);
		float val1 = (float) (Math.random() * mult) + mult / 3;
		float val2 = (float) (Math.random() * mult) + mult / 3;
		float val3 = (float) (Math.random() * mult) + mult / 3;

		yVals1.add(new BarEntry(new float[] { val1, val2, val3 }, i));
	}

	BarDataSet set1 = new BarDataSet(yVals1, "Statistics Vienna 2014");
	set1.setColors(getColors());
	set1.setStackLabels(new String[] { "Births", "Divorces", "Marriages" });

	ArrayList<IBarDataSet> dataSets = new ArrayList<IBarDataSet>();
	dataSets.add(set1);

	BarData data = new BarData(xVals, dataSets);
	data.setValueFormatter(new MyValueFormatter());

	mChart.setData(data);
	mChart.invalidate();
}