lecho.lib.hellocharts.model.SliceValue Java Examples

The following examples show how to use lecho.lib.hellocharts.model.SliceValue. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: NoteInfoModel.java    From Memory-capsule with Apache License 2.0 6 votes vote down vote up
@Override
public List<SliceValue> getPieChartNumberfromData() {//获取饼状图的数据
    List<SliceValue>mlist=new ArrayList<>();
    List<Integer> colors=new ArrayList<>();
    List<String> types=new ArrayList<>();
    types.add(0,"生活");
    types.add(1,"工作");
    types.add(2,"学习");
    types.add(3,"日记");
    types.add(4,"旅行");
    colors.add(0,R.color.colorlive);
    colors.add(1,R.color.colorwork);
    colors.add(2,R.color.colorstudy);
    colors.add(3,R.color.colordiary);
    colors.add(4,R.color.colortravel);
    for (int i=0;i<5;i++){
        SliceValue sliceValue=new SliceValue();
        sliceValue.setColor(colors.get(i));
        sliceValue.setLabel(types.get(i));
        sliceValue.setValue(QueryEveryTypeSumfromDataByType(types.get(i)));
    }
    return mlist;
}
 
Example #2
Source File: ReviewActivity.java    From aptoide-client with GNU General Public License v2.0 6 votes vote down vote up
private void setGraph(PieChartView graph, PieChartData data,int color){
    ArrayList<SliceValue> sliceValues = new ArrayList<>();
    SliceValue sliceValue2 = new SliceValue(1);
    SliceValue sliceValue = new SliceValue(0);

    sliceValues.add(sliceValue);
    sliceValues.add(sliceValue2);

    sliceValue.setColor(color);
    sliceValue2.setColor(getResources().getColor(R.color.dark_custom_gray));
    data.setHasCenterCircle(true);
    data.setCenterCircleColor(Color.BLACK);
    data.setValues(sliceValues);
    data.setCenterText1FontSize(12);
    data.setCenterText1Typeface(Typeface.DEFAULT_BOLD);
    data.setCenterText1Color(Color.WHITE);
    graph.setPieChartData(data);
    graph.setChartRotationEnabled(false);
}
 
Example #3
Source File: BandwidthFragment.java    From ETSMobile-Android2 with Apache License 2.0 6 votes vote down vote up
public void updatePieChart(HashMap<String, Double> map) {
    List<SliceValue> values = new ArrayList<SliceValue>();
    double rest;
    rest = map.get("limit") - map.get("total");
    double upload, download;
    upload = map.get("uploadTot");
    download = map.get("downloadTot");
    //TODO put labels
    values.add(new SliceValue((float) upload).setLabel("Upload : " + String.format("%.2f", upload) + " Go").setColor(Color.rgb(217, 119, 37)));
    values.add(new SliceValue((float) download).setLabel("Download : " + String.format("%.2f", download) + " Go").setColor(Color.rgb(217, 52, 37)));
    values.add(new SliceValue((float) rest).setLabel("Restant : " + String.format("%.2f", rest) + " Go").setColor(Color.rgb(105, 184, 57)));

    data = new PieChartData(values);

    chart.setVisibility(View.VISIBLE);
    data.setHasLabels(true);
    chart.setPieChartData(data);
    progressLayout.setVisibility(View.VISIBLE);
}
 
Example #4
Source File: PieChartRenderer.java    From hellocharts-android with Apache License 2.0 6 votes vote down vote up
/**
 * 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;
}
 
Example #5
Source File: PieChartRenderer.java    From hellocharts-android with Apache License 2.0 6 votes vote down vote up
/**
 * Method draws single slice from lastAngle to lastAngle+angle, if mode = {@link #MODE_HIGHLIGHT} slice will be
 * darken
 * and will have bigger radius.
 */
private void drawSlice(Canvas canvas, SliceValue sliceValue, float lastAngle, float angle, int mode) {
    sliceVector.set((float) (Math.cos(Math.toRadians(lastAngle + angle / 2))),
            (float) (Math.sin(Math.toRadians(lastAngle + angle / 2))));
    normalizeVector(sliceVector);
    drawCircleOval.set(originCircleOval);
    if (MODE_HIGHLIGHT == mode) {
        // Add additional touch feedback by setting bigger radius for that slice and darken color.
        drawCircleOval.inset(-touchAdditional, -touchAdditional);
        slicePaint.setColor(sliceValue.getDarkenColor());
        canvas.drawArc(drawCircleOval, lastAngle, angle, true, slicePaint);
    } else {
        slicePaint.setColor(sliceValue.getColor());
        canvas.drawArc(drawCircleOval, lastAngle, angle, true, slicePaint);
    }
}
 
Example #6
Source File: PieChartRenderer.java    From hellocharts-android with Apache License 2.0 6 votes vote down vote up
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;
    }
}
 
