目录
第8节.Setting Colors(MPAndroidChart中文翻译)
第9节.Formatting Data Values (ValueFormatter)(MPAndroidChart中文翻译)
第10节-Formatting Axis Values (AxisValueFormatter)(MPAndroidChart中文翻译)
第11节.General Settings & Styling(MPAndroidChart中文翻译)
第12节.Specific Settings & Styling(MPAndroidChart中文翻译)
第13节.Legend(MPAndroidChart中文翻译)
第14节.Dynamic & Realtime Data(MPAndroidChart中文翻译)
第15节. Modifying the Viewport(MPAndroidChart中文翻译)
第16节.Animations(MPAndroidChart中文翻译)
第17节. MarkerView (Popup View)(MPAndroidChart中文翻译)
第18节. The ChartData class(MPAndroidChart中文翻译)
第19节. ChartData subclasses(MPAndroidChart中文翻译)
第20节. The DataSet class (general DataSet styling)(MPAndroidChart中文翻译)
第21节. DataSet subclasses (specific DataSet styling)(MPAndroidChart中文翻译)
第22节. The ViewPortHandler(MPAndroidChart中文翻译)
第23节. Customizing the Fill-Line-Position (FillFormatter)(MPAndroidChart中文翻译)
第24节. Proguard(MPAndroidChart中文翻译)
第25节. Realm.io mobile database(MPAndroidChart中文翻译)
第26节. Creating your own (custom) DataSets(MPAndroidChart中文翻译)
第27节. Miscellaneous (more useful stuff)(MPAndroidChart中文翻译)
v3.0.0版本后出现,图表中的标记由IMarker接口表示.
IMarker interface (IMarker接口)
该接口允许你创建图表中显示突出条目时显示的自定义标识.该接口提供的方法如下所示:
public interface IMarker {
/**
* @return The desired (general) offset you wish the IMarker to have on the x- and y-axis.
* By returning x: -(width / 2) you will center the IMarker horizontally.
* By returning y: -(height / 2) you will center the IMarker vertically.
*/
MPPointF getOffset();
/**
* @return The offset for drawing at the specific `point`. This allows conditional adjusting of the Marker position.
* If you have no adjustments to make, return getOffset().
*
* @param posX This is the X position at which the marker wants to be drawn.
* You can adjust the offset conditionally based on this argument.
* @param posY This is the X position at which the marker wants to be drawn.
* You can adjust the offset conditionally based on this argument.
*/
MPPointF getOffsetForDrawingAtPos(float posX, float posY);
/**
* This method enables a specified custom IMarker to update it's content every time the IMarker is redrawn.
*
* @param e The Entry the IMarker belongs to. This can also be any subclass of Entry, like BarEntry or
* CandleEntry, simply cast it at runtime.
* @param highlight The highlight object contains information about the highlighted value such as it's dataset-index, the
* selected range or stack-index (only stacked bar entries).
*/
void refreshContent(Entry e, Highlight highlight);
/**
* Draws the IMarker on the given position on the screen with the given Canvas object.
*
* @param canvas
* @param posX
* @param posY
*/
void draw(Canvas canvas, float posX, float posY);
}
Creating a MarkerView (创建一个标识视图)
为了创建自定义的标记视图,你需要创建一个类并实现IMarker接口:
public class YourMarkerView implements IMarker { ... }
接口中提供的方法的返回值取决于个人需求.为了更好的理解,请看看上面展示的方法文档.
除了实现IMarker接口之外,你可以创建自己的类并通过下面提到的预定义标记继承它.这种方法更简单,不要求实现IMarker接口所有的方法.只有特殊的方法需要重写和自定义的.最终要的是重写refreshContent(...)方法来调整标记绘制的数据.一个简单的例子可能是这样的:
public class YourMarkerView extends MarkerView {
private TextView tvContent;
public MyMarkerView(Context context, int layoutResource) {
super(context, layoutResource);
// find your layout components
tvContent = (TextView) findViewById(R.id.tvContent);
}
// callbacks everytime the MarkerView is redrawn, can be used to update the
// content (user-interface)
@Override
public void refreshContent(Entry e, Highlight highlight) {
tvContent.setText("" + e.getY());
// this will perform necessary layouting
super.refreshContent(e, highlight);
}
private MPPointF mOffset;
@Override
public MPPointF getOffset() {
if(mOffset == null) {
// center the marker horizontally and vertically
mOffset = new MPPointF(-(getWidth() / 2), -getHeight());
}
return mOffset;
}
}
Getting / Setting the Marker(获取/设置标记)
为了给图表设置自己的标记,请使用setMarker(...)方法:
IMarker marker = new YourMarkerView();
chart.setMarker(marker);
为了获取图表中设置的标记,使用geMarker()方法:
IMarker marker = chart.getMarker();
Predefined Markers (预定的标记)
除了创建你自己的标记视图,为了更简单更快的使用,该库提供了一些自定义标记.这些标记包括:
- MarkerView: 基本标记.允许提供一个资源布局在图表表面上显示标记.集成该类并重写refreshContent(...)方法来调整标记数据.
- MarkerImage: 一个绘制图片的标记.允许提供一个图片资源在图表表面上显示标记.集成该类并重写refreshContent(...)方法来调整标记数据.
Legacy MarkerView (传统标记视图)
在v3.0.0之前的版本中,MarkerView类负责在图表中突出显示的位置绘制标记.关于该类的详细信息请访问旧的MarkerView wiki page.