// ======================================================================== float measurePrimaryYaxisTickLabels(Paint hash_mark_label_paint) { MinMax y_span = mRenderer.getYPrimaryAxisSpan(); Rect text_bounds_holder = new Rect(); List<Integer> y_label_widths = new ArrayList<Integer>(); for (Double ylabel : MathHelper.getLabels(y_span, mRenderer.getYLabels())) { String y_label = getLabel(ylabel, Axis.Y_AXIS); hash_mark_label_paint.getTextBounds(y_label, 0, y_label.length(), text_bounds_holder); int label_width = text_bounds_holder.width(); y_label_widths.add(label_width); } float max_label_width = Collections.max(y_label_widths); return max_label_width + VERTICAL_AXIS_LINE_LABEL_CLEARANCE; }
// ======================================================================== float measureXaxisTickLabels(Paint hash_mark_label_paint, MinMax x_span) { Rect text_bounds_holder = new Rect(); List<Integer> x_label_heights = new ArrayList<Integer>(); for (Double xlabel : MathHelper.getLabels(x_span, mRenderer.getXLabels())) { String x_label = getLabel(xlabel, Axis.X_AXIS); hash_mark_label_paint.getTextBounds(x_label, 0, x_label.length(), text_bounds_holder); int label_height = text_bounds_holder.height(); x_label_heights.add(label_height); } float max_x_label_height = Collections.max(x_label_heights); return max_x_label_height + VERTICAL_AXIS_LINE_LABEL_CLEARANCE + DEFAULT_HORIZONTAL_AXIS_HASH_MARK_LENGTH; }
protected void drawVerticalAxisLabels( Canvas canvas, Rect frame, Paint hash_mark_label_paint, double yPixelsPerUnit, double ySecondaryPixelsPerUnit, float vertical_axis_hash_mark_width, float vertical_secondary_axis_hash_mark_width) { MinMax y_span = mRenderer.getYPrimaryAxisSpan(); List<Double> yLabels = MathHelper.getLabels(y_span, mRenderer.getYLabels()); List<List<Double>> vertical_axis_datasets = new ArrayList<List<Double>>(); List<MinMax> vertical_axis_spans = new ArrayList<MinMax>(); vertical_axis_datasets.add(yLabels); vertical_axis_spans.add(y_span); if (mRenderer.hasSecondaryYAxis()) { MinMax secondary_span = mRenderer.getYSecondaryAxisSpan(); vertical_axis_datasets.add( MathHelper.getLabels(secondary_span, mRenderer.getSecondaryYLabels())); vertical_axis_spans.add(secondary_span); } double[] vertical_pixels_per_unit = new double[] {yPixelsPerUnit, ySecondaryPixelsPerUnit}; for (int vertical_axis_index = 0; vertical_axis_index < vertical_axis_datasets.size(); vertical_axis_index++) { // List<Double> vertical_labels = vertical_axis_datasets.get(0); List<Double> vertical_labels = vertical_axis_datasets.get(vertical_axis_index); MinMax label_span = vertical_axis_spans.get(vertical_axis_index); hash_mark_label_paint.setTextAlign(label_alignments[vertical_axis_index]); for (int i = 0; i < vertical_labels.size(); i++) { double label = vertical_labels.get(i); float label_value = (float) (frame.bottom - vertical_pixels_per_unit[vertical_axis_index] * (label - label_span.min.doubleValue())); float grid_line_startx, grid_line_stopx, hash_mark_startx, hash_mark_stopx; float label_x_offset; if (vertical_axis_index == 0) { grid_line_startx = frame.left; grid_line_stopx = frame.right; hash_mark_startx = grid_line_startx - vertical_axis_hash_mark_width; hash_mark_stopx = grid_line_startx; label_x_offset = frame.left - VERTICAL_AXIS_LINE_LABEL_CLEARANCE; } else { grid_line_startx = frame.left; grid_line_stopx = frame.right; hash_mark_startx = grid_line_stopx; hash_mark_stopx = grid_line_stopx + vertical_secondary_axis_hash_mark_width; label_x_offset = frame.right + VERTICAL_AXIS_LINE_LABEL_CLEARANCE; } if (mRenderer.isShowLabels()) { hash_mark_label_paint.setColor(grid_line_colors[vertical_axis_index]); // hash_mark_label_paint.setColor(mRenderer.getLabelsColor()); // XXX canvas.drawLine( hash_mark_startx, label_value, hash_mark_stopx, label_value, hash_mark_label_paint); String label_string = getLabel(label, vertical_axis_enums[vertical_axis_index]); hash_mark_label_paint.measureText(label_string); drawText( canvas, getLabel(label, vertical_axis_enums[vertical_axis_index]), label_x_offset, label_value - VERTICAL_AXIS_LABEL_HASH_MARK_CLEARANCE, hash_mark_label_paint, 0); } if (mRenderer.isShowGrid() && mRenderer.isShowGridHorizontalLines()) { hash_mark_label_paint.setColor(grid_line_colors[vertical_axis_index]); hash_mark_label_paint.setPathEffect(grid_line_path_effects[vertical_axis_index]); canvas.drawLine( grid_line_startx, label_value, grid_line_stopx, label_value, hash_mark_label_paint); hash_mark_label_paint.setPathEffect(null); } } } }
/** * The graphical representation of the XY chart. * * @param canvas the canvas to paint to * @param x the top left x value of the view to draw to * @param y the top left y value of the view to draw to * @param width the width of the view to draw to * @param height the height of the view to draw to */ @Override public void draw(Canvas canvas, int width, int height) { TextPaint hash_mark_label_paint = new TextPaint(); hash_mark_label_paint.setTextSize(DEFAULT_HASH_MARK_TEXT_SIZE); hash_mark_label_paint.setTypeface(DefaultRenderer.REGULAR_TEXT_FONT); hash_mark_label_paint.setAntiAlias(getAntiAliased()); boolean rotate = Orientation.VERTICAL.equals(mRenderer.getOrientation()); mScale = (float) (height) / width; mTranslate = Math.abs(width - height) / 2; if (mScale < 1) { mTranslate *= -1; } mCenter = new PointF(width / 2, height / 2); if (rotate) { transform(canvas, mRenderer.getOrientation().getAngle(), false); } MinMax x_span = mRenderer.getXAxisSpan(); float vertical_axis_hash_mark_width = 0; float vertical_secondary_axis_hash_mark_width = 0; float horizontal_axis_label_height = 0; // Measure all y-axis label widths to determine the axis line position if (mRenderer.isShowLabels() && mRenderer.getShowYAxis()) { vertical_axis_hash_mark_width = measurePrimaryYaxisTickLabels(hash_mark_label_paint); if (mRenderer.hasSecondaryYAxis()) { Log.e(TAG, "Has secondary axis!!!"); vertical_secondary_axis_hash_mark_width = measureSecondaryYaxisTickLabels(hash_mark_label_paint); } } if (mRenderer.isShowLabels() && mRenderer.getShowXAxis()) { horizontal_axis_label_height = measureXaxisTickLabels(hash_mark_label_paint, x_span); } Rect frame = new Rect( (int) Math.ceil(vertical_axis_hash_mark_width), 0, width - (int) Math.ceil(vertical_secondary_axis_hash_mark_width), height - (int) horizontal_axis_label_height); double xPixelsPerUnit = getPixelsPerUnit(frame.width(), x_span); double yPixelsPerUnit = getPixelsPerUnit(frame.height(), mRenderer.getYPrimaryAxisSpan()); double ySecondaryPixelsPerUnit = 0; if (mRenderer.hasSecondaryYAxis()) ySecondaryPixelsPerUnit = getPixelsPerUnit(frame.height(), mRenderer.getYSecondaryAxisSpan()); if (mRenderer.isShowLabels() || mRenderer.isShowGrid()) { List<Double> xLabels = MathHelper.getLabels(x_span, mRenderer.getXLabels()); if (mRenderer.isShowLabels()) { hash_mark_label_paint.setColor(mRenderer.getLabelsColor()); hash_mark_label_paint.setTextAlign(Align.CENTER); } drawXLabels( xLabels, mRenderer.getXTextLabelLocations(), canvas, hash_mark_label_paint, frame.left, frame.top, frame.bottom, xPixelsPerUnit, x_span.min.doubleValue(), DEFAULT_HORIZONTAL_AXIS_HASH_MARK_LENGTH, horizontal_axis_label_height - DEFAULT_HORIZONTAL_AXIS_HASH_MARK_LENGTH); drawVerticalAxisLabels( canvas, frame, hash_mark_label_paint, yPixelsPerUnit, ySecondaryPixelsPerUnit, vertical_axis_hash_mark_width, vertical_secondary_axis_hash_mark_width); } // This draws the plot boundaries. drawPlotBoundaries(canvas, frame, hash_mark_label_paint); // This draws the plot boundaries. drawAllDataSets( canvas, frame, hash_mark_label_paint, xPixelsPerUnit, yPixelsPerUnit, ySecondaryPixelsPerUnit); if (rotate) { transform(canvas, mRenderer.getOrientation().getAngle(), true); } }