Example #7
Source File: PieChartRenderer.java    From hellocharts-android with Apache License 2.0 6 votes vote down vote up
/**
 * Draw all slices for this PieChart, if mode == {@link #MODE_HIGHLIGHT} currently selected slices will be redrawn
 * and
 * highlighted.
 *
 * @param canvas
 */
private void drawSlices(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() && selectedValue.getFirstIndex() == sliceIndex) {
            drawSlice(canvas, sliceValue, lastAngle, angle, MODE_HIGHLIGHT);
        } else {
            drawSlice(canvas, sliceValue, lastAngle, angle, MODE_DRAW);
        }
        lastAngle += angle;
        ++sliceIndex;
    }
}
 
Example #8
Source File: ViewPagerChartsActivity.java    From hellocharts-android with Apache License 2.0 5 votes vote down vote up
private PieChartData generatePieChartData() {
    int numValues = 6;

    List<SliceValue> values = new ArrayList<SliceValue>();
    for (int i = 0; i < numValues; ++i) {
        values.add(new SliceValue((float) Math.random() * 30 + 15, ChartUtils.pickColor()));
    }

    PieChartData data = new PieChartData(values);
    return data;
}
 
Example #9
Source File: PieChartRenderer.java    From hellocharts-android with Apache License 2.0 5 votes vote down vote up
/**
 * 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());
    }
}
 
Example #10
Source File: PieChartView.java    From hellocharts-android with Apache License 2.0 5 votes vote down vote up
@Override
public void callTouchListener() {
    SelectedValue selectedValue = chartRenderer.getSelectedValue();

    if (selectedValue.isSet()) {
        SliceValue sliceValue = data.getValues().get(selectedValue.getFirstIndex());
        onValueTouchListener.onValueSelected(selectedValue.getFirstIndex(), sliceValue);
    } else {
        onValueTouchListener.onValueDeselected();
    }
}
 
Example #11
Source File: PieChartRenderer.java    From hellocharts-android with Apache License 2.0 5 votes vote down vote up
@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;
    float lastAngle = 0f; // No start angle here, see above
    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();
}
 
Example #12
Source File: PieChartRenderer.java    From hellocharts-android with Apache License 2.0 5 votes vote down vote up
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;
    }
}
 
Example #13
Source File: Prestener_datachart.java    From Memory-capsule with Apache License 2.0 4 votes vote down vote up
@Override
public List<SliceValue> getPieChartNumberfromDatatoActivity() {
    return noteInfoModelImp.getPieChartNumberfromData();
}
 
Example #14
Source File: PieChartActivity.java    From hellocharts-android with Apache License 2.0 4 votes vote down vote up
@Override
public void onValueSelected(int arcIndex, SliceValue value) {
    Toast.makeText(getActivity(), "Selected: " + value, Toast.LENGTH_SHORT).show();
}
 
Example #15
Source File: PieChartActivity.java    From hellocharts-android with Apache License 2.0 4 votes vote down vote up
/**
 * To animate values you have to change targets values and then call {@link Chart#startDataAnimation()}
 * method(don't confuse with View.animate()).
 */
private void prepareDataAnimation() {
    for (SliceValue value : data.getValues()) {
        value.setTarget((float) Math.random() * 30 + 15);
    }
}
 
Example #16
Source File: PieChartRenderer.java    From hellocharts-android with Apache License 2.0 4 votes vote down vote up
private void drawLabel(Canvas canvas, SliceValue sliceValue, float lastAngle, float angle) {
    sliceVector.set((float) (Math.cos(Math.toRadians(lastAngle + angle / 2))),
            (float) (Math.sin(Math.toRadians(lastAngle + angle / 2))));
    normalizeVector(sliceVector);

    final int numChars = valueFormatter.formatChartValue(labelBuffer, sliceValue);

    if (numChars == 0) {
        // No need to draw empty label
        return;
    }

    final float labelWidth = labelPaint.measureText(labelBuffer, labelBuffer.length - numChars, numChars);
    final int labelHeight = Math.abs(fontMetrics.ascent);

    final float centerX = originCircleOval.centerX();
    final float centerY = originCircleOval.centerY();
    final float circleRadius = originCircleOval.width() / 2f;
    final float labelRadius;

    if (hasLabelsOutside) {
        labelRadius = circleRadius * DEFAULT_LABEL_OUTSIDE_RADIUS_FACTOR;
    } else {
        if (hasCenterCircle) {
            labelRadius = circleRadius - (circleRadius - (circleRadius * centerCircleScale)) / 2;
        } else {
            labelRadius = circleRadius * DEFAULT_LABEL_INSIDE_RADIUS_FACTOR;
        }
    }

    final float rawX = labelRadius * sliceVector.x + centerX;
    final float rawY = labelRadius * sliceVector.y + centerY;

    float left;
    float right;
    float top;
    float bottom;

    if (hasLabelsOutside) {
        if (rawX > centerX) {
            // Right half.
            left = rawX + labelMargin;
            right = rawX + labelWidth + labelMargin * 3;
        } else {
            left = rawX - labelWidth - labelMargin * 3;
            right = rawX - labelMargin;
        }

        if (rawY > centerY) {
            // Lower half.
            top = rawY + labelMargin;
            bottom = rawY + labelHeight + labelMargin * 3;
        } else {
            top = rawY - labelHeight - labelMargin * 3;
            bottom = rawY - labelMargin;
        }
    } else {
        left = rawX - labelWidth / 2 - labelMargin;
        right = rawX + labelWidth / 2 + labelMargin;
        top = rawY - labelHeight / 2 - labelMargin;
        bottom = rawY + labelHeight / 2 + labelMargin;
    }

    labelBackgroundRect.set(left, top, right, bottom);
    drawLabelTextAndBackground(canvas, labelBuffer, labelBuffer.length - numChars, numChars,
            sliceValue.getDarkenColor());
}
 
