Exemplo n.º 1
0
  /**
   * Highlights the value selected by touch gesture. Unlike highlightValues(...), this generates a
   * callback to the OnChartValueSelectedListener.
   *
   * @param high - the highlight object
   * @param callListener - call the listener
   */
  public void highlightValue(Highlight high, boolean callListener) {

    Entry e = null;

    if (high == null) mIndicesToHighlight = null;
    else {

      if (mLogEnabled) Log.i(LOG_TAG, "Highlighted: " + high.toString());

      e = mData.getEntryForHighlight(high);
      if (e == null || e.getXIndex() != high.getXIndex()) {
        mIndicesToHighlight = null;
        high = null;
      } else {
        // set the indices to highlight
        mIndicesToHighlight = new Highlight[] {high};
      }
    }

    if (callListener && mSelectionListener != null) {

      if (!valuesToHighlight()) mSelectionListener.onNothingSelected();
      else {
        // notify the listener
        mSelectionListener.onValueSelected(e, high.getDataSetIndex(), high);
      }
    }
    // redraw the chart
    invalidate();
  }
Exemplo n.º 2
0
  /** draws all MarkerViews on the highlighted positions */
  protected void drawMarkers(Canvas canvas) {

    // if there is no marker view or drawing marker is disabled
    if (mMarkerView == null || !mDrawMarkerViews || !valuesToHighlight()) return;

    for (int i = 0; i < mIndicesToHighlight.length; i++) {

      Highlight highlight = mIndicesToHighlight[i];
      int xIndex = highlight.getXIndex();
      int dataSetIndex = highlight.getDataSetIndex();

      float deltaX = mXAxis.mAxisRange;

      if (xIndex <= deltaX && xIndex <= deltaX * mAnimator.getPhaseX()) {

        Entry e = mData.getEntryForHighlight(mIndicesToHighlight[i]);

        // make sure entry not null
        if (e == null || e.getXIndex() != mIndicesToHighlight[i].getXIndex()) continue;

        float[] pos = getMarkerPosition(e, highlight);

        // check bounds
        if (!mViewPortHandler.isInBounds(pos[0], pos[1])) continue;

        // callbacks to update the content
        mMarkerView.refreshContent(e, highlight);

        // mMarkerView.measure(MeasureSpec.makeMeasureSpec(0,
        // MeasureSpec.UNSPECIFIED),
        // MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
        // mMarkerView.layout(0, 0, mMarkerView.getMeasuredWidth(),
        // mMarkerView.getMeasuredHeight());
        // mMarkerView.draw(mDrawCanvas, pos[0], pos[1]);

        mMarkerView.measure(
            MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED),
            MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
        mMarkerView.layout(0, 0, mMarkerView.getMeasuredWidth(), mMarkerView.getMeasuredHeight());

        if (pos[1] - mMarkerView.getHeight() <= 0) {
          float y = mMarkerView.getHeight() - pos[1];
          mMarkerView.draw(canvas, pos[0], pos[1] + y);
        } else {
          mMarkerView.draw(canvas, pos[0], pos[1]);
        }
      }
    }
  }
  @Override
  public void drawHighlighted(Canvas c, Highlight[] indices) {

    BubbleData bubbleData = mChart.getBubbleData();

    float phaseX = mAnimator.getPhaseX();
    float phaseY = mAnimator.getPhaseY();

    for (Highlight indice : indices) {

      IBubbleDataSet dataSet = bubbleData.getDataSetByIndex(indice.getDataSetIndex());

      if (dataSet == null || !dataSet.isHighlightEnabled()) continue;

      BubbleEntry entryFrom = dataSet.getEntryForXIndex(mMinX);
      BubbleEntry entryTo = dataSet.getEntryForXIndex(mMaxX);

      int minx = dataSet.getEntryIndex(entryFrom);
      int maxx = Math.min(dataSet.getEntryIndex(entryTo) + 1, dataSet.getEntryCount());

      final BubbleEntry entry = (BubbleEntry) bubbleData.getEntryForHighlight(indice);
      if (entry == null || entry.getXIndex() != indice.getXIndex()) continue;

      Transformer trans = mChart.getTransformer(dataSet.getAxisDependency());

      sizeBuffer[0] = 0f;
      sizeBuffer[2] = 1f;

      trans.pointValuesToPixel(sizeBuffer);

      // calcualte the full width of 1 step on the x-axis
      final float maxBubbleWidth = Math.abs(sizeBuffer[2] - sizeBuffer[0]);
      final float maxBubbleHeight =
          Math.abs(mViewPortHandler.contentBottom() - mViewPortHandler.contentTop());
      final float referenceSize = Math.min(maxBubbleHeight, maxBubbleWidth);

      pointBuffer[0] = (float) (entry.getXIndex() - minx) * phaseX + (float) minx;
      pointBuffer[1] = (float) (entry.getVal()) * phaseY;
      trans.pointValuesToPixel(pointBuffer);

      float shapeHalf = getShapeSize(entry.getSize(), dataSet.getMaxSize(), referenceSize) / 2f;

      if (!mViewPortHandler.isInBoundsTop(pointBuffer[1] + shapeHalf)
          || !mViewPortHandler.isInBoundsBottom(pointBuffer[1] - shapeHalf)) continue;

      if (!mViewPortHandler.isInBoundsLeft(pointBuffer[0] + shapeHalf)) continue;

      if (!mViewPortHandler.isInBoundsRight(pointBuffer[0] - shapeHalf)) break;

      if (indice.getXIndex() < minx || indice.getXIndex() >= maxx) continue;

      final int originalColor = dataSet.getColor(entry.getXIndex());

      Color.RGBToHSV(
          Color.red(originalColor),
          Color.green(originalColor),
          Color.blue(originalColor),
          _hsvBuffer);
      _hsvBuffer[2] *= 0.5f;
      final int color = Color.HSVToColor(Color.alpha(originalColor), _hsvBuffer);

      mHighlightPaint.setColor(color);
      mHighlightPaint.setStrokeWidth(dataSet.getHighlightCircleWidth());
      c.drawCircle(pointBuffer[0], pointBuffer[1], shapeHalf, mHighlightPaint);
    }
  }