Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,19 @@
import android.graphics.RectF;
import android.graphics.Typeface;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.os.Environment;
import android.provider.MediaStore.Images;
import androidx.annotation.RequiresApi;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewParent;

import androidx.annotation.RequiresApi;

import com.github.mikephil.charting.animation.ChartAnimator;
import com.github.mikephil.charting.animation.Easing;
import com.github.mikephil.charting.animation.Easing.EasingFunction;
import com.github.mikephil.charting.components.Description;
import com.github.mikephil.charting.components.IMarker;
Expand Down Expand Up @@ -442,18 +442,71 @@ protected void drawDescription(Canvas c) {
mDescPaint.setColor(mDescription.getTextColor());
mDescPaint.setTextAlign(mDescription.getTextAlign());

String descriptionText = mDescription.getText();

float textHeight = mDescPaint.descent() - mDescPaint.ascent();
float textWidth;

if (TextUtils.isEmpty(descriptionText)) {
textWidth = 0f;
} else {
textWidth= mDescPaint.measureText(descriptionText);
}

float x, y;

// if no position specified, draw on default position
if (position == null) {
x = getWidth() - mViewPortHandler.offsetRight() - mDescription.getXOffset();
y = getHeight() - mViewPortHandler.offsetBottom() - mDescription.getYOffset();
//x = getWidth() - mViewPortHandler.offsetRight() - mDescription.getXOffset();
//y = getHeight() - mViewPortHandler.offsetBottom() - mDescription.getYOffset();

float xOffset = mDescription.getXOffset();
float yOffset = mDescription.getYOffset();

final int width = getWidth();
final int height = getHeight();

int gravity = mDescription.getGravity();
if (gravity == -1) {
gravity = Gravity.RIGHT | Gravity.BOTTOM;
}

final int layoutDirection = 0;
final int absoluteGravity = Gravity.getAbsoluteGravity(gravity, layoutDirection);
final int verticalGravity = gravity & Gravity.VERTICAL_GRAVITY_MASK;

switch (absoluteGravity & Gravity.HORIZONTAL_GRAVITY_MASK) {
case Gravity.CENTER_HORIZONTAL:
x = mViewPortHandler.offsetLeft() + (mViewPortHandler.contentWidth() - textWidth) / 2 + xOffset;
break;
case Gravity.RIGHT:
x = width - textWidth - mViewPortHandler.offsetRight() - xOffset;
break;
case Gravity.LEFT:
default:
x = mViewPortHandler.offsetLeft() + xOffset;
}

switch (verticalGravity) {
case Gravity.TOP:
y = mViewPortHandler.offsetTop() + yOffset;
break;
case Gravity.CENTER_VERTICAL:
y = mViewPortHandler.offsetTop() + (mViewPortHandler.contentHeight() - textHeight) / 2 + yOffset;
break;
case Gravity.BOTTOM:
y = height - mViewPortHandler.offsetBottom() - yOffset;
break;
default:
y = mViewPortHandler.offsetTop() + textHeight + yOffset;
}

} else {
x = position.x;
y = position.y;
}

c.drawText(mDescription.getText(), x, y, mDescPaint);
c.drawText(descriptionText, x, y, mDescPaint);
}
}

