基于 OpenHarmony HistogramComponent 柱状图开发指南

news/2024/7/21 8:48:52 标签: 华为, 物联网, harmonyos, ui, 网络

1. HistogramComponent 组件功能介绍

1.1. 功能介绍

应用开发过程,用鸿蒙提供的 Component 自定义柱状图效果。

HistogramComponent 组件可以更快速实现一个简单的柱状图功能。

HistogramComponent 对外提供数据源,修改柱状图颜色,间距的接口。

1.2. phone 模拟器上运行效果

2 . HistogramComponent 使用方法

2.1 新建工程,增加组件 Har 包依赖

在应用模块中调用 HAR,常用的添加依赖的方式包括如下两种。

方式一:依赖本地 HAR,将 histogramcomponent-debug.har 复制到 entry\libs 目录下
即可(由于 build.gradle 已经依赖的 libs 目录下的*.har,因此不需要在做修改)。

查看工程目录中 build.gradle 下的*.har 是存在。

以上操作无误之后就可以进行编码了!

3 . HistogramComponent 开发实现

3.1 . 主页面的布局文件

3.2. 主页面的 Ability 的代码

@Override
public void onStart(Intent intent) {
super.onStart(intent);
super.setUIContent(ResourceTable.Layout_ability_main);
bargraphview = (HistogramComponent)
findComponentById(ResourceTable.Id_bargraphview);
// 设置每组柱状图之间的间距
bargraphview.mLineSpaceWidth = 30;
final int[][] data = {{182, 89, 78, 88}, {34, 85, 16, 96}, {86, 127, 45,
41},{54, 75, 54, 12}};
final int[] colorData = {0xFF222233, 0xFFe67656,
0xFF188888,0xFF888888,0xFF888888};
final String[] textData = {"一月份", "二月份", "三月份", "四月份"};

// 核心接口,填充数据,柱状图颜色
bargraphview.setBarGraphData(data, colorData, textData);
}

3.3. HistogramComponent 组件核心代码

