TypeError: Невозможно прочитать свойство 'bar' из неопределенного - PullRequest
0 голосов
/ 30 января 2019

Невозможно отобразить диаграмму.

У меня есть таблица, которая содержит данные, которые можно перетаскивать.Рядом с таблицей у меня есть два дропбокса: перетаскиваемые данные из таблицы можно перетаскивать в ящик. При отбрасывании данных в ящик гистограмма должна отображаться.

Это мой дом.component.html

<div class="placeholders">
    <!--Drag Function-->
    <div class="box1"
    cdkDropList
    #productList="cdkDropList"
    [cdkDropListConnectedTo]="[dropList1,dropList2]"
    [cdkDropListData]="products" 
    (cdkDropListDropped)="drop($event)"
    >
    <div class = "table"> 
    <table class="table table-hover">
        <thead>
            <tr>
                <th>ProductName</th>
                <th>Sales</th>
            </tr>
        </thead>
        <tbody>
            <tr *ngFor = 'let product of products' cdkDrag>
                <td>{{product.productName}}</td>
                <td>{{product.sales}}</td>
            </tr>   
        </tbody>
    </table>
    </div> 
</div>
<!--Dropbox-->
<div class="box2" 
    cdkDropList
    #dropList1="cdkDropList"
    [cdkDropListData]="items" 
    (cdkDropListDropped)="drop($event)"
    [cdkDropListConnectedTo]="[productList]"
    >
    <div *ngFor="let item of items">   
        {{item.productName}} {{item.sales}} 
    </div>  
</div>

<div class="box3"
    cdkDropList
    #dropList2="cdkDropList"
    [cdkDropListData]="values"
    (cdkDropListDropped)="drop($event)"
    [cdkDropListConnectedTo]="[productList]"
    >
    <div *ngFor="let item of values">    
        {{item.productName}}  {{item.sales}} 
    </div>
</div>
</div>

Это мой home.component.ts

export class HomeComponent implements OnInit {
@ViewChild(barchart) barChart:barchart;
products:any=[];
items:any=[];
values:any=[];
data:any=[];

constructor(private service : ServiceService) { }

//Fetching of data
refreshData(){
  this.service.getAll().subscribe(res => {
  this.products=res;
  })  
} 

drop(event :CdkDragDrop<string[]>){ 
  if (event.previousContainer === event.container) {
    moveItemInArray(event.container.data, event.previousIndex, event.currentIndex);
  } else {
    transferArrayItem(event.previousContainer.data, event.container.data , event.previousIndex, event.currentIndex);  
    this.barChart.bar(event.container.data,event.container.id);
  }  
}
  ngOnInit() {
    this.refreshData();
  }
}

Это мой bar.ts

import * as d3 from 'd3';

export class barchart{
data=[];
margin = {top: 10, right: 20, bottom: 30, left: 40};

bar(value, id) {
  d3.selectAll("svg").remove(); 
  this.data =value;

const svg  = d3
    .select(id)
    .append('svg')
    .attr('width',150)
    .attr('height',150)

const contentWidth = 200;
const contentHeight = 120;

const x = d3     
    .scaleBand()
    .rangeRound([0, contentWidth])
    .padding(0.1)
    .domain(this.data.map(d => d.productName));

const y = d3
    .scaleLinear()
    .rangeRound([contentHeight, 0])
    .domain([0, d3.max(this.data, d => d.sales)]);

const g = svg.append('g')
    .attr('transform', 'translate(' + this.margin.left + ',' + this.margin.top + ')');

g.append('g')
  .attr('class','x axis')
  .attr('transform', 'translate(0,120)')
  .call(d3.axisBottom(x)); 

g.append('g') 
 .attr('class', 'y axis')
 .call(d3.axisLeft(y))

g.selectAll('.bar')
  .data(this.data)
  .enter()
  .append('rect')
  .attr('class', 'bar')
  .attr("fill",'steelblue')
  .on('mouseover', function(d) {
    d3.select(this)
      .attr('fill', '#3e8e41');
    })
  .on('mouseout',function(d){
    d3.select(this)
      .attr('fill','steelblue')
    })
  .attr('x', d => x(d.productName))
  .attr('y', d => y(d.sales))
  .attr('width', x.bandwidth())
  .attr('height', d => contentHeight - y(d.sales));
  }
}

При перетаскивании данных в раскрывающемся списке должна отображаться конкретная диаграмма.

Ответы [ 2 ]

0 голосов
/ 30 января 2019

Полагаю, вам вообще не следует использовать @ViewChild.@ViewChild property decorator позволяет искать элемент или директиву, соответствующие селектору в DOM представления, но ваш код выглядит так, как будто в html нет директивы или селектора barchart.Так что this.barchart всегда undefined.Тем не менее, у вас есть класс barchart, который можно использовать.Вы должны создать экземпляр barchart и вызвать bar:

// home.component.ts
// import your barchar class
import { barchart } from './pathtobarchart';

_barchart: barchart;

constructor(private service : ServiceService) {
  // create an instance of barchart class,
  // so you can use it inside the methods of
  // your HomeComponent
  this._barchart = new barchart();
}

drop(event :CdkDragDrop<string[]>) {
  if (event.previousContainer === event.container) {
    moveItemInArray(event.container.data, event.previousIndex, 
    event.currentIndex);
  } else {
   transferArrayItem(event.previousContainer.data, event.container.data , event.previousIndex, event.currentIndex);  
   this._barchart.bar(event.container.data,event.container.id);
  }  
}
0 голосов
/ 30 января 2019

Проблема:

drop(event :CdkDragDrop<string[]>){ 
  if (event.previousContainer === event.container) {
    moveItemInArray(event.container.data, event.previousIndex, event.currentIndex);
  } else {
    console.log("different container");
    transferArrayItem(event.previousContainer.data, event.container.data , event.previousIndex, event.currentIndex);  
    console.log(event.container.id);
    console.log(event.container)
    this.barChart.bar(event.container.data,event.container.id); <-- problem
  }  
}

Исправлено:

drop(event :CdkDragDrop<string[]>){ 
  if (event.previousContainer === event.container) {
    moveItemInArray(event.container.data, event.previousIndex, event.currentIndex);
  } else {
    console.log("different container");
    transferArrayItem(event.previousContainer.data, event.container.data , event.previousIndex, event.currentIndex);  
    console.log(event.container.id);
    console.log(event.container)
    barChart.bar(event.container.data,event.container.id); <-- fix
  }  
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...