Skip to content

Commit 5f37e31

Browse files
282764407@qq.com282764407@qq.com
authored andcommitted
支持热力图分页范围
1 parent 1d1c83b commit 5f37e31

5 files changed

Lines changed: 754 additions & 104 deletions

File tree

adapters/platform/harmony/library/src/main/ets/_core_link/core/chart/heatmap.ts

Lines changed: 32 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,8 @@ export class HeatmapChartRenderer extends BaseRenderer {
117117
},
118118
pagination: {
119119
showNavigation: true,
120-
navigationAction: null
120+
navigationAction: null,
121+
monthsPerPage: 6
121122
},
122123
monthLabels: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'],
123124
dayLabels: ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat']
@@ -138,27 +139,31 @@ export class HeatmapChartRenderer extends BaseRenderer {
138139
* 处理分页导航动作
139140
*/
140141
private handleNavigationAction(): void {
141-
const action = this.opts.extra.heatmap!.pagination!.navigationAction;
142+
let action = this.opts.extra.heatmap!.pagination!.navigationAction;
142143
if (!action) return;
143144

145+
let page = this.pagination.currentPage;
146+
144147
// 执行相应的导航动作
145148
switch (action) {
146149
case 'prev':
147-
this.prevPage();
150+
page = page - 1;
148151
break;
149152
case 'next':
150-
this.nextPage();
153+
page = page + 1;
151154
break;
152155
}
153156

154157
// 执行动作后重置为null,避免重复执行
155158
this.opts.extra.heatmap!.pagination!.navigationAction = null;
159+
160+
if (page < 0 || page >= this.pagination.totalPages) return;
161+
this.pagination.currentPage = page;
156162
}
157163

158164
protected render(): void {
159165
// 处理分页导航动作
160166
this.handleNavigationAction();
161-
162167
this.fillSeriesData();
163168
this.checkPaginationNeed();
164169
this.calculateLayoutMetrics();
@@ -207,7 +212,11 @@ export class HeatmapChartRenderer extends BaseRenderer {
207212
// 如果容器宽度小于所需宽度,启用分页
208213
if (this.opts.width < requiredWidth) {
209214
this.pagination.enabled = true;
210-
this.pagination.totalPages = 2; // 按6个月分页,一年分2页
215+
// 根据monthsPerPage动态计算总页数
216+
const monthsPerPage = this.opts.extra.heatmap!.pagination!.monthsPerPage!;
217+
const validMonthsPerPage = Math.max(1, Math.min(12, monthsPerPage));
218+
this.pagination.monthsPerPage = validMonthsPerPage;
219+
this.pagination.totalPages = Math.ceil(12 / validMonthsPerPage);
211220
this.pagination.currentPage = Math.max(0, Math.min(this.pagination.currentPage, this.pagination.totalPages - 1));
212221
this.preparePageData();
213222
} else {
@@ -286,15 +295,28 @@ export class HeatmapChartRenderer extends BaseRenderer {
286295
const navY = this.opts.height - this.layout.navigationHeight - 5;
287296
const centerX = this.opts.width / 2;
288297

289-
// 设置文本样式
298+
// 设置文字样式
290299
this.setFontSize(12);
291300
this.setFillStyle(this.opts.fontColor!);
292301
this.setTextAlign('center');
293302
this.setTextBaseline('middle');
294303

295304
// 绘制页面信息
296305
const pageInfo = `${this.pagination.currentPage + 1} / ${this.pagination.totalPages}`;
297-
const timeRange = this.pagination.currentPage === 0 ? 'Jan - Jun' : 'Jul - Dec';
306+
307+
// 根据monthsPerPage动态生成时间范围显示
308+
const monthsPerPage = this.pagination.monthsPerPage;
309+
const startMonth = this.pagination.currentPage * monthsPerPage;
310+
const endMonth = Math.min(startMonth + monthsPerPage - 1, 11);
311+
312+
const monthNames = this.opts.extra.heatmap!.monthLabels!;
313+
let timeRange: string;
314+
if (startMonth === endMonth) {
315+
timeRange = monthNames[startMonth];
316+
} else {
317+
timeRange = `${monthNames[startMonth]} - ${monthNames[endMonth]}`;
318+
}
319+
298320
const displayText = `${timeRange} (${pageInfo})`;
299321

300322
ctx.fillText(displayText, centerX, navY);
@@ -595,7 +617,7 @@ export class HeatmapChartRenderer extends BaseRenderer {
595617

596618
const spacing: Record<string, number> = this.drawData["spacing"];
597619
const drawnMonths = new Set();
598-
const labelY = spacing["outer"] + (this.opts.fontSize!) / 2 + spacing["textVertical"];
620+
const labelY = spacing["outer"] + this.opts.fontSize! / 2 + spacing["textVertical"];
599621

600622
// 确定要绘制的月份范围
601623
let startMonth = 0;
@@ -712,7 +734,7 @@ export class HeatmapChartRenderer extends BaseRenderer {
712734

713735
this.drawRoundedRect(
714736
currentX,
715-
legendY - (this.opts.extra.heatmap!.legend!.cellSize!) / 2,
737+
legendY - this.opts.extra.heatmap!.legend!.cellSize! / 2,
716738
this.opts.extra.heatmap!.legend!.cellSize!,
717739
this.opts.extra.heatmap!.legend!.cellSize!,
718740
this.opts.extra.heatmap!.legend!.cellRadius!

adapters/platform/harmony/library/src/main/ets/_core_link/core/types/extra.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -427,4 +427,5 @@ export interface HeatmapLegendOptions {
427427
export interface HeatmapPaginationOptions {
428428
showNavigation: boolean //是否显示分页导航控件,默认true
429429
navigationAction: 'prev' | 'next' | null //分页切换动作,'prev'上一页,'next'下一页,null表示无动作,默认null
430+
monthsPerPage: number //每页显示的月份数量,默认6,有效范围值[1,12]
430431
}

0 commit comments

Comments
 (0)