StatisticsView

介绍

各种常用的统计图,折线图,饼状图,渐变色处理

本项目基于PhilJay/MPAndroidChart

依赖导入

    repositories {maven { url "https://jitpack.io" }}
    implementation 'com.gitee.thorncts:statistics-view:v1.1.0'

调用方法

例:

   //绑定组件DefLineChartSetting curveChartSetting = new DefLineChartSetting(this,mDataBinding.curvechart);//创建Setting对象DefLineChartSetting.SetAttribute setDataAttribute = new DefLineChartSetting.SetAttribute();

渐变色

曲线图

XML设置渐变色,filldrawable(ContextCompat.getDrawable(this, R.drawable.xxx))调用

    <item android:width="278dp" android:height="78dp"><shape android:shape="rectangle"><!--渐变色--><!--半透明颜色 前2位是透明度,后6位是颜色--><gradientandroid:angle="90"android:endColor="#BF3299ff"android:startColor="#BFffffff"android:type="linear"/></shape></item>

柱状图

柱子渐变色

    //Fill(int startColor, int endColor).fill(new Fill(Color.parseColor("#ffffff"),Color.parseColor("#3299ff")))

曲线图

     <com.setting.view.DefLineChartandroid:id="@+id/deflinechart"android:layout_width="match_parent"android:layout_height="250dp" />

曲线图添加数据

例:

    private List<String> lines = new ArrayList<>();private List<Entry> values = new ArrayList<>();//图例数据lines.add("测试1");lines.add("测试2");//曲线图数据//  Entry 坐标点对象  构造函数 第一个参数为x点坐标 第二个为y点values.add(new Entry(0, 1));values.add(new Entry(1, 3));values.add(new Entry(2, 2));values.add(new Entry(3, 4));values.add(new Entry(4, 3));values.add(new Entry(5, 2.3f));

曲线图样式

图表的线和数据设置

        DefLineChartSetting.SetAttribute setDataAttribute = new DefLineChartSetting.SetAttribute();setDataAttribute.setting(setting)//图例数据.label(lines)//线模式为圆滑曲线(默认折线).mode(LineDataSet.Mode.CUBIC_BEZIER)// 曲线的平滑度,值越大越平滑.cubicintensity(0.2f)//设置线的颜色.color(Color.parseColor("#3299ff"))// 设置焦点圆心的颜色.circlecolor(Color.parseColor("#3299ff"))//设置线的宽度.linewidth(1f)//设置焦点圆心的大小.circleradius(3f)//是否禁用点击高亮线.highlightenabled(true)//点击后的高亮线的显示样式.highlightline(0f,5f,0f)//设置点击交点后显示高亮线宽.highlightlinewidth(1)//设置高亮线颜色.highlightcolor(Color.parseColor("#3299ff")).valuetextsize(0)// 设置曲线下面的填充色.drawfilled(true)//颜色填充样式.filldrawable(ContextCompat.getDrawable(this, R.drawable.bg_gradient_ramp))//折线图点的标记//标记样式(全局的)//PointDataView(参数1 Context,参数2 标记Background,参数3 文字大小,参数4 文字颜色).mv(new PointDataView(setting.getContext(),R.drawable.bg_chart_num,9,"#3299ff"))//添加数据(数据,第i+1条线).setData(values,0).setData(values2,1);

图表设置

        DefLineChartSetting.SetChart setChartAttribute = new DefLineChartSetting.SetChart();setChartAttribute.setting(setting)//隐藏描述.descriptionenabled(false)//设置图表描述信息.descriptiontext("测试")//没有数据时显示的文字.nodatatext("没有数据")//没有数据时显示文字的颜色.noDatatextcolor(Color.BLUE)//chart 绘图区后面的背景矩形将绘制.drawgridbackground(false)//禁止绘制图表边框的线.drawborders(false)// 设置是否可以触摸.touchenabled(true)//是否可以缩放 x和y轴, 默认为true.scaleenabled(false)//是否可以缩放 仅x轴.scalexenabled(false)//是否可以缩放 仅y轴.scaleyenabled(false)//是否支持x、y轴同时缩放,默认为false.pinchzoom(false)//设置是否可以通过双击屏幕放大图表。默认为true.doubletaptozoomenabled(false)//距视图窗口底部的偏移,类似与paddingbottom.extrabottomoffset(5).setChart();

