单列的堆叠柱状图

目的

MSingleColumnStackBarChart类被设计用于创建只有单列的堆叠柱状图,用于血糖数据的统计。以下是封装这个类的目的的详细描述:

  1. 抽象复杂性: 通过创建MSingleColumnStackBarChart类,你将复杂的MPAndroidChart库的使用和配置封装在一个独立的类中。这有助于降低代码的复杂性,使得在其他部分的代码中更容易理解和维护。

  2. 提高可读性: 将与图表配置相关的代码集中在一个类中,使得主要的业务逻辑部分的代码更加清晰。其他开发者在查看代码时能够更轻松地理解图表的配置和使用方式。

  3. 重用性: 通过封装这个类,你可以在不同的部分或项目中重复使用相同的图表配置。这意味着,如果将来有其他地方需要显示类似的单列堆叠柱状图,你可以轻松地引入这个类,而无需重新实现相同的配置。

  4. 模块化: 类的封装使得代码更加模块化。这允许你将图表的配置和数据处理与其他功能分离,促使代码更易于组织和维护。

  5. 简化调用: 通过提供简单的接口,类的使用者只需几行代码就能创建和显示单列堆叠柱状图。这有助于降低使用图表功能时的学习曲线,并使代码更加整洁。

总体而言,MSingleColumnStackBarChart类的封装旨在提供一种简单、灵活且易于使用的方式,以满足特定场景下(如血糖数据统计)显示单列堆叠柱状图的需求。这样的封装是为了在开发中提高效率、降低出错概率,并促使代码更具可维护性。

示例 

中间的就是柱状形,只要按百分比进行堆叠显示。