Example #17
Source File: PieChartView.java    From hellocharts-android with Apache License 2.0 4 votes vote down vote up
/**
 * Returns SliceValue that is under given angle, selectedValue (if not null) will be hold slice index.
 */
public SliceValue getValueForAngle(int angle, SelectedValue selectedValue) {
    return pieChartRenderer.getValueForAngle(angle, selectedValue);
}
 
Example #18
Source File: SimplePieChartValueFormatter.java    From hellocharts-android with Apache License 2.0 4 votes vote down vote up
@Override
public int formatChartValue(char[] formattedValue, SliceValue value) {
    return valueFormatterHelper.formatFloatValueWithPrependedAndAppendedText(formattedValue, value.getValue(),
            value.getLabelAsChars());
}
 
Example #19
Source File: DataChartActivity.java    From Memory-capsule with Apache License 2.0 4 votes vote down vote up
private void initPieChart(){//设置饼状图
    pieChartView=new PieChartView(this);
    List<SliceValue> values = new ArrayList<SliceValue>();
    int pieWidth=(int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,400, getResources().getDisplayMetrics());
    int pieHeigth=(int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,400, getResources().getDisplayMetrics());
    RelativeLayout.LayoutParams pieChartParams=new RelativeLayout.LayoutParams(pieWidth,pieHeigth);
    pieChartParams.addRule(RelativeLayout.CENTER_HORIZONTAL);
    pieChartParams.addRule(RelativeLayout.CENTER_VERTICAL);
    for (int i = 0; i < 5; ++i) {
        SliceValue sliceValue = new SliceValue((float) typesNum.get(i), colorData[i]);
        values.add(sliceValue);
    }
    pieChartView.setViewportCalculationEnabled(true);
    pieChartView.setChartRotationEnabled(false);
    pieChartView.setAlpha(0.9f);
    pieChartView.setCircleFillRatio(1f);
    pieChartView.setValueSelectionEnabled(true);
    final PieChartData pieChartData=new PieChartData();
    pieChartData.setHasLabels(true);
    pieChartData.setHasLabelsOnlyForSelected(false);
    pieChartData.setHasLabelsOutside(false);
    pieChartData.setHasCenterCircle(true);
    pieChartData.setCenterCircleColor(Color.WHITE);
    pieChartData.setCenterCircleScale(0.5f);
    pieChartData.setCenterText1Color(integer0);
    pieChartData.setCenterText2Color(Color.BLUE);
    pieChartData.setValues(values);
    pieChartView.setPieChartData(pieChartData);
    pieChartView.setOnValueTouchListener(new PieChartOnValueSelectListener() {
        @Override
        public void onValueSelected(int arcIndex, SliceValue value) {
            pieChartData.setCenterText1Color(integer0);
            pieChartData.setCenterText1FontSize(12);
            pieChartData.setCenterText1(stateChar[arcIndex]);
            pieChartData.setCenterText2Color(integer0);
            pieChartData.setCenterText2FontSize(16);
            pieChartData.setCenterText2(value.getValue()+"("+getCenterStringfromData(arcIndex)+")");
        }
        @Override
        public void onValueDeselected() {

        }
    });
    relativeLayout.addView(pieChartView,pieChartParams);
}
 
Example #20
Source File: DummyPieChartOnValueSelectListener.java    From hellocharts-android with Apache License 2.0 2 votes vote down vote up
@Override
public void onValueSelected(int arcIndex, SliceValue value) {

}
 
Example #21
Source File: PieChartOnValueSelectListener.java    From hellocharts-android with Apache License 2.0 votes vote down vote up
public void onValueSelected(int arcIndex, SliceValue value); 
Example #22
Source File: PieChartValueFormatter.java    From hellocharts-android with Apache License 2.0 votes vote down vote up
public int formatChartValue(char[] formattedValue, SliceValue value); 
Example #23
Source File: NoteInfoModelImp.java    From Memory-capsule with Apache License 2.0 votes vote down vote up
public List<SliceValue> getPieChartNumberfromData(); 
Example #24
Source File: PrestenerImp_datachart.java    From Memory-capsule with Apache License 2.0 votes vote down vote up
public List<SliceValue> getPieChartNumberfromDatatoActivity();