图例设置

        DefLineChartSetting.SetLegend setLegendAttribute = new DefLineChartSetting.SetLegend();setLegendAttribute.setting(setting)//是否显示图例.enabled(false)//是否绘制在图表里面.drawinside(false)//设置图例的位置.verticalalignment(Legend.LegendVerticalAlignment.BOTTOM).horizontalalignment(Legend.LegendHorizontalAlignment.LEFT).orientation(Legend.LegendOrientation.HORIZONTAL)//设置文字大小.textSize(10f)//设置图例标签文本的颜色.textcolor(Color.parseColor("#ff9933"))//正方形,圆形或线.form(Legend.LegendForm.CIRCLE)// 设置Form的大小.formsize(10f)//是否支持自动换行.wordwrapenabled(true)//设置Form的宽度.formlinewidth(10f).setLegend();

X轴设置

        DefLineChartSetting.SetXAxis setXAxisAttribute = new DefLineChartSetting.SetXAxis();setXAxisAttribute.setting(setting)//设置x轴标签的旋转角度.labelrotationangle(-15f)//是否绘制X轴上的网格线(背景里面的竖线).drawgridlines(false)//X轴颜色.axislinecolor(Color.parseColor("#e5ecfe"))//X轴粗细.axislinewidth(1)//X轴所在位置   默认为上面.position(XAxis.XAxisPosition.BOTTOM)//X轴最大数值.axismaximum(5 + 0.3f)//X轴最小数值.axisminimum(-0.3f)//X轴坐标的个数    第二个参数一般填false     true表示强制设置标签数 可能会导致X轴坐标显示不全等问题.labelcount(5,false)//设置字体颜色.textcolor(Color.parseColor("#666680")).setXAxis();//自定义X轴标签显示字符setting.getxAxis().setValueFormatter(new IAxisValueFormatter() {@Overridepublic String getFormattedValue(float value, AxisBase axis) {String tap = String.valueOf(value+1);return "2022-" + tap.substring(0, tap.indexOf("."));}});

Y轴设置

        DefLineChartSetting.SetYAxis setYAxisAttribute = new DefLineChartSetting.SetYAxis();setYAxisAttribute.setting(setting)//是否绘制Y轴上的网格线(背景里面的横线).drawgridlines(false)//Y轴颜色.axislinecolor(Color.parseColor("#e5ecfe"))//Y轴粗细.axislinewidth(1)//Y轴最大数值.axismaximum(5f)//Y轴最小数值.axisminimum(0f)//设置字体颜色.textcolor(Color.parseColor("#666680"))//Y轴坐标的个数    第二个参数一般填false     true表示强制设置标签数 可能会导致X轴坐标显示不全等问题.labelcount(5,false).axisrightenabled(false).setYAxis();//自定义Y轴标签显示字符setting.getyAxis().setValueFormatter(new IAxisValueFormatter() {@Overridepublic String getFormattedValue(float value, AxisBase axis) {if(value == 5f){return "(星)";}return String.valueOf(value).substring(0,1);}});

饼状图

    <com.setting.view.DefPieChartandroid:id="@+id/defpiechart"android:layout_width="match_parent"android:layout_height="250dp" />

饼状图添加数据

例:

    //图例数据private List<Datapiechart> listdatapie = new ArrayList();//饼状图数据private List<PieEntry> listdata = new ArrayList<>();//饼状图Item颜色private List<Integer> colors = new ArrayList<>();listdatapie.clear();listdatapie.add(new Datapiechart(Color.parseColor("#108ee9"),"居民小区",10,10));listdatapie.add(new Datapiechart(Color.parseColor("#ef0000"),"娱乐场所",10,10));listdatapie.add(new Datapiechart(Color.parseColor("#6db81f"),"交通道路",20,20));listdatapie.add(new Datapiechart(Color.parseColor("#FF6200EE"),"城郊结合部",30,30));listdatapie.add(new Datapiechart(Color.parseColor("#999999"),"其他",30,30));listdata.clear();listdata.add(new PieEntry(listdatapie.get(0).getNum(),listdatapie.get(0).getLable()));listdata.add(new PieEntry(listdatapie.get(1).getNum(),listdatapie.get(1).getLable()));listdata.add(new PieEntry(listdatapie.get(2).getNum(),listdatapie.get(2).getLable()));listdata.add(new PieEntry(listdatapie.get(3).getNum(),listdatapie.get(3).getLable()));listdata.add(new PieEntry(listdatapie.get(4).getNum(),listdatapie.get(4).getLable()));colors.clear();colors.add(Color.parseColor("#108ee9"));colors.add(Color.parseColor("#ef0000"));colors.add(Color.parseColor("#6db81f"));colors.add(Color.parseColor("#FF6200EE"));colors.add(Color.parseColor("#999999"));

