private Item createItem(String label, float percent, int sliceColor, int itemColor) { Item it = new Item(); it.mLabel = label; it.mItemColor = itemColor; it.cSliceColor = sliceColor; it.cPercent = percent; // Log.d(TAG, "Percent for "+it.mLabel+": "+it.cPercent); // Calculate the highlight color. Saturate at 0xff to make sure that high values // don't result in aliasing. it.mItemHighlight = Color.argb( 0xff, Math.min((int) (mHighlightStrength * (float) Color.red(itemColor)), 0xff), Math.min((int) (mHighlightStrength * (float) Color.green(itemColor)), 0xff), Math.min((int) (mHighlightStrength * (float) Color.blue(itemColor)), 0xff)); it.cSliceHighlight = Color.argb( 0xff, Math.min((int) (mHighlightStrength * (float) Color.red(sliceColor)), 0xff), Math.min((int) (mHighlightStrength * (float) Color.green(sliceColor)), 0xff), Math.min((int) (mHighlightStrength * (float) Color.blue(sliceColor)), 0xff)); float centerX = mPieBounds.width() / 2; float centerY = mPieBounds.height() / 2; float itemW = (mPieBounds.width() / 2) * it.cPercent; float itemH = (mPieBounds.height() / 2) * it.cPercent; it.cSliceBounds = new RectF(centerX - itemW, centerY - itemH, centerX + itemW, centerY + itemH); return it; }
/** Do all of the recalculations needed when the data array changes. */ private void onDataChanged() { int angle = 360 / mData.size(); // This is the extra degrees that cannot be equally divided by the number of sides int extra = 360 % mData.size(); // When the data changes, we have to recalculate // all of the angles. int currentAngle = 0; for (Item it : mData) { // Adds the extra degrees to the last slice to ensue no gaps if (mData.get(mData.size() - 1).mLabel.equals(it.mLabel)) { it.mEndAngle = currentAngle + angle + extra; } else { it.mEndAngle = currentAngle + angle; } it.mStartAngle = currentAngle; currentAngle = it.mEndAngle; // Recalculate the gradient shaders. There are // three values in this gradient, even though only // two are necessary, in order to work around // a bug in certain versions of the graphics engine // that expects at least three values if the // positions array is non-null. // it.mItemShader = new SweepGradient( mPieBounds.width() / 2.0f, mPieBounds.height() / 2.0f, new int[] { it.mItemHighlight, it.mItemHighlight, it.mItemColor, it.mItemColor, }, new float[] { 0, (float) (360 - it.mEndAngle) / 360.0f, (float) (360 - it.mStartAngle) / 360.0f, 1.0f }); it.cSliceShader = new SweepGradient( mPieBounds.width() / 2.0f, mPieBounds.height() / 2.0f, new int[] { it.cSliceHighlight, it.cSliceHighlight, it.cSliceColor, it.cSliceColor, }, new float[] { 0, (float) (360 - it.mEndAngle) / 360.0f, (float) (360 - it.mStartAngle) / 360.0f, 1.0f }); } calcCurrentItem(); onScrollFinished(); }
private int calcX(float x) { RectF r = mPlotBounds; return Math.round(r.left + r.width() * (x - getXMin()) / (getXMax() - getXMin())); }
@Override protected void onSizeChanged(int w, int h, int oldw, int oldh) { super.onSizeChanged(w, h, oldw, oldh); Log.d(TAG, "Slice Count: " + mData.size()); if (mData.size() > 0) { // // Set dimensions for text, pie chart, etc // // Account for padding float xpad = (float) (getPaddingLeft() + getPaddingRight()); float ypad = (float) (getPaddingTop() + getPaddingBottom()); // Account for the label if (mShowText) xpad += mTextWidth; float ww = (float) w - xpad; float hh = (float) h - ypad; // Figure out how big we can make the pie. float diameter = Math.min(ww, hh); mPieBounds = new RectF(0.0f, 0.0f, diameter, diameter); mPieBounds.offsetTo(getPaddingLeft(), getPaddingTop()); // Log.d(TAG, "mPieBounds>> "+mPieBounds); mPointerY = mTextY - (mTextHeight / 2.0f); float pointerOffset = mPieBounds.centerY() - mPointerY; // Make adjustments based on text position if (mTextPos == TEXTPOS_LEFT) { mTextPaint.setTextAlign(Paint.Align.RIGHT); if (mShowText) mPieBounds.offset(mTextWidth, 0.0f); mTextX = mPieBounds.left; if (pointerOffset < 0) { pointerOffset = -pointerOffset; mCurrentItemAngle = 225; } else { mCurrentItemAngle = 135; } mPointerX = mPieBounds.centerX() - pointerOffset; } else { mTextPaint.setTextAlign(Paint.Align.LEFT); mTextX = mPieBounds.right; if (pointerOffset < 0) { pointerOffset = -pointerOffset; mCurrentItemAngle = 315; } else { mCurrentItemAngle = 45; } mPointerX = mPieBounds.centerX() + pointerOffset; } mShadowBounds = new RectF( mPieBounds.left + 10, mPieBounds.bottom + 10, mPieBounds.right - 10, mPieBounds.bottom + 20); // Lay out the child view that actually draws the pie. mPieView.layout( (int) mPieBounds.left, (int) mPieBounds.top, (int) mPieBounds.right, (int) mPieBounds.bottom); mPieView.setPivot(mPieBounds.width() / 2, mPieBounds.height() / 2); mPointerView.layout(0, 0, w, h); onDataChanged(); } }