From b216e40bbe3304c3da459994cebfb8df7e1804a8 Mon Sep 17 00:00:00 2001 From: Viet Duong <> Date: Thu, 5 Mar 2020 20:08:16 +0700 Subject: [PATCH] Update BarChartRenderer, Fill --- .../mpchartexample/BarChartActivity.java | 10 +- .../charting/renderer/BarChartRenderer.java | 37 +++- .../github/mikephil/charting/utils/Fill.java | 176 +++++++----------- 3 files changed, 106 insertions(+), 117 deletions(-) diff --git a/MPChartExample/src/main/java/com/xxmassdeveloper/mpchartexample/BarChartActivity.java b/MPChartExample/src/main/java/com/xxmassdeveloper/mpchartexample/BarChartActivity.java index 89ec00a892..0a4797867c 100644 --- a/MPChartExample/src/main/java/com/xxmassdeveloper/mpchartexample/BarChartActivity.java +++ b/MPChartExample/src/main/java/com/xxmassdeveloper/mpchartexample/BarChartActivity.java @@ -3,6 +3,7 @@ import android.Manifest; import android.content.Intent; import android.content.pm.PackageManager; +import android.graphics.Color; import android.graphics.RectF; import android.net.Uri; import android.os.Bundle; @@ -160,7 +161,7 @@ private void setData(int count, float range) { chart.notifyDataSetChanged(); } else { - set1 = new BarDataSet(values, "The year 2017"); + set1 = new BarDataSet(values, "The year 2020"); set1.setDrawIcons(false); @@ -181,7 +182,7 @@ private void setData(int count, float range) { gradientFills.add(new Fill(startColor3, endColor3)); gradientFills.add(new Fill(startColor4, endColor4)); gradientFills.add(new Fill(startColor5, endColor5)); - + gradientFills.add(new Fill(Color.TRANSPARENT, Color.BLACK)); set1.setFills(gradientFills); ArrayList dataSets = new ArrayList<>(); @@ -191,6 +192,11 @@ private void setData(int count, float range) { data.setValueTextSize(10f); data.setValueTypeface(tfLight); data.setBarWidth(0.9f); + chart.setDrawBarShadow(true); + data.setRoundedBarCorners(true); + data.setCornerRadius(50); + data.setVerticalLineColor(Color.BLACK); + data.setDrawVerticalLine(true); chart.setData(data); } diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/BarChartRenderer.java b/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/BarChartRenderer.java index a857b8646a..a4d09053f8 100644 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/BarChartRenderer.java +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/BarChartRenderer.java @@ -5,6 +5,7 @@ import android.graphics.LinearGradient; import android.graphics.Paint; import android.graphics.Path; +import android.graphics.Rect; import android.graphics.RectF; import android.graphics.drawable.Drawable; @@ -167,22 +168,40 @@ protected void drawDataSet(Canvas c, IBarDataSet dataSet, int index) { } if (isRoundedBarCorners) { + + float cornerRadius = barData.getCornerRadius(); + + if (isCustomFill) { + dataSet.getFill(pos).fillRoundRect(c, + mRenderPaint, + buffer.buffer[j], buffer.buffer[j + 1], buffer.buffer[j + 2], buffer.buffer[j + 3], + cornerRadius); + } else { + c.drawRoundRect(buffer.buffer[j], buffer.buffer[j + 1], buffer.buffer[j + 2], buffer.buffer[j + 3], + cornerRadius, cornerRadius, mRenderPaint); + } + + /* int startColor = barData.getStartColor(); int endColor = barData.getEndColor(); - float cornerRadius = barData.getCornerRadius(); + Rect rect = c.getClipBounds(); - mRenderPaint.setShader(new LinearGradient( - buffer.buffer[j], - buffer.buffer[j + 3], - buffer.buffer[j], + LinearGradient gradient = new LinearGradient( + rect.left, + rect.bottom, + rect.left, buffer.buffer[j + 1], startColor, endColor, - android.graphics.Shader.TileMode.MIRROR)); - Path path2 = roundRect(new RectF(buffer.buffer[j], buffer.buffer[j + 1], buffer.buffer[j + 2], - buffer.buffer[j + 3]), cornerRadius, cornerRadius, true, true, false, false); - c.drawPath(path2, mRenderPaint); + android.graphics.Shader.TileMode.MIRROR); + + mRenderPaint.setShader(gradient); + + c.drawRoundRect(buffer.buffer[j], buffer.buffer[j + 1], buffer.buffer[j + 2], buffer.buffer[j + 3], + cornerRadius, cornerRadius, mRenderPaint); */ + } else { + if (isCustomFill) { dataSet.getFill(pos).fillRect( c, mRenderPaint, diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/utils/Fill.java b/MPChartLib/src/main/java/com/github/mikephil/charting/utils/Fill.java index d12e1fb8d7..ff1cb18878 100644 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/utils/Fill.java +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/utils/Fill.java @@ -4,21 +4,19 @@ import android.graphics.LinearGradient; import android.graphics.Paint; import android.graphics.Path; +import android.graphics.Rect; import android.graphics.RectF; import android.graphics.drawable.Drawable; import androidx.annotation.NonNull; import androidx.annotation.Nullable; -public class Fill -{ - public enum Type - { +public class Fill { + public enum Type { EMPTY, COLOR, LINEAR_GRADIENT, DRAWABLE } - public enum Direction - { + public enum Direction { DOWN, UP, RIGHT, LEFT } @@ -52,136 +50,129 @@ public enum Direction */ private int mAlpha = 255; - public Fill() - { + public Fill() { } - public Fill(int color) - { + public Fill(int color) { this.mType = Type.COLOR; this.mColor = color; calculateFinalColor(); } - public Fill(int startColor, int endColor) - { + public Fill(int startColor, int endColor) { this.mType = Type.LINEAR_GRADIENT; this.mGradientColors = new int[]{startColor, endColor}; } - public Fill(@NonNull int[] gradientColors) - { + public Fill(@NonNull int[] gradientColors) { this.mType = Type.LINEAR_GRADIENT; this.mGradientColors = gradientColors; } - public Fill(@NonNull int[] gradientColors, @NonNull float[] gradientPositions) - { + public Fill(@NonNull int[] gradientColors, @NonNull float[] gradientPositions) { this.mType = Type.LINEAR_GRADIENT; this.mGradientColors = gradientColors; this.mGradientPositions = gradientPositions; } - public Fill(@NonNull Drawable drawable) - { + public Fill(@NonNull Drawable drawable) { this.mType = Type.DRAWABLE; this.mDrawable = drawable; } - public Type getType() - { + public Type getType() { return mType; } - public void setType(Type type) - { + public void setType(Type type) { this.mType = type; } @Nullable - public Integer getColor() - { + public Integer getColor() { return mColor; } - public void setColor(int color) - { + public void setColor(int color) { this.mColor = color; calculateFinalColor(); } - public int[] getGradientColors() - { + public int[] getGradientColors() { return mGradientColors; } - public void setGradientColors(int[] colors) - { + public void setGradientColors(int[] colors) { this.mGradientColors = colors; } - public float[] getGradientPositions() - { + public float[] getGradientPositions() { return mGradientPositions; } - public void setGradientPositions(float[] positions) - { + public void setGradientPositions(float[] positions) { this.mGradientPositions = positions; } - public void setGradientColors(int startColor, int endColor) - { + public void setGradientColors(int startColor, int endColor) { this.mGradientColors = new int[]{startColor, endColor}; } - public int getAlpha() - { + public int getAlpha() { return mAlpha; } - public void setAlpha(int alpha) - { + public void setAlpha(int alpha) { this.mAlpha = alpha; calculateFinalColor(); } - private void calculateFinalColor() - { - if (mColor == null) - { + private void calculateFinalColor() { + if (mColor == null) { mFinalColor = null; - } else - { + } else { int alpha = (int) Math.floor(((mColor >> 24) / 255.0) * (mAlpha / 255.0) * 255.0); mFinalColor = (alpha << 24) | (mColor & 0xffffff); } } + public void fillRoundRect(Canvas c, Paint paint, + float left, float top, float right, float bottom, + float cornerRadius) { + Rect rect = c.getClipBounds(); + + LinearGradient gradient = new LinearGradient( + rect.left, + rect.bottom, + rect.left, + top, + mGradientColors, + mGradientPositions, + android.graphics.Shader.TileMode.MIRROR); + + paint.setShader(gradient); + + c.drawRoundRect(left, top, right, bottom, cornerRadius, cornerRadius, paint); + } + public void fillRect(Canvas c, Paint paint, float left, float top, float right, float bottom, - Direction gradientDirection) - { - switch (mType) - { + Direction gradientDirection) { + switch (mType) { case EMPTY: return; - case COLOR: - { + case COLOR: { if (mFinalColor == null) return; - if (isClipPathSupported()) - { + if (isClipPathSupported()) { int save = c.save(); c.clipRect(left, top, right, bottom); c.drawColor(mFinalColor); c.restoreToCount(save); - } - else - { + } else { // save Paint.Style previous = paint.getStyle(); int previousColor = paint.getColor(); @@ -199,31 +190,14 @@ public void fillRect(Canvas c, Paint paint, } break; - case LINEAR_GRADIENT: - { + case LINEAR_GRADIENT: { if (mGradientColors == null) return; LinearGradient gradient = new LinearGradient( - (int) (gradientDirection == Direction.RIGHT - ? right - : gradientDirection == Direction.LEFT - ? left - : left), - (int) (gradientDirection == Direction.UP - ? bottom - : gradientDirection == Direction.DOWN - ? top - : top), - (int) (gradientDirection == Direction.RIGHT - ? left - : gradientDirection == Direction.LEFT - ? right - : left), - (int) (gradientDirection == Direction.UP - ? top - : gradientDirection == Direction.DOWN - ? bottom - : top), + (int) (gradientDirection == Direction.RIGHT ? right : gradientDirection == Direction.LEFT ? left : left), + (int) (gradientDirection == Direction.UP ? bottom : gradientDirection == Direction.DOWN ? top : top), + (int) (gradientDirection == Direction.RIGHT ? left : gradientDirection == Direction.LEFT ? right : left), + (int) (gradientDirection == Direction.UP ? top : gradientDirection == Direction.DOWN ? bottom : top), mGradientColors, mGradientPositions, android.graphics.Shader.TileMode.MIRROR); @@ -234,8 +208,7 @@ public void fillRect(Canvas c, Paint paint, } break; - case DRAWABLE: - { + case DRAWABLE: { if (mDrawable == null) return; mDrawable.setBounds((int) left, (int) top, (int) right, (int) bottom); @@ -246,28 +219,22 @@ public void fillRect(Canvas c, Paint paint, } public void fillPath(Canvas c, Path path, Paint paint, - @Nullable RectF clipRect) - { - switch (mType) - { + @Nullable RectF clipRect) { + switch (mType) { case EMPTY: return; - case COLOR: - { + case COLOR: { if (mFinalColor == null) return; - if (clipRect != null && isClipPathSupported()) - { + if (clipRect != null && isClipPathSupported()) { int save = c.save(); c.clipPath(path); c.drawColor(mFinalColor); c.restoreToCount(save); - } - else - { + } else { // save Paint.Style previous = paint.getStyle(); int previousColor = paint.getColor(); @@ -285,15 +252,16 @@ public void fillPath(Canvas c, Path path, Paint paint, } break; - case LINEAR_GRADIENT: - { + case LINEAR_GRADIENT: { if (mGradientColors == null) return; + Rect rect = c.getClipBounds(); + LinearGradient gradient = new LinearGradient( - 0, - 0, - c.getWidth(), - c.getHeight(), + rect.left, + rect.bottom, + rect.left, + clipRect.top, mGradientColors, mGradientPositions, android.graphics.Shader.TileMode.MIRROR); @@ -304,8 +272,7 @@ public void fillPath(Canvas c, Path path, Paint paint, } break; - case DRAWABLE: - { + case DRAWABLE: { if (mDrawable == null) return; ensureClipPathSupported(); @@ -326,15 +293,12 @@ public void fillPath(Canvas c, Path path, Paint paint, } } - private boolean isClipPathSupported() - { + private boolean isClipPathSupported() { return Utils.getSDKInt() >= 18; } - private void ensureClipPathSupported() - { - if (Utils.getSDKInt() < 18) - { + private void ensureClipPathSupported() { + if (Utils.getSDKInt() < 18) { throw new RuntimeException("Fill-drawables not (yet) supported below API level 18, " + "this code was run on API level " + Utils.getSDKInt() + "."); }