implementation 'com.github.PhilJay:MPAndroidChart:v3.1.0-alpha'
https://github.com/tuteng/MPAndroidChart
1、折线图
<com.github.mikephil.charting.charts.LineChart
android:id="@+id/chart"
android:layout_width="match_parent"
android:layout_height="match_parent" />
setDragEnabled(boolean enabled): 打开或关闭对图表的拖动。
setScaleEnabled(boolean enabled):打开或关闭对图表所有轴的的缩放。
setScaleXEnabled(boolean enabled): 打开或关闭x轴的缩放
setScaleYEnabled(boolean enabled): 打开或关闭y轴的缩放。
设置屏幕之内限制多少个数据,左右滑动:
设置初始化:
lineChart.setDrawGridBackground(false);
lineChart.getLegend().setEnabled(false);// 不显示图例
// no description text
lineChart.getDescription().setEnabled(false);//右下角描述不显示
lineChart.setViewPortOffsets(20, 0, 0, 0);
// enable touch gestures
lineChart.setTouchEnabled(true);//手势
// enable scaling and dragging
lineChart.setDragEnabled(true);//拖拽
lineChart.setScaleEnabled(false);//缩放
// if disabled, scaling can be done on x- and y-axis separately
lineChart.setPinchZoom(true);//如果设置为true,挤压缩放被打开。如果设置为false,x和y轴可以被单独挤压缩放。
HomeMarkerView mv = new HomeMarkerView(getActivity(), R.layout.layout_home_marker_view);
//设置点击弹出的 布局
// Set the marker to the chart
mv.setChartView(lineChart);
lineChart.setMarker(mv);
//设置XY轴动画效果
lineChart.animateY(500);
lineChart.animateX(500);
XAxis xl = lineChart.getXAxis();
xl.setAvoidFirstLastClipping(true);//:如果设置为true,图表将避免第一个和最后一个标签条目被减掉在图表或屏幕的边缘。
xl.setEnabled(false);//Xz轴不显示
xl.setAxisMinimum(0f);//最小值
xl.setPosition(XAxis.XAxisPosition.BOTTOM);//数值位置
xl.setDrawAxisLine(false);//置为true,如果线在轴的侧面应该被画,否则不。
xl.setDrawGridLines(false);//设置为true打开绘制网格线对于轴来说。
xl.setDrawLabels(false);//设置为true打开绘制轴的标签。
YAxis leftAxis = lineChart.getAxisLeft();
leftAxis.setDrawZeroLine(true);//0刻度线
leftAxis.setDrawLabels(false);//绘制标签
leftAxis.setZeroLineColor(Color.GRAY);//0刻度线的颜色
lineChart.getLegend().setEnabled(false);//不显示图例
YAxis rightAxis = lineChart.getAxisRight();
rightAxis.setEnabled(false);//
rightAxis.setPosition(YAxis.YAxisLabelPosition.OUTSIDE_CHART);
//
float ratio = (float) orgDatas.size() / (float) 40;
//显示的时候是按照多大的比率缩放显示,1f表示不放大缩
lineChart.zoom(0f, 1f, 0, 0);
lineChart.zoom(ratio, 1f, 0, 0);
添加数据:List<Entry> entries = new ArrayList<>();
for (int i = 0; i < orgDatas.size(); i++) {
double value = 0;
entries.add(new Entry(i, (float) value, orgDatas.get(i)));
}
Collections.sort(entries, new EntryXComparator());
LineDataSet set1 = new LineDataSet(entries, "");
set1.setLineWidth(1.5f);
set1.setDrawCircles(false);
set1.setDrawValues(false);
set1.setDrawFilled(true);//是否铺满
set1.setFillColor(ColorTemplate.rgb("#fa7997"));//铺满颜色
set1.setColor(ColorTemplate.rgb("#fa7997"));
// set1.enableDashedHighlightLine(10f, 5f, 0f);
// create a data object with the data sets
LineData data = new LineData(set1);
// set data
lineChart.setData(data);
// 刷新
lineChart.invalidate();
2、饼图
<com.github.mikephil.charting.charts.PieChart
android:id="@+id/mPieChart"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="400px" />
初始化:
public static final int[] COLORFUL_COLORS1 = {自定义颜色
Color.rgb(191, 81, 79), Color.rgb(153, 189, 88), Color.rgb(127, 101, 161),
Color.rgb(73, 172, 198), Color.rgb(78, 130, 190)
};
mPieChart.setUsePercentValues(true);//如果设置为true,图标显示数据的百分比而不是他们的原始数据。数据提供了ValueFormatter情况下会格式化百分比数据。
mPieChart.getDescription().setEnabled(false);//描述
mPieChart.setRotationEnabled(false); //设置pieChart图表是否可以手动旋转
mPieChart.setDragDecelerationFrictionCoef(0.95f);
mPieChart.setDrawHoleEnabled(true);
mPieChart.setHoleColor(Color.WHITE);
mPieChart.setTransparentCircleColor(Color.WHITE);
mPieChart.setTransparentCircleAlpha(110);
mPieChart.setHoleRadius(80f);
mPieChart.setTransparentCircleRadius(61f);
mPieChart.setDrawCenterText(true);
mPieChart.setDrawSliceText(false);
mPieChart.setRotationAngle(0);
// enable rotation of the chart by touch
mPieChart.setRotationEnabled(true);
mPieChart.setHighlightPerTapEnabled(true);
mPieChart.setOnChartValueSelectedListener(new OnChartValueSelectedListener() {//点击事件
@Override
public void onValueSelected(Entry e, Highlight h) {
}
@Override
public void onNothingSelected() {
Bundle mBundle = new Bundle();
mBundle.putInt("flag", flag);//3公司 4银行 5银行
mBundle.putString("arr", arr);
mBundle.putString("stratDate", starTime);
mBundle.putBoolean("serchQquest", serchQquest);
startNextActivity(mBundle, MoreChartActivity.class);
}
});
mPieChart.animateY(1400, Easing.EaseInOutQuad);
// chart.spin(2000, 0, 360);
Legend l = mPieChart.getLegend();//图例
l.setVerticalAlignment(Legend.LegendVerticalAlignment.BOTTOM);
l.setHorizontalAlignment(Legend.LegendHorizontalAlignment.CENTER);
l.setOrientation(Legend.LegendOrientation.HORIZONTAL);
l.setWordWrapEnabled(true);
mPieChart.setEntryLabelColor(Color.WHITE);
mPieChart.setEntryLabelTextSize(12f);
mPieChart.setDrawHoleEnabled(false);
添加数据:
private void setData(int count, float range) {
ArrayList<PieEntry> entries = new ArrayList<>();
for (int i = 0; i < count; i++) {
entries.add(new PieEntry(i,"名字");
}
}
PieDataSet dataSet = new PieDataSet(entries, "");
//区域文字的大小 空隙大小
dataSet.setValueTextSize(11f); //设置区域文字的颜色
dataSet.setValueTextColor(Color.WHITE); //设置区域文字的字体
dataSet.setValueTypeface(Typeface.DEFAULT);
// add a lot of colors
ArrayList<Integer> colors = new ArrayList<>();
for (int c : COLORFUL_COLORS1)
colors.add(c);
for (int c : ColorTemplate.COLORFUL_COLORS)
colors.add(c);
for (int c : ColorTemplate.COLORFUL_COLORS)
colors.add(c);
for (int c : ColorTemplate.COLORFUL_COLORS)
colors.add(c);
for (int c : ColorTemplate.COLORFUL_COLORS)
colors.add(c);
colors.add(ColorTemplate.getHoloBlue());
dataSet.setColors(colors);
PieData data = new PieData(dataSet);
data.setValueFormatter(new PercentFormatter());
data.setValueTextSize(11f);
data.setValueTextColor(Color.WHITE);
mPieChart.setData(data);
mPieChart.highlightValues(null);
mPieChart.invalidate();
}
3、柱状图:单个
初始化:
// drawn
chart.setMaxVisibleValueCount(60);
// scaling can now only be done on x- and y-axis separately
chart.setPinchZoom(false);
chart.setScaleEnabled(false);
chart.setDrawBarShadow(false);
chart.setDrawGridBackground(false);
chart.setDrawValueAboveBar(true);
XAxis xAxis = chart.getXAxis();
xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
xAxis.setDrawGridLines(false);
xAxis.setDrawAxisLine(false);
xAxis.setTextColor(Color.LTGRAY);
xAxis.setTextSize(13f);
xAxis.setLabelCount(5);
xAxis.setCenterAxisLabels(true);
xAxis.setGranularity(1f);
xAxis.setEnabled(false);
YAxis left = chart.getAxisLeft();
xAxis.setEnabled(false);
left.setDrawLabels(false);
left.setSpaceTop(25f);
left.setSpaceBottom(25f);
left.setDrawAxisLine(false);
left.setDrawGridLines(false);
left.setDrawZeroLine(false); // draw a zero line
left.setZeroLineColor(Color.GRAY);
left.setZeroLineWidth(0.7f);
chart.getAxisRight().setEnabled(false);
chart.getLegend().setEnabled(false);
// 右侧Y轴
chart.getAxisRight().setEnabled(false); // 不启用
chart.getAxisLeft().setDrawGridLines(false);
chart.setOnChartValueSelectedListener(new OnChartValueSelectedListener() {
@Override
public void onValueSelected(Entry e, Highlight h) {
}
@Override
public void onNothingSelected() {
}
});
// add a nice and smooth animation
chart.animateY(500);
chart.getLegend().setEnabled(false);
添加数据: 四个不一样的柱状图
ArrayList<BarEntry> values = new ArrayList<>();
values.add(new BarEntry(0, barchatNum1));
if (flag == 3) {
barchatNum3 = -barchatNum3;
} else {
barchatNum2 = -barchatNum2;
}
values.add(new BarEntry(1, barchatNum2));
values.add(new BarEntry(2, barchatNum3));
values.add(new BarEntry(3, barchatNum4));
Legend l = chart.getLegend();
l.setVerticalAlignment(Legend.LegendVerticalAlignment.BOTTOM);
l.setHorizontalAlignment(Legend.LegendHorizontalAlignment.CENTER);
l.setOrientation(Legend.LegendOrientation.HORIZONTAL);
l.setDrawInside(false);
l.setXEntrySpace(7f);
l.setYEntrySpace(0f);
l.setYOffset(0f);
BarDataSet set1;
if (chart.getData() != null &&
chart.getData().getDataSetCount() > 0) {
set1 = (BarDataSet) chart.getData().getDataSetByIndex(0);
set1.setValues(values);
chart.getData().notifyDataChanged();
chart.notifyDataSetChanged();
} else {
int startColor1 = ContextCompat.getColor(this, R.color.bar_compny_s_1);
int startColor2 = ContextCompat.getColor(this, R.color.bar_compny_s_2);
int startColor3 = ContextCompat.getColor(this, R.color.bar_compny_s_3);
int startColor4 = ContextCompat.getColor(this, R.color.bar_compny_s_4);
int startColor5 = ContextCompat.getColor(this, R.color.bar_bank_s_2);
int startColor6 = ContextCompat.getColor(this, R.color.bar_bank_s_4);
int endColor1 = ContextCompat.getColor(this, R.color.bar_compny_e_1);
int endColor2 = ContextCompat.getColor(this, R.color.bar_compny_e_2);
int endColor3 = ContextCompat.getColor(this, R.color.bar_compny_e_3);
int endColor4 = ContextCompat.getColor(this, R.color.bar_compny_e_4);
int endColor5 = ContextCompat.getColor(this, R.color.bar_bank_e_2);
int endColor6 = ContextCompat.getColor(this, R.color.bar_bank_e_4);
set1 = new BarDataSet(values, "Data Set");
ArrayList<IBarDataSet> dataSets = new ArrayList<>();
dataSets.add(set1);
BarData data = new BarData(dataSets);//柱状图颜色渐变
List<GradientColor> gradientColors = new ArrayList<>();
gradientColors.add(new GradientColor(startColor1, endColor1));
gradientColors.add(new GradientColor(startColor2, endColor2));
gradientColors.add(new GradientColor(startColor3, endColor3));
gradientColors.add(new GradientColor(startColor4, endColor4));
set1.setGradientColors(gradientColors);
data.setDrawValues(true);//显示顶部的值
data.setValueTextSize(13f);
// data.setValueFormatter(new LargeValueFormatter());
data.setValueTextColor(R.color.colorTopWelcome);
data.setBarWidth(0.85f);
chart.setData(data);
chart.setFitBars(true);
}
chart.invalidate();
柱状图多个:
固定的每4个一组
private void setData1(float range, List<LinkedTreeMap<String, Object>> mapList1) {
float groupSpace = 0.08f;
float barSpace = 0.03f; // x4 DataSet
float barWidth = 0.2f; // x4 DataSet
//***这个必须满足否则绘出的柱形图很不规则***
// (0.05+0.2)*3 + 0.25 = 1
// 0.5
int mSeekBarX = 6; //
if (mapList1.size() > 6) {
mSeekBarX = mapList1.size();
}
/* 可以灵活去添加
List<String> list = new ArrayList<>();
for (int x = 0 ;x<mSeekBarX;x++){
list.add("A"+x);
}
mChart.getXAxis().setValueFormatter(new StringXAxisFormatter(list));
*/
//这里为了方便写死了,
chart.getAxisLeft().setDrawAxisLine(false);
chart.getXAxis().setDrawAxisLine(false);
// 对X轴格式的设置
chart.getXAxis().setValueFormatter(new IndexAxisValueFormatter(xAxisValue));
chart.getXAxis().setLabelCount(mSeekBarX);
ArrayList<BarEntry> yVals1 = new ArrayList<BarEntry>();
ArrayList<BarEntry> yVals2 = new ArrayList<BarEntry>();
ArrayList<BarEntry> yVals3 = new ArrayList<BarEntry>();
ArrayList<BarEntry> yVals4 = new ArrayList<BarEntry>();
for (int i = 0; i < mapList1.size(); i++) { //每个公司每年的数据
Map<String, Object> mDataJT = mapList1.get(i);
yVals1.add(new BarEntry(i, Float.valueOf(mapList1.get(i).get("num1").toString())));
yVals2.add(new BarEntry(i, Float.valueOf(mapList1.get(i).get("num2").toString())));
yVals3.add(new BarEntry(i, -Float.valueOf(mapList1.get(i).get("num3").toString())));
yVals4.add(new BarEntry(i, Float.valueOf(mapList1.get(i).get("num4").toString())));
}
BarDataSet set1, set2, set3, set4;
if (chart.getData() != null && chart.getData().getDataSetCount() > 0) {
set1 = (BarDataSet) chart.getData().getDataSetByIndex(0);
set2 = (BarDataSet) chart.getData().getDataSetByIndex(1);
set3 = (BarDataSet) chart.getData().getDataSetByIndex(2);
set4 = (BarDataSet) chart.getData().getDataSetByIndex(3);
set1.setValues(yVals1); //A公司的所有年份数据
set2.setValues(yVals2);
set3.setValues(yVals3);
set4.setValues(yVals4);
chart.getData().notifyDataChanged();
chart.notifyDataSetChanged();
} else {
int startColor1 = ContextCompat.getColor(this, R.color.bar_compny_s_1);
int startColor2 = ContextCompat.getColor(this, R.color.bar_compny_s_2);
int startColor4 = ContextCompat.getColor(this, R.color.bar_compny_s_4);
int startColor5 = ContextCompat.getColor(this, R.color.bar_bank_s_5);
int endColor1 = ContextCompat.getColor(this, R.color.bar_compny_e_1);
int endColor2 = ContextCompat.getColor(this, R.color.bar_compny_e_2);
int endColor4 = ContextCompat.getColor(this, R.color.bar_compny_e_4);
int endColor5 = ContextCompat.getColor(this, R.color.bar_bank_e_5);
List<GradientColor> gradientColors = new ArrayList<>();
gradientColors.add(new GradientColor(startColor1, endColor1));
// create 4 DataSets
set1 = new BarDataSet(yVals1, "11111");
set1.setDrawIcons(false);
set1.setColor(ColorTemplate.rgb("#2b5f9b"));
set2 = new BarDataSet(yVals2, "22222");
set2.setDrawIcons(false);
set2.setColor(ColorTemplate.rgb("#98302e"));
set3 = new BarDataSet(yVals3, "3333");
set3.setColor(Color.rgb(128, 0, 128));
set3.setDrawIcons(false);
set4 = new BarDataSet(yVals4, "44444");
set4.setDrawIcons(false);
set4.setColor(ColorTemplate.rgb("#f47c4e"));
set1.setGradientColor(startColor1, endColor1);
set2.setGradientColor(startColor2, endColor2);
set3.setGradientColor(startColor4, endColor4);
set4.setGradientColor(startColor5, endColor5);
//将数据放入 BarData , 只需要三条柱 所以set4就没有放入
BarData data = new BarData(set1, set2, set3, set4); //
data.setValueFormatter(new LargeValueFormatter());
data.setDrawValues(false);
// data.setValueTypeface(mTfLight);
chart.setData(data);
}
//设置柱状图的宽度及字体大小等等
chart.getBarData().setBarWidth(barWidth);
chart.getBarData().setValueTextSize(7f);
// restrict the x-axis range
chart.getXAxis().setAxisMinimum(0);
chart.setExtraBottomOffset(3 * 12f);
chart.setXAxisRenderer(new CustomXAxisRenderer(chart.getViewPortHandler(), chart.getXAxis(), chart.getTransformer(YAxis.AxisDependency.LEFT)));
// barData.getGroupWith(...) is a helper that calculates the width each group needs based on the provided parameters
chart.getXAxis().setAxisMaximum(0 + chart.getBarData().getGroupWidth(groupSpace, barSpace) * mSeekBarX);
chart.groupBars(0, groupSpace, barSpace);
chart.invalidate(); //刷新图表
Matrix m = new Matrix();
m.postScale(1.5f, 1f);//两个参数分别是x,y轴的缩放比例。例如:将x轴的数据放大为之前的1.5倍
chart.getViewPortHandler().refresh(m, chart, false);//将图表动画显示之前进行缩放
chart.animateX(1000); // 立即执行的动画,x轴
}
不固定多柱图:设置数据:必须是多组数据
BarCharInfo
List<List<Map<String, Object>>> ALL=new ArraryList();//全部数据
private void setData(List<List<Map<String, Object>>> mAllBankInfo) {//数据必须是每个都一样,必须是多组
1、获取一个组的有多少条柱子;
List<Map<String, Object>> mBankInfo = mAllBankInfo.get(0);
//第一个 所带有 柱子多少
ArrayList<Integer> colors = new ArrayList<>();
//不固定柱状图: 颜色添加
for (int c : COLORFUL_COLORS2)
colors.add(c);
for (int c : ColorTemplate.VORDIPLOM_COLORS)
colors.add(c);
for (int c : ColorTemplate.JOYFUL_COLORS)
colors.add(c);
for (int c : ColorTemplate.PASTEL_COLORS)
colors.add(c);
for (int c : ColorTemplate.LIBERTY_COLORS)
colors.add(c);
colors.add(ColorTemplate.getHoloBlue());
List dataSets = new ArrayList<>();
chart.getXAxis().setValueFormatter(new IndexAxisValueFormatter(xAxisValue));
int barAmount = mBankInfo.size(); //需要显示柱状图的类别 数量
//设置组间距占比30% 每条柱状图宽度占比 70% /barAmount 柱状图间距占比 0%
float groupSpace = 0.3f; //柱状图组之间的间距
float barWidth = (1f - groupSpace) / barAmount;
float barSpace = 0f;
int mSeekBarX = 6; //
if (mBankInfo.size() > 6) {
mSeekBarX = mBankInfo.size();
}
chart.getXAxis().setLabelCount(mSeekBarX);
for (int j = 0; j < mBankInfo.size(); j++) {//几个柱子
ArrayList<BarEntry> values5 = new ArrayList<>();
for (int k = 0; k < ALL.size(); k++) {
double mun = (double) ALL.get(k).get(j).get("num");
values5.add(new BarEntry(k, Float.valueOf(mun )));
}
BarDataSet set1 = new BarDataSet(values5, mBankInfo.get(j).get("name").toString());//每一类柱状图的名字
set1.setDrawIcons(false);
set1.setColors(colors.get(j));
dataSets.add(set1);
}
try {
BarData data = new BarData(dataSets); //
data.setValueFormatter(new LargeValueFormatter());
data.setDrawValues(false);
chart.setData(data);
chart.getBarData().setBarWidth(barWidth);
chart.getXAxis().setAxisMaximum(0 + chart.getBarData().getGroupWidth(groupSpace, barSpace) * mSeekBarX);
chart.groupBars(0, groupSpace, barSpace);
}catch (Exception e){
}
// barWidth = (float)(Math.round(barWidth*100))/100;//(这里的100就是2位小数点,如果要其它位,
chart.getBarData().setValueTextSize(7f);
// restrict the x-axis range
chart.getXAxis().setAxisMinimum(0);
// barData.getGroupWith(...) is a helper that calculates the width each group needs based on the provided parameters
chart.invalidate(); //刷新图表
Matrix m = new Matrix();//左右滑动。。。。。。
m.postScale(1.5f, 1f);//两个参数分别是x,y轴的缩放比例。例如:将x轴的数据放大为之前的1.5倍
chart.getViewPortHandler().refresh(m, chart, false);//将图表动画显示之前进行缩放
chart.animateX(1000); // 立即执行的动画,x轴
}