Custom tooltip contents @ ngx-charts | Angular2+ | TypeScript

雨燕双飞 提交于 2019-12-04 07:34:58

You can define your own tooltip templates and render any HTML you like in them:

<ngx-charts-line-chart        
    [scheme]="colorScheme"
    [results]="multi" ...>
  <ng-template #tooltipTemplate let-model="model">
    This is the single point tooltip template
    <pre>{{model|json}}</pre>
  </ng-template>

  <ng-template #seriesTooltipTemplate let-model="model">
    This is vertical line tooltip template
    <pre>{{model|json}}</pre>        
  </ng-template>
</ngx-charts-line-chart>

Example: https://swimlane.github.io/ngx-charts/#/ngx-charts/tooltip-templates

Code is here: https://github.com/swimlane/ngx-charts/blob/master/demo/app.component.html#L755-L760

Thank you, once again. Didn't want to leave the issue unresolved. The problem was the code snippet was inside a svg element. Here's the final version:

<!--  This is single point tooltip template -->
<xhtml:ng-template #tooltipTemplate let-model="model">
  <xhtml:div class="area-tooltip-container">
    <xhtml:div *ngFor="let tooltipItem of model | json | durationHhmm" class="tooltip-item" style="text-align: center;">
      <a style=" font-size: 1.2em;">{{tooltipItem.series}}</a><a *ngIf="tooltipShowTime==='DAY' || tooltipShowTime==='WEEK'" style=" font-size: 1.2em;"><br />{{tooltipItem.name | date: 'HH:mm'}} Uhr</a><a *ngIf="tooltipShowTime!=='DAY' && tooltipShowTime!=='WEEK'" style=" font-size: 1.3em; font-weight: 600;"><br />&#183;</a><br /><a style=" font-size: 1.2em; font-weight: 600;">{{tooltipItem.name | date: 'dd.MM.yyyy'}} &#183; </a><a style=" font-size: 1.3em; font-weight: 600;">{{tooltipItem.value}}</a>
    </xhtml:div>
  </xhtml:div>
</xhtml:ng-template>

<!-- Datapoints Y-Axis -->
<svg:g *ngFor="let series of data">
  <svg:g ngx-charts-circle-series
         [xScale]="xScale"
         [yScale]="yScale"
         [colors]="colors"
         [data]="series"
         [scaleType]="scaleType"
         [visibleValue]="hoveredVertical"
         [activeEntries]="activeEntries"
         [tooltipDisabled]="tooltipDisabled"
         [tooltipTemplate]="tooltipTemplate"
         (select)="onClick($event, series)"
         (activate)="onActivate($event)"
         (deactivate)="onDeactivate($event)"
  /> 
</svg:g>

The above solution does not work for multi-dimensional charts ( > 3) like Stacked Horizontal/Vertical Bar.

Another simple way which works for all cases is to add the tooltipText as an attribute as part of the model like below:

export let multi = [
  {
    name: 'Germany',
    series: [
      {
        name: '2010',
        value: 7300000,
        tooltipText: 't1'
      },
      {
        name: '2011',
        value: 8940000,
        tooltipText: 't2'
      }
    ]
  }
];

Then use the following code in markup,

<ngx-charts-bar-horizontal-stacked
      [view]="view"
      [scheme]="colorScheme"
      [results]="multi"
      [gradient]="gradient"
      [xAxis]="showXAxis"
      [yAxis]="showYAxis"
      [legend]="showLegend"
      [legendPosition]="legendPosition"
      [showXAxisLabel]="showXAxisLabel"
      [showYAxisLabel]="showYAxisLabel"
      [xAxisLabel]="xAxisLabel"
      [yAxisLabel]="yAxisLabel"
      (select)="onSelect($event)">
  <ng-template #tooltipTemplate let-model="model">
    <div class="tooltip">
      {{model.tooltipText}}
    </div>
  </ng-template>
</ngx-charts-bar-horizontal-stacked>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!