Java Code Examples for lecho.lib.hellocharts.util.ChartUtils#dp2px()

The following examples show how to use lecho.lib.hellocharts.util.ChartUtils#dp2px() . 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: AbstractChartRenderer.java    From hellocharts-android with Apache License 2.0 6 votes vote down vote up
public AbstractChartRenderer(Context context, Chart chart) {
    this.density = context.getResources().getDisplayMetrics().density;
    this.scaledDensity = context.getResources().getDisplayMetrics().scaledDensity;
    this.chart = chart;
    this.computator = chart.getChartComputator();

    labelMargin = ChartUtils.dp2px(density, DEFAULT_LABEL_MARGIN_DP);
    labelOffset = labelMargin;

    labelPaint.setAntiAlias(true);
    labelPaint.setStyle(Paint.Style.FILL);
    labelPaint.setTextAlign(Align.LEFT);
    labelPaint.setTypeface(Typeface.defaultFromStyle(Typeface.BOLD));
    labelPaint.setColor(Color.WHITE);

    labelBackgroundPaint.setAntiAlias(true);
    labelBackgroundPaint.setStyle(Paint.Style.FILL);
}
 
Example 2
Source File: LineChartRenderer.java    From hellocharts-android with Apache License 2.0 6 votes vote down vote up
public LineChartRenderer(Context context, Chart chart, LineChartDataProvider dataProvider) {
    super(context, chart);
    this.dataProvider = dataProvider;

    touchToleranceMargin = ChartUtils.dp2px(density, DEFAULT_TOUCH_TOLERANCE_MARGIN_DP);

    linePaint.setAntiAlias(true);
    linePaint.setStyle(Paint.Style.STROKE);
    linePaint.setStrokeCap(Cap.ROUND);
    linePaint.setStrokeWidth(ChartUtils.dp2px(density, DEFAULT_LINE_STROKE_WIDTH_DP));

    pointPaint.setAntiAlias(true);
    pointPaint.setStyle(Paint.Style.FILL);

    checkPrecision = ChartUtils.dp2px(density, 2);

}
 
Example 3
Source File: LineChartRenderer.java    From hellocharts-android with Apache License 2.0 6 votes vote down vote up
@Override
public boolean checkTouch(float touchX, float touchY) {
    selectedValue.clear();
    final LineChartData data = dataProvider.getLineChartData();
    int lineIndex = 0;
    for (Line line : data.getLines()) {
        if (checkIfShouldDrawPoints(line)) {
            int pointRadius = ChartUtils.dp2px(density, line.getPointRadius());
            int valueIndex = 0;
            for (PointValue pointValue : line.getValues()) {
                final float rawValueX = computator.computeRawX(pointValue.getX());
                final float rawValueY = computator.computeRawY(pointValue.getY());
                if (isInArea(rawValueX, rawValueY, touchX, touchY, pointRadius + touchToleranceMargin)) {
                    selectedValue.set(lineIndex, valueIndex, SelectedValueType.LINE);
                }
                ++valueIndex;
            }
        }
        ++lineIndex;
    }
    return isTouched();
}
 
Example 4
Source File: LineChartRenderer.java    From hellocharts-android with Apache License 2.0 6 votes vote down vote up
private void drawPoints(Canvas canvas, Line line, int lineIndex, int mode) {
    pointPaint.setColor(line.getPointColor());
    int valueIndex = 0;
    for (PointValue pointValue : line.getValues()) {
        int pointRadius = ChartUtils.dp2px(density, line.getPointRadius());
        final float rawX = computator.computeRawX(pointValue.getX());
        final float rawY = computator.computeRawY(pointValue.getY());
        if (computator.isWithinContentRect(rawX, rawY, checkPrecision)) {
            // Draw points only if they are within contentRectMinusAllMargins, using contentRectMinusAllMargins
            // instead of viewport to avoid some
            // float rounding problems.
            if (MODE_DRAW == mode) {
                drawPoint(canvas, line, pointValue, rawX, rawY, pointRadius);
                if (line.hasLabels()) {
                    drawLabel(canvas, line, pointValue, rawX, rawY, pointRadius + labelOffset);
                }
            } else if (MODE_HIGHLIGHT == mode) {
                highlightPoint(canvas, line, pointValue, rawX, rawY, lineIndex, valueIndex);
            } else {
                throw new IllegalStateException("Cannot process points in mode: " + mode);
            }
        }
        ++valueIndex;
    }
}
 
