Skip to content

Commit ee0384e

Browse files
Merge pull request #55 from mlakatkou/GS-3270
[update] zooming article
2 parents a097b90 + 58fb7f1 commit ee0384e

1 file changed

Lines changed: 90 additions & 92 deletions

File tree

docs/guides/zooming.md

Lines changed: 90 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -5,70 +5,76 @@ sidebar_label: "Zooming"
55

66
# Zooming
77

8-
98
dhtmlxGantt provides a built-in module for handy managing of time scale zooming. In case you want to customize the default zooming behaviour, there is a [flexible API](guides/zoom.md) that allows you to implement the ability to change the settings of time scale dynamically.
109

1110
## Built-in zooming module
1211

13-
14-
The embedded [zooming module](guides/zoom.md) is declared in the **gantt.ext.zoom** extension. To enable the module, you need to call **gantt.ext.zoom.init(zoomConfig)** and pass
15-
a **zoomConfig** object with configuration settings that contains an array of zooming levels. For example:
12+
The embedded [zooming module](guides/zoom.md) is declared in the `gantt.ext.zoom` extension. To enable the module, you need to call `gantt.ext.zoom.init(zoomConfig)` and pass
13+
a `zoomConfig` object with configuration settings that contains an array of zooming levels. For example:
1614

1715
~~~js
18-
var zoomConfig = {
16+
const zoomConfig = {
1917
levels: [
20-
{
21-
name:"day",
22-
scale_height: 27,
23-
min_column_width:80,
24-
scales:[
25-
{unit: "day", step: 1, format: "%d %M"}
26-
]
27-
},
28-
{
29-
name:"week",
30-
scale_height: 50,
31-
min_column_width:50,
32-
scales:[
33-
{unit: "week", step: 1, format: function (date) {
34-
var dateToStr = gantt.date.date_to_str("%d %M");
35-
var endDate = gantt.date.add(date, 6, "day");
36-
var weekNum = gantt.date.date_to_str("%W")(date);
37-
return "#" + weekNum + ", " + dateToStr(date) + " - " + dateToStr(endDate);
38-
}},
39-
{unit: "day", step: 1, format: "%j %D"}
40-
]
41-
},
42-
{
43-
name:"month",
44-
scale_height: 50,
45-
min_column_width:120,
46-
scales:[
47-
{unit: "month", format: "%F, %Y"},
48-
{unit: "week", format: "Week #%W"}
49-
]
18+
{
19+
name: "day",
20+
scale_height: 27,
21+
min_column_width: 80,
22+
scales: [
23+
{ unit: "day", step: 1, format: "%d %M" }
24+
]
25+
},
26+
{
27+
name: "week",
28+
scale_height: 50,
29+
min_column_width: 50,
30+
scales: [
31+
{
32+
unit: "week",
33+
step: 1,
34+
format: (date) => {
35+
const formatDate = gantt.date.date_to_str("%d %M");
36+
const endDate = gantt.date.add(date, 6, "day");
37+
const weekNumber = gantt.date.date_to_str("%W")(date);
38+
return `#${weekNumber}, ${formatDate(date)} - ${formatDate(endDate)}`;
39+
}
40+
},
41+
{ unit: "day", step: 1, format: "%j %D" }
42+
]
43+
},
44+
{
45+
name: "month",
46+
scale_height: 50,
47+
min_column_width: 120,
48+
scales: [
49+
{ unit: "month", format: "%F, %Y" },
50+
{ unit: "week", format: "Week #%W" }
51+
]
5052
},
5153
{
52-
name:"quarter",
53-
height: 50,
54-
min_column_width:90,
55-
scales:[
56-
{unit: "month", step: 1, format: "%M"},
57-
{
58-
unit: "quarter", step: 1, format: function (date) {
59-
var dateToStr = gantt.date.date_to_str("%M");
60-
var endDate = gantt.date.add(gantt.date.add(date, 3, "month"), -1, "day");
61-
return dateToStr(date) + " - " + dateToStr(endDate);
62-
}
63-
}
64-
]},
54+
name: "quarter",
55+
height: 50,
56+
min_column_width: 90,
57+
scales: [
58+
{ unit: "month", step: 1, format: "%M" },
59+
{
60+
unit: "quarter",
61+
step: 1,
62+
format: (date) => {
63+
const formatDate = gantt.date.date_to_str("%M");
64+
const endDate = gantt.date.add(gantt.date.add(date, 3, "month"), -1, "day");
65+
return `${formatDate(date)} - ${formatDate(endDate)}`;
66+
}
67+
}
68+
]
69+
},
6570
{
66-
name:"year",
67-
scale_height: 50,
68-
min_column_width: 30,
69-
scales:[
70-
{unit: "year", step: 1, format: "%Y"}
71-
]}
71+
name: "year",
72+
scale_height: 50,
73+
min_column_width: 30,
74+
scales: [
75+
{ unit: "year", step: 1, format: "%Y" }
76+
]
77+
}
7278
]
7379
};
7480

@@ -79,66 +85,63 @@ gantt.ext.zoom.init(zoomConfig);
7985
The detailed information about the zooming module and its API is given in the article [Zoom Extension](guides/zoom.md).
8086
:::
8187

