Search in sources :

Example 1 with Bar

use of com.db.chart.model.Bar in project WilliamChart by diogobernardino.

the class HorizontalBarChartView method onDrawChart.

/**
	 * Method responsible to draw bars with the parsed screen points.
	 *
	 * @param canvas The canvas to draw on
	 * @param data {@link java.util.ArrayList} of {@link com.db.chart.model.ChartSet}
	 * to use while drawing the Chart
	 */
@Override
public void onDrawChart(Canvas canvas, ArrayList<ChartSet> data) {
    final int nSets = data.size();
    final int nEntries = data.get(0).size();
    float offset;
    BarSet barSet;
    Bar bar;
    for (int i = 0; i < nEntries; i++) {
        // Set first offset to draw a group of bars
        offset = data.get(0).getEntry(i).getY() - drawingOffset;
        for (int j = 0; j < nSets; j++) {
            barSet = (BarSet) data.get(j);
            bar = (Bar) barSet.getEntry(i);
            // If entry value is 0 it won't be drawn
            if (!barSet.isVisible())
                continue;
            // Style it!
            if (!bar.hasGradientColor())
                style.barPaint.setColor(bar.getColor());
            else
                style.barPaint.setShader(new LinearGradient(this.getZeroPosition(), bar.getY(), bar.getX(), bar.getY(), bar.getGradientColors(), bar.getGradientPositions(), Shader.TileMode.MIRROR));
            applyShadow(style.barPaint, barSet.getAlpha(), bar.getShadowDx(), bar.getShadowDy(), bar.getShadowRadius(), bar.getShadowColor());
            // Draw background
            if (style.hasBarBackground)
                drawBarBackground(canvas, this.getInnerChartLeft(), offset, this.getInnerChartRight(), (offset + barWidth));
            // Draw bar
            if (// Positive
            bar.getValue() >= 0)
                drawBar(canvas, this.getZeroPosition(), offset, bar.getX(), offset + barWidth);
            else
                // Negative
                drawBar(canvas, bar.getX(), offset, this.getZeroPosition(), offset + barWidth);
            offset += barWidth;
            // If last bar of group no set spacing is necessary
            if (j != nSets - 1)
                offset += style.setSpacing;
        }
    }
}
Also used : LinearGradient(android.graphics.LinearGradient) Bar(com.db.chart.model.Bar) BarSet(com.db.chart.model.BarSet)

Example 2 with Bar

use of com.db.chart.model.Bar in project WilliamChart by diogobernardino.

the class HorizontalStackBarChartView method onDrawChart.

/**
	 * Method responsible to draw bars with the parsed screen points.
	 *
	 * @param canvas The canvas to draw on
	 * @param data {@link java.util.ArrayList} of {@link com.db.chart.model.ChartSet} to use
	 */