Expand Down Expand Up @@ -793,7 +846,7 @@ protected void drawMarkers(Canvas canvas) {
int entryIndex = set.getEntryIndex(e);

// make sure entry not null
if (e == null || entryIndex > set.getEntryCount() * mAnimator.getPhaseX())
if (e == null || entryIndex > set.getEntryCount() * mAnimator.getPhaseX() || !e.isDrawValue())
continue;

float[] pos = getMarkerPosition(highlight);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.github.mikephil.charting.components;

import android.graphics.Paint;
import android.view.Gravity;
import android.widget.TextView;

import com.github.mikephil.charting.utils.MPPointF;
import com.github.mikephil.charting.utils.Utils;
Expand All @@ -23,7 +25,10 @@ public class Description extends ComponentBase {
/**
* the alignment of the description text
*/
private Paint.Align mTextAlign = Paint.Align.RIGHT;
private Paint.Align mTextAlign = Paint.Align.LEFT;

/**@see TextView#setGravity(int)*/
private int mGravity = Gravity.RIGHT | Gravity.BOTTOM;

public Description() {
super();
Expand Down Expand Up @@ -92,4 +97,12 @@ public void setTextAlign(Paint.Align align) {
public Paint.Align getTextAlign() {
return mTextAlign;
}

public int getGravity() {
return mGravity;
}

public void setGravity(int gravity) {
this.mGravity = gravity;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ public abstract class BaseEntry {
/** optional icon image */
private Drawable mIcon = null;

/**单独控制是否绘制value*/
private boolean mDrawValue = true;

public BaseEntry() {

}
Expand Down Expand Up @@ -94,4 +97,12 @@ public Object getData() {
public void setData(Object data) {
this.mData = data;
}

public boolean isDrawValue() {
return mDrawValue;
}

public void setDrawValue(boolean mDrawValue) {
this.mDrawValue = mDrawValue;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ protected List<Highlight> buildHighlights(IDataSet set, int dataSetIndex, float
if (entries.size() == 0) {
// Try to find closest x-value and take all entries for that x-value
final Entry closest = set.getEntryForXValue(xVal, Float.NaN, rounding);
if (closest != null)
if (closest != null && closest.isDrawValue())
{
//noinspection unchecked
entries = set.getEntriesForXValue(closest.getX());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ public void drawValues(Canvas c) {
BarEntry entry = dataSet.getEntryForIndex(j / 4);
float val = entry.getY();

if (dataSet.isDrawValuesEnabled()) {
if (dataSet.isDrawValuesEnabled() && entry.isDrawValue()) {
drawValue(c, formatter.getBarLabel(entry), x, val >= 0 ?
(buffer.buffer[j + 1] + posOffset) :
(buffer.buffer[j + 3] + negOffset),
Expand Down Expand Up @@ -322,7 +322,7 @@ public void drawValues(Canvas c) {
|| !mViewPortHandler.isInBoundsLeft(x))
continue;

if (dataSet.isDrawValuesEnabled()) {
if (dataSet.isDrawValuesEnabled() && entry.isDrawValue()) {
drawValue(c, formatter.getBarLabel(entry), x, buffer.buffer[bufferIndex + 1] +
(entry.getY() >= 0 ? posOffset : negOffset),
color);
Expand Down Expand Up @@ -393,7 +393,7 @@ public void drawValues(Canvas c) {
|| !mViewPortHandler.isInBoundsLeft(x))
continue;

if (dataSet.isDrawValuesEnabled()) {
if (dataSet.isDrawValuesEnabled() && entry.isDrawValue()) {
drawValue(c, formatter.getBarStackedLabel(val, entry), x, y, color);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ public void drawValues(Canvas c) {

BubbleEntry entry = dataSet.getEntryForIndex(j / 2 + mXBounds.min);

if (dataSet.isDrawValuesEnabled()) {
if (dataSet.isDrawValuesEnabled() && entry.isDrawValue()) {
drawValue(c, formatter.getBubbleLabel(entry), x, y + (0.5f * lineHeight), valueTextColor);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,7 @@ public void drawValues(Canvas c) {

CandleEntry entry = dataSet.getEntryForIndex(j / 2 + mXBounds.min);

if (dataSet.isDrawValuesEnabled()) {
if (dataSet.isDrawValuesEnabled() && entry.isDrawValue()) {
drawValue(c, formatter.getCandleLabel(entry), x, y - yOffset, dataSet.getValueTextColor(j / 2));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ public void drawValues(Canvas c) {
negOffset = -negOffset - valueTextWidth;
}

if (dataSet.isDrawValuesEnabled()) {
if (dataSet.isDrawValuesEnabled() && entry.isDrawValue()) {
drawValue(c,
formattedValue,
buffer.buffer[j + 2] + (val >= 0 ? posOffset : negOffset),
Expand Down Expand Up @@ -292,7 +292,7 @@ public void drawValues(Canvas c) {
negOffset = -negOffset - valueTextWidth;
}

if (dataSet.isDrawValuesEnabled()) {
if (dataSet.isDrawValuesEnabled() && entry.isDrawValue()) {
drawValue(c, formattedValue,
buffer.buffer[bufferIndex + 2]
+ (entry.getY() >= 0 ? posOffset : negOffset),
Expand Down Expand Up @@ -379,7 +379,7 @@ public void drawValues(Canvas c) {
if (!mViewPortHandler.isInBoundsBottom(y))
continue;

if (dataSet.isDrawValuesEnabled()) {
if (dataSet.isDrawValuesEnabled() && entry.isDrawValue()) {
drawValue(c, formattedValue, x, y + halfTextHeight, color);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,11 @@ protected void drawHorizontalBezier(ILineDataSet dataSet) {
prev = cur;
cur = dataSet.getEntryForIndex(j);

if (!cur.isDrawValue()) {
//不支持分段, 第一个不绘制的Value开始中断绘制.
break;
}

final float cpx = (prev.getX())
+ (cur.getX() - prev.getX()) / 2.0f;

Expand Down Expand Up @@ -217,7 +222,7 @@ protected void drawCubicBezier(ILineDataSet dataSet) {
Entry next = cur;
int nextIndex = -1;

if (cur == null) return;
if (cur == null || !cur.isDrawValue()) return;

// let the spline start
cubicPath.moveTo(cur.getX(), cur.getY() * phaseY);
Expand All @@ -228,6 +233,11 @@ protected void drawCubicBezier(ILineDataSet dataSet) {
prev = cur;
cur = nextIndex == j ? next : dataSet.getEntryForIndex(j);

if (!cur.isDrawValue()) {
//不支持分段, 第一个不绘制的Value开始中断绘制.
break;
}

nextIndex = j + 1 < dataSet.getEntryCount() ? j + 1 : j;
next = dataSet.getEntryForIndex(nextIndex);

Expand Down Expand Up @@ -267,7 +277,19 @@ protected void drawCubicFill(Canvas c, ILineDataSet dataSet, Path spline, Transf
float fillMin = dataSet.getFillFormatter()
.getFillLinePosition(dataSet, mChart);

spline.lineTo(dataSet.getEntryForIndex(bounds.min + bounds.range).getX(), fillMin);
Entry endEntry = null;
Entry curEntry = null;
for (int i = bounds.min; i <= bounds.min + bounds.range; i++) {
curEntry = dataSet.getEntryForIndex(i);
if (!curEntry.isDrawValue()) {
break;
}
endEntry = curEntry;
}

if (endEntry != null) {
spline.lineTo(endEntry.getX(), fillMin);
}
spline.lineTo(dataSet.getEntryForIndex(bounds.min).getX(), fillMin);
spline.close();

Expand Down Expand Up @@ -333,7 +355,7 @@ protected void drawLinear(Canvas c, ILineDataSet dataSet) {
for (int j = mXBounds.min; j < max; j++) {

Entry e = dataSet.getEntryForIndex(j);
if (e == null) continue;
if (e == null || !e.isDrawValue()) continue;

mLineBuffer[0] = e.getX();
mLineBuffer[1] = e.getY() * phaseY;
Expand All @@ -342,7 +364,7 @@ protected void drawLinear(Canvas c, ILineDataSet dataSet) {

e = dataSet.getEntryForIndex(j + 1);

if (e == null) break;
if (e == null || !e.isDrawValue()) break;

if (isDrawSteppedEnabled) {
mLineBuffer[2] = e.getX();
Expand Down Expand Up @@ -398,15 +420,15 @@ protected void drawLinear(Canvas c, ILineDataSet dataSet) {

e1 = dataSet.getEntryForIndex(mXBounds.min);

if (e1 != null) {
if (e1 != null && e1.isDrawValue()) {

int j = 0;
for (int x = mXBounds.min; x <= mXBounds.range + mXBounds.min; x++) {

e1 = dataSet.getEntryForIndex(x == 0 ? 0 : (x - 1));
e2 = dataSet.getEntryForIndex(x);

if (e1 == null || e2 == null) continue;
if (e1 == null || e2 == null || !e2.isDrawValue()) continue;

mLineBuffer[j++] = e1.getX();
mLineBuffer[j++] = e1.getY() * phaseY;
Expand Down Expand Up @@ -516,6 +538,22 @@ private void generateFilledPath(final ILineDataSet dataSet, final int startIndex

currentEntry = dataSet.getEntryForIndex(x);

if (!currentEntry.isDrawValue()) {
//当前不需要绘制value
if (previousEntry.isDrawValue()) {
//上一个需要绘制value
filled.lineTo(previousEntry.getX(), fillMin);
}
previousEntry = currentEntry;
continue;
}

if (!previousEntry.isDrawValue()) {
//上一次不需要绘制Value, 但是这次需要
filled.moveTo(previousEntry.getX(), fillMin);
filled.lineTo(previousEntry.getX(), previousEntry.getY() * phaseY);
}

if (isDrawSteppedEnabled) {
filled.lineTo(currentEntry.getX(), previousEntry.getY() * phaseY);
}
Expand Down Expand Up @@ -581,7 +619,7 @@ public void drawValues(Canvas c) {

Entry entry = dataSet.getEntryForIndex(j / 2 + mXBounds.min);

if (dataSet.isDrawValuesEnabled()) {
if (dataSet.isDrawValuesEnabled() && entry.isDrawValue()) {
drawValue(c, formatter.getPointLabel(entry), x, y - valOffset, dataSet.getValueTextColor(j / 2));
}

Expand Down Expand Up @@ -682,6 +720,10 @@ protected void drawCircles(Canvas c) {

if (e == null) break;

if (!e.isDrawValue()) {
continue;
}

mCirclesBuffer[0] = e.getX();
mCirclesBuffer[1] = e.getY() * phaseY;

Expand Down
Loading