82-
83-
[Mouse wheel zoom](https://docs.dhtmlx.com/gantt/samples/03_scales/14_scale_zoom_by_wheelmouse.html)
84-
88+
**Related sample**: [Mouse wheel zoom](https://docs.dhtmlx.com/gantt/samples/03_scales/14_scale_zoom_by_wheelmouse.html)
8589

8690
## Custom zooming settings
8791

88-
8992
In case you don't want to use the zooming module and prefer controlling scale settings manually, you can do so via corresponding configuration options.
9093

9194
In fact, implementing a zooming feature means defining several presets of the time scale configuration (zoom levels) and providing the user with the ability to switch between them.
9295

9396
You'll need the following settings to configure the time scale:
9497

95-
- [gantt.config.scales](api/config/scales.md) - allows setting any number of time scale rows.
96-
97-
- [gantt.config.min_column_width](api/config/min_column_width.md), [gantt.config.scale_height](api/config/scale_height.md) - the scale column width and the overall height of the time scale.
98+
- [`gantt.config.scales`](api/config/scales.md) - allows setting any number of time scale rows
99+
- [`gantt.config.min_column_width`](api/config/min_column_width.md), [`gantt.config.scale_height`](api/config/scale_height.md) - the scale column width and the overall height of the time scale
98100

99101
Let's consider the following presets:
100102

101103
~~~js
102104
/* global gantt */
103-
function setScaleConfig(level) {
105+
const setScaleConfig = (level) => {
104106
switch (level) {
105107
case "day":
106108
gantt.config.scales = [
107-
{unit: "day", step: 1, format: "%d %M"}
109+
{ unit: "day", step: 1, format: "%d %M" }
108110
];
109111
gantt.config.scale_height = 27;
110112
break;
111-
case "week":
112-
var weekScaleTemplate = function (date) {
113-
var dateToStr = gantt.date.date_to_str("%d %M");
114-
var endDate = gantt.date.add(gantt.date.add(date, 1, "week"), -1, "day");
115-
return dateToStr(date) + " - " + dateToStr(endDate);
113+
case "week": {
114+
const formatWeekScale = (date) => {
115+
const formatDate = gantt.date.date_to_str("%d %M");
116+
const endDate = gantt.date.add(gantt.date.add(date, 1, "week"), -1, "day");
117+
return `${formatDate(date)} - ${formatDate(endDate)}`;
116118
};
117-
gantt.config.scales = [
118-
{unit: "week", step: 1, format: weekScaleTemplate},
119-
{unit: "day", step: 1, format: "%D"}
119+
120+
gantt.config.scales = [
121+
{ unit: "week", step: 1, format: formatWeekScale },
122+
{ unit: "day", step: 1, format: "%D" }
120123
];
121124
gantt.config.scale_height = 50;
122125
break;
126+
}
123127
case "month":
124-
gantt.config.scales = [
125-
{unit: "month", step: 1, format: "%F, %Y"},
126-
{unit: "day", step: 1, format: "%j, %D"}
128+
gantt.config.scales = [
129+
{ unit: "month", step: 1, format: "%F, %Y" },
130+
{ unit: "day", step: 1, format: "%j, %D" }
127131
];
128132
gantt.config.scale_height = 50;
129133
break;
130134
case "year":
131135
gantt.config.scales = [
132-
{unit: "year", step: 1, format: "%Y"},
133-
{unit: "month", step: 1, format: "%M"}
136+
{ unit: "year", step: 1, format: "%Y" },
137+
{ unit: "month", step: 1, format: "%M" }
134138
];
135139
gantt.config.scale_height = 90;
136140
break;
137141
}
138-
}
142+
};
139143
~~~
140144

141-
142145
The described function can configure the gantt object by one of the four predefined configs, from the "day" to "year" time scale.
143146
Gantt will require a complete repaint in order to display the change of configuration:
144147

@@ -147,30 +150,25 @@ setScaleConfig("year");
147150
gantt.init("gantt_here");
148151
~~~
149152

150-
151153
Then you can implement a UI for the user to switch the zoom level:
152154

153-
154155
~~~html
155156
<label><input type="radio" name="scale" value="day" checked/>Day scale</label>
156157
<label><input type="radio" name="scale" value="week"/>Week scale</label>
157158
<label><input type="radio" name="scale" value="month"/>Month scale</label>
158-
<label><input type="radio" name="scale" value="year"/>Year scale</label>
159+
<label><input type="radio" name="scale" value="year"/>Year scale</label>
159160
~~~
160161

161-
162162
~~~js
163-
var els = document.querySelectorAll("input[name='scale']");
164-
for (var i = 0; i < els.length; i++) {
165-
els[i].onclick = function(e){
166-
var el = e.target;
167-
var value = el.value;
168-
setScaleConfig(value);
163+
const scaleInputs = document.querySelectorAll("input[name='scale']");
164+
165+
scaleInputs.forEach((input) => {
166+
input.onclick = (event) => {
167+
const selectedScale = event.target.value;
168+
setScaleConfig(selectedScale);
169169
gantt.render();
170170
};
171-
}
171+
});
172172
~~~
173173

174-
175-
[Dynamic scales](https://docs.dhtmlx.com/gantt/samples/03_scales/05_dynamic_scales.html)
176-
174+
**Related sample**: [Dynamic scales](https://docs.dhtmlx.com/gantt/samples/03_scales/05_dynamic_scales.html)

0 commit comments

Comments
 (0)