From 1209138dec7b82a8e764c5f5dac2afe3713f347a Mon Sep 17 00:00:00 2001 From: Jae Sung Park Date: Thu, 24 Aug 2023 16:37:56 +0900 Subject: [PATCH] fix(api): Fix to return indexed categories Fix to return indexed values when .categories() is called for indexed x axis type Fix #3365 --- src/Chart/api/category.ts | 12 ++++++++---- test/api/category-spec.ts | 19 +++++++++++++++++++ 2 files changed, 27 insertions(+), 4 deletions(-) diff --git a/src/Chart/api/category.ts b/src/Chart/api/category.ts index bc55ea64f..9482b768b 100644 --- a/src/Chart/api/category.ts +++ b/src/Chart/api/category.ts @@ -2,6 +2,8 @@ * Copyright (c) 2017 ~ present NAVER Corp. * billboard.js project is licensed under the MIT license */ +import {isEmpty} from "../../module/util"; + export default { /** * Set specified category name on category axis. @@ -27,7 +29,7 @@ export default { }, /** - * Set category names on category axis. + * Set or get category names on category axis. * @function categories * @instance * @memberof Chart @@ -38,12 +40,14 @@ export default { * "Category 1", "Category 2", ... * ]); */ - categories(categories: string[]): string[] { + categories(categories?: string[]): string[] { const $$ = this.internal; const {config} = $$; - if (!arguments.length) { - return config.axis_x_categories; + if (!categories || !Array.isArray(categories)) { + const cat = config.axis_x_categories; + + return isEmpty(cat) ? Object.values($$.data.xs)[0] : cat; } config.axis_x_categories = categories; diff --git a/test/api/category-spec.ts b/test/api/category-spec.ts index c21b446db..f61b9f0fc 100644 --- a/test/api/category-spec.ts +++ b/test/api/category-spec.ts @@ -120,4 +120,23 @@ describe("API category", () => { expect(chart.$.tooltip.html()).to.be.empty; }); + + it("set options", () => { + args = { + data: { + columns: [ + ["data1", 100, 99, 98] + ], + }, + axis: { + x: { + type: "category" + } + } + }; + }); + + it("check for indexed categories", () => { + expect(chart.categories()).to.deep.equal([0,1,2]); + }); });