Search in sources :

Example 6 with SliceValue

use of lecho.lib.hellocharts.model.SliceValue in project hellocharts-android by lecho.

the class PieChartRenderer method drawSeparationLines.

private void drawSeparationLines(Canvas canvas) {
    final PieChartData data = dataProvider.getPieChartData();
    if (data.getValues().size() < 2) {
        //No need for separation lines for 0 or 1 slices.
        return;
    }
    final int sliceSpacing = ChartUtils.dp2px(density, data.getSlicesSpacing());
    if (sliceSpacing < 1) {
        //No need for separation lines
        return;
    }
    final float sliceScale = 360f / maxSum;
    float lastAngle = rotation;
    final float circleRadius = originCircleOval.width() / 2f;
    separationLinesPaint.setStrokeWidth(sliceSpacing);
    for (SliceValue sliceValue : data.getValues()) {
        final float angle = Math.abs(sliceValue.getValue()) * sliceScale;
        sliceVector.set((float) (Math.cos(Math.toRadians(lastAngle))), (float) (Math.sin(Math.toRadians(lastAngle))));
        normalizeVector(sliceVector);
        float x1 = sliceVector.x * (circleRadius + touchAdditional) + originCircleOval.centerX();
        float y1 = sliceVector.y * (circleRadius + touchAdditional) + originCircleOval.centerY();
        canvas.drawLine(originCircleOval.centerX(), originCircleOval.centerY(), x1, y1, separationLinesPaint);
        lastAngle += angle;
    }
}
Also used : PieChartData(lecho.lib.hellocharts.model.PieChartData) SliceValue(lecho.lib.hellocharts.model.SliceValue) Paint(android.graphics.Paint)

Example 7 with SliceValue

use of lecho.lib.hellocharts.model.SliceValue in project hellocharts-android by lecho.

the class PieChartRenderer method calculateMaxViewport.

/**
     * Viewport is not really important for PieChart, this kind of chart doesn't relay on viewport but uses pixels
     * coordinates instead. This method also calculates sum of all SliceValues.
     */
private void calculateMaxViewport() {
    tempMaximumViewport.set(0, MAX_WIDTH_HEIGHT, MAX_WIDTH_HEIGHT, 0);
    maxSum = 0.0f;
    for (SliceValue sliceValue : dataProvider.getPieChartData().getValues()) {
        maxSum += Math.abs(sliceValue.getValue());
    }
}
Also used : SliceValue(lecho.lib.hellocharts.model.SliceValue)

Example 8 with SliceValue

use of lecho.lib.hellocharts.model.SliceValue in project hellocharts-android by lecho.

the class PieChartRenderer method drawLabels.

public void drawLabels(Canvas canvas) {
    final PieChartData data = dataProvider.getPieChartData();
    final float sliceScale = 360f / maxSum;
    float lastAngle = rotation;
    int sliceIndex = 0;
    for (SliceValue sliceValue : data.getValues()) {
        final float angle = Math.abs(sliceValue.getValue()) * sliceScale;
        if (isTouched()) {
            if (hasLabels) {
                drawLabel(canvas, sliceValue, lastAngle, angle);
            } else if (hasLabelsOnlyForSelected && selectedValue.getFirstIndex() == sliceIndex) {
                drawLabel(canvas, sliceValue, lastAngle, angle);
            }
        } else {
            if (hasLabels) {
                drawLabel(canvas, sliceValue, lastAngle, angle);
            }
        }
        lastAngle += angle;
        ++sliceIndex;
    }
}
Also used : PieChartData(lecho.lib.hellocharts.model.PieChartData) SliceValue(lecho.lib.hellocharts.model.SliceValue) Paint(android.graphics.Paint)

Example 9 with SliceValue

use of lecho.lib.hellocharts.model.SliceValue in project hellocharts-android by lecho.

the class PieChartRenderer method checkTouch.

