废话少说直接上代码。
通过重写LineDataSet类实现背景颜色不同;
通过重写view类实现点的颜色不同;
重写LineDataSet类
通过重写getColor方法背景颜色不同;
public class Linechrset extends LineDataSet {
List<Entry> list;
public Linechrset(List<Entry> yVals, String label) {
super(yVals, label);
list=yVals;
}
@Override
public int getColor(int index) {
if(index<(list.size()-1)){
if((list.get(index+1).getVal())<50){
return mColors.get(0);
}else if(list.get(index+1).getVal()<70){
return mColors.get(1);
}else{
return mColors.get(2);
}
}else{
return mColors.get(0);
}
}
}
MainActivity
public class MainActivity extends AppCompatActivity {
private RelativeLayout adasdasd;
Runtt runnable=new Runtt();
private LineChart mChart;
int i=0;
private ArrayList<String> valueg = new ArrayList<>();
private ArrayList<Entry> values = new ArrayList<>();
private ArrayList<Map<String, Float>> list = new ArrayList<>();
private ArrayList<Integer> closs = new ArrayList<>();
private Handler handler;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mChart = (LineChart) findViewById(R.id.test_lineChart);
adasdasd = (RelativeLayout) findViewById(R.id.adasdasd);
handler = new Handler();
handler.postDelayed(runnable,3000);
}
class Runtt implements Runnable {
@Override
public void run() {
mChart.setTouchEnabled(false);
XAxis xAxis = mChart.getXAxis();
xAxis.setDrawAxisLine(false);
xAxis.setDrawLabels(true);
YAxis leftAxis = mChart.getAxisLeft();
leftAxis.removeAllLimitLines();
Handler handleree=new Handler();
handleree.postDelayed(new Runnable() {
@Override
public void run() {
closs=getBgee();
list=getBgeexy();
Relativett relativett=new Relativett(getApplicationContext(),list,closs);
adasdasd.addView(relativett);
}
},300);
adasdasd.removeAllViews();
leftAxis.enableGridDashedLine(10f, 10f, 0f);//设置虚线
leftAxis.setDrawAxisLine(false);
leftAxis.setDrawGridLines(false);
mChart.getXAxis().setPosition(XAxis.XAxisPosition.BOTTOM);
// mChart.getAxisRight().setEnabled(false);
valueg.add("第"+i+"名");
Random random=new Random();
int eeee=random.nextInt(100);
values.add(new Entry(eeee, i));
Legend l = mChart.getLegend();
l.setFormSize(50f);
l.setForm(Legend.LegendForm.LINE);
l.setPosition(Legend.LegendPosition.BELOW_CHART_CENTER);
Linechrset set1 = new Linechrset(values, "听力线");
set1.setColors(new int[]{Color.YELLOW,Color.GREEN,Color.BLUE});
set1.setDrawValues(false) ;
mChart.notifyDataSetChanged();
mChart.invalidate();
set1.setDrawCircles(false);
set1.setLineWidth(1f);
set1.setValueTextSize(9f);
LineData data = new LineData(valueg,set1);
mChart.setData(data);
i++;
handler.postDelayed(runnable,3000);
}
}
private ArrayList<Integer> getBgee() {
ArrayList<Integer> bgList = new ArrayList<>();
for(int i=0;i<values.size();i++){
if(values.get(i).getVal()<60){bgList.add(Color.RED);}else{bgList.add(Color.GREEN);}
}
return bgList;
}
private ArrayList<Map<String, Float>> getBgeexy() {
ArrayList<Map<String, Float>> bgList = new ArrayList<>();
for (int i=0;i<values.size();i++) {
PointD p = mChart.getPixelsForValues(values.get(i).getXIndex(), values.get(i).getVal(),YAxis.AxisDependency.LEFT);
Map<String,Float> map=new HashMap<>();
map.put("x", (float) p.x);
map.put("y", (float) p.y);
bgList.add(map);
}
return bgList;
}
}
重写view
画点
public class Relativett extends View {
ArrayList<Map<String,Float>> dian=new ArrayList<>();
ArrayList<Integer> colore=new ArrayList<>();
Paint paint = new Paint();
public Relativett(Context context,ArrayList<Map<String,Float>> dian,ArrayList<Integer> colore) {
super(context);
this.dian=dian;
this.colore=colore;
}
@Override
protected void onDraw(Canvas canvas) {
for(int ee=0;ee<dian.size();ee++){
paint.setColor(colore.get(ee));
canvas.drawCircle(dian.get(ee).get("x"), dian.get(ee).get("y"), 5, paint);
}
super.onDraw(canvas);
}
}