@Override
public void onDrawChart(Canvas canvas, ArrayList<ChartSet> data) {
    float offset;
    float currBottom;
    float negOffset;
    float negCurrBottom;
    float y0;
    float y1;
    float x1;
    float barSize;
    int bottomSetIndex;
    int topSetIndex;
    float cornersPatch;
    BarSet barSet;
    Bar bar;
    int dataSize = data.size();
    int setSize = data.get(0).size();
    float zeroPosition = this.getZeroPosition();
    for (int i = 0; i < setSize; i++) {
        // If bar needs background
        if (style.hasBarBackground)
            drawBarBackground(canvas, (int) this.getInnerChartLeft(), (int) (data.get(0).getEntry(i).getY() - barWidth / 2), (int) this.getInnerChartRight(), (int) (data.get(0).getEntry(i).getY() + barWidth / 2));
        // Vertical offset to keep drawing bars on top of the others
        offset = 0;
        negOffset = 0;
        // Bottom of the next bar to be drawn
        currBottom = zeroPosition;
        negCurrBottom = zeroPosition;
        // Unfortunately necessary to discover which set is the bottom and top in case there
        // are entries with value 0. To better understand check one of the methods.
        bottomSetIndex = discoverBottomSet(i, data);
        topSetIndex = discoverTopSet(i, data);
        for (int j = 0; j < dataSize; j++) {
            barSet = (BarSet) data.get(j);
            bar = (Bar) barSet.getEntry(i);
            barSize = Math.abs(zeroPosition - bar.getX());
            // Then no need to draw
            if (!barSet.isVisible() || bar.getValue() == 0 || barSize < 2)
                continue;
            style.barPaint.setColor(bar.getColor());
            applyShadow(style.barPaint, barSet.getAlpha(), bar.getShadowDx(), bar.getShadowDy(), bar.getShadowRadius(), bar.getShadowColor());
            y0 = (bar.getY() - barWidth / 2);
            y1 = (bar.getY() + barWidth / 2);
            if (bar.getValue() > 0) {
                x1 = zeroPosition + (barSize - offset);
                // Draw bar
                if (j == bottomSetIndex) {
                    drawBar(canvas, (int) currBottom, (int) y0, (int) x1, (int) y1);
                    if (bottomSetIndex != topSetIndex && style.cornerRadius != 0) {
                        // Patch top corners of bar
                        cornersPatch = (x1 - currBottom) / 2;
                        canvas.drawRect(new Rect((int) (x1 - cornersPatch), (int) y0, (int) x1, (int) y1), style.barPaint);
                    }
                } else if (j == topSetIndex) {
                    drawBar(canvas, (int) currBottom, (int) y0, (int) x1, (int) y1);
                    // Patch bottom corners of bar
                    cornersPatch = (x1 - currBottom) / 2;
                    canvas.drawRect(new Rect((int) currBottom, (int) y0, (int) (currBottom + cornersPatch), (int) y1), style.barPaint);
                } else {
                    // if(j != bottomSetIndex && j != topSetIndex){ // Middle sets
                    canvas.drawRect(new Rect((int) currBottom, (int) y0, (int) x1, (int) y1), style.barPaint);
                }
                currBottom = x1;
                // Increase the vertical offset to be used by the next bar
                if (barSize != 0)
                    // Sum 1 to compensate the loss of precision in float
                    offset -= barSize - 0;
            } else {
                // if(bar.getValue() < 0)
                x1 = zeroPosition - (barSize + negOffset);
                if (j == bottomSetIndex) {
                    drawBar(canvas, (int) x1, (int) y0, (int) negCurrBottom, (int) y1);
                    if (bottomSetIndex != topSetIndex && style.cornerRadius != 0) {
                        // Patch top corners of bar
                        cornersPatch = (negCurrBottom - x1) / 2;
                        canvas.drawRect(new Rect((int) (negCurrBottom - cornersPatch), (int) y0, (int) negCurrBottom, (int) y1), style.barPaint);
                    }
                } else if (j == topSetIndex) {
                    drawBar(canvas, (int) x1, (int) y0, (int) negCurrBottom, (int) y1);
                    // Patch bottom corners of bar
                    cornersPatch = (negCurrBottom - x1) / 2;
                    canvas.drawRect(new Rect((int) x1, (int) y0, (int) (x1 + cornersPatch), (int) y1), style.barPaint);
                } else {
                    // if(j != bottomSetIndex && j != topSetIndex){ // Middle sets
                    canvas.drawRect(new Rect((int) x1, (int) y0, (int) negCurrBottom, (int) y1), style.barPaint);
                }
                negCurrBottom = x1;
                // Increase the vertical offset to be used by the next bar
                if (barSize != 0)
                    negOffset += barSize;
            }
        }
    }
}
Also used : Bar(com.db.chart.model.Bar) Rect(android.graphics.Rect) BarSet(com.db.chart.model.BarSet)

Example 3 with Bar

use of com.db.chart.model.Bar in project WilliamChart by diogobernardino.

the class HorizontalStackBarChartView method defineRegions.

