3

I have a chart component like below:

<div id="chart">
    <ngx-charts-bar-horizontal [view]="view" [trimYAxisTicks]="false" [xAxisTickFormatting]="convertDecimalToNumber" [scheme]="colorScheme" [results]="data" [gradient]="gradient" [xAxis]="showXAxis" [yAxis]="showYAxis" [showXAxisLabel]="showXAxisLabel" [showDataLabel]="showDataLabel" [xAxisLabel]="xAxisLabel" [barPadding]="40">
    </ngx-charts-bar-horizontal>
</div>

I am manipulating all the styles of this chart by it's parent div here which has id="chart", but I need above same component 2 times in a parent component! So that creates problem with same 2 ids.

So, I decided to pass div's id from parent component as an @Input() like below:

<div class="container">
    <!-- Other tags -->
    <child-component [chartId]="users"></child-component>
    <!-- Other tags -->
    <child-component [chartId]="visuals"></child-component>
    <!-- Other tags -->
</div>

Edit on child component:

TS File:

@Input() chartId: string;

HTML:

<div [id]="chartId">
    <ngx-charts-bar-horizontal [view]="view" [trimYAxisTicks]="false" [xAxisTickFormatting]="convertDecimalToNumber" [scheme]="colorScheme" [results]="data" [gradient]="gradient" [xAxis]="showXAxis" [yAxis]="showYAxis" [showXAxisLabel]="showXAxisLabel" [showDataLabel]="showDataLabel" [xAxisLabel]="xAxisLabel" [barPadding]="40">
    </ngx-charts-bar-horizontal>
</div>

I tried these techniques: [id]="chartId", [attr.id]="chartId", id="{{chartId}}"

but none of above worked to set the dynamic id from Input.

Hope
  • 475
  • 6
  • 16

3 Answers3

4

HTML:

<div id="chart"> </div>

component:

@Input() chartId: string;
ngOnChanges(): void {
    const parent_div = document.getElementById('chart');
    parent_div.setAttribute('id', this.chartId);
}

In your child component give some initial id as a default value and use ngOnChanges to update the id.

coderman401
  • 582
  • 3
  • 17
Palak Jadav
  • 1,202
  • 1
  • 11
  • 23
1

In parent component.html:

<div class="container">
    <!-- Other tags -->
    <child-component chartId="users"></child-component>
    <!-- Other tags -->
    <child-component chartId="visuals"></child-component>
    <!-- Other tags -->
</div>

and in your child component.ts:

@Input() chartId: string;

and in your child component.html file:

<div id="{{ chartId }}">
    <ngx-charts-bar-horizontal [view]="view" [trimYAxisTicks]="false" [xAxisTickFormatting]="convertDecimalToNumber" [scheme]="colorScheme" [results]="data" [gradient]="gradient" [xAxis]="showXAxis" [yAxis]="showYAxis" [showXAxisLabel]="showXAxisLabel" [showDataLabel]="showDataLabel" [xAxisLabel]="xAxisLabel" [barPadding]="40">
    </ngx-charts-bar-horizontal>
</div>

EDIT

If the above solution didn't work for you try this:

in the parent component.ts file declare two variables:

users: string = 'yourID1';
visuals: string = 'yourID2';

and in parent component.html:

<div class="container">
    <!-- Other tags -->
    <child-component [chartId]="users"></child-component>
    <!-- Other tags -->
    <child-component [chartId]="visuals"></child-component>
    <!-- Other tags -->
</div>

I hope this will help

coderman401
  • 582
  • 3
  • 17
0

Sometimes the problem is at your ViewEncapsulation level. Even more, if you're trying to style a dom element inside your <ngx-charts-bar-horizontal/> .

I think you may use the ViewEncapsulation.None option inside your ChildComponent.

jafar_mnkd
  • 112
  • 6