饼状图样式

        DefPieChartSetting.SetAttribute setAttribute = new DefPieChartSetting.SetAttribute();setAttribute.setting(setting)//设置是否显示文本.drawvalues(false)//文字的大小.valuetextsize(10)//文字的颜色.valuetextcolor(Color.RED)//文字的样式.valuetypeface(Typeface.DEFAULT_BOLD)//设置Y值的位置是在圆内还是圆外.yvalueposition(PieDataSet.ValuePosition.OUTSIDE_SLICE)//当值位置为外边线时,表示线的前半段长度.valuelinepart1length(0.2f)//当值位置为外边线时,表示线的后半段长度.valuelinepart2length(0.4f)//当ValuePosition为OUTSIDE_SLICE时,指示偏移为切片大小的百分比.valuelinepart1Offsetpercentage(30f)//隐藏描述.descriptionenabled(false)//设置pieChart图表的描述.descriptiontext("测试")//设置pieChart图表背景色.backgroundcolor(Color.WHITE)//设置pieChart图表上下左右的偏移,类似于外边距.extraoffsets(0,0,0,0)//设置pieChart是否只显示饼图上百分比不显示文字.drawentrylabels(false)//设置pieChart图表是否可以手动旋转.rotationenabled(false)//使用百分比显示.usepercentvalues(true)//设置piecahrt图表点击Item高亮是否可用.highlightpertapenabled(false)//是否显示PieChart内部圆环.drawholeenabled(true)//设置PieChart内部圆的半径.holeradius(75f)//设置PieChart内部圆的颜色.holecolor(Color.WHITE)//是否绘制PieChart内部中心文本.drawcentertext(true)//设置PieChart内部圆文字的内容.centertext("100","人口总数")//设置PieChart内部圆文字的大小.centertextsize(25f,20f)//设置PieChart内部圆文字的颜色.centertextcolor(Color.parseColor("#3299ff"),Color.BLACK)//设置PieChart内部圆文字的样式.centertexttypeface(Typeface.DEFAULT_BOLD,Typeface.DEFAULT)//是否启用默认图列(true:下面属性才有意义).enabled(false)//图例位置.verticalalignment(Legend.LegendVerticalAlignment.TOP).horizontalalignment(Legend.LegendHorizontalAlignment.RIGHT).orientation(Legend.LegendOrientation.VERTICAL)//设置图例的形状.form(Legend.LegendForm.DEFAULT)//设置图例的大小.formsize(10)//设置每个图例实体中标签和形状之间的间距.formtotextspace(10f)//设置是否画在图表里.drawinside(false)//设置图列换行(注意使用影响性能,仅适用legend位于图表下面).wordwrapenabled(true)//设置图例实体之间延X轴的间距(setOrientation = HORIZONTAL有效).xentryspace(10f)//设置图例实体之间延Y轴的间距(setOrientation = VERTICAL 有效).yentryspace(8f)//设置比例块Y轴偏移量.yoffset(0f)//设置图例标签文本的大小.textsize(14f)//设置图例标签文本的颜色.textcolor(Color.parseColor("#ff9933"))//添加数据.setData(listdata,colors);

自定义图例

        //自定义图例//添加数据setting.getnAdapter().setItems(listdatapie);mDataBinding.defpiechartLegend.setLayoutManager(new LinearLayoutManager(setting.getContext(),RecyclerView.VERTICAL, false));mDataBinding.defpiechartLegend.setAdapter(setting.getnAdapter());

柱状图

     <com.setting.view.DefBarChartandroid:id="@+id/defbarchart"android:layout_width="match_parent"android:layout_height="250dp" />

柱状图添加数据

