lecho.lib.hellocharts.model.PieChartData Java Examples

The following examples show how to use lecho.lib.hellocharts.model.PieChartData. 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: 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 #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
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 #5
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 #6
Source File: PieChartRenderer.java    From hellocharts-android with Apache License 2.0 6 votes vote down vote up
@Override
public void onChartDataChanged() {
    super.onChartDataChanged();
    final PieChartData data = dataProvider.getPieChartData();
    hasLabelsOutside = data.hasLabelsOutside();
    hasLabels = data.hasLabels();
    hasLabelsOnlyForSelected = data.hasLabelsOnlyForSelected();
    valueFormatter = data.getFormatter();
    hasCenterCircle = data.hasCenterCircle();
    centerCircleScale = data.getCenterCircleScale();
    centerCirclePaint.setColor(data.getCenterCircleColor());
    if (null != data.getCenterText1Typeface()) {
        centerCircleText1Paint.setTypeface(data.getCenterText1Typeface());
    }
    centerCircleText1Paint.setTextSize(ChartUtils.sp2px(scaledDensity, data.getCenterText1FontSize()));
    centerCircleText1Paint.setColor(data.getCenterText1Color());
    centerCircleText1Paint.getFontMetricsInt(centerCircleText1FontMetrics);
    if (null != data.getCenterText2Typeface()) {
        centerCircleText2Paint.setTypeface(data.getCenterText2Typeface());
    }
    centerCircleText2Paint.setTextSize(ChartUtils.sp2px(scaledDensity, data.getCenterText2FontSize()));
    centerCircleText2Paint.setColor(data.getCenterText2Color());
    centerCircleText2Paint.getFontMetricsInt(centerCircleText2FontMetrics);

    onChartViewportChanged();
}
 
Example #7
Source File: PieChartRenderer.java    From hellocharts-android with Apache License 2.0 6 votes vote down vote up
/**
 * Draw center circle with text if {@link PieChartData#hasCenterCircle()} is set true.
 */
private void drawCenterCircle(Canvas canvas) {
    final PieChartData data = dataProvider.getPieChartData();
    final float circleRadius = originCircleOval.width() / 2f;
    final float centerRadius = circleRadius * data.getCenterCircleScale();
    final float centerX = originCircleOval.centerX();
    final float centerY = originCircleOval.centerY();

    canvas.drawCircle(centerX, centerY, centerRadius, centerCirclePaint);

    // Draw center text1 and text2 if not empty.
    if (!TextUtils.isEmpty(data.getCenterText1())) {

        final int text1Height = Math.abs(centerCircleText1FontMetrics.ascent);

        if (!TextUtils.isEmpty(data.getCenterText2())) {
            // Draw text 2 only if text 1 is not empty.
            final int text2Height = Math.abs(centerCircleText2FontMetrics.ascent);
            canvas.drawText(data.getCenterText1(), centerX, centerY - text1Height * 0.2f, centerCircleText1Paint);
            canvas.drawText(data.getCenterText2(), centerX, centerY + text2Height, centerCircleText2Paint);
        } else {
            canvas.drawText(data.getCenterText1(), centerX, centerY + text1Height / 4, centerCircleText1Paint);
        }
    }
}
 
Example #8
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 #9
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 #10
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 #11
Source File: PieChartView.java    From hellocharts-android with Apache License 2.0 5 votes vote down vote up
@Override
public void setPieChartData(PieChartData data) {
    if (BuildConfig.DEBUG) {
        Log.d(TAG, "Setting data for ColumnChartView");
    }

    if (null == data) {
        this.data = PieChartData.generateDummyData();
    } else {
        this.data = data;
    }

    super.onChartDataChange();
}
 
