/** * Returns the position of 0 value on chart. * * @return Position of 0 value on chart */ public float getZeroPosition(){ if(mOrientation == Orientation.VERTICAL) return verController.parsePos(0, 0); else return horController.parsePos(0, 0); }
/** * Mandatory horizontal border when necessary (ex: BarCharts) * Sets the attribute depending on the chart's orientation. * e.g. If orientation is VERTICAL it means that this attribute must be handled * by horizontal axis and not the vertical axis. */ void setMandatoryBorderSpacing(){ if(mOrientation == Orientation.VERTICAL) horController.mandatoryBorderSpacing = 1; else verController.mandatoryBorderSpacing = 1; }
/** * * @param minValue The minimum value that Y axis will have as a label * @param maxValue The maximum value that Y axis will have as a label * @return {@link com.db.chart.view.ChartView} self-reference. */ public ChartView setAxisBorderValues(int minValue, int maxValue){ if(mOrientation == Orientation.VERTICAL) verController.setBorderValues(minValue, maxValue); else horController.setBorderValues(minValue, maxValue); return this; }
/** * * @param spacing Spacing between top of the chart and the first label * @return {@link com.db.chart.view.ChartView} self-reference. */ public ChartView setTopSpacing(float spacing){ if(mOrientation == Orientation.VERTICAL) verController.topSpacing = spacing; else horController.borderSpacing = spacing; return this; }
/** * Set the format to be added to Y labels. * * @param format Format to be applied * @return {@link com.db.chart.view.ChartView} self-reference. */ public ChartView setLabelsFormat(DecimalFormat format){ if(mOrientation == Orientation.VERTICAL) verController.labelFormat = format; else horController.labelFormat = format; return this; }
/** * Sets the chart's orientation. * * @param orien Orientation.HORIZONTAL | Orientation.VERTICAL */ void setOrientation(Orientation orien){ mOrientation = orien; if(mOrientation == Orientation.VERTICAL) { verController.handleValues = true; }else{ horController.handleValues = true; } }
@SuppressLint("NewApi") @Override public boolean onPreDraw() { ChartView.this.getViewTreeObserver().removeOnPreDrawListener(this); style.init(); // Define chart frame mChartTop = getPaddingTop() + verController.getLabelHeight()/2; mChartBottom = getMeasuredHeight() - getPaddingBottom()-LABELTEXTMAXLINE*(int)(style.labelsPaint.descent()-style.labelsPaint.ascent()); mChartLeft = getPaddingLeft(); mChartRight = getMeasuredWidth() - getPaddingRight(); // Initialize controllers now that we have the measures verController.init(); if(mHasThresholdValue) { mThresholdStartValue = verController.parsePos(0, mThresholdStartValue); mThresholdEndValue = verController.parsePos(0, mThresholdEndValue); } // Mandatory: X axis after Y axis! horController.init(); // Process data to define screen positions digestData(); // Tells view to execute code before starting drawing onPreDrawChart(data); // Define regions mRegions = defineRegions(data); // Prepares the animation if needed and gets the first dump // of data to be drawn if(mAnim != null) data = mAnim.prepareEnterAnimation(ChartView.this); if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.HONEYCOMB) ChartView.this.setLayerType(LAYER_TYPE_SOFTWARE, null); return mReadyToDraw = true; }
/** * Convert {@link ChartEntry} values into screen points. */ private void digestData() { int nEntries = data.get(0).size(); for(ChartSet set: data){ for(int i = 0; i < nEntries; i++){ set.getEntry(i) .setCoordinates(horController.parsePos(i, set.getValue(i)), verController.parsePos(i, set.getValue(i))); } } }
/** * A step is seen as the step to be defined between 2 labels. * As an example a step of 2 with a max label value of 6 will end * up with {0, 2, 4, 6} as labels. * * @param step (real) value distance from every label * @return {@link com.db.chart.view.ChartView} self-reference. */ public ChartView setStep(int step){ if(step <= 0) throw new IllegalArgumentException("Step can't be lower or equal to 0"); if(mOrientation == Orientation.VERTICAL) verController.step = step; else horController.step = step; return this; }
@Override protected void onDraw(Canvas canvas) { mIsDrawing = true; super.onDraw(canvas); if(mReadyToDraw){ //long time = System.currentTimeMillis(); // Draw grid if(mGridType == GridType.FULL || mGridType == GridType.VERTICAL) drawVerticalGrid(canvas); if(mGridType == GridType.FULL || mGridType == GridType.HORIZONTAL) drawHorizontalGrid(canvas); // Draw Axis Y verController.draw(canvas); // Draw threshold if (mHasThresholdValue) drawThreshold(canvas, getInnerChartLeft(), mThresholdStartValue, getInnerChartRight(), mThresholdEndValue); if (mHasThresholdLabel) drawThreshold(canvas, data.get(0).getEntry(mThresholdStartLabel).getX(), getInnerChartTop(), data.get(0).getEntry(mThresholdEndLabel).getX(), getInnerChartBottom()); // Draw data if(!data.isEmpty()) onDrawChart(canvas, data); // Draw axis X horController.draw(canvas); //System.out.println("Time drawing "+(System.currentTimeMillis() - time)); } mIsDrawing = false; }
/** * Method not expected to be used often. More for testing. * Resets chart state to insert new configuration. */ public void reset(){ if(mAnim != null && mAnim.isPlaying()) mAnim.cancel(); init(); if(horController.mandatoryBorderSpacing != 0) { horController.reset(); } if(verController.mandatoryBorderSpacing != 0) { verController.reset(); } mHasThresholdLabel = false; mHasThresholdValue = false; style.thresholdPaint = null; style.gridPaint = null; }
/** * Inner Chart refers only to the area where chart data will be draw, * excluding labels, axis, etc. * * @return Position of the inner left side of the chart */ public float getInnerChartLeft(){ return verController.getInnerChartLeft(); }
/** * Inner Chart refers only to the area where chart data will be draw, * excluding labels, axis, etc. * * @return Position of the inner bottom side of the chart */ public float getInnerChartBottom(){ return verController.getInnerChartBottom(); }
/** * Set spacing between Labels and Axis. Will be applied to both X and Y. * * @param spacing Spacing between labels and axis * @return {@link com.db.chart.view.ChartView} self-reference. */ public ChartView setAxisLabelsSpacing(float spacing){ horController.setAxisLabelsSpacing(spacing); verController.setAxisLabelsSpacing(spacing); return this; }
/** * Show/Hide Y axis. * * @param bool If true axis won't be visible * @return {@link com.db.chart.view.ChartView} self-reference. */ public ChartView setYAxis(boolean bool){ verController.hasAxis = bool; return this; }
/** * Show/Hide Y labels and respective axis. * * @param position NONE - No labels * OUTSIDE - Labels will be positioned outside the chart * INSIDE - Labels will be positioned inside the chart * @return {@link com.db.chart.view.ChartView} self-reference. */ public ChartView setYLabels(YController.LabelPosition position){ verController.labelsPositioning = position; return this; }