/**
	 * (Optional) To be overridden in order for each chart to define its own clickable regions.
	 * This way, classes extending ChartView will only define their clickable regions.
	 * <p>
	 * Important: the returned vector must match the order of the data passed
	 * by the user. This ensures that onTouchEvent will return the correct index.
	 *
	 * @param data {@link java.util.ArrayList} of {@link com.db.chart.model.ChartSet}
	 * to use while defining each region of a {@link com.db.chart.view.BarChartView}
	 *
	 * @return {@link java.util.ArrayList} of {@link android.graphics.Region} with regions
	 * where click will be detected
	 */
@Override
void defineRegions(ArrayList<ArrayList<Region>> regions, ArrayList<ChartSet> data) {
    int dataSize = data.size();
    int setSize = data.get(0).size();
    float offset;
    float currBottom;
    float negOffset;
    float negCurrBottom;
    float x1;
    float barSize;
    BarSet barSet;
    Bar bar;
    float zeroPosition = this.getZeroPosition();
    for (int i = 0; i < setSize; i++) {
        // Vertical offset to keep drawing bars on top of the others
        offset = 0;
        negOffset = 0;
        // Bottom of the next bar to be drawn
        currBottom = zeroPosition;
        negCurrBottom = zeroPosition;
        for (int j = 0; j < dataSize; j++) {
            barSet = (BarSet) data.get(j);
            bar = (Bar) barSet.getEntry(i);
            barSize = Math.abs(zeroPosition - bar.getX());
            // Then no need to have region
            if (!barSet.isVisible())
                continue;
            if (bar.getValue() > 0) {
                x1 = zeroPosition + (barSize - offset);
                regions.get(j).get(i).set((int) currBottom, (int) (bar.getY() - barWidth / 2), (int) x1, (int) (bar.getY() + barWidth / 2));
                currBottom = x1;
                offset -= barSize - 2;
            } else if (bar.getValue() < 0) {
                x1 = zeroPosition - (barSize + negOffset);
                regions.get(j).get(i).set((int) x1, (int) (bar.getY() - barWidth / 2), (int) negCurrBottom, (int) (bar.getY() + barWidth / 2));
                negCurrBottom = x1;
                negOffset += barSize;
            } else {
                // If bar.getValue() == 0, force region to 1 pixel
                x1 = zeroPosition + (1 - offset);
                regions.get(j).get(i).set((int) currBottom, (int) (bar.getY() - barWidth / 2), (int) x1, (int) (bar.getY() + barWidth / 2));
            }
        }
    }
}
Also used : Bar(com.db.chart.model.Bar) BarSet(com.db.chart.model.BarSet)

Example 4 with Bar

use of com.db.chart.model.Bar in project WilliamChart by diogobernardino.

the class BarCardTwo method show.

