Skip to content

Instantly share code, notes, and snippets.

@malikkurosaki
Created October 1, 2019 06:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save malikkurosaki/01fb28c91427031d3f4a154bfbdf3097 to your computer and use it in GitHub Desktop.
Save malikkurosaki/01fb28c91427031d3f4a154bfbdf3097 to your computer and use it in GitHub Desktop.
membuat chart

Membuat chart

package probus.malikkurosaki.probussystem;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.View;

import java.util.Random;

import androidx.annotation.ColorInt;

public class Helper_pie_chart extends View {

    private Paint slicePaint;
    private int[] sliceClrs = { Color.GREEN, Color.BLUE, Color.RED, Color.YELLOW };
    private RectF rectf; // Our box
    private float[] datapoints; // Our values

    public Helper_pie_chart(Context context, AttributeSet attrs) {
        super(context, attrs);
        slicePaint = new Paint();
        slicePaint.setAntiAlias(true);
        slicePaint.setDither(true);
        slicePaint.setStyle(Paint.Style.FILL);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        if (this.datapoints != null) {
            int startTop = 0;
            int startLeft = 0;
            int endBottom = getWidth();
            int endRight = endBottom; // To make this an equal square
            // Create the box
             // Creating the box

            /*float[] scaledValues = scale(); // Get the scaled values
            float sliceStartPoint = 0;
            for (int i = 0; i < scaledValues.length; i++) {
                slicePaint.setColor(sliceClrs[i]);
                canvas.drawArc(rectf, sliceStartPoint, scaledValues[i], true, slicePaint); // Draw slice
                sliceStartPoint += scaledValues[i]; // Update starting point of the next slice
            }*/

            rectf = new RectF(0, 0, endRight, endBottom); // Creating the box
            float[] scaledValues2 = scale(); // Get the scaled values
            float sliceStartPoint2 = 0;

            slicePaint.setColor(Color.LTGRAY);
            canvas.drawArc(rectf, sliceStartPoint2, 360, true, slicePaint);

            rectf = new RectF(startLeft, startTop, endRight, endBottom);
            int[] warna = {Color.YELLOW,Color.BLUE,Color.GREEN,Color.BLUE};
            for (int i = 0; i < scaledValues2.length; i++) {
                slicePaint.setColor(warna[i]);
                canvas.drawArc(rectf, sliceStartPoint2, scaledValues2[i], true, slicePaint); // Draw slice
                sliceStartPoint2 += scaledValues2[i]+2; // Update starting point of the next slice
            }
        }
    }

    public void setDataPoints(float[] datapoints) {
        this.datapoints = datapoints;
        invalidate(); // Tells the chart to redraw itself
    }

    public void setSliceClrs(int[] sliceClrs) {
        this.sliceClrs = sliceClrs;
        invalidate();
    }

    private float[] scale() {
        float[] scaledValues = new float[this.datapoints.length];
        float total = getTotal(); // Total all values supplied to the chart
        for (int i = 0; i < this.datapoints.length; i++) {
            scaledValues[i] = (this.datapoints[i] / total) * 352; // Scale each value
        }
        return scaledValues;
    }

    private float getTotal() {
        float total = 0;
        for (float val : this.datapoints)
            total += val;
        return total;
    }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment