17. MarkerView (Popup View)
Since release v3.0.0, markers (popup views) in the chart are represented by the IMarker
interface.
- 从版本v3.0.0开始,图表中的标记(弹出视图)由IMarker接口表示。
IMarker interface
This interface allows you to create custom marker views displayed at highlighted entries in your chart. The methods provided by the interface look as follows:
- 此界面允许您创建显示在图表中突出显示的条目的自定义标记视图。 界面提供的方法如下:
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
- 创建MarkerView
In order to create your custom marker view, you need to create a new class that implements the IMarker interface:
- 要创建自定义标记视图,您需要创建一个实现IMarker接口的新类:
public class YourMarkerView implements IMarker { ... }
What you return from the methods provided by the interface depends on your personal requirements. Have a look at the above shown documentation of the methods for better understanding.
- 您从界面提供的方法返回的内容取决于您的个人要求。 看看上面显示的方法文档,以便更好地理解。
I addition to implementing the IMarker interface, you can create your own class and extend it by one of the predefined markers mentioned below. This approach is easier and does not require to implement all methods provided by the IMarker interface. Only particular methods can be overridden and customised. The most important thing is then to override the refreshContent(...) method to adjust the data drawn by the marker. A simple example could look like this:
- 除了实现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
- 获取/设置标记
In order to set your marker to the chart, use the setMarker(...)
method:
- 要将标记设置为图表,请使用
setMarker(...)
方法:
IMarker marker = new YourMarkerView();
chart.setMarker(marker);
To access an existing marker set to the chart, use the getMarker()
method:
- 要访问图表的现有标记集,请使用
getMarker()
方法:
IMarker marker = chart.getMarker();
Predefined Markers
- 预定义标记
Besides creating your own custom marker view, this library provides a few predefined markers for easier and faster use. Those markers include:
- 除了创建自己的自定义标记视图外,此库还提供了一些预定义标记,以便更轻松,更快速地使 这些标记包括:
-
MarkerView: The basic marker. Allows to provide a layout resource that is rendered on the chart surface representing the marker. Extend this class and override the
refreshContent(...)
method for adjusting marker data.- MarkerView:基本标记。 允许提供在表示标记的图表表面上呈现的布局资源。 扩展此类并覆盖refreshContent(...)方法以调整标记数据。
-
MarkerImage: A marker for drawing images. Allows to provide a drawable resource that is rendered on the chart surface representing the marker. Extend this class and override the
refreshContent(...)
method for adjusting marker data.- MarkerImage:绘制图像的标记。 允许提供在表示标记的图表表面上呈现的可绘制资源。 扩展此类并覆盖refreshContent(...)方法以调整标记数据。
Legacy MarkerView
- 传统的MarkerView
In versions prior to v3.0.0, the MarkerView
class was responsible for drawing markers at highlighted positions in the chart. For detailed information regarding this class, please visit the old MarkerView wiki page.
- 在v3.0.0之前的版本中,MarkerView类负责在图表中突出显示的位置绘制标记。 有关此课程的详细信息,请访问旧的MarkerView维基页面。
18. The ChartData class
This wiki entry is intended to provide better insight into the data model behind MPAndroidChart.
- 此Wiki条目旨在更好地了解MPAndroidChart背后的数据模型。
The ChartData
class is the baseclass of all data classes (subclasses), like LineData
, BarData
, ... and so on. It is used to provide data for the Chart
via the setData(...)
method of the chart.
- ChartData类是所有数据类(子类)的基类,如LineData,BarData,...等等。 它用于通过图表的setData(...)方法为Chart提供数据。
The following mentioned methods are implemented in the ChartData
class and can therefore be used for all subclasses.
- 以下提到的方法在ChartData类中实现,因此可以用于所有子类。
Styling data
- 样式数据
-
setValueTextColor(int color)
: Sets the color of the value-text (color in which the value-labels are drawn) for allDataSets
this data object contains.- setValueTextColor(int color):设置此数据对象包含的所有DataSet的value-text(绘制值标签的颜色)的颜色。
-
setValueTextColors(List colors)
: Sets a list of colors to be used as value colors.- setValueTextColors(List colors):设置要用作值颜色的颜色列表。
-
setValueTextSize(float size)
: Sets the size (in dp) of the value-text for allDataSets
this data object contains.- setValueTextSize(float size):设置此数据对象包含的所有DataSet的值文本的大小(以dp为单位)。
-
setValueTypeface(Typeface tf)
: Sets theTypeface
for all value-labels for allDataSets
this data object contains.- setValueTypeface(Typeface tf):为所有数据对象包含的DataSet的所有值标签设置字体。
-
setValueFormatter(ValueFormatter f)
: Sets a customValueFormatter
for allDataSets
this data object contains, more on theValueFormatter
here.- setValueFormatter(ValueFormatter f):为包含的所有DataSet设置一个自定义ValueFormatter,更多关于ValueFormatter的信息。
-
setDrawValues(boolean enabled)
: Enables / disables drawing values (value-text) for allDataSets
this data object contains.- setDrawValues(boolean enabled):启用/禁用此数据对象包含的所有DataSet的绘图值(值 - 文本)。
Getters / Convenience
- 吸气/方便
-
getDataSetByIndex(int index)
: Returns theDataSet
object at the given index in the data-objectsDataSet
list.- getDataSetByIndex(int index):返回数据对象DataSet列表中给定索引处的DataSet对象。
-
contains(Entry entry)
: Checks if this data object contains the specified Entry. Returns true if so, false if not. NOTE: Performance is pretty bad on this one, do not over-use in performance critical situations.- contains(条目条目):检查此数据对象是否包含指定的条目。 如果是,则返回true,否则返回false。 注意:在这一点上性能非常糟糕,在性能危急情况下不要过度使用。
-
contains(T dataSet)
: Returns true if this data object contains the providedDataSet
, false if not.
吸气/方便- contains(T dataSet):如果此数据对象包含提供的DataSet,则返回true,否则返回false。
Clearing
- 空地
-
clearValues()
: Clears the data object of allDataSet
objects and thereby allEntries
. Does not remove the provided x-values.- clearValues():清除所有DataSet对象的数据对象,从而清除所有条目。 不删除提供的x值。
Highlighting
- 突出
-
setHighlightEnabled(boolean enabled)
: Set this to true to allow highlighting via touch for thisChartData
object and all underlyingDataSets
.- setHighlightEnabled(boolean enabled):将此项设置为true以允许通过触摸突出显示此ChartData对象和所有基础DataSet。
-
setDrawVerticalHighlightIndicator(boolean enabled)
: Enables / disables the vertical highlight-indicator-line. If disabled, the indicator is not drawn.- setDrawVerticalHighlightIndicator(boolean enabled):启用/禁用垂直高亮显示指示符行。 如果禁用,则不绘制指标。
-
setDrawHorizontalHighlightIndicator(boolean enabled)
: Enables / disables the horizontal highlight-indicator-line. If disabled, the indicator is not drawn.- setDrawHorizontalHighlightIndicator(boolean enabled):启用/禁用水平高亮显示指示符行。 如果禁用,则不绘制指标。
动态数据
- setDrawHorizontalHighlightIndicator(boolean enabled):启用/禁用水平高亮显示指示符行。 如果禁用,则不绘制指标。
Dynamic Data
-
notifyDataChanged()
: Lets the data object know it's underlying data has changed and performs all necessary recalculations.- notifyDataChanged():让数据对象知道它的基础数据已经改变并执行所有必要的重新计算。
For ways of adding and removing data from an existing data object, please visit the dynamic & realtime data section.
- 有关在现有数据对象中添加和删除数据的方法,请访问动态和实时数据部分。
19. ChartData subclasses
This wiki entry focuses on the subclasses of the ChartData class. All other subclasses of ChartData not mentioned here do not provide any specific enhancements.
- 此wiki条目侧重于ChartData类的子类。 此处未提及的ChartData的所有其他子类不提供任何特定的增强功能。
BarData (class BarData)
-
setGroupSpace(float percent): Sets the space between groups of bars of different datasets in percent of the total width of one bar. 100 = space is exactly one bar width, default: 80
- setGroupSpace(float percent):设置不同数据集的条形组之间的间距,以百分之一的总宽度为单位。 100 =空格正好是一个条宽,默认值:80
-
isGrouped(): Returns true if this data object is grouped (consists of more than 1 DataSet), false if it is not.
- isGrouped():如果此数据对象已分组(由多个DataSet组成),则返回true,否则返回false。
ScatterData (class ScatterData)
-
getGreatestShapeSize(): Returns the largest shape-size across all ScatterDataSets this data object contains.
PieData (class PieData)- getGreatestShapeSize():返回此数据对象包含的所有ScatterDataSets中的最大shape-size。
PieData(类PieData)
- getGreatestShapeSize():返回此数据对象包含的所有ScatterDataSets中的最大shape-size。
-
getDataSet(): Returns the PieDataSet object that is set for this data object. PieData objects cannot contain multiple PieDataSets.
setDataSet(PieDataSet set): Sets the PieDataSet this data object should represent.- getDataSet():返回为此数据对象设置的PieDataSet对象。 PieData对象不能包含多个PieDataSet。
setDataSet(PieDataSet set):设置此数据对象应表示的PieDataSet。
- getDataSet():返回为此数据对象设置的PieDataSet对象。 PieData对象不能包含多个PieDataSet。
BubbleData (class BubbleData)
- setHighlightCircleWidth(float width): Sets the width of the circle that surrounds the bubble when in highlighted state for all BubbleDataSet objects this data object contains, in dp.
- setHighlightCircleWidth(float width):设置此数据对象包含的所有BubbleDataSet对象在突出显示状态时围绕气泡的圆的宽度,以dp为单位。
CombinedData (class CombinedData)
This data object is designed to contain instances of all other data objects. Use the setData(...) methods to provide the data for this object. This is used for the CombinedChart only.
- 此数据对象旨在包含所有其他数据对象的实例。 使用setData(...)方法为此对象提供数据。 这仅用于CombinedChart。
This is what it looks like internally:
- 这就是它内部的样子:
20. The DataSet class (general DataSet styling)
The DataSet
class is the baseclass of all data-set classes (subclasses), like LineDataSet
, BarDataSet
, ... and so on.
- DataSet类是所有数据集类(子类)的基类,如LineDataSet,BarDataSet,...等等。
The DataSet class represents one group or type of entries (Entry) in the Chart
that belong together. It is designed to logically separate different groups of values inside the Chart
(e.g. the values for a specific line in the LineChart
, or the values of a specific group of bars in the BarChart
).
- DataSet类表示图表中属于一起的一个组或条目类型(条目)。 它旨在逻辑上分隔图表中不同的值组(例如,LineChart中特定行的值,或BarChart中特定条形组的值)。
The following mentioned methods are implemented in the DataSet
class and can therefore be used for all subclasses.
- 以下提到的方法在DataSet类中实现,因此可以用于所有子类。
Styling data
- 样式数据
-
setValueTextColor(int color)
: Sets the color of the value-text (color in which the value-labels are drawn) for thisDataSet
object.- setValueTextColor(int color):设置此DataSet对象的value-text(绘制值标签的颜色)的颜色。
-
setValueTextColors(List colors)
: Sets a list of colors to be used as value colors.- setValueTextColors(List colors):设置要用作值颜色的颜色列表。
-
setValueTextSize(float size)
: Sets the size (in dp) of the value-text for thisDataSet
object.- setValueTextSize(float size):设置此DataSet对象的value-text的大小(以dp为单位)。
-
setValueTypeface(Typeface tf)
: Sets theTypeface
for all value-labels for thisDataSet
object.- setValueTypeface(Typeface tf):为此DataSet对象设置所有值标签的字体。
-
setValueFormatter(ValueFormatter f)
: Sets a customValueFormatter
for thisDataSet
object, more on theValueFormatter
here.- setValueFormatter(ValueFormatter f):为此DataSet对象设置自定义ValueFormatter,在此处为ValueFormatter设置更多。
-
setDrawValues(boolean enabled)
: Enables / disables drawing values (value-text) for thisDataSet
object.- setDrawValues(boolean enabled):启用/禁用此DataSet对象的绘图值(值 - 文本)。
If all values in your whole data object (not data-set) should e.g. have the same color, you can simply call one of the above mentioned on the ChartData
object.
- 如果整个数据对象(不是数据集)中的所有值都应该例如 具有相同的颜色,您可以简单地调用ChartData对象上面提到的一个。
Highlighting
突出
-
setHighlightEnabled(boolean enabled)
: Set this to true to allow highlighting via touch for this specificDataSet
.- setHighlightEnabled(boolean enabled):将此属性设置为true以允许通过触摸突出显示此特定DataSet。
-
setDrawVerticalHighlightIndicator(boolean enabled)
: Enables / disables the vertical highlight-indicator-line. If disabled, the indicator is not drawn.- setDrawVerticalHighlightIndicator(boolean enabled):启用/禁用垂直高亮显示指示符行。 如果禁用,则不绘制指标。
-
setDrawHorizontalHighlightIndicator(boolean enabled)
: Enables / disables the horizontal highlight-indicator-line. If disabled, the indicator is not drawn.- setDrawHorizontalHighlightIndicator(boolean enabled):启用/禁用水平高亮显示指示符行。 如果禁用,则不绘制指标。
Getters / Convenience
- 吸气/方便
-
contains(Entry entry)
: Checks if thisDataSet
object contains the specifiedEntry
. Returns true if so, false if not. NOTE: Performance is pretty bad on this one, do not over-use in performance critical situations.- contains(条目条目):检查此DataSet对象是否包含指定的条目。 如果是,则返回true,否则返回false。 注意:在这一点上性能非常糟糕,在性能危急情况下不要过度使用。