Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(resize): Intent to ship resize.auto='viewBox' #3894

Merged
merged 1 commit into from
Oct 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions demo/chart.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,10 +116,12 @@ var billboardDemo = {
Object.keys(demos).forEach(function(key) {
html.push("<li><h4>" + key + "</h4>");

Object.keys(demos[key]).sort().forEach(function (v, i) {
i === 0 && html.push("<ul>");
html.push("<li><a href='#"+ [key, v].join(".") + "'>" + v + "</a></li>");
});
Object.keys(demos[key])
.sort(Intl.Collator().compare)
.forEach(function (v, i) {
i === 0 && html.push("<ul>");
html.push("<li><a href='#"+ [key, v].join(".") + "'>" + v + "</a></li>");
});

html.push("</ul></li>");
});
Expand Down
36 changes: 35 additions & 1 deletion demo/demo.js
Original file line number Diff line number Diff line change
Expand Up @@ -5691,7 +5691,41 @@ d3.select(".chart_area")
},
clipPath: false
}
}
},
resizeViewBox: [
{
options: {
data: {
columns: [
["sample", 70, 200, 120, 400, 300, 250]
],
type: "bar"
},
resize: {
auto: "viewBox"
}
}
},
{
options: {
size: {
width: 480,
height: 240
},
data: {
columns: [
["data1", 70],
["data2", 170],
["data3", 120]
],
type: "pie"
},
resize: {
auto: "viewBox"
}
}
}
]
},
DonutChartOptions: {
DonutCornerRadius: {
Expand Down
8 changes: 5 additions & 3 deletions src/ChartInternal/ChartInternal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
utcFormat as d3UtcFormat,
utcParse as d3UtcParse
} from "d3-time-format";
import type {d3Selection} from "../../types/types";
import type {d3Selection, d3Transition} from "../../types/types";
import {$CIRCLE, $COMMON, $TEXT} from "../config/classes";
import Options from "../config/Options/Options";
import Store from "../config/Store/Store";
Expand Down Expand Up @@ -139,7 +139,8 @@ export default class ChartInternal {
* @returns {d3Selection}
* @private
*/
$T(selection: SVGElement | d3Selection, force?: boolean, name?: string): d3Selection {
$T(selection: SVGElement | d3Selection | d3Transition, force?: boolean,
name?: string): d3Selection {
const {config, state} = this;
const duration = config.transition_duration;
const subchart = config.subchart_show;
Expand All @@ -162,6 +163,7 @@ export default class ChartInternal {
state.rendered &&
!subchart;

// @ts-ignore
t = (transit ? t.transition(name).duration(duration) : t) as d3Selection;
}

Expand Down Expand Up @@ -791,7 +793,7 @@ export default class ChartInternal {

list.push(() => callFn(config.onresize, $$.api));

if (config.resize_auto) {
if (config.resize_auto === true) {
list.push(() => {
state.resizing = true;

Expand Down
13 changes: 9 additions & 4 deletions src/ChartInternal/internals/size.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,11 +132,16 @@ export default {

updateSvgSize(): void {
const $$ = this;
const {state: {clip, current, hasAxis, width, height}, $el: {svg}} = $$;
const {config, state: {clip, current, hasAxis, width, height}, $el: {svg}} = $$;

svg
.attr("width", current.width)
.attr("height", current.height);
if (config.resize_auto === "viewBox") {
svg
.attr("viewBox", `0 0 ${current.width} ${current.height}`);
} else {
svg
.attr("width", current.width)
.attr("height", current.height);
}

if (hasAxis) {
const brush = svg.select(`.${$SUBCHART.brush} .overlay`);
Expand Down
20 changes: 14 additions & 6 deletions src/config/Options/common/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -162,28 +162,36 @@ export default {
* @memberof Options
* @type {object}
* @property {object} [resize] resize object
* @property {boolean} [resize.auto=true] Set chart resize automatically on viewport changes.
* @property {boolean|string} [resize.auto=true] Set chart resize automatically on viewport changes.
* - **NOTE:** Available options
* - true: Enables automatic resize.
* - false: Disables automatic resize.
* - "viewBox": Enables automatic resize, and size will be fixed based on the viewbox.
* @property {boolean|number} [resize.timer=true] Set resize timer option.
* - **NOTE:**
* - **NOTE:** Available options
* - The resize function will be called using:
* - true: `setTimeout()`
* - false: `requestIdleCallback()`
* - Given number(delay in ms) value, resize function will be triggered using `setTimer()` with given delay.
* - Given number(delay in ms) value, resize function will be triggered using `setTimeout()` with given delay.
* @see [Demo](https://naver.github.io/billboard.js/demo/#ChartOptions.resizeViewBox)
* @example
* resize: {
* auto: false,
*
* // set resize function will be triggered using `setTimer()`
* // set resize based on viewBox value
* auto: "viewBox",
*
* // set resize function will be triggered using `setTimeout()`
* timer: true,
*
* // set resize function will be triggered using `requestIdleCallback()`
* timer: false,
*
* // set resize function will be triggered using `setTimer()` with a delay of `100ms`.
* // set resize function will be triggered using `setTimeout()` with a delay of `100ms`.
* timer: 100
* }
*/
resize_auto: true,
resize_auto: <boolean | "viewBox">true,
resize_timer: true,

/**
Expand Down
18 changes: 18 additions & 0 deletions test/internals/bb-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,24 @@ describe("Interface & initialization", () => {

expect(chart.$.chart.node().getBoundingClientRect().height).to.be.equal(height);
});

it("check if viewBox attribute set", () => {
chart = util.generate({
resize: {
auto: "viewBox"
},
data: {
columns: [
["data1", 300, 350, 300, 120, 100, 200],
],
type: "bar"
}
});

const {svg} = chart.$;

expect(svg.attr("viewBox")).to.be.equal("0 0 640 480");
});
});

describe("set defaults options", () => {
Expand Down
6 changes: 5 additions & 1 deletion types/options.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -160,8 +160,12 @@ export interface ChartOptions {
resize?: {
/**
* Indicate if the chart should automatically get resized when the window gets resized.
* - **NOTE:** Available options
* - true: Enables automatic resize.
* - false: Disables automatic resize.
* - "viewBox": Enables automatic resize, and size will be fixed based on the viewbox.
*/
auto?: boolean;
auto?: boolean | "viewBox";

/**
* Set resize timer option.
Expand Down
Loading