没错!还是去年的这个时候的笔记,存一下。
核心功能:
支持x,y轴缩放
支持拖拽
支持手指滑动
支持高亮显示
支持保存图表到文件中
支持从文件(txt)中读取数据
预先定义颜色模板
自动生成标注
支持自定义x,y轴的显示标签
支持x,y轴动画
支持x,y轴设置最大值和附加信息
支持自定义字体,颜色,背景,手势,虚线等
主要的Api方法:
setDescription(String desc): 设置表格的描述
setDescriptionTypeface(Typefacet) :自定义表格中显示的字体
setDrawYValues(booleanenabled) : 设置是否显示y轴的值的数据
setValuePaintColor(intcolor) :设置表格中y轴的值的颜色,但是必须设置setDrawYValues(true)
setValueTypeface(Typefacet):设置字体
setValueFormatter(DecimalFormatformat) : 设置显示的格式
setPaint(Paint p, intwhich) : 自定义笔刷
public ChartDatagetDataCurrent() :返回ChartData对象当前显示的图表。它包含了所有信息的显示值最小和最大值等
public float getYChartMin(): 返回当前最小值
public float getYChartMax(): 返回当前最大值
public float getAverage() :返回所有值的平均值。
public float getAverage(inttype) : 返回平均值
public PointF getCenter() :返回中间点
public Paint getPaint(intwhich) : 得到笔刷
setTouchEnabled(booleanenabled) : 设置是否可以触摸,如为false,则不能拖动,缩放等
setDragScaleEnabled(booleanenabled) : 设置是否可以拖拽,缩放
setOnChartValueSelectedListener(OnChartValueSelectedListenerl): 设置表格上的点,被点击的时候,的回调函数
setHighlightEnabled(booleanenabled) : 设置点击value的时候,是否高亮显示
public voidhighlightValues(Highlight[] highs) : 设置高亮显示
saveToGallery(String title): 保存图表到图库中
saveToPath(String title,String pathOnSD) : 保存.
setScaleMinima(float x,float y) : 设置最小的缩放
centerViewPort(int xIndex,float val) : 设置视口
fitScreen() : 适应屏幕
// 设置 pieChart 图表基本属性
mChart.setUsePercentValues(false); //使用百分比显示
mChart.getDescription().setEnabled(false); //设置pieChart图表的描述
mChart.setBackgroundColor(Color.YELLOW); //设置pieChart图表背景色
mChart.setExtraOffsets(5,10, 60, 10); //设置pieChart图表上下左右的偏移,类似于外边距
mChart.setDragDecelerationFrictionCoef(0.95f);//设置pieChart图表转动阻力摩擦系数[0,1]
mChart.setRotationAngle(0); //设置pieChart图表起始角度
mChart.setRotationEnabled(true); //设置pieChart图表是否可以手动旋转
mChart.setHighlightPerTapEnabled(true); //设置piecahrt图表点击Item高亮是否可用
mChart.animateY(1400,Easing.EasingOption.EaseInOutQuad);// 设置pieChart图表展示动画效果
// 设置 pieChart 图表Item文本属性
mChart.setDrawEntryLabels(true); //设置pieChart是否只显示饼图上百分比不显示文字(true:下面属性才有效果)
mChart.setEntryLabelColor(Color.WHITE); //设置pieChart图表文本字体颜色
mChart.setEntryLabelTypeface(mTfRegular); //设置pieChart图表文本字体样式
mChart.setEntryLabelTextSize(10f); //设置pieChart图表文本字体大小
// 设置 pieChart 内部圆环属性
mChart.setDrawHoleEnabled(true); //是否显示PieChart内部圆环(true:下面属性才有意义)
mChart.setHoleRadius(28f); //设置PieChart内部圆的半径(这里设置28.0f)
mChart.setTransparentCircleRadius(31f); //设置PieChart内部透明圆的半径(这里设置31.0f)
mChart.setTransparentCircleColor(Color.BLACK);//设置PieChart内部透明圆与内部圆间距(31f-28f)填充颜色
mChart.setTransparentCircleAlpha(50); //设置PieChart内部透明圆与内部圆间距(31f-28f)透明度[0~255]数值越小越透明
mChart.setHoleColor(Color.WHITE); //设置PieChart内部圆的颜色
mChart.setDrawCenterText(true); //是否绘制PieChart内部中心文本(true:下面属性才有意义)
mChart.setCenterTextTypeface(mTfLight); //设置PieChart内部圆文字的字体样式
mChart.setCenterText("Test"); //设置PieChart内部圆文字的内容
mChart.setCenterTextSize(10f); //设置PieChart内部圆文字的大小
mChart.setCenterTextColor(Color.RED); //设置PieChart内部圆文字的颜色
// pieChart添加数据
setData();
// 获取pieCahrt图列
Legend l = mChart.getLegend();
l.setEnabled(true); //是否启用图列(true:下面属性才有意义)
l.setVerticalAlignment(Legend.LegendVerticalAlignment.TOP);
l.setHorizontalAlignment(Legend.LegendHorizontalAlignment.RIGHT);
l.setOrientation(Legend.LegendOrientation.VERTICAL);
l.setForm(Legend.LegendForm.DEFAULT);//设置图例的形状
l.setFormSize(10); //设置图例的大小
l.setFormToTextSpace(10f); //设置每个图例实体中标签和形状之间的间距
l.setDrawInside(false);
l.setWordWrapEnabled(true); //设置图列换行(注意使用影响性能,仅适用legend位于图表下面)
l.setXEntrySpace(10f); //设置图例实体之间延X轴的间距(setOrientation = HORIZONTAL有效)
l.setYEntrySpace(8f); //设置图例实体之间延Y轴的间距(setOrientation = VERTICAL 有效)
l.setYOffset(0f); //设置比例块Y轴偏移量
l.setTextSize(14f); //设置图例标签文本的大小
l.setTextColor(Color.parseColor("#ff9933"));//设置图例标签文本的颜色
//pieChart 选择监听
mChart.setOnChartValueSelectedListener(this);
//设置MARKERVIEW
CustomMarkerView mv = newCustomMarkerView(this, new PercentFormatter());
mv.setChartView(mChart);
mChart.setMarker(mv);
/**
* 设置饼图的数据
*/
private void setData(){
ArrayList<PieEntry> pieEntryList =new ArrayList<PieEntry>();
ArrayList<Integer> colors = newArrayList<Integer>();
colors.add(Color.parseColor("#f17548"));
colors.add(Color.parseColor("#FF9933"));
//饼图实体 PieEntry
PieEntry CashBalance = new PieEntry(70,"现金余额 1500");
PieEntry ConsumptionBalance = newPieEntry(30, "消费余额 768");
pieEntryList.add(CashBalance);
pieEntryList.add(ConsumptionBalance);
//饼状图数据集 PieDataSet
PieDataSet pieDataSet = newPieDataSet(pieEntryList, "资产总览");
pieDataSet.setSliceSpace(3f); //设置饼状Item之间的间隙
pieDataSet.setSelectionShift(10f); //设置饼状Item被选中时变化的距离
pieDataSet.setColors(colors); //为DataSet中的数据匹配上颜色集(饼图Item颜色)
//最终数据 PieData
PieData pieData = new PieData(pieDataSet);
pieData.setDrawValues(true); //设置是否显示数据实体(百分比,true:以下属性才有意义)
pieData.setValueTextColor(Color.BLUE); //设置所有DataSet内数据实体(百分比)的文本颜色
pieData.setValueTextSize(12f); //设置所有DataSet内数据实体(百分比)的文本字体大小
pieData.setValueTypeface(mTfLight); //设置所有DataSet内数据实体(百分比)的文本字体样式
pieData.setValueFormatter(newPercentFormatter());//设置所有DataSet内数据实体(百分比)的文本字体格式
mChart.setData(pieData);
mChart.highlightValues(null);
mChart.invalidate(); //将图表重绘以显示设置的属性和数据
}