2

I'm creating a POC for a Ganttproject for my company. I'm using the Resource Gantt from Anychart version 8.1.0 in an Angular 5 web project. And I've set up most of the chart finding different documentations and suggestions across the internet, but I got stuck on the click event now. Following the examples from Anychart i found following code to listen to an event on the chart:

chart.listen("rowClick", function(event) {
  var msg = event['item'].get('name');
  if (event['period']) msg += '\nPeriod: ' + event['period']['id'];
  console.log(msg);
});

If we go to the anychart playground we can change this code for the above mentioned snippet:

chart.listen('rowSelect', function(e) {
  e.item.remove();
});

So here we see the event getting the item and period properties. But when I do this in my POC project the item and period properties are missing:

This is the code snippet:

chart: anychart.charts.Gantt;

ngOnInit() {
  this.chart = anychart.ganttResource();
}

ngAfterViewInit() {
this.ganttSvc.getGanttData(this.tokenManager.getUserId()).subscribe((values: 
  GanttDataRow[]) => {
  // set data to grid
  const treedata = anychart.data.tree(values, 'as-table');
  this.chart.data(treedata);
  // add to container and draw
  this.chart.container(this.container.nativeElement).draw();
  // scale
  this.chart.zoomTo('day', 1, 'first-date');
  // eventlisteners
  this.chart.listen('rowSelect', function() {
    event.preventDefault();
    this.clickedDetail(event);
  });
});

clickedDetail(event) {
if (event['period']) {
  this.selectedGanttItem = event['period'];
  this.toggleSideBar();
}

}

Any ideas on what I'm doing wrong?

gogessj4
  • 89
  • 8

4 Answers4

1

Please, try the following code, this works with anychart@8.2.1-rc.0 perfectly:

chart: anychart.charts.Gantt = null;
  ngOnInit() {

    const rawData = [
      {
        'name': 'Activities',
        'actualStart': Date.UTC(2007, 0, 25),
        'actualEnd': Date.UTC(2007, 2, 14),
        'children': [
          {
            'name': 'Draft plan',
            'actualStart': Date.UTC(2007, 0, 25),
            'actualEnd': Date.UTC(2007, 1, 3)
          },
          {
            'name': 'Board meeting',
            'actualStart': Date.UTC(2007, 1, 4),
            'actualEnd': Date.UTC(2007, 1, 4)
          },
          {
            'name': 'Research option',
            'actualStart': Date.UTC(2007, 1, 4),
            'actualEnd': Date.UTC(2007, 1, 24)
          },
          {
            'name': 'Final plan',
            'actualStart': Date.UTC(2007, 1, 24),
            'actualEnd': Date.UTC(2007, 2, 14)
          }
        ]
      }];

    const treeData = anychart.data.tree(rawData, 'as-tree');    
    this.chart = anychart.ganttProject();
    this.chart.data(treeData);

    this.chart.listen('rowSelect', function (event) {
      console.log(event['item'].get('name'));
    });

  ngAfterViewInit() {
    this.chart.container(this.container.nativeElement);
    this.chart.draw();
  }
AnyChart Support
  • 3,770
  • 1
  • 10
  • 16
  • 1
    Is It also possible to do this with a marker? because I can see the item and period. And I can see the Item holding his children with periods and markers. But can I know which marker is clicked? – gogessj4 Apr 17 '18 at 10:40
  • Do you mean line/area/text markers on the timeline? – AnyChart Support Apr 18 '18 at 02:26
  • 1
    No the milestone markers as described here: https://docs.anychart.com/Gantt_Chart/Markers_(Multiple_Milestones) – gogessj4 Apr 19 '18 at 09:12
  • @gogessj4 Unfortunately, this event returns the full info about the whole clicked row without info about an actually clicked element. – AnyChart Support Apr 19 '18 at 09:56
  • 1
    Hmmm that's a pity... We need to trigger sidepanels and modal windows on top of the chart when a period or marker is clicked to show more detailed info. For the period this worked fine. Is there any possibility this will be included in the future? It seems to me that this can't be that much different then periods as both are children on the same row. Or am I mistaken on this? For now I think I can make a workAround for the markers by overriding the tooltip, but will look into that later. – gogessj4 Apr 19 '18 at 11:11
  • I will suggest this improvement to our dev team. So, now it can be implemented with a hard-code trick. I've prepared a sample for you here (hover markers and check the browser console) - https://playground.anychart.com/boQO40E2 – AnyChart Support Apr 20 '18 at 10:14
1

My problem was in this section:

this.chart.listen('rowSelect', function (event) {
      console.log(event['item'].get('name'));
    });

I had following code:

this.chart.listen('rowSelect', () => {
      this.clickedDetail(event);
    });

as soon as I changed it to following it worked:

this.chart.listen('rowSelect', (event) => {
      this.clickedDetail(event);
    });

Before I had hold of the MouseClickEvent itself and not the emitted event of the chart. Thanks for the example it helped a lot.

Just one small question though. I noticed you use rowSelect, and I was using rowClick before. Are they very different in behaviour?

gogessj4
  • 89
  • 8
  • Yes, they have some differences. rowSelect is dispatched only when you select any new row. If you click once a row you dispatch rowSelect event. Next click on the same row won't dispatch it, because this row is already selected. Click on any other row will dispatch it again. rowClick event is dispatched every time you click on any row despite of selection state. – AnyChart Support Apr 18 '18 at 02:20
0

Unfortunately, there's a bug in index.d.ts related to chart listeners. In the current version of index.d.ts file the callback function of a chart listener doesn't take as an argument event object. That's why the item and period properties are missing. This issue is already known and our dev team is preparing a fix for it which will be available with the release of 8.2.0 version. We will notify you via this thread when the fix becomes available.

AnyChart Support
  • 3,770
  • 1
  • 10
  • 16
0

Our team has prepared a dev-preview of 8.2.1 update and you can try it in your projects. This update includes many bug fixes of index.d.ts. To get this update replace in ‘package.json’ the current AnyChart dependence with the following "anychart": "8.2.1-rc.0" Or you can download it manually via NPM: $ npm install anychart@8.2.1-rc.0

In this update, you can get access to 'event' object in a listener.

this.chart.listen('rowSelect', function(event) {
    event.preventDefault();
});

Very soon our team will provide public release of 8.2.1 update.

AnyChart Support
  • 3,770
  • 1
  • 10
  • 16
  • I've update to version 8.2.1. But regarding the data this hasn't changed anything it seems I'm unable to get to the item or period properties.... – gogessj4 Apr 16 '18 at 15:19
  • I've just tested the anychart@8.2.1-rc.0 and it works ok with AnyGantt. Please, check the altered answer. – AnyChart Support Apr 17 '18 at 03:19
  • Our team has released a public 8.2.1 update and you can try it in your projects. This update includes many bug fixes of index.d.ts. To get this update execute 'npm update' or replace in 'package.json' the current AnyChart dependence with the following "anychart": "8.2.1" Or you can download it manually via NPM: $ npm install anychart@8.2.1 – AnyChart Support Apr 23 '18 at 05:45