@Override
public void show(Runnable action) {
    super.show(action);
    Tooltip tip = new Tooltip(mContext);
    tip.setBackgroundColor(Color.parseColor("#f39c12"));
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
        tip.setEnterAnimation(PropertyValuesHolder.ofFloat(View.ALPHA, 1)).setDuration(150);
        tip.setExitAnimation(PropertyValuesHolder.ofFloat(View.ALPHA, 0)).setDuration(150);
    }
    mChart.setTooltips(tip);
    mChart.setOnEntryClickListener(new OnEntryClickListener() {

        @Override
        public void onClick(int setIndex, int entryIndex, Rect rect) {
            mTextViewValue.setText(Integer.toString((int) mValues[0][entryIndex]));
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR1)
                mTextViewValue.animate().alpha(1).setDuration(200);
            else
                mTextViewValue.setVisibility(View.VISIBLE);
        }
    });
    mChart.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR1)
                mTextViewValue.animate().alpha(0).setDuration(100);
            else
                mTextViewValue.setVisibility(View.INVISIBLE);
        }
    });
    BarSet barSet = new BarSet();
    Bar bar;
    for (int i = 0; i < mLabels.length; i++) {
        bar = new Bar(mLabels[i], mValues[0][i]);
        switch(i) {
            case 0:
                bar.setColor(Color.parseColor("#77c63d"));
                break;
            case 1:
                bar.setColor(Color.parseColor("#27ae60"));
                break;
            case 2:
                bar.setColor(Color.parseColor("#47bac1"));
                break;
            case 3:
                bar.setColor(Color.parseColor("#16a085"));
                break;
            case 4:
                bar.setColor(Color.parseColor("#3498db"));
                break;
            default:
                break;
        }
        barSet.addBar(bar);
    }
    mChart.addData(barSet);
    mChart.setBarSpacing(Tools.fromDpToPx(4));
    mChart.setBorderSpacing(0).setXAxis(false).setYAxis(false).setLabelsColor(Color.parseColor("#FF8E8A84")).setXLabels(XRenderer.LabelPosition.NONE);
    int[] order = { 4, 3, 2, 1, 0 };
    mChart.show(new Animation().setOverlap(.5f, order).setEndAction(action));
}
Also used : Rect(android.graphics.Rect) Bar(com.db.chart.model.Bar) OnEntryClickListener(com.db.chart.listener.OnEntryClickListener) BarSet(com.db.chart.model.BarSet) Tooltip(com.db.chart.tooltip.Tooltip) Animation(com.db.chart.animation.Animation) CardView(android.support.v7.widget.CardView) TextView(android.widget.TextView) HorizontalBarChartView(com.db.chart.view.HorizontalBarChartView) View(android.view.View)

Example 5 with Bar

use of com.db.chart.model.Bar in project WilliamChart by diogobernardino.

the class StackBarChartView method onDrawChart.

/**
	 * Method responsible to draw bars with the parsed screen points.
	 *
	 * @param canvas The canvas to draw on
	 * @param data {@link java.util.ArrayList} of {@link com.db.chart.model.ChartSet} to use
	 */