Example #12
Source File: PieChartView.java    From hellocharts-android with Apache License 2.0 5 votes vote down vote up
public PieChartView(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
    pieChartRenderer = new PieChartRenderer(context, this, this);
    touchHandler = new PieChartTouchHandler(context, this);
    setChartRenderer(pieChartRenderer);
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
        this.rotationAnimator = new PieChartRotationAnimatorV8(this);
    } else {
        this.rotationAnimator = new PieChartRotationAnimatorV14(this);
    }
    setPieChartData(PieChartData.generateDummyData());
}
 
Example #13
Source File: ReviewActivity.java    From aptoide-client with GNU General Public License v2.0 5 votes vote down vote up
private void setValue(PieChartData data, int score, TextView label,PieChartView chartView){
    int color = getColorBasedOnScore(score);
    setGraph(chartView, data,color);
    label.setBackgroundColor(color);
    data.getValues().get(0).setTarget(score);
    data.getValues().get(1).setTarget(10-score);
    data.setCenterText1(score + "/10");
}
 
Example #14
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 #15
Source File: PieChartView.java    From hellocharts-android with Apache License 2.0 4 votes vote down vote up
@Override
public PieChartData getPieChartData() {
    return data;
}
 
Example #16
Source File: ReviewActivity.java    From aptoide-client with GNU General Public License v2.0 4 votes vote down vote up
private void init() {
    final View speed_chart = findViewById(R.id.speed_chart);
    final View usability_chart = findViewById(R.id.usability_chart);
    final View addictive_chart = findViewById(R.id.addictive_chart);
    final View stability_chart = findViewById(R.id.stability_chart);

    speedChart = (PieChartView) speed_chart.findViewById(R.id.chart);
    usabilityChart = (PieChartView) usability_chart.findViewById(R.id.chart);
    addictiveChart = (PieChartView) addictive_chart.findViewById(R.id.chart);
    stabilityChart = (PieChartView) stability_chart.findViewById(R.id.chart);

    screenshots = new ArrayList<>();
    screenshots.add((ImageView) speed_chart.findViewById(R.id.screenshot));
    screenshots.add((ImageView) usability_chart.findViewById(R.id.screenshot));
    screenshots.add((ImageView) addictive_chart.findViewById(R.id.screenshot));
    screenshots.add((ImageView) stability_chart.findViewById(R.id.screenshot));

    bigImage = (ImageView) findViewById(R.id.bigImage);

    speedLabel = (TextView) speed_chart.findViewById(R.id.designation);
    usabilityLabel = (TextView) usability_chart.findViewById(R.id.designation);
    addictiveLabel = (TextView) addictive_chart.findViewById(R.id.designation);
    stabilityLabel = (TextView) stability_chart.findViewById(R.id.designation);

    speedLabel.setText(R.string.review_speed);
    usabilityLabel.setText(R.string.review_usability);
    addictiveLabel.setText(R.string.review_addictive);
    stabilityLabel.setText(R.string.review_stability);

    title = (TextView) findViewById(R.id.app_name);
    finalVeredict = (TextView) findViewById(R.id.final_veredict);
    reviewer = (TextView) findViewById(R.id.reviewer);
    rating = (TextView) findViewById(R.id.rating);

    appIcon = (ImageView) findViewById(R.id.app_icon);
    avatar = (ImageView) findViewById(R.id.avatar);

    speedData = new PieChartData();
    usabilityData = new PieChartData();
    addictiveData = new PieChartData();
    stabilityData = new PieChartData();

    prosLabel = (TextView) findViewById(R.id.pros_label);
    consLabel = (TextView) findViewById(R.id.cons_label);
    vername_date = (TextView) findViewById(R.id.vername_date);

    consContainer = (LinearLayout) findViewById(R.id.cons_container);
    prosContainer = (LinearLayout) findViewById(R.id.pros_container);
}
 
Example #17
Source File: PieChartDataProvider.java    From hellocharts-android with Apache License 2.0 votes vote down vote up
public PieChartData getPieChartData(); 
Example #18
Source File: PieChartDataProvider.java    From hellocharts-android with Apache License 2.0 votes vote down vote up
public void setPieChartData(PieChartData data);