@Override
public boolean checkTouch(float touchX, float touchY) {
    selectedValue.clear();
    final PieChartData data = dataProvider.getPieChartData();
    final float centerX = originCircleOval.centerX();
    final float centerY = originCircleOval.centerY();
    final float circleRadius = originCircleOval.width() / 2f;
    sliceVector.set(touchX - centerX, touchY - centerY);
    // Check if touch is on circle area, if not return false;
    if (sliceVector.length() > circleRadius + touchAdditional) {
        return false;
    }
    // Check if touch is not in center circle, if yes return false;
    if (data.hasCenterCircle() && sliceVector.length() < circleRadius * data.getCenterCircleScale()) {
        return false;
    }
    // Get touchAngle and align touch 0 degrees with chart 0 degrees, that why I subtracting start angle,
    // adding 360
    // and modulo 360 translates i.e -20 degrees to 340 degrees.
    final float touchAngle = (pointToAngle(touchX, touchY, centerX, centerY) - rotation + 360f) % 360f;
    final float sliceScale = 360f / maxSum;
    // No start angle here, see above
    float lastAngle = 0f;
    int sliceIndex = 0;
    for (SliceValue sliceValue : data.getValues()) {
        final float angle = Math.abs(sliceValue.getValue()) * sliceScale;
        if (touchAngle >= lastAngle) {
            selectedValue.set(sliceIndex, sliceIndex, SelectedValueType.NONE);
        }
        lastAngle += angle;
        ++sliceIndex;
    }
    return isTouched();
}
Also used : PieChartData(lecho.lib.hellocharts.model.PieChartData) SliceValue(lecho.lib.hellocharts.model.SliceValue) Paint(android.graphics.Paint)

Example 10 with SliceValue

use of lecho.lib.hellocharts.model.SliceValue in project hellocharts-android by lecho.

the class PieChartRenderer method getValueForAngle.

/**
     * Returns SliceValue that is under given angle, selectedValue (if not null) will be hold slice index.
     */
public SliceValue getValueForAngle(int angle, SelectedValue selectedValue) {
    final PieChartData data = dataProvider.getPieChartData();
    final float touchAngle = (angle - rotation + 360f) % 360f;
    final float sliceScale = 360f / maxSum;
    float lastAngle = 0f;
    int sliceIndex = 0;
    for (SliceValue sliceValue : data.getValues()) {
        final float tempAngle = Math.abs(sliceValue.getValue()) * sliceScale;
        if (touchAngle >= lastAngle) {
            if (null != selectedValue) {
                selectedValue.set(sliceIndex, sliceIndex, SelectedValueType.NONE);
            }
            return sliceValue;
        }
        lastAngle += tempAngle;
        ++sliceIndex;
    }
    return null;
}
Also used : PieChartData(lecho.lib.hellocharts.model.PieChartData) SliceValue(lecho.lib.hellocharts.model.SliceValue) Paint(android.graphics.Paint)

Aggregations

SliceValue (lecho.lib.hellocharts.model.SliceValue)13 PieChartData (lecho.lib.hellocharts.model.PieChartData)10 Paint (android.graphics.Paint)5 ArrayList (java.util.ArrayList)5 View (android.view.View)4 TextView (android.widget.TextView)4 SelectedValue (lecho.lib.hellocharts.model.SelectedValue)4 PieChartView (lecho.lib.hellocharts.view.PieChartView)4 Snackbar (com.nispok.snackbar.Snackbar)3 Map (java.util.Map)3 TreeMap (java.util.TreeMap)3 PieChartOnValueSelectListener (lecho.lib.hellocharts.listener.PieChartOnValueSelectListener)3 Column (lecho.lib.hellocharts.model.Column)3 ColumnChartData (lecho.lib.hellocharts.model.ColumnChartData)3 SubcolumnValue (lecho.lib.hellocharts.model.SubcolumnValue)3 ColumnChartView (lecho.lib.hellocharts.view.ColumnChartView)3 MaterialIconView (net.steamcrafted.materialiconlib.MaterialIconView)3 FragmentActivity (android.support.v4.app.FragmentActivity)2 RecyclerView (android.support.v7.widget.RecyclerView)2 ImageView (android.widget.ImageView)2