例:

        //柱状图数据private List<String> barlines = new ArrayList<>();private List<BarEntry> barvalues = new ArrayList<>();private List<BarEntry> barvalues2 = new ArrayList<>();//柱形宽度private float width = 0.3f;barlines.add("测试1");barlines.add("测试2");//  x值 = 月份 - 1barvalues.add(new BarEntry(0, 1));barvalues.add(new BarEntry(1, 3));barvalues.add(new BarEntry(2, 2));barvalues.add(new BarEntry(3, 4));barvalues.add(new BarEntry(4, 3));barvalues.add(new BarEntry(5, 2.3f));barvalues2.add(new BarEntry(0+width, 2));barvalues2.add(new BarEntry(1+width, 1));barvalues2.add(new BarEntry(2+width, 4));barvalues2.add(new BarEntry(3+width, 1));barvalues2.add(new BarEntry(4+width, 3));barvalues2.add(new BarEntry(5+width, 4));

柱状图样式

图表的柱形和数据设置

        DefBarChartSetting.SetAttribute setDataAttribute = new DefBarChartSetting.SetAttribute();setDataAttribute.setting(setting)//设置数据:图例名称.label(barlines)//是否显示值.drawvalues(true)//值的颜色.textColor("#3299ff")//值的大小.textsize(10f)//图例标签的颜色.color("#3299ff")//柱子渐变色(不使用渐变色两个参数相同即可)fill(new Fill(Color.parseColor("#ffffff"),Color.parseColor("#3299ff")))//设置柱子的宽度.barwidth(width)//折线图点的标记//标记样式(全局的)//PointDataView(参数1 Context,参数2 标记Background,参数3 文字大小,参数4 文字颜色).mv(new PointDataView(setting.getContext(),R.drawable.bg_chart_num,9,"#3299ff"))//添加数据(数据,第i+1柱形).setData(barvalues,0);

图表设置

        DefBarChartSetting.SetChart setChartAttribute = new CurveChartSetting.SetChart();setChartAttribute.setting(setting)//隐藏描述.descriptionenabled(false)//设置图表描述信息.descriptiontext("测试")//没有数据时显示的文字.nodatatext("没有数据")//没有数据时显示文字的颜色.noDatatextcolor(Color.BLUE)//chart 绘图区后面的背景矩形将绘制.drawgridbackground(false)//禁止绘制图表边框的线.drawborders(false)// 设置是否可以触摸.touchenabled(true)//是否可以缩放 x和y轴, 默认为true.scaleenabled(false)//是否可以缩放 仅x轴.scalexenabled(false)//是否可以缩放 仅y轴.scaleyenabled(false)//是否支持x、y轴同时缩放,默认为false.pinchzoom(false)//设置是否可以通过双击屏幕放大图表。默认为true.doubletaptozoomenabled(false)//距视图窗口底部的偏移,类似与paddingbottom.extrabottomoffset(5).setChart();

图例设置

        DefBarChartSetting.SetLegend setLegendAttribute = new CurveChartSetting.SetLegend();setLegendAttribute.setting(setting)//是否显示图例.enabled(false)//是否绘制在图表里面.drawinside(false)//设置图例的位置.verticalalignment(Legend.LegendVerticalAlignment.BOTTOM).horizontalalignment(Legend.LegendHorizontalAlignment.LEFT).orientation(Legend.LegendOrientation.HORIZONTAL)//设置文字大小.textSize(10f)//设置图例标签文本的颜色.textcolor(Color.parseColor("#ff9933"))//正方形,圆形或线.form(Legend.LegendForm.CIRCLE)// 设置Form的大小.formsize(10f)//是否支持自动换行.wordwrapenabled(true)//设置Form的宽度.formlinewidth(10f).setLegend();

