Skip to content

Commit 1d17303

Browse files
savvinsergeyignatvilesov
authored andcommitted
Add ability automatically scroll chart to today date (#42)
1 parent 38454da commit 1d17303

File tree

9 files changed

+68
-6
lines changed

9 files changed

+68
-6
lines changed

CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
## 1.6.0
2+
* Added ability automatically scroll chart to today date
13
## 1.5.1
24
* Fixed tooltip label and date format of milestone line
35
## 1.5.0

capabilities.json

+7
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,13 @@
210210
"bool": true
211211
}
212212
},
213+
"scrollToCurrentTime": {
214+
"displayName": "Scroll to current time",
215+
"displayNameKey": "Visual_ScrollToCurrentTime",
216+
"type": {
217+
"bool": true
218+
}
219+
},
213220
"durationUnit": {
214221
"displayName": "Duration unit",
215222
"displayNameKey": "Visual_DurationUnit",

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "powerbi-visuals-gantt",
3-
"version": "1.5.1",
3+
"version": "1.6.0",
44
"description": "A Gantt chart is a type of bar chart which illustrates a project timeline or schedule. The Gantt Chart visual shows the Tasks, Start Dates, Durations, % Complete, and Resources for a project. The Gantt Chart visual can be used to show current schedule status using percent-complete shadings and a vertical \"TODAY\" line. The Legend may be used to group or filter tasks based upon data values.",
55
"repository": {
66
"type": "git",

pbiviz.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"displayName": "Gantt",
55
"guid": "Gantt1448688115699",
66
"visualClassName": "Gantt",
7-
"version": "1.5.1",
7+
"version": "1.6.0",
88
"description": "A Gantt chart is a type of bar chart which illustrates a project timeline or schedule. The Gantt Chart visual shows the Tasks, Start Dates, Durations, % Complete, and Resources for a project. The Gantt Chart visual can be used to show current schedule status using percent-complete shadings and a vertical \"TODAY\" line. The Legend may be used to group or filter tasks based upon data values.",
99
"supportUrl": "http://community.powerbi.com",
1010
"gitHubUrl": "https://github.com/Microsoft/powerbi-visuals-gantt"

src/gantt.ts

+22-2
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ module powerbi.extensibility.visual {
100100
import timeScale = d3.time.Scale;
101101

102102
const PercentFormat: string = "0.00 %;-0.00 %;0.00 %";
103+
const ScrollMargin: number = 100;
103104
const MillisecondsInASecond: number = 1000;
104105
const MillisecondsInAMinute: number = 60 * MillisecondsInASecond;
105106
const MillisecondsInAHour: number = 60 * MillisecondsInAMinute;
@@ -411,7 +412,9 @@ module powerbi.extensibility.visual {
411412
LegendPosition.Top);
412413

413414
this.ganttDiv.on("scroll", function (evt) {
414-
const taskLabelsWidth: number = self.viewModel.settings.taskLabels.show ? self.viewModel.settings.taskLabels.width : 0;
415+
const taskLabelsWidth: number = self.viewModel.settings.taskLabels.show
416+
? self.viewModel.settings.taskLabels.width
417+
: 0;
415418
self.axisGroup
416419
.attr("transform", SVGUtil.translate(taskLabelsWidth + self.margin.left, Gantt.TaskLabelsMarginTop + this.scrollTop));
417420
self.lineGroup
@@ -949,9 +952,12 @@ module powerbi.extensibility.visual {
949952
this.renderAxis(xAxisProperties);
950953
this.renderTasks(groupedTasks);
951954

952-
this.createMilestoneLine(groupedTasks);
953955
this.updateTaskLabels(groupedTasks, settings.taskLabels.width);
954956
this.updateElementsPositions(this.margin);
957+
this.createMilestoneLine(groupedTasks);
958+
if (settings.general.scrollToCurrentTime) {
959+
this.scrollToMilestoneLine(axisLength);
960+
}
955961

956962
if (this.interactivityService) {
957963
let behaviorOptions: GanttBehaviorOptions = {
@@ -1492,6 +1498,20 @@ module powerbi.extensibility.visual {
14921498
.remove();
14931499
}
14941500

1501+
private scrollToMilestoneLine(axisLength: number,
1502+
timestamp: number = Date.now()): void {
1503+
1504+
let scrollValue = this.timeScale(new Date(timestamp));
1505+
scrollValue -= scrollValue > ScrollMargin
1506+
? ScrollMargin
1507+
: 0;
1508+
1509+
if (axisLength > scrollValue) {
1510+
(this.body.node() as SVGSVGElement)
1511+
.querySelector(Selectors.Body.selector).scrollLeft = scrollValue;
1512+
}
1513+
}
1514+
14951515
private renderTooltip(selection: Selection<Line | Task>): void {
14961516
this.tooltipServiceWrapper.addTooltip(
14971517
selection,

src/settings.ts

+1
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ module powerbi.extensibility.visual {
4141

4242
export class GeneralSettings {
4343
groupTasks: boolean = false;
44+
scrollToCurrentTime: boolean = false;
4445
durationUnit: string = "day";
4546
durationMin: number = 0;
4647
}

stringResources/en-US/resources.resjson

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
"Role_ExtraInformation": "Extra information",
99
"Visual_General": "General",
1010
"Visual_GroupTasks": "Group Tasks",
11+
"Visual_ScrollToCurrentTime": "Scroll to current time",
1112
"Visual_Legend": "Legend",
1213
"Visual_LegendName": "Legend Name",
1314
"Visual_LegendColor": "Legend color",

test/visualBuilder.ts

+6-2
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,13 @@ module powerbi.extensibility.visual.test {
4747
return this.visual;
4848
}
4949

50-
public get mainElement() {
50+
public get body() {
5151
return this.element
52-
.children("div.gantt-body")
52+
.children("div.gantt-body");
53+
}
54+
55+
public get mainElement() {
56+
return this.body
5357
.children("svg.gantt");
5458
}
5559

test/visualTest.ts

+27
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ module powerbi.extensibility.visual.test {
6060
}
6161

6262
const defaultTaskDuration: number = 1;
63+
const datesAmountForScroll: number = 90;
6364

6465
describe("Gantt", () => {
6566
let visualBuilder: GanttBuilder,
@@ -388,6 +389,32 @@ module powerbi.extensibility.visual.test {
388389

389390
describe("Format settings test", () => {
390391
describe("General", () => {
392+
it("Scroll to current time", (done) => {
393+
let todayDate = new Date();
394+
let startDate = new Date();
395+
let endDate = new Date();
396+
397+
startDate.setDate(todayDate.getDate() - datesAmountForScroll);
398+
endDate.setDate(todayDate.getDate() + datesAmountForScroll);
399+
400+
defaultDataViewBuilder.valuesStartDate = GanttData.getRandomUniqueDates(
401+
defaultDataViewBuilder.valuesTaskTypeResource.length,
402+
startDate,
403+
endDate
404+
);
405+
dataView = defaultDataViewBuilder.getDataView();
406+
dataView.metadata.objects = {
407+
general: {
408+
scrollToCurrentTime: true
409+
}
410+
};
411+
412+
visualBuilder.updateRenderTimeout(dataView, () => {
413+
expect(visualBuilder.body.scrollLeft()).not.toEqual(0);
414+
done();
415+
});
416+
});
417+
391418
describe("Duration units", () => {
392419

393420
function checkDurationUnit(durationUnit: string) {

0 commit comments

Comments
 (0)