/**
* 定义 DrawTask 对象的实例
* 这里进行具体的绘画工作
*/
private DrawTask drawTask = new DrawTask() {
@Override
public void onDraw(Component component, Canvas canvas) {
bottomHeight = heightMeasureSpec - mBottomXWidth;
if (barGraphDataList == null || barGraphDataList.length <= 0)
return;
//画柱状图
drawBarGraph(canvas);
//画 XY 轴坐标
drawXYLine(canvas);
//给 XY 轴坐标写字
drawXYText(canvas);
}
};
//画柱状图
private void drawBarGraph(Canvas canvas) {
if (barGraphDataList != null && barGraphDataList.length > 0) {
for (int i = 0; i < barGraphDataList[0].length; i++) {
float startX = mLineSpaceWidth * (i + 1) + mLineWidth *
barGraphDataList.length * i + mLeftYWidth + (10) + mLineWidth / 2;
int index = 0;
while (index < barGraphDataList.length) {
if (barGraphColorList != null && barGraphColorList.length >
index) {
mBarGraphPaint.setColor(new
Color(barGraphColorList[index]));
mBarGraphTextPaint.setColor(new
Color(barGraphColorList[index]));
} else {
mBarGraphPaint.setColor(new Color(barGraphBgColor));
mBarGraphTextPaint.setColor(new Color(barGraphBgColor));
}
float stopY = bottomHeight * 0.9f / maxHeight *
barGraphDataList[index][i];
canvas.drawLine(new Point(startX, bottomHeight), new
Point(startX, bottomHeight - stopY), mBarGraphPaint);
String text = String.valueOf(barGraphDataList[index][i]);
float textWidth = mBarGraphTextPaint.measureText(text);
canvas.drawText(mBarGraphTextPaint,text, startX - textWidth
/ 2, bottomHeight - stopY - 10);
startX += mLineWidth;
index++;
}
}
}
}
//画 X 轴和 Y 轴的竖线+箭头
private void drawXYLine(Canvas canvas) {
/**
* 让 Y 轴文字与最左有 dip2px(10)的边距
* */
//Y 轴竖线
canvas.drawLine(new Point((10) + mLeftYWidth, bottomHeight), new
Point((10) + mLeftYWidth, 10), mXYLinePaint);
//X 轴竖线
canvas.drawLine(new Point((10) + mLeftYWidth, bottomHeight), new
Point(widthMeasureSpec - 10, bottomHeight), mXYLinePaint);
//画个箭头??Y 轴
canvas.drawLine(new Point((10) + mLeftYWidth, 10), new Point((6) +
mLeftYWidth, 20), mXYLinePaint);
canvas.drawLine(new Point((10) + mLeftYWidth, 10), new Point((14) +
mLeftYWidth, 20), mXYLinePaint);
//X 轴箭头
canvas.drawLine(new Point(widthMeasureSpec - 10, bottomHeight), new
Point(widthMeasureSpec - 20, bottomHeight - (4)), mXYLinePaint);
canvas.drawLine(new Point(widthMeasureSpec - 10, bottomHeight), new
Point(widthMeasureSpec - 20, bottomHeight + (4)), mXYLinePaint);
}
//给 Y 轴和 X 轴写相应的文字
private void drawXYText(Canvas canvas) {
if (isShowYText) {
//Y 轴写字
for (int i = 1; i <= 5; i++) {
float startY = bottomHeight - bottomHeight * 0.9f / maxHeight *
maxHeight / 5 * i;
canvas.drawLine(new Point((10) + mLeftYWidth, startY), new
Point((15) + mLeftYWidth, startY), mYTextPaint);
float width = mYTextPaint.measureText(maxHeight / 5 * i + "");
float dy = 12.0f;
canvas.drawText(mYTextPaint,maxHeight / 5 * i + "", (int) ((10)
+ mLeftYWidth - width - (5)), startY + dy);
}
}
if (!isShowXText) {
return;
}
//X 轴写字
if (barGraphTextList != null && barGraphTextList.length > 0) {
for (int i = 0; i < barGraphTextList.length; i++) {
float startX = mLineSpaceWidth * (i + 1) + mLineWidth *
barGraphDataList.length * i + mLeftYWidth + (10);
//中间有一个间隔
startX = startX + (mLineWidth * barGraphDataList.length) * 1.0f / 2;
float textWidth = mXTextPaint.measureText(barGraphTextList[i]);
canvas.drawText(mXTextPaint,barGraphTextList[i], startX -
textWidth / 2, heightMeasureSpec - (5));
}
}
}
// 对外提供的核心接口
public void setBarGraphData(int[][] barGraphDataList, int[]
barGraphColorList, String[] barGraphTextList) {
this.barGraphDataList = barGraphDataList;
this.barGraphColorList = barGraphColorList;
this.barGraphTextList = barGraphTextList;
for(int i = 0; i < barGraphDataList.length; ++i) {
for(int j = 0; j < barGraphDataList[i].length; ++j) {
if (this.maxHeight < barGraphDataList[i][j]) {
this.maxHeight = barGraphDataList[i][j];
}
}
}
while(this.maxHeight % 5 != 0) {
++this.maxHeight;
}
if (barGraphTextList != null && barGraphTextList.length > 0) {
this.isShowXText = true;
}
if (this.isShowYText) {
this.mLeftYWidth =
this.mYTextPaint.measureText(String.valueOf(this.maxHeight));
}
this.mBottomXWidth = 10.0F;
if (this.isShowXText) {
FontMetrics fontMetrics = this.mXTextPaint.getFontMetrics();
this.mBottomXWidth += ((fontMetrics.bottom - fontMetrics.top) /
2.0F - fontMetrics.bottom) * 2.0F;
}
this.measureWidth(this.heightMeasureSpec);
this.invalidate();
}

为了能让大家更好的学习鸿蒙(HarmonyOS NEXT)开发技术,这边特意整理了《鸿蒙开发学习手册》(共计890页),希望对大家有所帮助:https://qr21.cn/FV7h05

《鸿蒙开发学习手册》:

如何快速入门:https://qr21.cn/FV7h05

  1. 基本概念
  2. 构建第一个ArkTS应用
  3. ……

开发基础知识:https://qr21.cn/FV7h05

  1. 应用基础知识
  2. 配置文件
  3. 应用数据管理
  4. 应用安全管理
  5. 应用隐私保护
  6. 三方应用调用管控机制
  7. 资源分类与访问
  8. 学习ArkTS语言
  9. ……

