Hellocharts框架是android的第三方好用的图表库。
1、Gradle导入
compile 'com.github.lecho:hellocharts-android:v1.5.8'
2、因为我使用的是LineChartView,其他的类推
初始化工作
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my_level);
abilityChart = (LineChartView) findViewById(R.id.abilityChart);
myRankChart = (LineChartView) findViewById(R.id.myRankChart);
getAxisXLables();//获取X轴标注
initTheatLineChart();
}
getAxisLables
//x轴的显示
private void getAxisXLables(){
for (int i=0;i<date.length;i++){
mAxisXValuesList.add(new AxisValue(i).setLabel(date[i]));
}
}
//初始化abilityLineChart的设置
private void initTheatLineChart() {
getAxisPoints();//获取坐标点
Line line = new Line(mTheatPointValuesList).setColor(Color.parseColor("#E9967A")).setCubic(true);//折线的颜色
List<Line> lines = new ArrayList<>();
line.setShape(ValueShape.CIRCLE);//折线图上每个数据点的形状
line.setCubic(true);//曲线是否平滑
line.setFilled(true);//是否填充曲线的面积
//line.setHasLabels(true);//曲线的数据坐标是否加上备注
line.setHasLabelsOnlyForSelected(true);//点击坐标显示数据
line.setHasLines(true);//是否用直线显示
line.setHasPoints(true);//是否显示原点
lines.add(line);
LineChartData data = new LineChartData();
data.setLines(lines);
//坐标轴
Axis axisX = new Axis();//x轴
axisX.setHasTiltedLabels(false);//X轴下面坐标轴字体是斜的显示还是直的显示,true是斜的显示
axisX.setTextColor(Color.WHITE);
axisX.setTextColor(Color.parseColor("#336699"));//灰色
axisX.setName("能力曲线");//表格名称
axisX.setTextSize(20);//设置字体的大小
axisX.setMaxLabelChars(7);//最多几个X轴坐标,意思就是你的缩放让X轴上数据的个数7<=x<=mAxisValues.length
axisX.setValues(mAxisXValuesList);//填充X轴的坐标名称
data.setAxisXBottom(axisX);//X轴在底部
Axis axisY = new Axis();//Y轴
axisY.setName("");//Y轴标注
axisY.setHasLines(true);
axisY.setTextSize(11);
axisY.setTextColor(Color.parseColor("#336699"));
data.setAxisYLeft(axisY);//Y轴设置在左边
abilityChart.setInteractive(true);
abilityChart.setZoomType(ZoomType.HORIZONTAL);//缩放类型
abilityChart.setMaxZoom((float)3);//缩放比例
abilityChart.setLineChartData(data);
//abilityChart.setVisibility(View.VISIBLE);
Viewport v = new Viewport(abilityChart.getMaximumViewport());
v.left = 0;
v.right = 7;
abilityChart.setCurrentViewport(v);
}
//能力曲线的每个点的显示
private void getAxisPoints() {
for (int i=0;i<score.length;i++){
mTheatPointValuesList.add(new PointValue(i,(float)theat[i]));
}
}
String[] date = {"5-23","5-24","5-25","5-26","5-27","5-28","5-29","5-30","5-31","6-1","6-2","6-3"};//X轴的标注
double[] theat = {2.3,1.98,2.02,1.68,1.84,1.96,2.10,2.06,2.08,2.11,2.09,2.11};//能力曲线点数据
private List<PointValue> mTheatPointValuesList = new ArrayList<>();
private List<AxisValue> mAxisXValuesList = new ArrayList<>();