21. DataSet subclasses (specific DataSet styling)
This wiki entry focuses on the subclasses of the DataSet
class. All other subclasses of DataSet
not mentioned here do not provide any specific enhancements.
- 此wiki条目侧重于DataSet类的子类。 此处提到的DataSet的所有其他子类都不提供任何特定的增强功能。
Line-, Bar-, Scatter-, Bubble- & CandleDataSet (below mentioned methods can be used for any of the mentioned DataSet
classes)
- 下面提到的方法可以用于任何提到的DataSet类)
-
setHighLightColor(int color)
: Sets the color that is used for drawing the highlight indicators. Don't forget to resolve the color usinggetResources().getColor(...)
orColor.rgb(...)
(or simplyColor.BLACK
).- setHighLightColor(int color):设置用于绘制高光指示符的颜色。 不要忘记使用getResources()。getColor(...)或Color.rgb(...)(或简称Color.BLACK)来解析颜色。
Line-, Bar-, Scatter-, Candle- & RadarDataSet
-
setDrawHighlightIndicators(boolean enabled)
: Enables / disables both vertical and horizontal highlight indicator lines. CallsetDrawVerticalHighlightIndicator(...)
andsetDrawHorizontalHighlightIndicator(...)
for individual configuration.- setDrawHighlightIndicators(boolean enabled):启用/禁用垂直和水平高亮显示指示符行。 为单个配置调用setDrawVerticalHighlightIndicator(...)和setDrawHorizontalHighlightIndicator(...)。
-
setHighlightLineWidth(float width)
: Sets the width of the highlight lines (crosshairs) in dp.- setHighlightLineWidth(float width):设置dp中高光线(十字准线)的宽度。
Line- & RadarDataSet (methods only for LineDataSet
and RadarDataSet
)
-
setFillColor(int color)
: Sets the color that is used for filling the line surface.- setFillColor(int color):设置用于填充线条曲面的颜色。
-
setFillAlpha(int alpha)
: Sets the alpha value (transparency) that is used for filling the line surface (0-255), default: 85, 255 = fully opaque, 0 = fully transparent- setFillAlpha(int alpha):设置用于填充线表面的alpha值(透明度)(0-255),默认值:85,255 =完全不透明,0 =完全透明
-
setFillDrawable(Drawable d)
: Sets aDrawable
that should cover the fill-area. This also allows to use gradients.- setFillDrawable(Drawable d):设置应覆盖填充区域的Drawable。 这也允许使用渐变。
-
setDrawFilled(boolean filled)
: Set to true if the DataSet should be drawn filled (surface, area), and not just as a line, disabling this will give a performance boost. Please note that this method uses thecanvas.clipPath(...)
method for drawing the filled area. For devices with API level < 18 (Android 4.3), hardware acceleration of the chart should be turned off - see here. Default: false- setDrawFilled(boolean filled):如果应该绘制填充的数据集(表面,区域),而不仅仅是一行,则设置为true,禁用此选项将提高性能。 请注意,此方法使用canvas.clipPath(...)方法绘制填充区域。 对于API级别<18(Android 4.3)的设备,应关闭图表的硬件加速 - 请参见此处。 默认值:false
-
setLineWidth(float width)
: Set the line width for this DataSet (min = 0.2f, max = 10f); default 1f NOTE: thinner line == better performance, thicker line == worse performance- setLineWidth(float width):设置此DataSet的行宽(min = 0.2f,max = 10f); 默认1f注意:更细的线==更好的性能,更粗的线==更差的性能
Below mentioned methods are only applicable for the specifically mentioned DataSet
subclass.
- 下面提到的方法仅适用于特别提到的DataSet子类。
LineDataSet (class LineDataSet
)
-
setCircleRadius(float size)
: Sets the size (radius) of the circle shaped value indicators, default size = 4f- setCircleRadius(float size):设置圆形值指标的大小(半径),默认大小= 4f
-
setDrawCircles(boolean enabled)
: Set this to true to enable the drawing of circle indicators for thisLineDataSet
, default true- setDrawCircles(boolean enabled):将此属性设置为true以启用此LineDataSet的圆形指示器绘制,默认为true
-
setDrawCubic(boolean enabled)
: If set to true, the linechart lines are drawn in cubic-style instead of linear. This has a negative effect on performance! Default: false- setDrawCubic(boolean enabled):如果设置为true,则线条图线以立方体样式而不是线性线条绘制。 这对性能有负面影响! 默认值:false
-
setCubicIntensity(float intensity)
: Sets the intensity for cubic lines (if enabled). Max = 1f = very cubic, Min = 0.05f = low cubic effect, Default: 0.2f- setCubicIntensity(float intensity):设置立方线的强度(如果启用)。 Max = 1f =非常立方,Min = 0.05f =低立方效应,默认值:0.2f
-
setCircleColor(int color)
: Sets the color all circle indicators of this dataset should have.- setCircleColor(int color):设置此数据集应具有的所有圆圈指示符的颜色。
-
setCircleColors(List colors)
: Sets the colors the outer-circles of thisLineDataSet
should have. There are various other methods for setting circle colors as well.- setCircleColors(List colors):设置此LineDataSet的外圆所具有的颜色。 还有各种其他方法来设置圆形颜色。
-
setCircleColorHole(int color)
: Sets the color of the inner circle of the line-circles (the hole).- setCircleColorHole(int color):设置直线圆(孔)的内圆的颜色。
-
setDrawCircleHole(boolean enabled)
: Set this to true to allow drawing a hole in each circle of this dataset. If set to false, circles will be drawn filled (without hole).- setDrawCircleHole(boolean enabled):将此值设置为true以允许在此数据集的每个圆中绘制一个孔。 如果设置为false,则将绘制圆圈(无孔)。
-
enableDashedLine(float lineLength, float spaceLength, float phase)
: Enables the line to be drawn in dashed mode, e.g. like this "- - - - - -". "lineLength" is the length of the line pieces, "spaceLength" is the length of space in between the pieces, "phase" is the offset, in degrees (normally, use 0)- enableDashedLine(float lineLength,float spaceLength,float phase):允许以虚线模式绘制线条,例如 像这样 ”- - - - - -”。 “lineLength”是线条的长度,“spaceLength”是各块之间的空间长度,“phase”是偏移量,以度为单位(通常使用0)
BarDataSet (class BarDataSet
)
-
setBarSpacePercent(float percent)
: Sets the space between the bars in percent of the total bar width.- setBarSpacePercent(float percent):以总条宽的百分比设置条之间的间距。
-
setBarShadowColor(int color)
: Sets the color used for drawing the bar-shadows. The bar shadows is a surface behind the bar that indicates the maximum value. Don't for get to usegetResources().getColor(...)
to set this. OrColor.rgb(...)
.- setBarShadowColor(int color):设置用于绘制条形阴影的颜色。 条形阴影是条形图后面的一个表示最大值的表面。 不要使用getResources()。getColor(...)来设置它。 或者Color.rgb(...)。
-
setHighLightAlpha(int alpha)
: Set the alpha value (transparency) that is used for drawing the highlight indicator bar. min = 0 (fully transparent), max = 255 (fully opaque).- setHighLightAlpha(int alpha):设置用于绘制高亮显示指示条的alpha值(透明度)。 min = 0(完全透明),max = 255(完全不透明)。
-
setStackLabels(String[] labels)
: Sets labels for different values of bar-stacks, in case there are one.- setStackLabels(String [] labels):为条形图堆栈的不同值设置标签(如果有的话)。
ScatterDataSet (class ScatterDataSet
)
-
setScatterShapeSize(float size)
: Sets the size in density pixels the drawn scattershape will have. This only applies for non custom shapes.- setScatterShapeSize(float size):设置绘制的scattershape将具有的密度像素大小。 这仅适用于非自定义形状。
-
setScatterShape(ScatterShape shape)
: Sets the shape that is drawn on the position where the values are at.- setScatterShape(ScatterShape shape):设置在值所在的位置绘制的形状。
CandleDataSet (class CandleDataSet
)
-
setBodySpace(float space)
: Sets the space that is left out on the left and right side of each candle body, default 0.1f (10%), max 0.45f, min 0f- setBodySpace(float space):设置每个烛体左侧和右侧遗漏的空间,默认为0.1f(10%),最大0.45f,min 0f
-
setShadowWidth(float width)
: Sets the width of the candle-shadow-line in dp. Default 3f.- setShadowWidth(float width):以dp为单位设置蜡烛阴影线的宽度。 默认3f。
-
setShadowColor(int color)
: Sets the color of the candle-shadow-line.- setShadowColor(int color):设置蜡烛阴影线的颜色。
-
setDecreasingColor(int color)
: Sets the one and ONLY color that should be used for thisDataSet
when open > close.- setDecreasingColor(int color):设置打开>关闭时应该用于此DataSet的唯一颜色。
-
setIncreasingColor(int color)
: Sets the one and ONLY color that should be used for this DataSet when open <= close.- setIncreasingColor(int color):设置打开<= close时应该用于此DataSet的唯一颜色。
-
setDecreasingPaintStyle(Paint.Style style)
: Sets paint style when open > close (fill or stroke).- setDecreasingPaintStyle(Paint.Style style):在打开>关闭(填充或描边)时设置绘画样式。
-
setIncreasingPaintStyle(Paint.Style style)
: Sets paint style when open <= close (fill or stroke).- setIncreasingPaintStyle(Paint.Style style):设置打开时的绘画样式<=关闭(填充或描边)。
BubbleDataSet (class BubbleDataSet
)
-
setHighlightCircleWidth(float width)
: Sets the width of the circle that surrounds the bubble when in highlighted state, in dp.- setHighlightCircleWidth(float width):设置处于高亮状态时围绕气泡的圆的宽度,单位为dp。
Information concerning CandleDataSet
colors: The usual setColors(...)
, setColor(...)
, ... methods can still be used for coloring the chart all at once. If specific colors (for body, shadow, ...) are needed, use the above mentioned methods.
- 有关CandleDataSet颜色的信息:通常的setColors(...),setColor(...),...方法仍然可以用于一次着色图表。 如果需要特定颜色(用于身体,阴影......),请使用上述方法。
PieDataSet (class PieDataSet
)
-
setSliceSpace(float degrees)
: Sets the space that is left out between the piechart-slices in dp, default: 0 --> no space, maximum 20, minimum 0 (no space)- setSliceSpace(float degrees):设置dp中饼图切片之间遗漏的空间,默认值:0 - >无空格,最大20,最小0(无空格)
-
setSelectionShift(float shift)
: Sets the distance the highlighted piechart-slice of this DataSet is "shifted" away from the center of the chart, default 12f- setSelectionShift(float shift):设置此DataSet的突出显示的饼图切片远离图表中心“移位”的距离,默认为12f
22. The ViewPortHandler
The ViewPortHandler
class is responsible for handling the view-port of the chart. This means it's responsible for what is visible in the chart-view, it's current state in terms of translation and zoom / scale level, the size of the chart and it's drawing area and current offsets. The ViewPortHandler
allows to directly access all the above mentioned properties and modify them.
- ViewPortHandler类负责处理图表的视口。 这意味着它负责图表视图中可见的内容,它是转换和缩放/比例级别的当前状态,图表的大小以及它的绘图区域和当前偏移。 ViewPortHandler允许直接访问所有上述属性并进行修改。
Unlike modifying the view-port via the Chart
class, as described here, modifying the ViewPortHandler
directly is not always a completely safe way of changing what is visible and should be performed with care by a person familiar with the API. Incorrect use can lead to unexpected behaviour. However, the ViewPortHandler
offers more advanced methods of view-port modification.
- 与通过Chart类修改视图端口不同,如此处所述,直接修改ViewPortHandler并不总是一种完全安全的方式来更改可见内容并且应该由熟悉API的人员小心执行。 使用不当可能会导致意外行为。 但是,ViewPortHandler提供了更高级的视口修改方法。
Getting an instance
An instance of the ViewPortHandler
can be acquired the following way only:
- ViewPortHandler的实例只能通过以下方式获取:
ViewPortHandler handler = chart.getViewPortHandler();
Scale & Translation
- 规模与翻译
-
getScaleX()
: Returns the current scale / zoom level on the x-axis.- getScaleX():返回x轴上的当前缩放/缩放级别。
-
getScaleY()
: Returns the current scale / zoom level on the y-axis.- getScaleY():返回y轴上的当前缩放/缩放级别。
-
getTransX()
: Returns the current translation (movement) on the x-axis.- getTransX():返回x轴上的当前平移(移动)。
-
getTransY()
: Returns the current translation (movement) on the y-axis.- getTransY():返回y轴上的当前平移(移动)。
Chart dimensions & content
- 图表尺寸和内容
-
getChartWidth()
: Returns the width of the chart.- getChartWidth():返回图表的宽度。
-
getChartHeight()
: Returns the height of the chart.- getChartHeight():返回图表的高度。
-
getContentRect()
: Returns the current content area as aRectF
object.
- getContentRect():以RectF对象的形式返回当前内容区域。
More methods can be found in the JavaDocs or via studying the API.
- 可以在JavaDocs中找到更多方法,也可以通过研究API来找到。
23. Customizing the Fill-Line-Position (FillFormatter)
The FillFormatter interface allows to customize where the filled line of a LineDataSet should end. All that needs to be done is create a new class and implement the FillFormatter interface. Use the
- FillFormatter接口允许自定义LineDataSet的填充行应该结束的位置。 所有需要做的就是创建一个新类并实现FillFormatter接口。 使用
public float getFillLinePosition(LineDataSet dataSet, LineDataProvider provider)
method of the interface for implementing a custom logic that calculates the ending point of the fill line for the individual LineDataSet.
- 用于实现自定义逻辑的接口的方法,该逻辑计算单个LineDataSet的填充线的终点。
Creating a class the implements the interface:
- 创建一个实现接口的类:
public class MyCustomFillFormatter implements FillFormatter {
@Override
public float getFillLinePosition(LineDataSet dataSet, LineDataProvider dataProvider) {
float myDesiredFillPosition = ...;
// put your logic here...
return myDesiredFillPosition;
}
}
And then set the custom-formatter to your LineDataSet
:
- 然后将自定义格式化程序设置为LineDataSet:
lineDataSet.setFillFormatter(new MyCustomFillFormatter());
Here is the default implementation (logic) of the DefaultFillFormatter.
- 这是DefaultFillFormatter的默认实现(逻辑)。
24. Proguard
In case you are using Proguard, you will need to whitelist MPAndroidChart, which requires to add the following line to your Proguard configuration file:
- 如果您使用的是Proguard,则需要将MPAndroidChart列入白名单,这需要在Proguard配置文件中添加以下行:
-keep class com.github.mikephil.charting.** { *; }
If you don't do this, animations might not work.
- 如果您不这样做,动画可能无法正常工作。
If you are having issues with the Realm.io classes, add the following to your config file:
- 如果您遇到Realm.io类问题,请将以下内容添加到配置文件中:
-dontwarn io.realm.**
Additional information on Proguard.
- 有关Proguard的其他信息。
25. Realm.io mobile database
Please refer to the official MPAndroidChart-Realm repository to - plot data directly from Realm database with MPAndroidChart.
- 请参考官方MPAndroidChart-Realm存储库,使用MPAndroidChart直接从Realm数据库绘制数据。
A detailed guide can be found here.
- 可在此处找到详细指南。
26. Xamarin
27. Creating your own (custom) DataSets
Since v2.2.0, MPAndroidChart allows you to easily create your own custom DataSet
objects and use them in the charts.
- 从v2.2.0开始,MPAndroidChart允许您轻松创建自己的自定义DataSet对象并在图表中使用它们。
What you need to do
- 你需要做什么
- Create your own custom class (e.g.
CustomDataSet
)- 创建自己的自定义类(例如CustomDataSet)
- Let it
extend
BaseDataSet<? extends Entry>
- 让它扩展BaseDataSet <? 扩展Entry>
- Let it implement the
IDataSet
interface of your choice (e.g.IBarDataSet
) - depending on the kind of chart you want to create- 让它实现您选择的IDataSet接口(例如IBarDataSet) - 具体取决于您要创建的图表类型
- Implement all (by you) required methods and let them return your values of choice
- 实现所有(由您)所需的方法,让它们返回您选择的值
例
- 实现所有(由您)所需的方法,让它们返回您选择的值
Example
Creating a custom BarDataSet
to be used in a BarChart
.
- 创建要在BarChart中使用的自定义BarDataSet。
public class CustomBarDataSet extends BaseDataSet<BarEntry> implements IBarDataSet {
// implement all by the extended class and interface required methods
}
After creating the CustomBarDataSet and implementing all methods required by the interface it can be used in any BarChart just like a normal BarDataSet.
- 在创建CustomBarDataSet并实现接口所需的所有方法之后,它可以在任何BarChart中使用,就像普通的BarDataSet一样。
28. Miscellaneous (more useful stuff)
Chart content
- clear(): Clears the chart of all data (by setting the data object to null). Calls invalidate() to refresh the chart.
- clear():清除所有数据的图表(通过将数据对象设置为null)。 调用invalidate()来刷新图表。
- clearValues(): Clears the chart of all DataSet objects and thereby all Entries. Does not remove the provided x-values from the chart. Calls invalidate() to refresh the chart.
- clearValues():清除所有DataSet对象的图表,从而清除所有条目。 不从图表中删除提供的x值。 调用invalidate()来刷新图表。
- isEmpty(): Will return true if the charts data object is null, or if it contains no entries.
- isEmpty():如果图表数据对象为空,或者它不包含任何条目,则返回true。
Useful getter methods
-
有用的getter方法
-
getData(): Will return the data object you set for the chart.
- getData():将返回您为图表设置的数据对象。
-
getViewPortHandler(): Will return ViewPortHandler object of the chart that contains information about the charts size and bounds (offsets, content-area), as well as about the charts current scale (zoom) and translation (scrolling) state.
- getViewPortHandler():将返回图表的ViewPortHandler对象,其中包含有关图表大小和边界(偏移,内容区域)的信息,以及有关图表当前比例(缩放)和平移(滚动)状态的信息。
-
getRenderer(): Returns the main DataRenderer that is responsible for drawing the chart data.
- getRenderer():返回负责绘制图表数据的主DataRenderer。
-
getCenter(): Returns the center point of the whole chart-view.
- getCenter():返回整个图表视图的中心点。
-
getCenterOffsets(): Returns the center point of the chart drawing-area.
- getCenterOffsets():返回图表绘图区域的中心点。
-
getPercentOfTotal(float value): Returns the percentage the provided value makes up of the total value-sum inside the chart.
- getPercentOfTotal(float value):返回提供的值构成图表内总和值的百分比。
-
getYMin(): Returns lowest value the chart holds.
- getYMin():返回图表所包含的最低值。
-
getYMax(): Returns biggest value the chart holds.
- getYMax():返回图表所包含的最大值。
-
getLowestVisibleXIndex(): Returns the lowest x-index (value on the x-axis) that is still visible on the chart.
- getLowestVisibleXIndex():返回在图表上仍然可见的最低x-index(x轴上的值)。
-
getHighestVisibleXIndex(): Returns the highest x-index (value on the x-axis) that is still visible on the chart.
- getHighestVisibleXIndex():返回在图表上仍然可见的最高x-index(x轴上的值)。
Some more methods (of the Chart class)
-
saveToGallery(String title): Saves the current chart state as an image to the gallery. Don't forget to add "WRITE_EXTERNAL_STORAGE" permission to your manifest.
- saveToGallery(String title):将当前图表状态作为图像保存到库中。 不要忘记为清单添加“WRITE_EXTERNAL_STORAGE”权限。
-
saveToPath(String title, String pathOnSD): Saves the current chart state as an image to the specified path. Don't forget to add "WRITE_EXTERNAL_STORAGE" permission to your manifest.
- saveToPath(String title,String pathOnSD):将当前图表状态保存为指定路径的图像。 不要忘记为清单添加“WRITE_EXTERNAL_STORAGE”权限。
-
getChartBitmap(): Returns the Bitmap object that represents the chart, this Bitmap always contains the latest drawing state of the chart.
- getChartBitmap():返回表示图表的Bitmap对象,此Bitmap始终包含图表的最新绘图状态。
-
setHardwareAccelerationEnabled(boolean enabled): Allows to enable/disable hardware acceleration for the chart, only API level 11+.
- setHardwareAccelerationEnabled(boolean enabled):允许启用/禁用图表的硬件加速,仅限API级别11+。
网友评论