安卓画图 ——雷达图 使用mpandroidchartlibrary-2-1-6
程序员文章站
2022-03-25 14:02:48
...
效果图
主要代码
clearChart(mChart);
x.add("击杀");
x.add("助攻");
x.add("CS");
x.add("视野");
x.add("目标");
x.add("生存");
y.add(0, new Entry(95,0));
y.add(1, new Entry(60,1));
y.add(2, new Entry(40,2));
y.add(3, new Entry(80,3));
y.add(4, new Entry(85,4));
y.add(5, new Entry(75,5));
RadarDataSet radarDataSet = new RadarDataSet(y, "吹逼怪能力图");
radarDataSet.setLineWidth(1f);//线宽
radarDataSet.setColor(Color.RED);//颜色
radarDataSet.setHighLightColor(Color.WHITE);//高度线的颜色
radarDataSet.setColor(ColorTemplate.VORDIPLOM_COLORS[0]);
// 实心填充
radarDataSet.setFillColor(ColorTemplate.VORDIPLOM_COLORS[0]);
radarDataSet.setDrawFilled(true);
radarDataSets.add(radarDataSet);
radarData = new RadarData(x, radarDataSets);
mChart.setData(radarData);
全部代码:
xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="0.5"
android:orientation="horizontal" >
<com.github.mikephil.charting.charts.RadarChart
android:id="@+id/chart"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="0.5"
android:orientation="vertical" >
<Button
android:id="@+id/bt1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="0.25"
android:text="吹逼怪"/>
<Button
android:id="@+id/bt2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="0.25"
android:text="蛇皮怪"/>
<Button
android:id="@+id/bt3"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="0.25"
android:text="砍人王"/>
<Button
android:id="@+id/bt4"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="0.25"
android:text="铁头娃"/>
</LinearLayout>
</LinearLayout>
java代码:
package com.example.something_good;
import java.util.ArrayList;
import com.github.mikephil.charting.charts.RadarChart;
import com.github.mikephil.charting.components.Legend;
import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.data.RadarData;
import com.github.mikephil.charting.data.RadarDataSet;
import com.github.mikephil.charting.utils.ColorTemplate;
import android.os.Bundle;
import android.app.Activity;
import android.graphics.Color;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends Activity {
public RadarChart mChart;
public Button bt1,bt2,bt3,bt4;
public ArrayList<String> x = new ArrayList<String>();
public ArrayList<Entry> y = new ArrayList<Entry>();
public ArrayList<RadarDataSet> radarDataSets = new ArrayList<RadarDataSet>();
public RadarData radarData = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mChart=(RadarChart) findViewById(R.id.chart);
bt1=(Button) findViewById(R.id.bt1);
bt2=(Button) findViewById(R.id.bt2);
bt3=(Button) findViewById(R.id.bt3);
bt4=(Button) findViewById(R.id.bt4);
mChart.setDescription("个人能力图");//数据描述、
mChart.setNoDataTextDescription("数据");//没数据显示
mChart.setBackgroundColor(Color.WHITE);//背景颜色
Legend legend = mChart.getLegend();//设置实例
legend.setForm(Legend.LegendForm.CIRCLE);//样式
legend.setFormSize(20f);//字体
legend.setTextColor(Color.RED);//设置颜色
mChart.animateY(2000);//X轴的动画
bt1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
clearChart(mChart);
x.add("击杀");
x.add("助攻");
x.add("CS");
x.add("视野");
x.add("目标");
x.add("生存");
y.add(0, new Entry(95,0));
y.add(1, new Entry(60,1));
y.add(2, new Entry(40,2));
y.add(3, new Entry(80,3));
y.add(4, new Entry(85,4));
y.add(5, new Entry(75,5));
RadarDataSet radarDataSet = new RadarDataSet(y, "吹逼怪能力图");
radarDataSet.setLineWidth(1f);//线宽
radarDataSet.setColor(Color.RED);
radarDataSet.setHighLightColor(Color.WHITE);
radarDataSet.setColor(ColorTemplate.VORDIPLOM_COLORS[0]);
radarDataSet.setFillColor(ColorTemplate.VORDIPLOM_COLORS[0]);
radarDataSet.setDrawFilled(true);
radarDataSets.add(radarDataSet);
radarData = new RadarData(x, radarDataSets);
mChart.setData(radarData);
}
});
bt2.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
clearChart(mChart);
x.add("击杀");
x.add("助攻");
x.add("CS");
x.add("视野");
x.add("目标");
x.add("生存");
y.add(0, new Entry(70,0));
y.add(1, new Entry(100,1));
y.add(2, new Entry(65,2));
y.add(3, new Entry(92,3));
y.add(4, new Entry(88,4));
y.add(5, new Entry(65,5));
RadarDataSet radarDataSet = new RadarDataSet(y, "蛇皮怪能力图");
radarDataSet.setLineWidth(1f);
radarDataSet.setColor(Color.RED);
radarDataSet.setHighLightColor(Color.WHITE);
radarDataSet.setColor(ColorTemplate.VORDIPLOM_COLORS[1]);
radarDataSet.setFillColor(ColorTemplate.VORDIPLOM_COLORS[1]);
radarDataSet.setDrawFilled(true);
radarDataSets.add(radarDataSet);
radarData = new RadarData(x, radarDataSets);
mChart.setData(radarData);
}
});
bt3.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
clearChart(mChart);
x.add("击杀");
x.add("助攻");
x.add("CS");
x.add("视野");
x.add("目标");
x.add("生存");
y.add(0, new Entry(100,0));
y.add(1, new Entry(80,1));
y.add(2, new Entry(95,2));
y.add(3, new Entry(90,3));
y.add(4, new Entry(99,4));
y.add(5, new Entry(90,5));
RadarDataSet radarDataSet = new RadarDataSet(y, "砍人王能力图");
radarDataSet.setLineWidth(1f);
radarDataSet.setColor(Color.RED);
radarDataSet.setHighLightColor(Color.WHITE);
radarDataSet.setColor(ColorTemplate.VORDIPLOM_COLORS[2]);
radarDataSet.setFillColor(ColorTemplate.VORDIPLOM_COLORS[2]);
radarDataSet.setDrawFilled(true);
radarDataSets.add(radarDataSet);
radarData = new RadarData(x, radarDataSets);
mChart.setData(radarData);
}
});
bt4.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
clearChart(mChart);
x.add("击杀");
x.add("助攻");
x.add("CS");
x.add("视野");
x.add("目标");
x.add("生存");
y.add(0, new Entry(95,0));
y.add(1, new Entry(95,1));
y.add(2, new Entry(95,2));
y.add(3, new Entry(95,3));
y.add(4, new Entry(95,4));
y.add(5, new Entry(95,5));
RadarDataSet radarDataSet = new RadarDataSet(y, "铁头娃能力图");
radarDataSet.setLineWidth(1f);//线宽
radarDataSet.setColor(Color.RED);//现实颜色
radarDataSet.setHighLightColor(Color.WHITE);//高度线的颜色
radarDataSet.setColor(ColorTemplate.VORDIPLOM_COLORS[2]);
radarDataSet.setFillColor(ColorTemplate.VORDIPLOM_COLORS[2]);
radarDataSet.setDrawFilled(true);
radarDataSets.add(radarDataSet);
radarData = new RadarData(x, radarDataSets);
mChart.setData(radarData);
}
});
}
//刷新代码
public void clearChart(View view) {
mChart.clear();
mChart.invalidate();
x.clear();
y.clear();
radarDataSets.clear();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}