基于ArkTS 开发:https://qr21.cn/FV7h05

  1. Ability开发
  2. UI开发
  3. 公共事件与通知
  4. 窗口管理
  5. 媒体
  6. 安全
  7. 网络与链接
  8. 电话服务
  9. 数据管理
  10. 后台任务(Background Task)管理
  11. 设备管理
  12. 设备使用信息统计
  13. DFX
  14. 国际化开发
  15. 折叠屏系列
  16. ……

鸿蒙开发面试真题(含参考答案):https://qr18.cn/F781PH

鸿蒙开发面试大盘集篇(共计319页):https://qr18.cn/F781PH

1.项目开发必备面试题
2.性能优化方向
3.架构方向
4.鸿蒙开发系统底层方向
5.鸿蒙音视频开发方向
6.鸿蒙车载开发方向
7.鸿蒙南向开发方向


http://www.niftyadmin.cn/n/5477329.html

相关文章

自动驾驶基础技术-无迹卡尔曼滤波UKF

自动驾驶基础技术-无迹卡尔曼滤波UKF Unscented Kalman Filter是解决非线性卡尔曼滤波的另一种思路&#xff0c;它利用Unscented Transform来解决概率分布非线性变换的问题。UnScented Kalman Filter不需要像Extended Kalman Filter一样计算Jacobin矩阵&#xff0c;在计算量大…

如何将本地websocket服务端从本地暴露至公网实现客户端远程连接

文章目录 1. Java 服务端demo环境2. 在pom文件引入第三包封装的netty框架maven坐标3. 创建服务端,以接口模式调用,方便外部调用4. 启动服务,出现以下信息表示启动成功,暴露端口默认99995. 创建隧道映射内网端口6. 查看状态->在线隧道,复制所创建隧道的公网地址加端口号7. 以…

paddle实现手写数字模型(一)

参考文档&#xff1a;paddle官网文档环境&#xff1a;Python 3.12.2 &#xff0c;pip 24.0 &#xff0c;paddlepaddle 2.6.0 python -m pip install paddlepaddle2.6.0 -i https://pypi.tuna.tsinghua.edu.cn/simple调试代码如下&#xff1a; LeNet.py import paddle import p…

如何使用GraphQL和Apollo构建一个宝可梦应用

宝可梦是一个由视频游戏、动画系列与电影、交换卡牌游戏以及其他相关媒体组成的日本媒体特许经营权。 在本文中&#xff0c;我们将使用一个宝可梦GraphQL API&#xff0c;该API提供有关不同宝可梦的数据。 我们将使用Apollo和GraphQL来处理数据获取&#xff0c;以及React来构…

【MacOs】proxychains配置使用

一、开始 1. 安装proxychains 使用brew进行安装 brew install proxychains-ng没有homebrew的&#xff0c;可以使用该命令安装 /usr/bin/ruby -e "$(curl -fsSL https://cdn.jsdelivr.net/gh/ineo6/homebrew-install/install)"2. 配置代理配置文件 cd /opt/homeb…

EditPlus来啦(免费使用!)

hello&#xff0c;我是小索奇 今天推荐一款编辑器&#xff0c;是索奇学习JavaSE时入手滴&#xff0c;非常好用哈&#xff0c;小索奇还是通过老杜-杜老师入手滴&#xff0c;相信很多人也是通过老杜认识嘞&#xff0c;来寻找破解版或者准备入手这个间接使用的编辑器~ EditPlus是…

6-99 快速排序(Java语言描述)

编程实现快速排序函数。public static void quickSort(int arr[], int low, int high)。其中arr存放待排序的数据,数组长度不大于1000。 函数接口定义: /* 对长度为n的数组arr执行快速排序 */ public static void quickSort(int arr[], int low, int high); 请实现quickSo…

Java | Leetcode Java题解之第17题电话号码的字母组合

题目&#xff1a; 题解&#xff1a; class Solution {public List<String> letterCombinations(String digits) {List<String> combinations new ArrayList<String>();if (digits.length() 0) {return combinations;}Map<Character, String> phoneM…