Example 5
Source File: PieChartRenderer.java    From hellocharts-android with Apache License 2.0 6 votes vote down vote up
public PieChartRenderer(Context context, Chart chart, PieChartDataProvider dataProvider) {
    super(context, chart);
    this.dataProvider = dataProvider;
    touchAdditional = ChartUtils.dp2px(density, DEFAULT_TOUCH_ADDITIONAL_DP);

    slicePaint.setAntiAlias(true);
    slicePaint.setStyle(Paint.Style.FILL);

    centerCirclePaint.setAntiAlias(true);
    centerCirclePaint.setStyle(Paint.Style.FILL);
    centerCirclePaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC));

    centerCircleText1Paint.setAntiAlias(true);
    centerCircleText1Paint.setTextAlign(Align.CENTER);

    centerCircleText2Paint.setAntiAlias(true);
    centerCircleText2Paint.setTextAlign(Align.CENTER);

    separationLinesPaint.setAntiAlias(true);
    separationLinesPaint.setStyle(Paint.Style.STROKE);
    separationLinesPaint.setStrokeCap(Paint.Cap.ROUND);
    separationLinesPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
    separationLinesPaint.setColor(Color.TRANSPARENT);
}
 
Example 6
Source File: CheckableLineChartRenderer.java    From SoloPi with Apache License 2.0 5 votes vote down vote up
private void drawChecked(Canvas canvas) {
    if (checkedValue == null){
        return;
    }
    int lineIndex = checkedValue.getFirstIndex();
    Line line = dataProvider.getLineChartData().getLines().get(lineIndex);
    PointValue pointValue = line.getValues().get(checkedValue.getSecondIndex());
    int pointRadius = ChartUtils.dp2px(density, line.getPointRadius());
    float rawX = computator.computeRawX(pointValue.getX());
    float rawY = computator.computeRawY(pointValue.getY());
    canvas.drawCircle(rawX, rawY, pointRadius, checkedPaint);
}
 
Example 7
Source File: LineChartRenderer.java    From hellocharts-android with Apache License 2.0 5 votes vote down vote up
private int calculateContentRectInternalMargin() {
    int contentAreaMargin = 0;
    final LineChartData data = dataProvider.getLineChartData();
    for (Line line : data.getLines()) {
        if (checkIfShouldDrawPoints(line)) {
            int margin = line.getPointRadius() + DEFAULT_TOUCH_TOLERANCE_MARGIN_DP;
            if (margin > contentAreaMargin) {
                contentAreaMargin = margin;
            }
        }
    }
    return ChartUtils.dp2px(density, contentAreaMargin);
}
 
Example 8
Source File: LineChartRenderer.java    From hellocharts-android with Apache License 2.0 5 votes vote down vote up
private void highlightPoint(Canvas canvas, Line line, PointValue pointValue, float rawX, float rawY, int lineIndex,
                            int valueIndex) {
    if (selectedValue.getFirstIndex() == lineIndex && selectedValue.getSecondIndex() == valueIndex) {
        int pointRadius = ChartUtils.dp2px(density, line.getPointRadius());
        pointPaint.setColor(line.getDarkenColor());
        drawPoint(canvas, line, pointValue, rawX, rawY, pointRadius + touchToleranceMargin);
        if (line.hasLabels() || line.hasLabelsOnlyForSelected()) {
            drawLabel(canvas, line, pointValue, rawX, rawY, pointRadius + labelOffset);
        }
    }
}
 
