【OpenHarmony】 鸿蒙 UI开发之MpChart(一)

打印 上一主题 下一主题

主题 1522|帖子 1522|积分 4566

<hr> 往期笔录记录:

鸿蒙应用开发与鸿蒙体系开发哪个更有远景?

嵌入式开发适不适合做鸿蒙南向开发?看完这篇你就了解了~

对于大前端开发来说,转鸿蒙开发毕竟是福还是祸?

鸿蒙岗位需求突增!移动端、PC端、IoT到底该怎么选?

记录一场鸿蒙开发岗位口试履历~

连续更新中……

<hr> 简介

mpchart是一个包含各种范例图表的图表库,重要用于业务数据汇总,例如销售数据走势图,股价走势图等场景中使用,方便开发者快速实现图表UI,mpchart重要包括线形图、柱状图、饼状图、蜡烛图、气泡图、雷达图、瀑布图等自定义图表库。
结果展示:


安装教程

  1. ohpm install @ohos/mpchart
复制代码
使用说明

柱状图


  • 柱状图数据初始化:
  1. import {
  2.   BarChart, // 柱状图图表类
  3.   BarChartModel, // 柱状图配置构建类
  4.   BarData, // 柱状图数据包
  5.   BarDataSet, // 柱状图数据集合
  6.   BarEntry, // 柱状图数据结构
  7.   ChartGesture, // 手势事件模式
  8.   Description, // 图表Description(描述)部件
  9.   EntryOhos, // 图表数据结构基础类
  10.   Fill, // 图表填充类型构建类
  11.   Highlight, // 图表高亮数据
  12.   IBarDataSet, // 柱状图数据集合的操作类
  13.   JArrayList, // 工具类:数据集合
  14.   Legend, // 图表Legend(图例)部件
  15.   LimitLabelPosition, // 图表的LimitLine标签位置枚举类
  16.   LimitLine, // 图表LimitLine
  17.   MarkerView, // 图表的Marker(标志气泡)部件
  18.   OnChartGestureListener, // 手势事件监听
  19.   OnChartValueSelectedListener, // 数据选择监听
  20.   XAxis, // 图表X轴部件
  21.   XAxisPosition, // 图表X轴标签位置枚举类
  22.   YAxis, // 图表Y轴部件
  23.   YAxisLabelPosition // 图表Y轴标签位置枚举类
  24. } from '@ohos/mpchart';
  25. // 构造数据选择监听器
  26.   private valueSelectedListener: OnChartValueSelectedListener = {
  27.     onValueSelected: (e: EntryOhos, h: Highlight) => {
  28.     // ...todoSomething
  29.     },
  30.     onNothingSelected: () => {
  31.     // ...todoSomething
  32.     }
  33.   }
  34.   // 构造手势识别事件监听器
  35.   private chartGestureListener: OnChartGestureListener = {
  36.     onChartGestureStart: (isTouchEvent: boolean, me: TouchEvent | GestureEvent, lastPerformedGestureMode: ChartGesture) => {
  37.     },
  38.     onChartGestureEnd: (isTouchEvent: boolean, me: TouchEvent | GestureEvent, lastPerformedGestureMode: ChartGesture) => {
  39.     // ...todoSomething
  40.     },
  41.     onChartLongPressed: (isTouchEvent: boolean, me: TouchEvent | GestureEvent) => {
  42.     // ...todoSomething
  43.     },
  44.     onChartDoubleTapped: (isTouchEvent: boolean, me: TouchEvent | GestureEvent) => {
  45.     // ...todoSomething
  46.     },
  47.     onChartSingleTapped: (isTouchEvent: boolean, me: TouchEvent | GestureEvent) => {
  48.     // ...todoSomething
  49.     },
  50.     onChartFling: (isTouchEvent: boolean, me1: TouchEvent | GestureEvent, me2: TouchEvent, velocityX: number, velocityY: number) => {
  51.     // ...todoSomething
  52.     },
  53.     onChartScale: (isTouchEvent: boolean, me: TouchEvent | GestureEvent, scaleX: number, scaleY: number) => {
  54.     // ...todoSomething
  55.     },
  56.     onChartTranslate: (isTouchEvent: boolean, me: TouchEvent | GestureEvent, dX: number, dY: number) => {
  57.     // ...todoSomething
  58.     }
  59.   }
  60.   // 图表数据初始化
  61.   aboutToAppear() {
  62.           // Step1:必须:初始化图表配置构建类
  63.     this.model = new BarChartModel();
  64.     // Step2:配置图表指定样式,各部件间没有先后之分
  65.     // 为图表添加数据选择的监听器
  66.     this.model.setOnChartValueSelectedListener(this.valueSelectedListener);
  67.     // 为图表添加手势识别监听器
  68.     this.model.setOnChartGestureListener(this.chartGestureListener);
  69.         // 获取图表描述部件,设置图表描述部件不可用,即图表不进行绘制描述部件
  70.     let description: Description | null = this.model.getDescription()
  71.     if (description) {
  72.       description.setEnabled(false);
  73.     }
  74.         // 获取图表图例部件,设置图表图例部件不可用
  75.     let l: Legend | null = this.model.getLegend();
  76.     if (l) {
  77.       l.setEnabled(false);
  78.     }
  79.     // 设置图表数据最大的绘制数,如果超过该数值,则不进行绘制图表的数值标签
  80.     this.model.setMaxVisibleValueCount(40);
  81.         // 是否绘制图表的背景色,绘制范围为图表柱状图的绘制范围,不包含轴线之外的部分
  82.     this.model.setDrawGridBackground(false);
  83.     // 设置图表的背景色,颜色的规格需要满足CanvasRenderingContext2D.fillstyle/strokestyle规格
  84.     this.model.setGridBackgroundColor('#500000ff')
  85.     // 设置不绘制柱状图的柱体阴影背景
  86.     this.model.setDrawBarShadow(false);
  87.         // 设置柱状图的数值在柱体上方
  88.     this.model.setDrawValueAboveBar(false);
  89.     // 设置柱状图的高亮范围是否为整个柱体,只在堆叠柱状图中有区别
  90.     this.model.setHighlightFullBarEnabled(false);
  91.         // 为左Y轴设置LimitLine,可设置限制线的宽度,线段样式,限制标签的位置,标签字体大小等
  92.     this.limitLine1 = new LimitLine(120, 'Upper Limit');
  93.     this.limitLine1.setLineWidth(4);
  94.     this.limitLine1.enableDashedLine(10, 10, 0);
  95.     this.limitLine1.setLabelPosition(LimitLabelPosition.RIGHT_TOP);
  96.     this.limitLine1.setTextSize(10);
  97.     this.limitLine2 = new LimitLine(50, 'Lower Limit');
  98.     this.limitLine2.setLineWidth(4);
  99.     this.limitLine2.enableDashedLine(10, 10, 0);
  100.     this.limitLine2.setLineColor(Color.Yellow);
  101.     this.limitLine2.setLabelPosition(LimitLabelPosition.RIGHT_BOTTOM);
  102.     this.limitLine2.setTextSize(10);
  103.     // 设置图表左Y轴信息
  104.     this.leftAxis = this.model.getAxisLeft();
  105.     if (this.leftAxis) {
  106.       this.leftAxis.setAxisMinimum(0); // this replaces setStartAtZero(true)
  107.       this.leftAxis.setDrawLimitLinesBehindData(false);
  108.       // 添加LimitLines
  109.       this.leftAxis.addLimitLine(this.limitLine1);
  110.       this.leftAxis.addLimitLine(this.limitLine2);
  111.     }
  112.         // 设置图表右Y轴信息
  113.     this.rightAxis = this.model.getAxisRight();
  114.     if (this.rightAxis) {
  115.       this.rightAxis.setEnabled(false);
  116.       this.rightAxis.setAxisMinimum(0);
  117.     }
  118.         // 设置X轴信息
  119.     this.xAxis = this.model.getXAxis();
  120.     if (this.xAxis) {
  121.       this.xAxis.setPosition(XAxisPosition.BOTTOM);
  122.     }
  123.         // 为图表设置markerView
  124.     this.normalMarker = new MarkerView();
  125.     this.model.setMarker(this.normalMarker);
  126.     // 也可设置定义图表MarkerView
  127.     this.stackMarker = new CustomMarkerView();
  128.         // 生成单一颜色数据
  129.     this.data = this.getNormalData();
  130.     // 将数据与图表配置类绑定
  131.     this.model.setData(this.data);
  132.     // 设置图表最大的X轴显示范围,如不设置,则默认显示全部数据
  133.     this.model.setVisibleXRangeMaximum(20);
  134.   }
  135.    private getNormalData(): BarData {
  136.     let values: JArrayList<BarEntry> = new JArrayList<BarEntry>();
  137.     values.add(new BarEntry(1, 73.3));
  138.     values.add(new BarEntry(2, 5.4));
  139.     let dataSet: BarDataSet = new BarDataSet(values, 'DataSet');
  140.     dataSet.setHighLightColor(Color.Black);
  141.     dataSet.setDrawIcons(false);
  142.     // 为柱体添加颜色信息
  143.     dataSet.setColorByColor(Color.Pink);
  144.     let dataSetList: JArrayList<IBarDataSet> = new JArrayList<IBarDataSet>();
  145.     dataSetList.add(dataSet);
  146.     let barData: BarData = new BarData(dataSetList);
  147.     //设置柱状图宽度
  148.     barData.setBarWidth(0.85);
  149.     //设置顶部圆角半径
  150.     barData.setTopRadius(5);
  151.     return barData;
  152.   }
  153.   private getGradientData(): BarData {
  154.     let values: JArrayList<BarEntry> = new JArrayList<BarEntry>();
  155.     values.add(new BarEntry(1, 32.9));
  156.     values.add(new BarEntry(2, 44.7));
  157.     let dataSet: BarDataSet = new BarDataSet(values, 'DataSet');
  158.     dataSet.setHighLightColor(Color.Black);
  159.     dataSet.setDrawIcons(false);
  160.     let startColor1: string = '#ffffbb33';
  161.     let startColor2: string = '#ff33b5e5';
  162.     let startColor3: string = '#ffffbb33';
  163.     let startColor4: string = '#ff99cc00';
  164.     let startColor5: string = '#ffff4444';
  165.     let endColor1: string = '#ff0099cc';
  166.     let endColor2: string = '#ffaa66cc';
  167.     let endColor3: string = '#ff669900';
  168.     let endColor4: string = '#ffcc0000';
  169.     let endColor5: string = '#ffff8800';
  170.     let gradientFills: JArrayList<Fill> = new JArrayList<Fill>();
  171.     gradientFills.add(new Fill(startColor1, endColor1));
  172.     gradientFills.add(new Fill(startColor2, endColor2));
  173.     gradientFills.add(new Fill(startColor3, endColor3));
  174.     gradientFills.add(new Fill(startColor4, endColor4));
  175.     gradientFills.add(new Fill(startColor5, endColor5));
  176.         // 为柱体添加渐变的颜色信息
  177.     dataSet.setFills(gradientFills);
  178.     let dataSetList: JArrayList<IBarDataSet> = new JArrayList<IBarDataSet>();
  179.     dataSetList.add(dataSet);
  180.     let barData: BarData = new BarData(dataSetList);
  181.     barData.setBarWidth(0.85);
  182.     return barData;
  183.   }
  184.   private getStackData(): BarData {
  185.     let values: JArrayList<BarEntry> = new JArrayList<BarEntry>();
  186.     values.add(new BarEntry(1, [38.0, 28.0, 39.8]));
  187.     values.add(new BarEntry(2, [18.2, 16.1, 16.1]));
  188.     let set1: BarDataSet | null = null;
  189.     set1 = new BarDataSet(values, "Statistics Vienna 2014");
  190.     set1.setDrawIcons(false);
  191.     // 为柱体添加指定分段的颜色信息
  192.     set1.setColorsByArr([Color.Red, Color.Green, Color.Pink]);
  193.     set1.setStackLabels(["Births", "Divorces", "Marriages"]);
  194.     let dataSets: JArrayList<IBarDataSet> = new JArrayList<IBarDataSet>();
  195.     dataSets.add(set1);
  196.     let data: BarData = new BarData(dataSets);
  197.     data.setValueTextColor(Color.White);
  198.     return data;
  199.   }
