private void init(final LineChart mChart) {
//右下角描述字體顏色
mChart.setDescriptionColor(Color.WHITE);
// 在chart上的右下角加描述
mChart.setDescription("kg");
// enable value highlighting
mChart.setHighlightEnabled(true);
// enable touch gestures
//設置是否可以觸碰
mChart.setTouchEnabled(true);
mChart.setDragDecelerationFrictionCoef(0.9f);
// enable scaling and dragging
mChart.setDragEnabled(true);
mChart.setScaleEnabled(true);
mChart.setDrawGridBackground(false);
mChart.setHighlightPerDragEnabled(true);
// if disabled, scaling can be done on x- and y-axis separately
mChart.setPinchZoom(true);
// 是否可以通過雙擊放大
mChart.setDoubleTapToZoomEnabled(true);
// set an alternative background color
// mChart.setBackgroundColor(528194);
// mChart.setBackgroundColor(R.color.bg_blue);
mChart.getAxisRight().setEnabled(false);
mChart.animateX(2500);
Typeface tf = Typeface.create(Typeface.SANS_SERIF, Typeface.NORMAL);
// get the legend (only possible after setting data)
Legend l = mChart.getLegend();
// modify the legend ...
l.setForm(LegendForm.CIRCLE);
l.setTypeface(tf);
l.setTextSize(11f);
l.setTextColor(Color.WHITE);
l.setPosition(LegendPosition.BELOW_CHART_LEFT);
XAxis xAxis = mChart.getXAxis();
xAxis.setTypeface(tf);
xAxis.setTextSize(12f);
xAxis.setPosition(XAxisPosition.BOTTOM);
// x軸坐標字體顏色
xAxis.setTextColor(Color.WHITE);
xAxis.setDrawGridLines(false);
// 是否繪制x軸
xAxis.setDrawAxisLine(false);
xAxis.setSpaceBetweenLabels(1);
// Normal line
LimitLine ll1 = null;
ll1 = new LimitLine();
ll1.setLineWidth(0f);
ll1.enableDashedLine(5f, 0f, 0f);
ll1.setLabelPosition(LimitLabelPosition.RIGHT_TOP);
ll1.setTextSize(10f);
ll1.setLineColor(Color.WHITE);
// ll1.setTextColor(Color.BLACK);
//y軸初始化
YAxis leftAxis = mChart.getAxisLeft();
leftAxis.addLimitLine(ll1);
leftAxis.setTypeface(tf);
//y軸字體顏色
leftAxis.setTextColor(Color.WHITE);
//是否繪制y軸
leftAxis.setDrawAxisLine(false);
leftAxis.setAxisMaxValue(150);
// 設置Y軸展示數據范圍--最小值
leftAxis.setAxisMinValue(0);
// leftAxis.setAxisMinValue(34f);
//y軸的顏色
leftAxis.setAxisLineColor(Color.WHITE);
leftAxis.setStartAtZero(false);
leftAxis.enableGridDashedLine(10f, 10f, 10f);
// 是否繪制網格線
leftAxis.setDrawGridLines(true);
leftAxis.setGridColor(Color.WHITE);
//圖表上點的數據展示 MyMarkView要自己創建
MyMarkView mv = new MyMarkView(this.getActivity());?
mChart.setMarkerView(mv);
// 設置圖表點擊事件, 監聽高亮位置
mChart.setOnChartValueSelectedListener(new OnChartValueSelectedListener() {
@Override
public void onValueSelected(Entry e, int dataSetIndex, Highlight h) {
entry = e;
}
@Override
public void onNothingSelected() {
((HistoryDataActivity) context).changePage(3);
Intent intent = new Intent(
Constant.ActionConstant.ACTION_HISTORY_YEAR);
intent.putExtra("history_year", entry.getXIndex());
context.sendBroadcast(intent);
}
});
setData();
}
private void setData() {
if (yearWeightList == null)?
{return;}
ArrayListxVals = new ArrayList();
ArrayListyVals1 = new ArrayList();
for (int i = 0; i < yearWeightList.size(); i++)
?{
int num = yearWeightList.get(i);
// xVals.add(i+"");
yVals1.add(new Entry(num, i + 1));
}
//x軸數據 為String類型
xVals.add("");
xVals.add("1");
xVals.add("2");
xVals.add("3");
xVals.add("4");
xVals.add("5");
xVals.add("6");
xVals.add("7");
xVals.add("8");
xVals.add("9");
xVals.add("10");
xVals.add("11");
xVals.add("12");
xVals.add("");
// 創建一個數據集,并給出它的類型
LineDataSet set1 = null;set1 = new LineDataSet(yVals1, "");
set1.setAxisDependency(AxisDependency.LEFT);
// 左下角圓圈顏色 以及圖表上連線的顏色
set1.setColor(Color.TRANSPARENT);
// 圖表數據圓圈顏色
set1.setCircleColor(Color.WHITE);
set1.setLineWidth(0f);
// 取消數值的顯示,如何獲取當前數量
int valuesCount = set1.getValueCount();
//圖表上的點是否展示數據
set1.setDrawValues(false);
set1.setCircleSize(2f);
set1.setValueTextColor(Color.WHITE);
// 是否可以填充
set1.setDrawFilled(true);
// 填充顏色
set1.setFillColor(ColorTemplate.getHoloBlue());
// 填充透明度
set1.setFillAlpha(45);
// 設置曲線允許平滑 決定是曲線true還是直線false 若為曲線相鄰兩點連線會發生彎曲
set1.setDrawCubic(true);
// 設置曲線平滑度
set1.setCubicIntensity(0.1f);
set1.setHighLightColor(Color.rgb(244,117,?117));
set1.setDrawCircleHole(false);
ArrayListdataSets = new ArrayList();
dataSets.add(set1);
// create a data object with the datasets
LineData data = new LineData(xVals, dataSets);
// 數據顏色
data.setValueTextColor(Color.WHITE);
data.setValueTextSize(9f);
// set data
mChart.setData(data);
mChart.invalidate();
}