在第一步,须要将依赖的库添加到你的项目中android
想要使用 LineChart,BarChart,ScatterChart,CandleStickChart,PieChart,BubbleChart或者RadarChart,须要先在xml中定义git
<com.github.mikephil.charting.charts.LineChart
android:id="@+id/chart"
android:layout_width="match_parent"
android:layout_height="match_parent" />
复制代码
而后在你的Activity或者Fragment中获取控件github
LineChart chart = (LineChart) findViewById(R.id.chart);
复制代码
或者使用代码建立,在布局中添加canvas
LineChart chart = new LineChart(Context);
RelativeLayout rl = (RelativeLayout) findViewById(R.id.relativeLayout);
rl.add(chart);
复制代码
在你拥有了图表的实例以后,你能够建立数据并添加到图表中.下面是一个使用折线图的例子,每一条数据均可以使用Entry这个类来标识x,y坐标. 在其余图表类型中,好比BarChart使用其余的类(好比BarEntry)来达到相同的目的.数组
添加数据到图表中,须要将全部的数据都包裹在Entry对象中bash
YourData[] dataObjects = ...;
List<Entry> entries = new ArrayList<Entry>();
for (YourData data : dataObjects) {
entries.add(new Entry(data.getValueX(), data.getValueY()));
}
复制代码
而后,你须要给你建立的LineDataSet对象添加List数据集合,DataSet对象持有数据,这些数据用户能够自定义样式. 下面的"Label"用于描述数据的做用,若是图例开启的话,还会做为图例显示在图表上网络
LineDataSet dataSet = new LineDataSet(entries, "Label"); // 添加数据
dataSet.setColor(...);
dataSet.setValueTextColor(...); // 自定义数据样式
复制代码
最后一步,你须要把建立好的LineDataSet添加到LineData中,添加完数据,利用Chart的实例去设置数据,而且刷新一下app
LineData lineData = new LineData(dataSet);
chart.setData(lineData);
chart.invalidate(); // 刷新
复制代码
这个库可让你最大限度的经过手势触摸与图表交互,而且经过回调函数获取反馈ide
若是使用手势操做图表,可使用OnChartGestureListener来监听回调函数
public interface OnChartGestureListener {
//当手指刚触摸到图表时触发,action_down
void onChartGestureStart(MotionEvent me, ChartTouchListener.ChartGesture lastPerformedGesture);
//当手指在图表上操做结束时调用,action_up、action_cancel
void onChartGestureEnd(MotionEvent me, ChartTouchListener.ChartGesture lastPerformedGesture);
//长按图表回调
public void onChartLongPressed(MotionEvent me);
//双击图表回调
public void onChartDoubleTapped(MotionEvent me);
//单击图表回调
public void onChartSingleTapped(MotionEvent me);
//滑动图表回调
public void onChartFling(MotionEvent me1, MotionEvent me2, float velocityX, float velocityY);
//缩放图表回调
public void onChartScale(MotionEvent me, float scaleX, float scaleY);
//拖拽图表回调
public void onChartTranslate(MotionEvent me, float dX, float dY);
}
复制代码
将你的Chart去设置手势监听器就能够实现这些回调
chart.setOnChartGestureListener(this);
复制代码
高亮样式能够由我的自定义
dataSet.setHighlightEnabled(true); // 是否容许高亮
dataSet.setDrawHighlightIndicators(true); // 设置是否有拖拽高亮指示器
dataSet.setHighlightColor(Color.BLACK); // 设置高亮颜色
复制代码
OnChartValueSelectedListener,经过触摸高亮数据时的回调
public interface OnChartValueSelectedListener {
//当图表内的一项被选中时的回调
public void onValueSelected(Entry e, Highlight h);
//当没有选择项时的回调
public void onNothingSelected();
}
复制代码
Highlight类,一般被用于获取高亮条目的信息,或者用来给图表或者Entry对提供数据用于高亮,Highlight类提供两个构造函数
//标准的Highlight构造函数
public Highlight(float x, int dataSetIndex) { ... }
//给BarEntry使用的Highlight构造函数
public Highlight(float x, int dataSetIndex, int stackIndex) { ... }
复制代码
一个经过代码方式利用Highlight进行高亮的例子
// highlight the entry and x-position 50 in the first (0) DataSet
Highlight highlight = new Highlight(50f, 0);
chart.highlightValue(highlight, false); // highlight this value, don't call listener 复制代码
全部用户输入的手势高亮形式都是由默认的ChartHighlighter类完成的,经过自定义ChartHighlighter类的实现来替换默认的,完成更多的不一样的高亮形式 setHighlighter(ChartHighlighter highlighter): 传入自定义的ChartHighlighter实现类对象来完成高亮样式的设置
坐标轴类经过如下部分实现具体的样式
可使用IAxisValueFormatter接口来格式化坐标轴数据,使用axis.setValueFormatter(IAxisValueFormatter formatter)设置本身的定制数据格式
像边界线和约束线这种用于表示特殊信息的线咱们称之为限制线,限制线在Y轴上横向呈现,在X轴上纵向呈现
限制线顾名思义是给用户提供格外信息的一种简洁、简单的线
例如,您的图表可能会显示用户的各类血压测量结果.为了通知用户,收缩压超过140毫米汞柱被认为是健康风险,你能够在140添加一个限制线来提供这些信息
YAxis leftAxis = chart.getAxisLeft();
LimitLine ll = new LimitLine(140f, "健康的血压线");
ll.setLineColor(Color.RED);
ll.setLineWidth(4f);
ll.setTextColor(Color.BLACK);
ll.setTextSize(12f);
// .. 更多的样式设置
leftAxis.addLimitLine(ll);
复制代码
任何与横轴有关的数据信息都存放在X轴中,像折线图、柱状图、网状图、蜡烛图、雷达图等都有XAxis对象
获取X轴实例对象使用
XAxis xAxis = chart.getXAxis();
复制代码
示例代码
XAxis xAxis = chart.getXAxis();
xAxis.setPosition(XAxisPosition.BOTTOM);
xAxis.setTextSize(10f);
xAxis.setTextColor(Color.RED);
xAxis.setDrawAxisLine(true);
xAxis.setDrawGridLines(false);
// 设置定制的数据格式
xAxis.setValueFormatter(new MyCustomFormatter());
复制代码
任何与纵轴有关的数据信息都存放在Y轴中,像折线图、柱状图、网状图、蜡烛图等都有左右两个YAxis对象,雷达图只有一个YAxis对象
获取YAxis实例对象
YAxis leftAxis = chart.getAxisLeft();
YAxis rightAxis = chart.getAxisRight();
YAxis leftAxis = chart.getAxis(AxisDependency.LEFT);
YAxis yAxis = radarChart.getYAxis(); // 雷达图获取YAxis方法
复制代码
在运行时,可使用public AxisDependency getAxisDependency()来肯定YAxis是图表的哪一边的 想要设置效果必须在给图表设置数据以前
默认状况下,全部添加到图表的数据都和左边的Y轴数据相对应,没有进一步设置的状况下右边的Y轴数据以及样式比例均与左边统一,若是你想要设置不一样比例的Y轴,你能够经过设置对应的数据来绘制对应的坐标轴实现
LineDataSet dataSet = ...; // 获取dataset
dataSet.setAxisDependency(AxisDependency.RIGHT);
复制代码
除了网格线与YAxis上的每一个值水平并排,还有所谓的零线,它在轴上的零(0)值处绘制,相似于网格线,但能够单独配置。
零线代码示例:
// 数据设置在左边坐标轴
YAxis left = mChart.getAxisLeft();
left.setDrawLabels(false); // 不设置坐标轴数据标签
left.setDrawAxisLine(false); // 不绘制坐标轴线
left.setDrawGridLines(false); // 不绘制网格线
left.setDrawZeroLine(true); // 绘制零线
mChart.getAxisRight().setEnabled(false); // 不设置右边Y轴
复制代码
更多的代码示例:
YAxis yAxis = mChart.getAxisLeft();
yAxis.setTypeface(...); // 设置一个不一样的字体
yAxis.setTextSize(12f); // 设置字体大小
yAxis.setAxisMinimum(0f); // 设置坐标轴从0开始
yAxis.setAxisMaximum(100f); // 设置坐标轴最大值为100
yAxis.setTextColor(Color.BLACK); // 设置字体颜色为黑色
yAxis.setValueFormatter(new MyValueFormatter());
yAxis.setGranularity(1f); // 设置间隔为1
yAxis.setLabelCount(6, true); // 设置标签个数
//... 更多
复制代码
若是想要给图表添加数据须要使用如下方式:
public void setData(ChartData data) { ... }
复制代码
基类CharData包含在渲染时图表所须要的全部的数据和信息 对于每一种数据图表,都有一个CharData的子类用于给图表设置数据
在构造函数中,可使用List<? extends IDataSet>做为数据去显示,
两种不一样的构造函数
/** List constructor */
public LineData(List<ILineDataSet> sets) { ... }
/** Constructor with one or multiple ILineDataSet objects */
public LineData(ILineDataSet...) { ... }
复制代码
一个DataSet对象表明一组图表中一块儿的数据,这样能够在逻辑上分离图表上的不一样组数据. 对于每种类型的图表,存在容许特定样式的扩展DataSet的不一样对象(例如Line DataSet)
举个例子,若是你想要显示两家不一样公司上一年季度性的财政收入,推荐使用两个LineDataSet,每个中包含四组数据 那么如何去设置LineDataSet对象呢
public LineDataSet(List<Entry> entries,String label){...}
复制代码
能够看到,LineDataSet的构造函数中须要List一系列的数据以及用于描述数据信息的label, 此label还能够进一步在LineData中去找到对应的LineDataSet对象 这些Entry的列表中包含了图表的全部数据,而Entry类就至关于X、Y坐标值的一个包装类,用于存放一个具体的坐标值
public Entry(float x, float y) { ... }
复制代码
上面的例子,咱们第一步能够建立一个用于存放公司收入的Entry的集合
List<Entry> valsComp1 = new ArrayList<Entry>();
List<Entry> valsComp2 = new ArrayList<Entry>();
复制代码
而后填充数据,确保Entry内的X轴数据是精确的
Entry c1e1 = new Entry(0f, 100000f); // 一公司第一季度数据
valsComp1.add(c1e1);
Entry c1e2 = new Entry(1f, 140000f); // 一公司第二季度数据
valsComp1.add(c1e2);
// 继续添加
Entry c2e1 = new Entry(0f, 130000f); // 二公司第一季度数据
valsComp2.add(c2e1);
Entry c2e2 = new Entry(1f, 115000f); // 二公司第二季度数据
valsComp2.add(c2e2);
//...
复制代码
如今咱们能够经过这一系列数据去建立LineDataSet对象
LineDataSet setComp1 = new LineDataSet(valsComp1, "Company 1");
setComp1.setAxisDependency(AxisDependency.LEFT);
LineDataSet setComp2 = new LineDataSet(valsComp2, "Company 2");
setComp2.setAxisDependency(AxisDependency.LEFT);
复制代码
经过使用setAxisDependency来指定具体数据绘制的轴,而后经过建立IDataSets的List来构成ChartData对象
// 使用ILineDataSet接口
List<ILineDataSet> dataSets = new ArrayList<ILineDataSet>();
dataSets.add(setComp1);
dataSets.add(setComp2);
LineData data = new LineData(dataSets);
mLineChart.setData(data);
mLineChart.invalidate(); // 刷新
复制代码
在调用invalidate方法刷新后,数据就会被绘制出来
若是咱们想特别定制X轴数据来替换原先的0到3,咱们可使用IAxisValueFormatter接口 这个接口可让咱们自定义X轴数据显示样式 举个例子,像如下这样设置数据格式:
// 绘制在X轴上的数据数组
final String[] quarters = new String[] { "Q1", "Q2", "Q3", "Q4" };
IAxisValueFormatter formatter = new IAxisValueFormatter() {
@Override
public String getFormattedValue(float value, AxisBase axis) {
return quarters[(int) value];
}
@Override
public int getDecimalDigits() { return 0; }
};
XAxis xAxis = mLineChart.getXAxis();
xAxis.setGranularity(1f); // 最小的间隔设置为1
xAxis.setValueFormatter(formatter);
复制代码
最后渲染的图表的样子
BarChart,ScatterChart,BubbleChart和CandleStickChart的设置数据与LineChart同样 其中特殊的是BarChart将会在下面进行解释:
若是List没有正确的X轴排列顺序,可能致使一些意外的状况 咱们可使用EntryXComparator来进行排序
List<Entry> entries = ...;
Collections.sort(entries, new EntryXComparator());
复制代码
柱状图的设置数据方式与折线图相似,最大的不一样在于柱状图存放数据的BarEntry有一些不一样的样式设置
建立BarDataSet
List<BarEntry> entries = new ArrayList<>();
entries.add(new BarEntry(0f, 30f));
entries.add(new BarEntry(1f, 80f));
entries.add(new BarEntry(2f, 60f));
entries.add(new BarEntry(3f, 50f));
// 跳过第五个
entries.add(new BarEntry(5f, 70f));
entries.add(new BarEntry(6f, 60f));
BarDataSet set = new BarDataSet(entries, "BarDataSet");
复制代码
将数据设置到图表上
BarData data = new BarData(set);
data.setBarWidth(0.9f); // 设置数据条的宽度
chart.setData(data);
chart.setFitBars(true); // 让X轴与全部的数据条适配
chart.invalidate(); // 刷新
复制代码
当柱状图被建立时,每个条都有1f的宽度,设置为0.9f的宽度后,将会在两个条各存在0.1f的间隔 设置setFitBars(true)将会让X轴的范围很好的适配柱状图,而不会出现有哪一个条被边缘线切断
刷新后显示:
YourData[] group1 = ...;
YourData[] group2 = ...;
List<BarEntry> entriesGroup1 = new ArrayList<>();
List<BarEntry> entriesGroup2 = new ArrayList<>();
// 填充数据
for(int i = 0; i < group1.length; i++) {
entriesGroup1.add(new BarEntry(i, group1.getValue()));
entriesGroup2.add(new BarEntry(i, group2.getValue()));
}
//两组数据
BarDataSet set1 = new BarDataSet(entriesGroup1, "Group 1");
BarDataSet set2 = new BarDataSet(entriesGroup2, "Group 2");
复制代码
这样就获取到两组数据,如今用这两组数据去完成群组柱状图
float groupSpace = 0.06f; //群组间的间隔
float barSpace = 0.02f; // 每个柱状条间隔
float barWidth = 0.45f; // 每个柱状条的宽度
// (0.02 + 0.45) * 2 + 0.06 = 1.00 -> 总共合起来仍是1f
BarData data = new BarData(set1, set2); //设置组数据
data.setBarWidth(barWidth); // 设置柱状条宽度
barChart.setData(data);
barChart.groupBars(1980f, groupSpace, barSpace); // 设置组样式宽度等
barChart.invalidate(); // 刷新
复制代码
groupBars(...)对群组数据进行了具体的设置,这个方法的具体参数
public void groupBars(float fromX, float groupSpace, float barSpace) { ... }
复制代码
fromX标识X轴数据从哪边开始进行渲染
最后渲染出来为:
经过使用setCenterAxisLabels(...)方法可让标签数据正确显示在群组柱状条中间
XAxis xAxis = chart.getXAxis();
xAxis.setCenterAxisLabels(true);
复制代码
设置与普通的柱状图相似,不一样的是BarEntry的构造函数使用
public BarEntry(float x, float [] yValues) { ... }
复制代码
第二个参数是一个叠加的数据值数组
BarEntry stackedEntry = new BarEntry(0f, new float[] { 10, 20, 30 });
复制代码
这样在图表上显示高度为10,20,30的三段数据
不一样于其余的图形,饼图的构造函数是这样的:
public PieEntry(float value, String label) { ... }
复制代码
第一个参数便是当前所占区域的大小数据,第二个参数用于描述当前区域的信息
完整的饼图的建立
List<PieEntry> entries = new ArrayList<>();
entries.add(new PieEntry(18.5f, "Green"));
entries.add(new PieEntry(26.7f, "Yellow"));
entries.add(new PieEntry(24.0f, "Red"));
entries.add(new PieEntry(30.8f, "Blue"));
PieDataSet set = new PieDataSet(entries, "Election Results");
PieData data = new PieData(set);
pieChart.setData(data);
pieChart.invalidate(); // 刷新
复制代码
饼图没有X轴,数据的显示顺序由添加顺序来定义 渲染完成后:
在这个小例子中咱们有两组数据用于表明两个公司季度收入,咱们想要设置不一样的颜色去区分它们 咱们想要:
LineDataSet setComp1 = new LineDataSet(valsComp1, "Company 1");
setComp1.setColors(new int[] { R.color.red1, R.color.red2, R.color.red3, R.color.red4 }, Context);
LineDataSet setComp2 = new LineDataSet(valsComp2, "Company 2");
setComp2.setColors(new int[] { R.color.green1, R.color.green2, R.color.green3, R.color.green4 }, Context);
复制代码
给DataSet设置颜色的方法
固然也可使用颜色模板
LineDataSet set = new LineDataSet(...);
set.setColors(ColorTemplate.VORDIPLOM_COLORS);
复制代码
在没有设置颜色的时候,将会使用默认的颜色
IValueFormatter接口用于建立自定义格式类来在图表绘制以前格式数据 使用IValueFormatter,简单的建立一个类去实现这个接口,并返回你从getFormattedValue(...)方法获取的数据想要显示的样子
public class MyValueFormatter implements IValueFormatter {
private DecimalFormat mFormat;
public MyValueFormatter() {
mFormat = new DecimalFormat("###,###,##0.0"); // 使用金融类型的格式
}
@Override
public String getFormattedValue(float value, Entry entry, int dataSetIndex, ViewPortHandler viewPortHandler) {
// 写下你的代码逻辑
return mFormat.format(value) + " $"; // 好比在数据前添加一个$符
}
}
复制代码
给ChartData或者DataSet添加格式化
// 图表内的数据均使用这种格式
lineData.setValueFormatter(new MyValueFormatter());
// 仅在该DataSet内使用这种格式
lineDataSet.setValueFormatter(new MyValueFormatter());
复制代码
建立一个类实现IAxisValueFormatter接口,用于格式化坐标轴上的值
public class MyYAxisValueFormatter implements IAxisValueFormatter {
private DecimalFormat mFormat;
public MyAxisValueFormatter() {
//格式化数字
mFormat = new DecimalFormat("###,###,##0.0");
}
@Override
public String getFormattedValue(float value, AxisBase axis) {
// value参数标识在坐标轴上的位置或者说是数据值,axis标识是哪一个坐标轴
return mFormat.format(value) + " $";
}
/** 只有为数字时返回 其他返回0*/
@Override
public int getDecimalDigits() { return 1; }
}
复制代码
下面的例子展现怎么把数组中的数据展现到坐标轴上去
public class MyXAxisValueFormatter implements IAxisValueFormatter {
private String[] mValues;
public MyXAxisValueFormatter(String[] values) {
this.mValues = values;
}
@Override
public String getFormattedValue(float value, AxisBase axis) {
return mValues[(int) value];
}
@Override
public int getDecimalDigits() { return 0; }
}
复制代码
在建立好格式化类以后,简单去运用它
YAxis left = chart.getAxisLeft();
left.setValueFormatter(new MyYAxisValueFormatter());
String[] values = new String[] { ... };
XAxis xAxis = chart.getXAxis();
xAxis.setValueFormatter(new MyXAxisValueFormatter(values));
复制代码
设置完以后,图表用格式化类来具体指定值来代替默认的坐标轴的最小值和最大值范围
若是使用上面的数组格式化坐标轴标签值,可使用一下的代码去限制标签间的间隔
axis.setGranularity(1f);
复制代码
这将能够预防坐标轴绘制重叠的数据,由于在缩放到必定的程度时,图表将会中止计算更小的标签范围
下面是一些能够直接在Chart上使用的样式设置方法
图例一般有一个标识和一字符串组成,标识为表明该数据的DataSet的颜色决定,字符串是DataSet的描述
Legend legend = chart.getLegend();
复制代码
例子
Legend l = chart.getLegend();
l.setFormSize(10f);
l.setForm(LegendForm.CIRCLE);
l.setPosition(LegendPosition.BELOW_CHART_LEFT);
l.setTypeface(...);
l.setTextSize(12f);
l.setTextColor(Color.BLACK);
l.setXEntrySpace(5f);
l.setYEntrySpace(5f);
l.setCustom(ColorTemplate.VORDIPLOM_COLORS, new String[] { "Set1", "Set2", "Set3", "Set4", "Set5" });
// and many more...
复制代码
MPAndroidChart并不支持实时数据,可是为了向图表添加新数据或动态删除数据,有各类方法能够将Entry对象添加到已有的DataSet对象或从已有的ChartData对象中删除DataSet对象
Class DataSet(以及全部子类):
Class ChartData(以及全部子类):
一样也有删除数据的方法
Class DataSet(以及全部子类):
Class ChartData(以及全部子类):
在动态删除或者添加数据以后,必定要在调用invalidate()刷新页面以前调用notifyDataSetChanged()方法更新数据
// EXAMPLE 1
// add entries to the "data" object
exampleData.addEntry(...);
chart.notifyDataSetChanged(); // 让Chart知道数据发生变化
chart.invalidate(); // 刷新
// EXAMPLE 2
// add entries to "dataSet" object
dataSet.addEntry(...);
exampleData.notifyDataChanged(); // 让DataSet知道数据发生变化
chart.notifyDataSetChanged(); // 让Chart知道数据发生变化
chart.invalidate(); // 刷新
复制代码
这些方法只适用于LineChart,BarChart,ScatterChart和CandleStickChart
全部的moveViewTo相似的方法都会自动调用invalidate方法去刷新页面,不须要手动去调用
例子:
chart.setData(...);
// 开始设置视窗
chart.setVisibleXRangeMaximum(20);
chart.moveViewToX(10);
复制代码
全部的图表都提供了动画以供操做使用
有三种不一样的动画存在
mChart.animateX(3000); // animate horizontal 3000 milliseconds
mChart.animateY(3000); // animate vertical 3000 milliseconds
mChart.animateXY(3000, 3000); // animate horizontal and vertical 3000 milliseconds
复制代码
你能够从Easing.EasingOption中选择想要的预置平缓动画效果
public enum EasingOption {
Linear,
EaseInQuad,
EaseOutQuad,
EaseInOutQuad,
EaseInCubic,
EaseOutCubic,
EaseInOutCubic,
EaseInQuart,
EaseOutQuart,
EaseInOutQuart,
EaseInSine,
EaseOutSine,
EaseInOutSine,
EaseInExpo,
EaseOutExpo,
EaseInOutExpo,
EaseInCirc,
EaseOutCirc,
EaseInOutCirc,
EaseInElastic,
EaseOutElastic,
EaseInOutElastic,
EaseInBack,
EaseOutBack,
EaseInOutBack,
EaseInBounce,
EaseOutBounce,
EaseInOutBounce,
}
复制代码
public void animateY(int durationmillis, Easing.EasingOption option);
复制代码
例如调用时就将动画效果设置进去
mChart.animateY(3000, Easing.EasingOption.EaseOutBack);
复制代码
public void animateY(int durationmillis, EasingFunction function);
复制代码
建立一个类去实现EasingFunction接口,在该类中实现本身的逻辑
/**
* Interface for creating custom made easing functions.
*/
public interface EasingFunction {
/**
* Called everytime the animation is updated.
* @param input - the time passed since the animation started (value between 0 and 1)
*/
public float getInterpolation(float input);
}
复制代码
调用的时候(在Android3.0如下的版本中将会Crash掉)
mChart.animateY(3000, new MyEasingFunction());
复制代码
IMarker接口是你可以在建立自定义的标注样式去显示你图表中高亮的数据 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);
/**
* 刷新方法
*
* @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); /** * 使用传递来的Canvas对象在指定位置绘制对应的标注 * * @param canvas * @param posX * @param posY */ void draw(Canvas canvas, float posX, float posY); } 复制代码
你须要建立一个类去实现IMarker接口
public class YourMarkerView implements 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;
}
}
复制代码
想要在图表中使用你设置好的marker,使用setMarker方法
IMarker marker = new YourMarkerView();
chart.setMarker(marker);
复制代码
获取一个已经存在的marker,使用getMarker()方法
IMarker marker = chart.getMarker();
复制代码
ChartData类是全部图表数据类的基类
public class LineData extends ChartData { ...
复制代码
下面的是已经被实现且在子类中可使用的方法
BarData
ScatterData
PieData
BubbleData
DataSet数据类用法与ChartData几乎如出一辙
Line-, Bar-, Scatter-, Bubble- & CandleDataSet
Line-, Bar-, Scatter-, Candle- & RadarDataSet
Line- & RadarDataSet
LineDataSet
BarDataSet
ScatterDataSet
CandleDataSet
BubbleDataSet
PieDataSet
经过如下方式来获取视窗Handler实例
ViewPortHandler handler = chart.getViewPortHandler();
复制代码
新建一个类实现FillFormatter接口
并重写下面这个方法
public float getFillLinePosition(LineDataSet dataSet, LineDataProvider provider)
复制代码
能够实现单个的DataSet的实如今哪一个位置中止绘制
public class MyCustomFillFormatter implements FillFormatter {
@Override
public float getFillLinePosition(LineDataSet dataSet, LineDataProvider dataProvider) {
float myDesiredFillPosition = ...;
// put your logic here...
return myDesiredFillPosition;
}
}
复制代码
把格式化添加到图表中
lineDataSet.setFillFormatter(new MyCustomFillFormatter());
复制代码
默认的实现---->DefaultFillFormatter.