@Override
public void onDrawChart(Canvas canvas, ArrayList<ChartSet> data) {
    float verticalOffset;
    float currBottomY;
    float negVerticalOffset;
    float negCurrBottomY;
    float x0;
    float x1;
    float y1;
    float barSize;
    int bottomSetIndex;
    int topSetIndex;
    float cornersPatch;
    BarSet barSet;
    Bar bar;
    int dataSize = data.size();
    int setSize = data.get(0).size();
    float zeroPosition = this.getZeroPosition();
    for (int i = 0; i < setSize; i++) {
        // If bar needs background
        if (style.hasBarBackground) {
            drawBarBackground(canvas, (int) (data.get(0).getEntry(i).getX() - barWidth / 2), (int) this.getInnerChartTop(), (int) (data.get(0).getEntry(i).getX() + barWidth / 2), (int) this.getInnerChartBottom());
        }
        // Vertical offset to keep drawing bars on top of the others
        verticalOffset = 0;
        negVerticalOffset = 0;
        // Bottom of the next bar to be drawn
        currBottomY = zeroPosition;
        negCurrBottomY = zeroPosition;
        // Unfortunately necessary to discover which set is the bottom and top in case there
        // are entries with value 0. To better understand check one of the methods.
        bottomSetIndex = discoverBottomSet(i, data);
        topSetIndex = discoverTopSet(i, data);
        for (int j = 0; j < dataSize; j++) {
            barSet = (BarSet) data.get(j);
            bar = (Bar) barSet.getEntry(i);
            barSize = Math.abs(zeroPosition - bar.getY());
            // Then no need to draw
            if (!barSet.isVisible() || bar.getValue() == 0 || barSize < 2)
                continue;
            style.barPaint.setColor(bar.getColor());
            applyShadow(style.barPaint, barSet.getAlpha(), bar.getShadowDx(), bar.getShadowDy(), bar.getShadowRadius(), bar.getShadowColor());
            x0 = (bar.getX() - barWidth / 2);
            x1 = (bar.getX() + barWidth / 2);
            if (bar.getValue() > 0) {
                y1 = zeroPosition - (barSize + verticalOffset);
                // Draw bar
                if (j == bottomSetIndex) {
                    drawBar(canvas, (int) x0, (int) y1, (int) x1, (int) currBottomY);
                    if (bottomSetIndex != topSetIndex && style.cornerRadius != 0) {
                        // Patch top corners of bar
                        cornersPatch = (currBottomY - y1) / 2;
                        canvas.drawRect(new Rect((int) x0, (int) y1, (int) x1, (int) (y1 + cornersPatch)), style.barPaint);
                    }
                } else if (j == topSetIndex) {
                    drawBar(canvas, (int) x0, (int) y1, (int) x1, (int) currBottomY);
                    // Patch bottom corners of bar
                    cornersPatch = (currBottomY - y1) / 2;
                    canvas.drawRect(new Rect((int) x0, (int) (currBottomY - cornersPatch), (int) x1, (int) currBottomY), style.barPaint);
                } else {
                    // if(j != bottomSetIndex && j != topSetIndex){ // Middle sets
                    canvas.drawRect(new Rect((int) x0, (int) y1, (int) x1, (int) currBottomY), style.barPaint);
                }
                currBottomY = y1;
                // Increase the vertical offset to be used by the next bar
                if (barSize != 0)
                    // Sum 1 to compensate the loss of precision in float
                    verticalOffset += barSize + 2;
            } else {
                // if(bar.getValue() < 0)
                y1 = zeroPosition + (barSize - negVerticalOffset);
                if (j == bottomSetIndex) {
                    drawBar(canvas, (int) x0, (int) negCurrBottomY, (int) x1, (int) y1);
                    if (bottomSetIndex != topSetIndex && style.cornerRadius != 0) {
                        // Patch top corners of bar
                        cornersPatch = (y1 - negCurrBottomY) / 2;
                        canvas.drawRect(new Rect((int) x0, (int) negCurrBottomY, (int) x1, (int) (negCurrBottomY + cornersPatch)), style.barPaint);
                    }
                } else if (j == topSetIndex) {
                    drawBar(canvas, (int) x0, (int) negCurrBottomY, (int) x1, (int) y1);
                    // Patch bottom corners of bar
                    cornersPatch = (y1 - negCurrBottomY) / 2;
                    canvas.drawRect(new Rect((int) x0, (int) (y1 - cornersPatch), (int) x1, (int) y1), style.barPaint);
                } else {
                    // if(j != bottomSetIndex && j != topSetIndex){ // Middle sets
                    canvas.drawRect(new Rect((int) x0, (int) negCurrBottomY, (int) x1, (int) y1), style.barPaint);
                }
                negCurrBottomY = y1;
                // Increase the vertical offset to be used by the next bar
                if (barSize != 0)
                    negVerticalOffset -= barSize;
            }
        }
    }
}
Also used : Bar(com.db.chart.model.Bar) Rect(android.graphics.Rect) BarSet(com.db.chart.model.BarSet)

Aggregations

Bar (com.db.chart.model.Bar)16 BarSet (com.db.chart.model.BarSet)16 Before (org.junit.Before)4 Rect (android.graphics.Rect)3 LinearGradient (android.graphics.LinearGradient)2 Paint (android.graphics.Paint)2 MediumTest (android.test.suitebuilder.annotation.MediumTest)2 Test (org.junit.Test)2 CardView (android.support.v7.widget.CardView)1 View (android.view.View)1 TextView (android.widget.TextView)1 Animation (com.db.chart.animation.Animation)1 OnEntryClickListener (com.db.chart.listener.OnEntryClickListener)1 Tooltip (com.db.chart.tooltip.Tooltip)1 HorizontalBarChartView (com.db.chart.view.HorizontalBarChartView)1