Example 9
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 10
Source File: AxesRenderer.java    From hellocharts-android with Apache License 2.0 5 votes vote down vote up
public AxesRenderer(Context context, Chart chart) {
    this.chart = chart;
    computator = chart.getChartComputator();
    density = context.getResources().getDisplayMetrics().density;
    scaledDensity = context.getResources().getDisplayMetrics().scaledDensity;
    axisMargin = ChartUtils.dp2px(density, DEFAULT_AXIS_MARGIN_DP);
    for (int position = 0; position < 4; ++position) {
        labelPaintTab[position].setStyle(Paint.Style.FILL);
        labelPaintTab[position].setAntiAlias(true);
        namePaintTab[position].setStyle(Paint.Style.FILL);
        namePaintTab[position].setAntiAlias(true);
        linePaintTab[position].setStyle(Paint.Style.STROKE);
        linePaintTab[position].setAntiAlias(true);
    }
}
 
Example 11
Source File: ColumnChartRenderer.java    From hellocharts-android with Apache License 2.0 5 votes vote down vote up
public ColumnChartRenderer(Context context, Chart chart, ColumnChartDataProvider dataProvider) {
    super(context, chart);
    this.dataProvider = dataProvider;
    subcolumnSpacing = ChartUtils.dp2px(density, DEFAULT_SUBCOLUMN_SPACING_DP);
    touchAdditionalWidth = ChartUtils.dp2px(density, DEFAULT_COLUMN_TOUCH_ADDITIONAL_WIDTH_DP);

    columnPaint.setAntiAlias(true);
    columnPaint.setStyle(Paint.Style.FILL);
    columnPaint.setStrokeCap(Cap.SQUARE);
}
 
Example 12
Source File: BubbleChartRenderer.java    From hellocharts-android with Apache License 2.0 4 votes vote down vote up
private void calculateMaxViewport() {
    float maxZ = Float.MIN_VALUE;
    tempMaximumViewport.set(Float.MAX_VALUE, Float.MIN_VALUE, Float.MIN_VALUE, Float.MAX_VALUE);
    BubbleChartData data = dataProvider.getBubbleChartData();
    // TODO: Optimize.
    for (BubbleValue bubbleValue : data.getValues()) {
        if (Math.abs(bubbleValue.getZ()) > maxZ) {
            maxZ = Math.abs(bubbleValue.getZ());
        }
        if (bubbleValue.getX() < tempMaximumViewport.left) {
            tempMaximumViewport.left = bubbleValue.getX();
        }
        if (bubbleValue.getX() > tempMaximumViewport.right) {
            tempMaximumViewport.right = bubbleValue.getX();
        }
        if (bubbleValue.getY() < tempMaximumViewport.bottom) {
            tempMaximumViewport.bottom = bubbleValue.getY();
        }
        if (bubbleValue.getY() > tempMaximumViewport.top) {
            tempMaximumViewport.top = bubbleValue.getY();
        }
    }

    maxRadius = (float) Math.sqrt(maxZ / Math.PI);

    // Number 4 is determined by trials and errors method, no magic behind it:).
    bubbleScaleX = tempMaximumViewport.width() / (maxRadius * 4);
    if (bubbleScaleX == 0) {
        // case for 0 viewport width.
        bubbleScaleX = 1;
    }

    bubbleScaleY = tempMaximumViewport.height() / (maxRadius * 4);
    if (bubbleScaleY == 0) {
        // case for 0 viewport height.
        bubbleScaleY = 1;
    }

    // For cases when user sets different than 1 bubble scale in BubbleChartData.
    bubbleScaleX *= data.getBubbleScale();
    bubbleScaleY *= data.getBubbleScale();

    // Prevent cutting of bubbles on the edges of chart area.
    tempMaximumViewport.inset(-maxRadius * bubbleScaleX, -maxRadius * bubbleScaleY);

    minRawRadius = ChartUtils.dp2px(density, dataProvider.getBubbleChartData().getMinBubbleRadius());
}
 
Example 13
Source File: BubbleChartRenderer.java    From hellocharts-android with Apache License 2.0 3 votes vote down vote up
public BubbleChartRenderer(Context context, Chart chart, BubbleChartDataProvider dataProvider) {
    super(context, chart);
    this.dataProvider = dataProvider;

    touchAdditional = ChartUtils.dp2px(density, DEFAULT_TOUCH_ADDITIONAL_DP);

    bubblePaint.setAntiAlias(true);
    bubblePaint.setStyle(Paint.Style.FILL);

}