调用示例
List<MSingleColumnStackBarChart.MBarData> dataList = new ArrayList<>();for (int x = 0; x < 1; x++) {MSingleColumnStackBarChart.MBarData data = new MSingleColumnStackBarChart.MBarData(15, getColor(R.color.colorHHigh), "15% 很高 > 13.0 mmol/L");dataList.add(data);data = new MSingleColumnStackBarChart.MBarData(10, getColor(R.color.colorHigh), "10% 偏高 > 10.0 mmol/L");dataList.add(data);data = new MSingleColumnStackBarChart.MBarData(60, getColor(R.color.colorNormal), "60% 正常 3.9-10.0 mmol/L");dataList.add(data);data = new MSingleColumnStackBarChart.MBarData(10, getColor(R.color.colorLow), "10% 偏低 < 3.9 mmol/L");dataList.add(data);data = new MSingleColumnStackBarChart.MBarData(5, getColor(R.color.colorLLow), "5% 很低 < 3.0 mmol/L ");dataList.add(data);
}BarChart barChart = findViewById(R.id.bar_chart);
MSingleColumnStackBarChart barChartView = new MSingleColumnStackBarChart(barChart);
barChartView.setBarDataSets(dataList);
完整的代码实现类
package com.jaredrummler.compent;import android.graphics.Color;
import android.graphics.PointF;import com.github.mikephil.charting.charts.BarChart;
import com.github.mikephil.charting.components.Legend;
import com.github.mikephil.charting.components.LegendEntry;
import com.github.mikephil.charting.components.XAxis;
import com.github.mikephil.charting.components.YAxis;
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.formatter.ValueFormatter;
import com.github.mikephil.charting.highlight.Highlight;
import com.github.mikephil.charting.listener.OnChartValueSelectedListener;import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;/*** author :hello* date : 2024/1/15 8:47* description : 只有一列数据的StackBarChart*/
public class MSingleColumnStackBarChart {private BarChart barChart;private MLegend mLegend;public MSingleColumnStackBarChart(BarChart barChart) {this.barChart = barChart;this.mLegend = new MLegend();init();}public void setBarDataSets(List<MBarData> dataList) {MBarDataSet barDataSet = new MBarDataSet(0, dataList);BarData barData = new BarData(barDataSet.getBarDataSet());barData.setBarWidth(.8f);this.barChart.setData(barData);this.mLegend.setLegendConfig(barDataSet.getBarDataSetsColors(), barDataSet.getBarLegendLabels().toArray(new String[0]));this.barChart.invalidate();}public void init() {setXAxisConfig();setYAxisConfig();///所有值均绘制在其条形顶部上方barChart.setDrawValueAboveBar(false);// 添加下面这行代码,关闭堆叠模式barChart.setDrawBarShadow(false);barChart.setFitBars(true);barChart.getDescription().setEnabled(false);barChart.animateXY(1000,1000);//选中高亮显示barChart.setHighlightFullBarEnabled(false);//双击缩放barChart.setDoubleTapToZoomEnabled(false);// 设置 是否可以缩放barChart.setScaleEnabled(false);barChart.setDrawGridBackground(false);barChart.setOnChartValueSelectedListener(new OnChartValueSelectedListener() {@Overridepublic void onValueSelected(Entry e, Highlight h) {mLegend.showLegendSelected(h.getStackIndex());}@Overridepublic void onNothingSelected() {mLegend.resetLegendDefault();}});barChart.invalidate(); // 刷新图表}private void setXAxisConfig() {// 使柱状图的中心与 X 轴标签对齐XAxis xAxis = barChart.getXAxis();xAxis.setEnabled(false);xAxis.setDrawLabels(false);//取消 垂直 网格线xAxis.setDrawGridLines(false);}private void setYAxisConfig() {YAxis yLeftAxis = barChart.getAxisLeft();yLeftAxis.setEnabled(false);yLeftAxis.setDrawLabels(false);yLeftAxis.setDrawGridLines(false);yLeftAxis.setAxisMinimum(0);yLeftAxis.setAxisMaximum(100);YAxis yRightAxis = barChart.getAxisRight();yRightAxis.setEnabled(false);yRightAxis.setDrawLabels(false);yRightAxis.setDrawGridLines(false);}private static class MBarDataSet {private BarDataSet barDataSet;private List<BarEntry> barEntries;private List<MBarData> dataList;public MBarDataSet(int index, List<MBarData> dataList) {this.dataList = dataList;barEntries = covertToBarEntry(index, dataList);barDataSet = new BarDataSet(barEntries, "");barDataSet.setColors(dataList.stream().map(MBarData::getColor).collect(Collectors.toList()));
//            barDataSet.setValueTextSize(10f);barDataSet.setDrawValues(false);
//            barDataSet.setValueTextColor(Color.WHITE);
//            barDataSet.setValueFormatter(new ValueFormatter() {
//                @Override
//                public String getFormattedValue(float value) {
//                    return String.format("%.1f", value) + "%";
//                }
//
//            }); // 设置值文本的位置为外部barDataSet.setBarShadowColor(Color.WHITE);barDataSet.setBarBorderWidth(2f);barDataSet.setBarBorderColor(Color.WHITE);}public BarDataSet getBarDataSet() {return barDataSet;}public List<BarEntry> getBarEntries() {return barEntries;}private List<BarEntry> covertToBarEntry(float index, List<MBarData> dataList) {// y 数据ArrayList<BarEntry> yValues = new ArrayList<>();float[] stackedValues = new float[dataList.size()];// 遍历 yourDataList,获取每个数据项的百分比值,构建堆叠数据for (int i = 0; i < dataList.size(); i++) {float yValue = dataList.get(i).getYValue();stackedValues[i] = yValue;}BarEntry barEntry = new BarEntry(index, stackedValues);yValues.add(barEntry);return yValues;}private List<Integer> getBarDataSetsColors() {List<Integer> barColors = new ArrayList<>();for (MBarData data : this.dataList) {barColors.add(data.getColor());}return barColors;}private List<String> getBarLegendLabels() {List<String> legendLabels = new ArrayList<>();for (MBarData data : this.dataList) {legendLabels.add(data.getLabel());}return legendLabels;}}public static class MBarData {private float yValue;private int color;private String label;public MBarData(float percentage, int color, String label) {this.yValue = percentage;this.color = color;this.label = label;}public float getYValue() {return yValue;}public int getColor() {return color;}public String getLabel() {return label;}}private class MLegend {public static final float SELECTED_FORM_SIZE = 16f;public static final float DEFAULT_FORM_SIZE = 10f;public void showLegendSelected(int index) {// 选中时突出显示相应的 Legend 标签// 获取 Legend 对象Legend legend = barChart.getLegend();// 获取当前 Legend 的条目LegendEntry[] entries = legend.getEntries();for (int i = 0; i < entries.length; i++) {if (i == index) {entries[index].formSize = SELECTED_FORM_SIZE;} else {entries[i].formSize = DEFAULT_FORM_SIZE;}}// 刷新图表barChart.invalidate();}public void resetLegendDefault() {// 获取当前 Legend 的条目// 获取 Legend 对象Legend legend = barChart.getLegend();LegendEntry[] entries = legend.getEntries();for (int i = 0; i < entries.length; i++) {entries[i].formSize = DEFAULT_FORM_SIZE;}barChart.invalidate();}public void setLegendConfig(List<Integer> barColors, String[] legendLabels) {Legend legend = barChart.getLegend();legend.setFormToTextSpace(8f);legend.setStackSpace(16f);legend.setForm(Legend.LegendForm.CIRCLE);legend.setTextSize(14f);YAxis yAxis = barChart.getAxisLeft();float spaceTop = yAxis.getSpaceTop();float spaceBottom = yAxis.getSpaceBottom();float yAxisHeight = yAxis.getAxisMaximum() - spaceTop - spaceBottom;legend.setYEntrySpace(yAxisHeight / (legendLabels.length));legend.setYOffset(spaceTop);legend.setVerticalAlignment(Legend.LegendVerticalAlignment.TOP);legend.setHorizontalAlignment(Legend.LegendHorizontalAlignment.RIGHT);legend.setOrientation(Legend.LegendOrientation.VERTICAL);legend.setStackSpace(1f);legend.setDrawInside(false);if (legendLabels != null && legendLabels.length > 0) {List<LegendEntry> legendEntries = new ArrayList<>();for (int i = 0; i < legendLabels.length; i++) {LegendEntry entry = new LegendEntry();entry.label = legendLabels[i];entry.formColor = barColors.get(i);entry.formSize = 10f;legendEntries.add(entry);}legend.setCustom(legendEntries);}}}
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/629865.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

React全局状态管理

redux是一个状态管理框架&#xff0c;它可以帮助我们清晰定义state和处理函数&#xff0c;提高可读性&#xff0c;并且redux中的状态是全局共享&#xff0c;规避组件间通过props传递状态等操作。 快速使用 在React应用的根节点&#xff0c;需要借助React的Context机制存放整个…

mobi文件怎么转换成pdf?

mobi文件怎么转换成pdf&#xff1f;在数字化时代&#xff0c;电子书籍成为了越来越受欢迎的阅读方式。我们可以通过多种格式的电子书来获取知识和娱乐&#xff0c;其中一种常见的格式就是Mobi文件。Mobi文件是亚马逊公司开发的一种电子书格式&#xff0c;它主要用于Kindle设备和…

SL4010升压恒压电源芯片DC3.7V升压5V、12V、24V/5A

SL4010是一款升压恒压电源芯片&#xff0c;可以将DC3.7V的输入电压升压至5V、12V或24V的输出电压&#xff0c;并可提供高达5A的输出电流。这款芯片采用了先进的升压技术&#xff0c;能够实现高效、稳定的电压转换&#xff0c;同时还具有低噪声、低功耗和低成本等优点。在各种需…

【论文阅读】Consistency Models

文章目录 IntroductionDiffusion ModelsConsistency ModelsDefinitionParameterizationSampling Training Consistency Models via DistillationTraining Consistency Models in IsolationExperiment Introduction 相比于单步生成的模型&#xff08;例如 GANs, VAEs, normalizi…

推荐几个Github高星GoLang管理系统

在Web开发领域&#xff0c;Go语言&#xff08;Golang&#xff09;以其高效、简洁、高并发等特性逐渐成为许多开发者的首选语言。有许多优秀的Go语言Web后台管理系统&#xff0c;这些项目星星众多&#xff0c;提供了丰富的功能和良好的代码质量。本文将介绍一些GitHub高星的GoLa…

学会这个昼夜系统,你也能做出一款饥荒生存类游戏DEMO!

学会这个昼夜系统&#xff0c;你也能做出一款饥荒生存类游戏DEMO&#xff01; 《饥荒》作为生存类游戏的老大哥&#xff0c;深受大家喜爱&#xff0c;这款游戏于2012年年底正式公测上线&#xff0c;距今已有10年的时间&#xff0c;从最初的单机版慢慢推出了联机版&#xff0c;…

Android平台Unity下如何通过WebCamTexture采集摄像头数据并推送至RTMP服务器或轻量级RTSP服务

技术背景 我们在对接Unity下推送模块的时候&#xff0c;遇到这样的技术诉求&#xff0c;开发者希望在Android的Unity场景下&#xff0c;获取到前后摄像头的数据&#xff0c;并投递到RTMP服务器&#xff0c;实现低延迟的数据采集处理。 在此之前&#xff0c;我们已经有了非常成…

大模型学习之书生·浦语大模型5——基于LMDeploy大模型量化部署实践

目录 大模型部署背景 LMDeploy部署 量化 TurboMind API server 动手实践环节

LCR 173. 点名(二分)

一、题目描述 LCR 173. 点名 某班级 n 位同学的学号为 0 ~ n-1。点名结果记录于升序数组 records。假定仅有一位同学缺席&#xff0c;请返回他的学号。 示例 1: 输入: records [0,1,2,3,5] 输出: 4示例 2: 输入: records [0, 1, 2, 3, 4, 5, 6, 8] 输出: 7 二、题目解析…

flink1.14.5使用CDH6.3.2的yarn提交作业

使用CDH6.3.2安装了hadoop集群&#xff0c;但是CDH不支持flink的安装&#xff0c;网上有CDH集成flink的文章&#xff0c;大都比较麻烦&#xff1b;但其实我们只需要把flink的作业提交到yarn集群即可&#xff0c;接下来以CDH yarn为基础&#xff0c;flink on yarn模式的配置步骤…

Resemble Enhance音频失真损坏修复AI工具:一个开源语音超分辨率AI模型

Resemble Enhance是一款强大的音频处理工具&#xff0c;可以将嘈杂的录音转化为清晰而有力的声音&#xff0c;为用户提供更优质的听觉体验。这个工具不仅可以有效去除录音中的各种噪声和杂音&#xff0c;还能够恢复音频失真并扩展音频带宽&#xff0c;使原本的声音听起来更加清…

高级分布式系统-第10讲 分布式控制系统

高级分布式系统汇总&#xff1a;高级分布式系统目录汇总-CSDN博客 自动化是关于一切人造系统自动、智能、自主、高效和安全运行的科学与技术 计算机控制技术是实现自动化的主要方法和手段 分布式控制技术是伴随着机器大工业生产而诞生的特殊计算机控制技术 计算机控制系统 …

Qt/QML编程之路:使用camera摄像头(35)

汽车应用中,camera起到了越来越多的作用,数字化的作用,这点无可争议,而作为GUI设计工具,如何让Camera类的应用能更好的发挥作用呢? You can use Camera to capture images and movies from a camera, and manipulate the capture and processing settings that get appl…

网站防御爬虫攻击有哪些方式

很多网站都深受爬虫困扰&#xff0c;网站在被爬虫大量抓取的的时候经常容易被爬虫把服务器资源抓崩了&#xff0c;有的时候&#xff0c;同行也会来爬取我们网站进行数据采集&#xff0c;影响我们站点的原创性&#xff0c;那么如何进行相对应的防护还是非常重要的&#xff01; …

mysql新增用户密码控制局域网访问权限

方法一、通过navicat中sql语句新增 CREATE USER usernamelocalhost IDENTIFIED BY password; GRANT ALL PRIVILEGES ON *.* TO usernamelocalhost WITH GRANT OPTION; FLUSH PRIVILEGES;把其中的username和password改成自己的即可 如果将上面的localhost改成%&#xff0c;则这…

从CISC到RISC-V:揭开指令集的面纱

对于大多数同学来说&#xff0c;计算机或智能手机的运行似乎就像魔法一样神奇。你可能知道它们内部都是一些复杂的电子组件&#xff0c;比如CPU、内存等等&#xff0c;但这些组件是如何协同工作&#xff0c;让我们可以在电脑上打字&#xff0c;或者在手机上看视频呢&#xff1f…

1.环境部署

1.虚拟机安装redhat8系统 这个其实很简单&#xff0c;但是有一点小细节需要注意。 因为我的电脑是 16核心的&#xff0c;所以选择内核16&#xff0c;可以最大发挥虚拟机的性能 磁盘选择SATA&#xff0c;便于后期学习 将一些没用的设备移除 选择安装redhat 8 时间选择上海 选择…

无法解析的外部符号ShellExecuteExW

问题情况 在QT使用&#xff1a;ShellExecuteEx时遇上这么一个错误&#xff1a;遇上这么一个错误&#xff1a; error: LNK2019: 无法解析的外部符号 __imp_ShellExecuteExW *ReportService.obj&#x1f44e; error: LNK2019: 无法解析的外部符号 __imp_ShellExecuteExW&#x…

RocketMQ源码阅读-Producer消息发送

RocketMQ源码阅读-Producer消息发送 1. 从单元测试入手2. 启动过程3. 同步消息发送过程4. 异步消息发送过程5. 小结 Producer是消息的生产者。 Producer和Consummer对Rocket来说都是Client&#xff0c;Server是Broker。 客户端在源码中是一个单独的Model&#xff0c;目录为rock…

ASP.NET Core 的 Web Api 实现限流 中间件

Microsoft.AspNetCore.RateLimiting 中间件提供速率限制&#xff08;限流&#xff09;中间件。 它是.NET 7 以上版本才支持的中间件&#xff0c;刚看了一下&#xff0c;确实挺好用&#xff0c;下面给大家简单介绍一下&#xff1a; RateLimiterOptionsExtensions 类提供下列用…