Skip to content

Commit

Permalink
- Now Added option to show/hide labels
Browse files Browse the repository at this point in the history
  • Loading branch information
Taosif7 committed Oct 16, 2020
1 parent e54a1e3 commit b24cf90
Show file tree
Hide file tree
Showing 7 changed files with 93 additions and 23 deletions.
6 changes: 6 additions & 0 deletions .idea/compiler.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions .idea/gradle.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.util.Property;
Expand Down Expand Up @@ -44,6 +45,7 @@ public class RingChart extends ViewGroup {
// Properties
private RingView backgroundRing;
private renderMode mode = renderMode.MODE_OVERLAP;
private boolean showLabels = true;

public RingChart(Context ctx) {
super(ctx);
Expand Down Expand Up @@ -92,7 +94,8 @@ private void init() {

// Add background ring if its overlap mode
if (mode == renderMode.MODE_OVERLAP) {
backgroundRing = new RingView(getContext(), new RingChartData(1.0f, colorSecondary));
backgroundRing = new RingView(getContext(), new RingChartData(1.0f, colorSecondary, "BackgroundRing-Kwv0Tv1HrB"));
backgroundRing.setShouldDrawLabel(false);
addView(backgroundRing);
}

Expand Down Expand Up @@ -331,6 +334,10 @@ public void setLayoutMode(renderMode mode) {
this.mode = mode;
}

public void showLabels(boolean showLabels) {
this.showLabels = showLabels;
}

/**
* Method to animate rings in indeterminate loading style
*/
Expand Down Expand Up @@ -457,9 +464,13 @@ public enum renderMode {MODE_OVERLAP, MODE_CONCENTRIC}
*/
protected class RingView extends View {
private final RectF RingOval = new RectF();
private int labelAngleOffset;
private float labelSize;
private boolean shouldDrawLabel = true;
private Path labelCirclePath;
private int bgColor;
private Paint colorPaint, labelPaint;
RingChartData data;
int bgColor;
Paint colorPaint;
float headAngle, tailAngle = 0f;

public RingView(Context ctx, RingChartData data) {
Expand All @@ -472,6 +483,7 @@ public RingView(Context ctx, RingChartData data) {
this.colorPaint.setStrokeCap(Paint.Cap.ROUND);
headAngle = (data.value * 360f);
updateBgColor();
updateLabelPaint();
}

public void updateData(RingChartData ringData) {
Expand All @@ -488,19 +500,45 @@ protected void onDraw(Canvas canvas) {
canvas.drawArc(RingOval, 270f, 360f, false, colorPaint);
colorPaint.setColor(data.color);
}

canvas.drawArc(RingOval, 270f + tailAngle, headAngle, false, colorPaint);

// Draw label
if (showLabels && shouldDrawLabel) {
if (mode == renderMode.MODE_CONCENTRIC) {
canvas.rotate(-92, RingOval.centerX(), RingOval.centerY());
} else {
canvas.rotate((tailAngle + headAngle) - 90 - labelAngleOffset, RingOval.centerX(), RingOval.centerY());
}
canvas.drawTextOnPath(data.label, labelCirclePath, 0, (width * 0.4f) / 2, labelPaint);
}
}

@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
RingOval.set(width, width /*[SIC]*/, w - width, h - width);
this.colorPaint.setStrokeWidth(width);
updateLabelPaint();
}

private void updateBgColor() {
this.bgColor = ColorUtils.blendARGB(data.color, Color.BLACK, 0.4f);
bgColor = ColorUtils.setAlphaComponent(bgColor, 30);
}

private void updateLabelPaint() {
labelPaint = new Paint();
labelPaint.setColor(Color.WHITE);
labelPaint.setTextSize(width * 0.7f);
labelCirclePath = new Path();
labelCirclePath.addCircle(RingOval.centerX(), RingOval.centerY(), RingOval.width() / 2f, Path.Direction.CW);
labelSize = labelPaint.measureText(data.label);
labelAngleOffset = (int) ((labelSize / (float) (Math.PI * RingOval.width())) * 360);
}

public void setShouldDrawLabel(boolean shouldDraw) {
this.shouldDrawLabel = shouldDraw;
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,11 @@

import androidx.annotation.FloatRange;

import java.util.UUID;

public class RingChartData implements Comparable<RingChartData> {
public float value;
public int color;
public String label;

/**
* Doughnut ring data item
*
* @param value absolute Integer value
* @param color resolved int colour !!!NOT RESOURCE ID
*/
public RingChartData(@FloatRange(from = 0.0f, to = 1.0f) float value, int color) {
this.value = value;
this.color = color;
this.label = UUID.randomUUID().toString();
}

/**
* Doughnut ring data item
*
Expand Down
15 changes: 13 additions & 2 deletions app/src/main/java/com/taosif7/android/ringchart/MainActivity.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package com.taosif7.android.ringchart;

import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.ToggleButton;
import android.widget.ViewSwitcher;

import androidx.appcompat.app.AppCompatActivity;
Expand Down Expand Up @@ -36,6 +36,7 @@ protected void onCreate(Bundle savedInstanceState) {
MaterialButton BTN_animStart = findViewById(R.id.animationStartBtn);
MaterialButton BTN_animStop = findViewById(R.id.animationStopBtn);
final ViewSwitcher VS_chartHolder = findViewById(R.id.chart_holder);
ToggleButton TB_labels = findViewById(R.id.toggle_labels);

// Prepare Data
dataPoints.put("red", (float) Math.random());
Expand Down Expand Up @@ -94,6 +95,16 @@ public void onClick(View view) {
VS_chartHolder.showNext();
}
});

TB_labels.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
chart_concentric.showLabels(b);
chart_overlap.showLabels(b);
chart_concentric.stopAnimateLoading();
chart_overlap.stopAnimateLoading();
}
});
}

public void changeValue(View view) throws Exception {
Expand Down
34 changes: 31 additions & 3 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clipToPadding="false"
android:paddingBottom="20dp"
android:theme="@style/Theme.MaterialComponents.DayNight">

<RelativeLayout
Expand Down Expand Up @@ -47,8 +49,9 @@
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/chart_holder"
android:gravity="center_horizontal"
android:orientation="horizontal">
android:orientation="horizontal"
android:paddingStart="20dp"
android:paddingEnd="20dp">

<TextView
style="@style/TextAppearance.AppCompat.Title"
Expand All @@ -74,10 +77,35 @@

</LinearLayout>

<TableLayout
<LinearLayout
android:id="@+id/label_controls"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/anim_buttons"
android:orientation="horizontal"
android:paddingStart="20dp"
android:paddingEnd="20dp">

<TextView
style="@style/TextAppearance.AppCompat.Title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="10dp"
android:text="Labels: " />

<androidx.appcompat.widget.AppCompatToggleButton
android:id="@+id/toggle_labels"
style="@style/Widget.MaterialComponents.Button.OutlinedButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true" />

</LinearLayout>

<TableLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/label_controls"
android:layout_marginTop="20dp"
android:orientation="vertical"
android:paddingStart="20dp"
Expand Down

0 comments on commit b24cf90

Please sign in to comment.