<menu xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" xmlns:app="http://schemas.android.com/apk/res-auto" tools:context=".DemoActivity" > <item android:id="@+id/BarChart" android:title="BarChart" android:orderInCategory="100" app:showAsAction="never" /> <item android:id="@+id/LineChart" android:title="LineChart" android:orderInCategory="100"></item> <item android:id="@+id/PieChart" android:title="PieChart" android:orderInCategory="100"></item> <item android:id="@+id/ScatterChart" android:title="ScatterChart" android:orderInCategory="100" app:showAsAction="never" /> <item android:id="@+id/RadarChart" android:title="RadarChart" android:orderInCategory="100" app:showAsAction="never" /> </menu>activity_main.xml文件如下
<RelativeLayout 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" > <RelativeLayout android:id="@+id/content" android:layout_width="fill_parent" android:layout_height="fill_parent" /> </RelativeLayout>
package com.example.chenyu.mpandroidcharttest; import android.app.FragmentManager; import android.app.FragmentTransaction; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; import android.view.ViewConfiguration; import java.lang.reflect.Field; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); FragmentManager fm = getFragmentManager(); // 开启Fragment事务 FragmentTransaction transaction = fm.beginTransaction(); BarCharFragment barCharFragment = new BarCharFragment(); transaction.replace(R.id.content, barCharFragment); transaction.commit(); //显示右上角的3个点 makeActionOverflowMenuShown(); } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.menu_main, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { FragmentManager fm = getFragmentManager(); // 开启Fragment事务 FragmentTransaction transaction = fm.beginTransaction(); int id = item.getItemId(); switch (id) { case R.id.BarChart: BarCharFragment barCharFragment = new BarCharFragment(); transaction.replace(R.id.content, barCharFragment); transaction.commit(); return true; case R.id.LineChart: LineCharFragment lineCharFragment = new LineCharFragment(); transaction.replace(R.id.content, lineCharFragment); transaction.commit(); return true; case R.id.RadarChart: RadarCharFragment radarCharFragment = new RadarCharFragment(); transaction.replace(R.id.content, radarCharFragment); transaction.commit(); return true; case R.id.PieChart: PieCharFragment pieCharFragment = new PieCharFragment(); transaction.replace(R.id.content, pieCharFragment); transaction.commit(); return true; case R.id.ScatterChart: ScatterChartFragment scatterChartFragment = new ScatterChartFragment(); transaction.replace(R.id.content, scatterChartFragment); transaction.commit(); return true; } return super.onOptionsItemSelected(item); } private void makeActionOverflowMenuShown() { //devices with hardware menu button (e.g. Samsung Note) don't show action overflow menu try { ViewConfiguration config = ViewConfiguration.get(this); Field menuKeyField = ViewConfiguration.class.getDeclaredField("sHasPermanentMenuKey"); if (menuKeyField != null) { menuKeyField.setAccessible(true); menuKeyField.setBoolean(config, false); } } catch (Exception e) { } } }
package com.example.chenyu.mpandroidcharttest; import android.app.Fragment; import android.os.Bundle; import android.support.annotation.Nullable; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import com.github.mikephil.charting.charts.BarChart; import com.github.mikephil.charting.components.LimitLine; import com.github.mikephil.charting.data.BarData; import com.github.mikephil.charting.data.BarDataSet; import com.github.mikephil.charting.data.BarEntry; import com.github.mikephil.charting.utils.ColorTemplate; import java.util.ArrayList; /** * Created by Think on 2015/11/29. */ public class BarCharFragment extends Fragment { public BarChart barChart; public ArrayList<BarEntry> entries = new ArrayList<BarEntry>(); public BarDataSet dataset; public ArrayList<String> labels = new ArrayList<String>(); @Nullable @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.bar_chart, container, false); barChart = (BarChart) view.findViewById(R.id.bar_chart); initEntriesData(); initLableData(); show(); return view; } public void initEntriesData() { entries.add(new BarEntry(4f, 0)); entries.add(new BarEntry(8f, 1)); entries.add(new BarEntry(6f, 2)); entries.add(new BarEntry(12f, 3)); entries.add(new BarEntry(18f, 4)); entries.add(new BarEntry(9f, 5)); } public void initLableData() { labels.add("一月"); labels.add("二月"); labels.add("三月"); labels.add("四月"); labels.add("五月"); labels.add("六月"); } public void show() { dataset = new BarDataSet(entries, "# of Calls"); dataset.setColors(ColorTemplate.COLORFUL_COLORS); BarData data = new BarData(labels, dataset); LimitLine line = new LimitLine(10f); barChart.setData(data); // chart.animateXY(5000,5000); // chart.animateX(5000); barChart.animateY(2000); barChart.setDescription("hello MPandroidChart"); } }
ArrayList<String> labels = new ArrayList<String>(); labels.add("January"); labels.add("February"); labels.add("March"); labels.add("April"); labels.add("May"); labels.add("June");
public void initEntriesData() { entries.add(new BarEntry(4f, 0)); entries.add(new BarEntry(8f, 1)); entries.add(new BarEntry(6f, 2)); entries.add(new BarEntry(12f, 3)); entries.add(new BarEntry(18f, 4)); entries.add(new BarEntry(9f, 5)); }
BarData data = new BarData(labels, dataset); chart.setData(data);
barChart.setDescription("hello MPandroidChart");
ColorTemplate.LIBERTY_COLORS ColorTemplate.COLORFUL_COLORS ColorTemplate.JOYFUL_COLORS ColorTemplate.PASTEL_COLORS ColorTemplate.VORDIPLOM_COLORS
dataset.setColors(ColorTemplate.COLORFUL_COLORS);
// chart.animateXY(5000,5000); // chart.animateX(5000); barChart.animateY(2000);至于每个是什么效果,你自己可以亲测下
<RelativeLayout 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" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity" > <com.github.mikephil.charting.charts.LineChart android:id="@+id/spread_line_chart" android:layout_width="match_parent" android:layout_height="fill_parent" android:layout_gravity="center_horizontal" /> </RelativeLayout>LineCharFragment.java文件
package com.example.chenyu.mpandroidcharttest; import android.app.Fragment; import android.graphics.Color; import android.os.Bundle; import android.support.annotation.Nullable; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import com.github.mikephil.charting.charts.LineChart; import com.github.mikephil.charting.components.Legend; import com.github.mikephil.charting.data.Entry; import com.github.mikephil.charting.data.LineData; import com.github.mikephil.charting.data.LineDataSet; import java.util.ArrayList; /** * Created by Think on 2015/11/29. */ public class LineCharFragment extends Fragment { public LineChart lineChart; public ArrayList<String> x = new ArrayList<String>(); public ArrayList<Entry> y = new ArrayList<Entry>(); public ArrayList<LineDataSet> lineDataSets = new ArrayList<LineDataSet>(); public LineData lineData = null; @Nullable @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.liner_chart, container, false); lineChart = (LineChart) view.findViewById(R.id.spread_line_chart); LineData resultLineData = getLineData(40, 100); showChart(); return view; } /** * gv * 初始化数据 * count 表示坐标点个数,range表示等下y值生成的范围 */ public LineData getLineData(int count, float range) { for (int i = 0; i < count; i++) { //X轴显示的数据 x.add(i + ""); } for (int i = 0; i < count; i++) {//y轴的数据 float result = (float) (Math.random() * range) + 3; y.add(new Entry(result, i)); } LineDataSet lineDataSet = new LineDataSet(y, "折线图");//y轴数据集合 lineDataSet.setLineWidth(1f);//线宽 lineDataSet.setCircleSize(2f);//现实圆形大小 lineDataSet.setColor(Color.RED);//现实颜色 lineDataSet.setCircleSize(Color.BLUE);//圆形颜色 lineDataSet.setHighLightColor(Color.WHITE);//高度线的颜色 lineDataSets.add(lineDataSet); lineData = new LineData(x, lineDataSets); return lineData; } /** * 设置样式 */ public void showChart() { lineChart.setDrawBorders(false);//是否添加边框 lineChart.setDescription("有风险的数据");//数据描述 lineChart.setNoDataTextDescription("我需要数据");//没数据显示 lineChart.setDrawGridBackground(true);//是否显示表格颜色 lineChart.setBackgroundColor(Color.YELLOW);//背景颜色 lineChart.setData(lineData);//设置数据 Legend legend = lineChart.getLegend();//设置比例图片标示,就是那一组Y的value legend.setForm(Legend.LegendForm.CIRCLE);//样式 legend.setFormSize(6f);//字体 legend.setTextColor(Color.WHITE);//设置颜色 lineChart.animateX(2000);//X轴的动画 } }更具树状图的解说,这个可以对比参照,而且我基本上每行代码都有注释,大家应该可以看懂
<RelativeLayout 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" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity" > <com.github.mikephil.charting.charts.PieChart android:id="@+id/pie_chart" android:layout_width="match_parent" android:layout_height="fill_parent" android:layout_gravity="center_horizontal" /> </RelativeLayout>PieCharFragment.java文件如下
package com.example.chenyu.mpandroidcharttest; import android.app.Fragment; import android.graphics.Color; import android.os.Bundle; import android.support.annotation.Nullable; import android.util.DisplayMetrics; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import com.github.mikephil.charting.charts.PieChart; import com.github.mikephil.charting.components.Legend; import com.github.mikephil.charting.data.Entry; import com.github.mikephil.charting.data.PieData; import com.github.mikephil.charting.data.PieDataSet; import java.util.ArrayList; /** * Created by Think on 2015/11/29. */ public class PieCharFragment extends Fragment { public PieChart mChart; @Nullable @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.pie_chart, container, false); mChart = (PieChart) view.findViewById(R.id.pie_chart); PieData mPieData = getPieData(4, 100); showChart(mChart, mPieData); return view; } private void showChart(PieChart pieChart, PieData pieData) { pieChart.setHoleColorTransparent(true); pieChart.setHoleRadius(60f); //半径 pieChart.setTransparentCircleRadius(64f); // 半透明圈 //pieChart.setHoleRadius(0) //实心圆 pieChart.setDescription("测试饼状图"); // mChart.setDrawYValues(true); pieChart.setDrawCenterText(true); //饼状图中间可以添加文字 pieChart.setDrawHoleEnabled(true); pieChart.setRotationAngle(90); // 初始旋转角度 // draws the corresponding description value into the slice // mChart.setDrawXValues(true); // enable rotation of the chart by touch pieChart.setRotationEnabled(true); // 可以手动旋转 // display percentage values pieChart.setUsePercentValues(true); //显示成百分比 // mChart.setUnit(" €"); // mChart.setDrawUnitsInChart(true); // add a selection listener // mChart.setOnChartValueSelectedListener(this); // mChart.setTouchEnabled(false); // mChart.setOnAnimationListener(this); pieChart.setCenterText("Quarterly Revenue"); //饼状图中间的文字 // pieChart.animateY(3000); //设置数据 pieChart.setData(pieData); // undo all highlights // pieChart.highlightValues(null); // pieChart.invalidate(); Legend mLegend = pieChart.getLegend(); //设置比例图 mLegend.setPosition(Legend.LegendPosition.RIGHT_OF_CHART); //最右边显示 // mLegend.setForm(LegendForm.LINE); //设置比例图的形状,默认是方形 mLegend.setXEntrySpace(7f); mLegend.setYEntrySpace(5f); pieChart.animateXY(1000, 1000); //设置动画 // mChart.spin(2000, 0, 360); } /** * @param count 分成几部分 * @param range */ private PieData getPieData(int count, float range) { ArrayList<String> xValues = new ArrayList<String>(); //xVals用来表示每个饼块上的内容 for (int i = 0; i < count; i++) { xValues.add("Quarterly" + (i + 1)); //饼块上显示成Quarterly1, Quarterly2, Quarterly3, Quarterly4 } ArrayList<Entry> yValues = new ArrayList<Entry>(); //yVals用来表示封装每个饼块的实际数据 // 饼图数据 /** * 将一个饼形图分成四部分, 四部分的数值比例为14:14:34:38 * 所以 14代表的百分比就是14% */ float quarterly1 = 14; float quarterly2 = 14; float quarterly3 = 34; float quarterly4 = 38; yValues.add(new Entry(quarterly1, 0)); yValues.add(new Entry(quarterly2, 1)); yValues.add(new Entry(quarterly3, 2)); yValues.add(new Entry(quarterly4, 3)); //y轴的集合 PieDataSet pieDataSet = new PieDataSet(yValues, "Quarterly Revenue 2014"/*显示在比例图上*/); pieDataSet.setSliceSpace(0f); //设置个饼状图之间的距离 ArrayList<Integer> colors = new ArrayList<Integer>(); // 饼图颜色 colors.add(Color.rgb(205, 205, 205)); colors.add(Color.rgb(114, 188, 223)); colors.add(Color.rgb(255, 123, 124)); colors.add(Color.rgb(57, 135, 200)); pieDataSet.setColors(colors); DisplayMetrics metrics = getResources().getDisplayMetrics(); float px = 5 * (metrics.densityDpi / 160f); pieDataSet.setSelectionShift(px); // 选中态多出的长度 PieData pieData = new PieData(xValues, pieDataSet); return pieData; } }效果图如下
<RelativeLayout 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" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity" > <com.github.mikephil.charting.charts.ScatterChart android:id="@+id/scatter_chart" android:layout_width="match_parent" android:layout_height="fill_parent" android:layout_gravity="center_horizontal" /> </RelativeLayout>ScatterChartFragment.java文件如下
package com.example.chenyu.mpandroidcharttest; import android.app.Fragment; import android.graphics.Color; import android.os.Bundle; import android.support.annotation.Nullable; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import com.github.mikephil.charting.charts.BarChart; import com.github.mikephil.charting.charts.ScatterChart; import com.github.mikephil.charting.components.Legend; import com.github.mikephil.charting.components.LimitLine; import com.github.mikephil.charting.data.BarData; import com.github.mikephil.charting.data.BarDataSet; import com.github.mikephil.charting.data.BarEntry; import com.github.mikephil.charting.data.Entry; import com.github.mikephil.charting.data.LineData; import com.github.mikephil.charting.data.LineDataSet; import com.github.mikephil.charting.data.ScatterData; import com.github.mikephil.charting.data.ScatterDataSet; import com.github.mikephil.charting.utils.ColorTemplate; import java.util.ArrayList; /** * Created by Think on 2015/11/29. */ public class ScatterChartFragment extends Fragment { public ScatterChart scatterChart; public ArrayList<String> x = new ArrayList<String>(); public ArrayList<Entry> y = new ArrayList<Entry>(); public ArrayList<ScatterDataSet> scatterDataSets = new ArrayList<ScatterDataSet>(); public ScatterData scatterData = null; @Nullable @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.scatter_chart, container, false); scatterChart = (ScatterChart) view.findViewById(R.id.scatter_chart); ScatterData resultScatterData = getRadarData(40, 100); showChart(); return view; } /** * gv * 初始化数据 * count 表示坐标点个数,range表示等下y值生成的范围 */ public ScatterData getRadarData(int count, float range) { for (int i = 0; i < count; i++) { //X轴显示的数据 x.add(i + ""); } for (int i = 0; i < count; i++) {//y轴的数据 float result = (float) (Math.random() * range) + 3; y.add(new Entry(result, i)); } ScatterDataSet scatterDataSet = new ScatterDataSet(y, "散状图");//y轴数据集合 scatterDataSet.setColor(Color.RED);//现实颜色 scatterDataSet.setScatterShapeSize(6f); scatterDataSet.setDrawValues(true); scatterDataSet.setHighLightColor(Color.WHITE);//高度线的颜色 scatterDataSets.add(scatterDataSet); scatterData = new ScatterData(x, scatterDataSets); return scatterData; } /** * 设置样式 */ public void showChart() { scatterChart.setDrawBorders(false);//是否添加边框 scatterChart.setDescription("有风险的数据");//数据描述 scatterChart.setNoDataTextDescription("我需要数据");//没数据显示 scatterChart.setDrawGridBackground(true);//是否显示表格颜色 scatterChart.setBackgroundColor(Color.YELLOW);//背景颜色 scatterChart.setData(scatterData);//设置数据 Legend legend = scatterChart.getLegend();//设置比例图片标示,就是那一组Y的value legend.setForm(Legend.LegendForm.CIRCLE);//样式 legend.setFormSize(6f);//字体 legend.setTextColor(Color.WHITE);//设置颜色 scatterChart.animateX(2000);//X轴的动画 } }效果图如下
<RelativeLayout 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" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity" > <com.github.mikephil.charting.charts.RadarChart android:id="@+id/radar_chart" android:layout_width="match_parent" android:layout_height="fill_parent" android:layout_gravity="center_horizontal" /> </RelativeLayout>RadarCharFragment.java文件如下
package com.example.chenyu.mpandroidcharttest; import android.app.Fragment; import android.graphics.Color; import android.os.Bundle; import android.support.annotation.Nullable; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import com.github.mikephil.charting.charts.LineChart; 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.LineData; import com.github.mikephil.charting.data.LineDataSet; import com.github.mikephil.charting.data.RadarData; import com.github.mikephil.charting.data.RadarDataSet; import java.util.ArrayList; /** * Created by Think on 2015/11/29. */ public class RadarCharFragment extends Fragment { public RadarChart radarChart; 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; @Nullable @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.radar_chart, container, false); radarChart = (RadarChart) view.findViewById(R.id.radar_chart); RadarData resultLineData = getRadarData(40, 100); showChart(); return view; } /** * gv * 初始化数据 * count 表示坐标点个数,range表示等下y值生成的范围 */ public RadarData getRadarData(int count, float range) { for (int i = 0; i < count; i++) { //X轴显示的数据 x.add(i + ""); } for (int i = 0; i < count; i++) {//y轴的数据 float result = (float) (Math.random() * range) + 3; y.add(new Entry(result, i)); } RadarDataSet radarDataSet = new RadarDataSet(y, "雷达图");//y轴数据集合 radarDataSet.setLineWidth(1f);//线宽 radarDataSet.setColor(Color.RED);//现实颜色 radarDataSet.setHighLightColor(Color.WHITE);//高度线的颜色 radarDataSets.add(radarDataSet); radarData = new RadarData(x, radarDataSets); return radarData; } /** * 设置样式 */ public void showChart() { // radarChart.setDrawBorders(false);//是否添加边框 radarChart.setDescription("有风险的数据");//数据描述 radarChart.setNoDataTextDescription("我需要数据");//没数据显示 // radarChart.setDrawGridBackground(true);//是否显示表格颜色 radarChart.setBackgroundColor(Color.YELLOW);//背景颜色 radarChart.setData(radarData);//设置数据 Legend legend = radarChart.getLegend();//设置比例图片标示,就是那一组Y的value legend.setForm(Legend.LegendForm.CIRCLE);//样式 legend.setFormSize(6f);//字体 legend.setTextColor(Color.WHITE);//设置颜色 radarChart.animateY(2000);//X轴的动画 } }效果图: