标签:
在 android开源图表库MPAndroidChart文档翻译(上) 中,介绍了mpandroidchart的创建,回调方法,还有轴。这篇文章继续翻译剩余内容。文档内容比较多,这是中篇。最后的内容在下篇做翻译。
要给图表设置数据,调用的方法为
public void setData(ChartData data) { ... }
// this is just one of many constructors
public LineData(ArrayList<String> xVals, ArrayList<ILineDataSet> sets) { ... }例如,你可能想在LineChart中想展示两家公司一年的季度收入。这种情况,建议使用两个不同的LineDataSet对象,每个包含四个值(一年四个季度),使用ArrayList描述X轴上的标签,只需要简单数据源 "1.Q", "2.Q", "3.Q", "4.Q"。
当然,使用一个LineDataSet对象,包含这两个公司的8个值也是可以的。
初始化LineDataSet对象
public LineDataSet(ArrayList<Entry> yVals, String label) { ... }Entry类型的ArrayList包含了表中的所有值,一个Entry对象包含了x轴的位置,和它对应的值。
public Entry(float val, int xIndex) { ... }全部放在一起(超过一年的两家公司的季度收入例子): ArrayList<Entry> valsComp1 = new ArrayList<Entry>();
ArrayList<Entry> valsComp2 = new ArrayList<Entry>();然后,填充这些Enty对象值。确保对应对象包含正确x轴索引。 (当然,在这里可以使用一个循环,在这种情况下,循环的计数器变量可以是在x轴上的索引)。
Entry c1e1 = new Entry(100.000f, 0); // 0 == quarter 1
valsComp1.add(c1e1);
Entry c1e2 = new Entry(50.000f, 1); // 1 == quarter 2 ...
valsComp1.add(c1e2);
// and so on ...
Entry c2e1 = new Entry(120.000f, 0); // 0 == quarter 1
valsComp2.add(c2e1);
Entry c2e2 = new Entry(110.000f, 1); // 1 == quarter 2 ...
valsComp2.add(c2e2);
//...这个时候,已经创建好了Entry对象的list,可以创建LineDataSet对象了
LineDataSet setComp1 = new LineDataSet(valsComp1, "Company 1"); setComp1.setAxisDependency(AxisDependency.LEFT); LineDataSet setComp2 = new LineDataSet(valsComp2, "Company 2"); setComp2.setAxisDependency(AxisDependency.LEFT);通过调用setAxisDependency,DataSet轴中应该对指定绘制,最后异步,但是重要,我们创建IDataSets 列表和x轴条目列表,并建立我们的ChartData对象:
// use the interface ILineDataSet
ArrayList<ILineDataSet> dataSets = new ArrayList<ILineDataSet>();
dataSets.add(setComp1);
dataSets.add(setComp2);
ArrayList<String> xVals = new ArrayList<String>();
xVals.add("1.Q"); xVals.add("2.Q"); xVals.add("3.Q"); xVals.add("4.Q");
LineData data = new LineData(xVals, dataSets);
mLineChart.setData(data);
mLineChart.invalidate(); // refresh调用invalidate之后,表会刷新,提供的数据会重绘
我们希望用红色代表一个公司的数据
用绿色代表另一个公司数据。
代码为
LineDataSet setComp1 = new LineDataSet(valsComp1, "Company 1");
// sets colors for the dataset, resolution of the resource name to a "real" color is done internally
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);setColors(int [] colors, Context c): 通过color资源id设置颜色
LineDataSet set = new LineDataSet(...); set.setColors(ColorTemplate.VORDIPLOM_COLORS);没有设置颜色,使用默认值。
从1.6.2版本可用,在2.1.4版本做出修改。
ValueFormatter接口可以用来指定绘制的格式
ublic class MyValueFormatter implements ValueFormatter {
private DecimalFormat mFormat;
public MyValueFormatter() {
mFormat = new DecimalFormat("###,###,##0.0"); // use one decimal
}
@Override
public String getFormattedValue(float value, Entry entry, int dataSetIndex, ViewPortHandler viewPortHandler) {
// write your logic here
return mFormat.format(value) + " $"; // e.g. append a dollar-sign
}
}使用
// usage on whole data object lineData.setValueFormatter(new MyValueFormatter()); // usage on individual dataset object lineDataSet.setValueFormatter(new MyValueFormatter());预定义的格式
LargeValueFormatter: 用于较大值得转换,如1000转换成1k,不支持小数点。
PercentFormatter:使用百分号,在饼状图中特别有用
StackedValueFormatter: 在 BarChart中使用. 它允许指定所有的堆栈值是否应绘制,或者是最高值
public class MyCustomXAxisValueFormatter implements XAxisValueFormatter {
@Override
public String getXValue(String original, int index, ViewPortHandler viewPortHandler) {
// original is the original value to use, x-index is the index in your x-values array
// implement your logic here ...
return ...;
}
}
// usage on XAxis, get axis instance: XAxis xAxis = chart.getXAxis(); // set the formatter xAxis.setValueFormatter(new MyCustomXAxisValueFormatter());
自定义格式转换MyCustomXValueFormatter
public class MyYAxisValueFormatter implements YAxisValueFormatter {
private DecimalFormat mFormat;
public MyYAxisValueFormatter () {
mFormat = new DecimalFormat("###,###,##0.0"); // use one decimal
}
@Override
public String getFormattedValue(float value, YAxis yAxis) {
// write your logic here
// access the YAxis object to get more information
return mFormat.format(value) + " $"; // e.g. append a dollar-sign
}
}设置
// get an instance of the YAxis (e.g. left axis) YAxis leftAxis = chart.getAxisLeft(); leftAxis.setValueFormatter(new MyYAxisValueFormatter());
折线图,条形表,散点图,K线图,气泡图
setAutoScaleMinMaxEnabled(boolean enabled): 指示是否启用在y轴自动缩放。如果启用,当视角变换时,Y轴自动调整到和当前的X轴范围适应的最小值,默认值false
setKeepPositionOnRotation(boolean enabled): 伸缩之后是否能还原到原来的位置. 默认值false
Legend legend = chart.getLegend();
setEnabled(boolean enabled)
Legend l = chart.getLegend();
l.setFormSize(10f); // set the size of the legend forms/shapes
l.setForm(LegendForm.CIRCLE); // set what type of form/shape should be used
l.setPosition(LegendPosition.BELOW_CHART_LEFT);
l.setTypeface(...);
l.setTextSize(12f);
l.setTextColor(Color.BLACK);
l.setXEntrySpace(5f); // set the space between the legend entries on the x-axis
l.setYEntrySpace(5f); // set the space between the legend entries on the y-axis
// set custom labels and colors
l.setCustom(ColorTemplate.VORDIPLOM_COLORS, new String[] { "Set1", "Set2", "Set3", "Set4", "Set5" });
// and many more...
// EXAMPLE 1 // add entries to the "data" object exampleData.addEntry(...); chart.notifyDataSetChanged(); // let the chart know it's data changed chart.invalidate(); // refresh // EXAMPLE 2 // add entries to "dataSet" object dataSet.addEntry(...); exampleData.notifyDataChanged(); // let the data know a dataSet changed chart.notifyDataSetChanged(); // let the chart know it's data changed chart.invalidate(); // refresh
示例
DynamicalAddingActivity
RealtimeDataActivity
这个库有多种修改视图方法,需要注意的是,这些方法只适用于LineChart, BarChart, ScatterChart and CandleStickChart。
下面提到的方法是Chart类提供的方法,另一种方法是通过ViewPortHandler直接访问,但是这种方法只推荐熟悉API的高级用户使用。
限制可见性
setVisibleXRangeMaximum(float maxXRange): 设置X轴上可见区域的最大值。
setVisibleXRangeMinimum(float minXRange):设置X轴上可见的最小值,限制最小缩放。
setVisibleYRangeMaximum(float maxYRange, AxisDependency axis): 设置Y轴可见的最大值。
setViewPortOffsets(float left, float top, float right, float bottom): 设置视图偏移量,影响自动偏移量,使用resetViewPortOffsets()方法撤销操作
etExtraOffsets(float left, float top, float right, float bottom):设置额外的偏移量,这样做不会改变自动偏移量
移动视图
fitScreen(): 充满边界
moveViewToX(float xValue):移动到X轴固定位置
moveViewTo(float xValue, float yValue, AxisDependency axis): 移动到X,Y轴固定位置
移动动画设置
moveViewToAnimated(float xValue, float yValue, AxisDependency axis, long duration)
centerViewToAnimated(float xValue, float yValue, AxisDependency axis, long duration)
注意:所有的moveViewTo方法都会自动刷新视图,不需要调用invalidate()方法
缩放
zoomIn()
zoomOut():
zoom(float scaleX, float scaleY, float x, float y):
zoom(float scaleX, float scaleY, float xValue, float yValue, AxisDependency axis):
缩放动画
zoomAndCenterAnimated(float scaleX, float scaleY, float xValue, float yValue, AxisDependency axis, long duration):
chart.setData(...); // first set data // now modify viewport chart.setVisibleXRangeMaximum(20); // allow 20 values to be displayed at once on the x-axis, not more chart.moveViewToX(10); // set the left edge of the chart to x-index 10 // moveViewToX(...) also calls invalidate()
mChart.animateX(3000); // animate horizontal 3000 milliseconds // or: mChart.animateY(3000); // animate vertical 3000 milliseconds // or: mChart.animateXY(3000, 3000); // animate horizontal and vertical 3000 milliseconds如果调用那个了animate(...) 方法, 不需要调用 invalidate() 就可以刷新图表
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, EasingFunction function);
/**
* 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);
}
public class CustomMarkerView extends MarkerView {
private TextView tvContent;
public CustomMarkerView (Context context, int layoutResource) {
super(context, layoutResource);
// this markerview only displays a textview
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.getVal()); // set the entry-value as the display text
}
@Override
public int getXOffset(float xpos) {
// this will center the marker-view horizontally
return -(getWidth() / 2);
}
@Override
public int getYOffset(float ypos) {
// this will cause the marker-view to be above the selected value
return -getHeight();
}
}使用CustomMarkerView mv = new CustomMarkerView(Context, R.layout.custom_marker_view_layout); // set the marker to the chart chart.setMarkerView(mv);
欢迎扫描二维码,关注公众账号
android开源图表库MPAndroidChart文档翻译(中)
标签:
原文地址:http://blog.csdn.net/robertcpp/article/details/51622083