X轴设置

        DefBarChartSetting.SetXAxis setXAxisAttribute = new CurveChartSetting.SetXAxis();setXAxisAttribute.setting(setting)//设置x轴标签的旋转角度.labelrotationangle(-15f)//是否绘制X轴上的网格线(背景里面的竖线).drawgridlines(false)//X轴颜色.axislinecolor(Color.parseColor("#e5ecfe"))//X轴粗细.axislinewidth(1)//X轴所在位置   默认为上面.position(XAxis.XAxisPosition.BOTTOM)//X轴最大数值.axismaximum(5 + 0.3f)//X轴最小数值.axisminimum(-0.3f)//X轴坐标的个数    第二个参数一般填false     true表示强制设置标签数 可能会导致X轴坐标显示不全等问题.labelcount(5,false)//设置字体颜色.textcolor(Color.parseColor("#666680")).setXAxis();//自定义X轴标签显示字符setting.getxAxis().setValueFormatter(new IAxisValueFormatter() {@Overridepublic String getFormattedValue(float value, AxisBase axis) {String tap = String.valueOf(value+1);return "2022-" + tap.substring(0, tap.indexOf("."));}});

Y轴设置

        DefBarChartSetting.SetYAxis setYAxisAttribute = new CurveChartSetting.SetYAxis();setYAxisAttribute.setting(setting)//是否绘制Y轴上的网格线(背景里面的横线).drawgridlines(false)//Y轴颜色.axislinecolor(Color.parseColor("#e5ecfe"))//Y轴粗细.axislinewidth(1)//Y轴最大数值.axismaximum(5f)//Y轴最小数值.axisminimum(0f)//设置字体颜色.textcolor(Color.parseColor("#666680"))//Y轴坐标的个数    第二个参数一般填false     true表示强制设置标签数 可能会导致X轴坐标显示不全等问题.labelcount(5,false).axisrightenabled(false).setYAxis();//自定义Y轴标签显示字符setting.getyAxis().setValueFormatter(new IAxisValueFormatter() {@Overridepublic String getFormattedValue(float value, AxisBase axis) {if(value == 5f){return "(星)";}return String.valueOf(value).substring(0,1);}});

运行结果

完整项目

本项目StatisticsView

StatisticsView相关推荐

  1. Project Management Library项目管理甘特图控件

    2019独角兽企业重金招聘Python工程师标准>>> Project Management Library是一款项目管理控件,包含了项目管理相关的Windows客户端控件,如:Pr ...

  2. 项目管理控件Project Management Library

    Project Management Library是一款项目管理控件,包含了项目管理相关的Windows客户端控件,如:ProjectView, ResourcesView, ScheduleVie ...

最新文章

  1. 一大清早,我就被编辑赶出了办公室……
  2. 决策树准确率低原因_机器学习决策树算法--剪枝算法
  3. css教程–十步学会用css建站(全)
  4. 2_1 AdpterMode.cpp 适配器模式
  5. 5月19-20日WebRTCon 2018 梳理全球WebRTC技术实践与案例
  6. tar中的zxvf都是什么?
  7. 实现简单的注解型MVC框架 —— 低配SpringMVC
  8. 软件工程中交流的思考
  9. 用计算机三级处理文件,【题目】计算机三级题目,献给为计算机三级挣扎的同学们...
  10. Java加密与解密的艺术~MD算法实现
  11. 手把手教你调试Linux C++ 代码(一步到位包含静态库和动态库调试)
  12. 阿里有php的研发团队么,【阿里巴巴】阿里集团-MMC技术部-研发工程师JAVA
  13. The Unsolvable Problem
  14. 洛谷 P1433 吃奶酪 Label:dfs 剪枝Ex
  15. android申请蓝牙动态权限,Android权限动态申请
  16. 最近羊毛小更新 青龙面板 薅羊毛 22/6/6更新
  17. limits.conf详解
  18. 主观意义上第一个基于DEFI的DAM平台DIPPER,年关崛起的黑马究竟是否能在新年创造区块链新纪元?而DIPPER的意义和价值又是什么?
  19. java程序设计——实验四
  20. calc()语法规则

热门文章

  1. Proactol什么是脂肪燃烧
  2. Vue项目启动内存溢出 js stack overflow
  3. 基于matlab的SMO实现
  4. java的mvc模式是什么_什么是mvc模式
  5. 【计算机网络】知识点整理 第四章 网络层(王道考研视频学习记录)
  6. word计算机桌面加密,word文档加密,怎么让word自动加密 -电脑资料
  7. android drawtext 方法,8.2.13 drawText方法:绘制字符串
  8. wpf入门第七篇 使用Squirrel自动更新应用
  9. Aspose.Words for .NET使用教程(七):将文档转换为EPUB
  10. 介绍-fs、ramdisk、ramfs、tmpfs、initramfs和rootfs