复制代码

  • 添加数据到自定义竖向柱状图表组件
  1. // 为组件设置配置构建类,如果需要在页面初始化就显示图表,则需要在aboutToAppear方法中完成图表数据构建
  2. // 如果在之后通过事件触发,
  3. // 可通过barData.notifyDataSetChanged();来触发数据更新,
  4. // 可通过this.model.notifyDataSetChanged();来触发坐标轴数据更新,
  5. // 可通过this.model.invalidate();来触发绘制更新。
  6. BarChart({ model: this.model })
  7.   .width('100%')
  8.   .height('30%')
复制代码
线形图


  • 线形图数据初始化:
  1. import {
  2.   JArrayList, // 工具类:数据集合
  3.   XAxis, // 图表X轴部件
  4.   XAxisPosition, // 图表X轴标签位置枚举类
  5.   YAxis, // 图表Y轴部件
  6.   Description, // 图表描述标签
  7.   Legend, // 图表Legend(图例)部件
  8.   OnChartValueSelectedListener, //
  9.   Highlight,// 图表高亮数据
  10.   EntryOhos,// 图表数据结构基础类
  11.   YAxisLabelPosition,// 图表Y轴标签位置枚举类
  12.   LineDataSet, //线形图数据集合
  13.   ILineDataSet, // 线形图数据集合的操作类
  14.   LineData, //线形图数据包
  15.   Mode, //线形图形状
  16.   LineChart, // 线形图图表类
  17.   LineChartModel,// 线形图配置构建类
  18.   LimitLine, // 图表LimitLine
  19.   LimitLabelPosition, // 图表的LimitLine标签位置枚举类
  20.   ChartColorStop, //颜色类
  21.   LegendForm, //图例形状
  22. } from '@ohos/mpchart';
  23. // 构造数据选择监听器
  24.   private valueSelectedListener: OnChartValueSelectedListener = {
  25.     onValueSelected: (e: EntryOhos, h: Highlight) => {
  26.     // ...todoSomething
  27.     },
  28.     onNothingSelected: () => {
  29.     // ...todoSomething
  30.     }
  31.   }
  32.   // 图表数据初始化
  33.   aboutToAppear() {
  34.           // Step1:必须:初始化图表配置构建类
  35.     this.model = new LineChartModel();
  36.     // Step2:配置图表指定样式,各部件间没有先后之分
  37.     // 为图表添加数据选择的监听器
  38.     this.model.setOnChartValueSelectedListener(this.valueSelectedListener);
  39.         // 获取图表描述部件,设置图表描述部件不可用,即图表不进行绘制描述部件
  40.     let description: Description | null = this.model.getDescription()
  41.     if (description) {
  42.       description.setEnabled(false);
  43.     }
  44.         // 获取图表图例部件,设置图表图例形状为线形
  45.     let legend: Legend | null = this.model.getLegend();
  46.     if (legend) {
  47.       legend.setEnabled(true);
  48.       // draw legend entries as lines
  49.       legend.setForm(LegendForm.LINE);
  50.     }
  51.     // 设置图表数据最大的绘制数,如果超过该数值,则不进行绘制图表的数值标签
  52.     this.model.setMaxVisibleValueCount(60);
  53.         // 为左Y轴设置LimitLine,可设置限制线的宽度,线段样式,限制标签的位置,标签字体大小等
  54.     this.limitLine1 = new LimitLine(120, 'Upper Limit');
  55.     this.limitLine1.setLineWidth(4);
  56.     //设置虚线样式
  57.     this.limitLine1.enableDashedLine(10, 10, 0);
  58.     //设置标签位置
  59.     this.limitLine1.setLabelPosition(LimitLabelPosition.RIGHT_TOP);
  60.     this.limitLine1.setTextSize(10);
  61.     this.limitLine2 = new LimitLine(50, 'Lower Limit');
  62.     this.limitLine2.setLineWidth(4);
  63.     this.limitLine2.enableDashedLine(10, 10, 0);
  64.     this.limitLine2.setLineColor(Color.Yellow);
  65.     this.limitLine2.setLabelPosition(LimitLabelPosition.RIGHT_BOTTOM);
  66.     this.limitLine2.setTextSize(10);
  67.     // 设置图表左Y轴信息
  68.     this.leftAxis = this.model.getAxisLeft();
  69.     if (this.leftAxis) {
  70.       //设置绘制标签个数
  71.       this.leftAxis.setLabelCount(8, false);
  72.       //设置标签位置
  73.       this.leftAxis.setPosition(YAxisLabelPosition.OUTSIDE_CHART)
  74.       //设置距离顶部距离
  75.       this.leftAxis.setSpaceTop(15);
  76.       //设置最大值
  77.       this.leftAxis.setAxisMinimum(0);
  78.       //设置最小值
  79.       this.leftAxis.setAxisMaximum(200);
  80.     }
  81.         // 设置图表右Y轴信息
  82.     this.rightAxis = this.model.getAxisRight();
  83.     if (this.rightAxis) {
  84.       this.rightAxis.setLabelCount(8, false);
  85.       this.rightAxis.setDrawGridLines(false);
  86.       this.rightAxis.setSpaceTop(15);
  87.       this.rightAxis.setAxisMinimum(0);
  88.       this.rightAxis.setAxisMaximum(200);
  89.       this.rightAxis.setEnabled(false);
  90.     }
  91.         // 设置X轴信息
  92.         this.xAxis = this.model.getXAxis();
  93.     if (this.xAxis) {
  94.       this.xAxis.setPosition(XAxisPosition.BOTTOM);
  95.       this.xAxis.setDrawGridLines(false);
  96.       this.xAxis.setGranularity(1);
  97.       this.xAxis.setLabelCount(7);
  98.     }
  99.         // 为图表设置markerView
  100.     this.normalMarker = new MarkerView();
  101.     this.model.setMarker(this.normalMarker);
  102.     // 也可设置定义图表MarkerView
  103.     this.stackMarker = new CustomMarkerView();
  104.     // 生成图表数据
  105.     let lineData: LineData = this.getLineData();
  106.     // 将数据与图表配置类绑定
  107.     this.model.setData(lineData);
  108.     // 设置图表最大的X轴显示范围,如不设置,则默认显示全部数据
  109.     this.model.setVisibleXRangeMa
复制代码
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

慢吞云雾缓吐愁

论坛元老
这个人很懒什么都没写!
快速回复 返回顶部 返回列表