Skip to content

Commit

Permalink
fix(api): Fix to return indexed categories
Browse files Browse the repository at this point in the history
Fix to return indexed values when .categories() is called
for indexed x axis type

Fix #3365
  • Loading branch information
netil authored and netil committed Sep 5, 2023
1 parent d00052c commit 1209138
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 4 deletions.
12 changes: 8 additions & 4 deletions src/Chart/api/category.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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
Expand All @@ -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;
Expand Down
19 changes: 19 additions & 0 deletions test/api/category-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
});
});

0 comments on commit 1209138

Please sign in to comment.