[文章]【软通动力】HarmonyOS三方件开发指南(9)-HistogramComponent

阅读量0
0
0
`1. HistogramComponent组件功能介绍
1.1. 功能介绍
        在开发柱状图的过程中,通过自定义组件HistogramComponent可以更快速实现一个简单的柱状图功能,对外提供数据源,修改柱状图颜色和间距的接口。
1.2. phone模拟器上运行效果

2 . HistogramComponent使用方法
新建工程,增加组件Har包依赖,在应用模块中调用HAR,常用的添加依赖方式包括如下两种。
Ø  方式一:依赖本地HAR,将histogramcomponent-debug.har复制到entrylibs目录下即可(由于build.gradle已经依赖的libs目录下的*.har,因此不需要再做修改)。

Ø  方式二:查看工程目录中build.gradle下的*.har是否存在。
2.png

3.png

以上操作无误之后就可以进行编码了!
3 . HistogramComponent开发实现

3.1 . 主页面的布局文件
4.png

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

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

  13. }
复制代码
3.3. HistogramComponent组件核心代码
  1. /**
  2. * 定义DrawTask对象的实例
  3. * 这里进行具体的绘画工作
  4. */
  5. private DrawTask drawTask = new DrawTask() {
  6.     @Override
  7.     public void onDraw(Component component, Canvas canvas) {
  8.         bottomHeight = heightMeasureSpec - mBottomXWidth;
  9.         if (barGraphDataList == null || barGraphDataList.length <= 0)
  10.             return;
  11.         //画柱状图
  12.         drawBarGraph(canvas);
  13.         //画XY轴坐标
  14.         drawXYLine(canvas);
  15.         //给XY轴坐标写字
  16.         drawXYText(canvas);
  17.     }
  18. };

  19. //画柱状图
  20. private void drawBarGraph(Canvas canvas) {
  21.     if (barGraphDataList != null && barGraphDataList.length > 0) {
  22.         for (int i = 0; i < barGraphDataList[0].length; i++) {
  23.             float startX = mLineSpaceWidth * (i + 1) + mLineWidth * barGraphDataList.length * i + mLeftYWidth + (10) + mLineWidth / 2;
  24.             int index = 0;
  25.             while (index < barGraphDataList.length) {
  26.                 if (barGraphColorList != null && barGraphColorList.length > index) {
  27.                     mBarGraphPaint.setColor(new Color(barGraphColorList[index]));
  28.                     mBarGraphTextPaint.setColor(new Color(barGraphColorList[index]));
  29.                 } else {
  30.                     mBarGraphPaint.setColor(new Color(barGraphBgColor));
  31.                     mBarGraphTextPaint.setColor(new Color(barGraphBgColor));
  32.                 }

  33.                 float stopY = bottomHeight * 0.9f / maxHeight * barGraphDataList[index][i];

  34.                 canvas.drawLine(new Point(startX, bottomHeight), new Point(startX, bottomHeight - stopY), mBarGraphPaint);

  35.                 String text = String.valueOf(barGraphDataList[index][i]);
  36.                 float textWidth = mBarGraphTextPaint.measureText(text);
  37.                 canvas.drawText(mBarGraphTextPaint,text, startX - textWidth / 2, bottomHeight - stopY - 10);
  38.                 startX += mLineWidth;
  39.                 index++;
  40.             }
  41.         }
  42.     }
  43. }

  44. //画X轴和Y轴的竖线+箭头
  45. private void drawXYLine(Canvas canvas) {
  46.     /**
  47.      * 让Y轴文字与最左有dip2px(10)的边距
  48.      **/
  49.     //Y轴竖线
  50.     canvas.drawLine(new Point((10) + mLeftYWidth, bottomHeight), new Point((10) + mLeftYWidth, 10), mXYLinePaint);
  51.     //X轴竖线
  52.     canvas.drawLine(new Point((10) + mLeftYWidth, bottomHeight), new Point(widthMeasureSpec - 10, bottomHeight), mXYLinePaint);
  53.     //画个箭头Y轴
  54.     canvas.drawLine(new Point((10) + mLeftYWidth, 10), new Point((6) + mLeftYWidth, 20), mXYLinePaint);
  55.     canvas.drawLine(new Point((10) + mLeftYWidth, 10), new Point((14) + mLeftYWidth, 20), mXYLinePaint);
  56.     //X轴箭头
  57.     canvas.drawLine(new Point(widthMeasureSpec - 10, bottomHeight), new Point(widthMeasureSpec - 20, bottomHeight - (4)), mXYLinePaint);
  58.     canvas.drawLine(new Point(widthMeasureSpec - 10, bottomHeight), new Point(widthMeasureSpec - 20, bottomHeight + (4)), mXYLinePaint);
  59. }
  60. //给Y轴和X轴写相应的文字
  61. private void drawXYText(Canvas canvas) {
  62.     if (isShowYText) {
  63.         //Y轴写字
  64.         for (int i = 1; i <= 5; i++) {
  65.             float startY = bottomHeight - bottomHeight * 0.9f / maxHeight * maxHeight / 5 * i;
  66.             canvas.drawLine(new Point((10) + mLeftYWidth, startY), new Point((15) + mLeftYWidth, startY), mYTextPaint);
  67.             float width = mYTextPaint.measureText(maxHeight / 5 * i + "");

  68.             float dy = 12.0f;
  69.             canvas.drawText(mYTextPaint,maxHeight / 5 * i + "", (int) ((10) + mLeftYWidth - width - (5)), startY + dy);
  70.         }
  71.     }
  72.     if (!isShowXText) {
  73.         return;
  74.     }
  75.     //X轴写字
  76.     if (barGraphTextList != null && barGraphTextList.length > 0) {
  77.         for (int i = 0; i < barGraphTextList.length; i++) {
  78.             float startX = mLineSpaceWidth * (i + 1) + mLineWidth * barGraphDataList.length * i + mLeftYWidth + (10);
  79.             //中间有一个间隔
  80.             startX = startX + (mLineWidth * barGraphDataList.length) * 1.0f / 2;
  81.             float textWidth = mXTextPaint.measureText(barGraphTextList[i]);
  82.             canvas.drawText(mXTextPaint,barGraphTextList[i], startX - textWidth / 2, heightMeasureSpec - (5));
  83.         }
  84.     }
  85. }

  86. // 对外提供的核心接口

  87. public void setBarGraphData(int[][] barGraphDataList, int[] barGraphColorList, String[] barGraphTextList) {
  88.     this.barGraphDataList = barGraphDataList;
  89.     this.barGraphColorList = barGraphColorList;
  90.     this.barGraphTextList = barGraphTextList;

  91.     for(int i = 0; i < barGraphDataList.length; ++i) {
  92.         for(int j = 0; j < barGraphDataList[i].length; ++j) {
  93.             if (this.maxHeight < barGraphDataList[i][j]) {
  94.                 this.maxHeight = barGraphDataList[i][j];
  95.             }
  96.         }
  97.     }

  98.     while(this.maxHeight % 5 != 0) {
  99.         ++this.maxHeight;
  100.     }

  101.     if (barGraphTextList != null && barGraphTextList.length > 0) {
  102.         this.isShowXText = true;
  103.     }

  104.     if (this.isShowYText) {
  105.         this.mLeftYWidth = this.mYTextPaint.measureText(String.valueOf(this.maxHeight));
  106.     }

  107.     this.mBottomXWidth = 10.0F;
  108.     if (this.isShowXText) {
  109.         FontMetrics fontMetrics = this.mXTextPaint.getFontMetrics();
  110.         this.mBottomXWidth += ((fontMetrics.bottom - fontMetrics.top) / 2.0F - fontMetrics.bottom) * 2.0F;
  111.     }

  112.     this.measureWidth(this.heightMeasureSpec);
  113.     this.invalidate();
  114. }
复制代码
项目源代码地址:https://github.com/isoftstone-dev/BarGraphView_HarmonyOS
©著作权归作者所有,如需转载,请注明出处,否则将追究法律责任

`
1.png

回帖

声明:本文内容及配图由入驻作者撰写或者入驻合作网站授权转载。文章观点仅代表作者本人,不代表电子发烧友网立场。文章及其配图仅供工程师学习之用,如有内容图片侵权或者其他问题,请联系本站作侵删。 侵权投诉
链接复制成功,分享给好友