我目前正在为Angular2使用ngx-charts,我想自定义一些图例,例如将图例放在图表下方,或重命名图例字段,或使图例变宽(部分文本被截断)。等。
有什么好的选项可以自定义图例吗?或者这通常是不可能的,或者最好的方法是什么?目前,我的图表图例如下所示:

有些图例太宽了,有些被截断了,我想把图例放在图表下面。
发布于 2019-01-03 23:10:44
图例位置
要将图例放置在图表下方,可以使用legendPosition输入:
<ngx-charts-chart [legendPosition]="'below'" [results]="chartData" [legend]="true"></ngx-charts-chart>legendPosition的唯一选项是“right”(默认)和“below”。
重命名图例字段
似乎没有自定义图例字段的选项(高级饼图中有一个更可自定义的图例,但您的示例是其他类型的图表)。我认为最好的解决办法是将字段标题完全按照您希望它们在图例中出现的方式传递到您的chartData的chartData[i].name值中,然后自定义您的工具提示以在其中显示不同的字段名称。
This answer gives an overview of how to customize tooltips.在那里,我将一个示例与一个折线图组合在一起,它看起来像默认的工具提示,只是使用了不同的字段名称:
<ngx-charts-line-chart [legendPosition]="'right'" [results]="chartData" [legend]="true">
<ng-template #tooltipTemplate let-model="model">
<xhtml:div class="area-tooltip-container">
<span class="tooltip-label">{{ formatFieldName(model.series) }} • {{ formatXvalue(model.name) }}</span>
<span class="tooltip-val">{{ formatYvalue(model.value) }}</span>
</xhtml:div>
</ng-template>
<ng-template #seriesTooltipTemplate let-model="model">
<xhtml:div class="area-tooltip-container">
<xhtml:div *ngFor="let tooltipItem of model" class="tooltip-item">
<span class="tooltip-item-color" [style.background-color]="tooltipItem.color">
</span>
{{ formatFieldName(tooltipItem.series) }}: {{ formatYvalue(tooltipItem.value) }}
</xhtml:div>
</xhtml:div>
</ng-template>
</ngx-charts-line-chart>更改图例宽度
图例宽度是在其他图表扩展的ngx-charts-chart组件中计算的。它将根据您的数据类型将宽度设置为图表容器的大约1/6或1/12。没有办法给这个图例输入不同的宽度,所以当自动宽度对你不起作用时,你最简单的解决方案是将legendPosition设置为“下方”。
但是,您不需要使用图表中内置的图例!这里有一个更复杂(但更可调)的替代方案:在图表中设置[legend]="false",然后在图表之外添加一个新的ngx-charts-legend组件。
您可以向此外部图例输入宽度和高度,也可以将其包装在div中,以便更好地处理大小调整。在使用后一种方法时,我必须将图例的宽度设置为其容器的100%。
要使此解决方案工作,您必须将外部图例激活事件绑定到图表,并设置一些图例输入属性onInit。在包含图表的组件中,您将需要如下内容:
import { ColorHelper } from '@swimlane/ngx-charts';
...
export class ChartContainerComponent implements OnInit {
public activeEntries: any[];
public chartData: { name: string, series: { name: string, value?: string | number }[] }[];
public chartNames: string[];
public colors: ColorHelper;
public colorScheme = { domain: ['#0000FF', '#008000'] }; // Custom color scheme in hex
public legendLabelActivate(item: any): void {
this.activeEntries = [item];
}
public legendLabelDeactivate(item: any): void {
this.activeEntries = [];
}
public ngOnInit(): void {
// Get chartNames
this.chartNames = this.chartData.map((d: any) => d.name);
// Convert hex colors to ColorHelper for consumption by legend
this.colors = new ColorHelper(this.colorScheme, 'ordinal', this.chartNames, this.colorScheme);
}然后我的模板(图表和图例各占宽度的一半)如下所示:
<div fxLayout="row">
<div fxFlex="50%">
<ngx-charts-line-chart [legend]="false" [activeEntries]="activeEntries" [results]="chartData" [scheme]="colorScheme"></ngx-charts-line-chart>
</div>
<div fxFlex="50%">
<ngx-charts-legend fxFlex="100%" class="chart-legend" [data]="chartNames" [title]="'Legend Title'" [colors]="colors" (labelActivate)="legendLabelActivate($event)" (labelDeactivate)="legendLabelDeactivate($event)"></ngx-charts-legend>
</div>
</div>发布于 2019-12-04 05:20:03
如果你正在寻找一个纯粹的CSS解决方案,这应该是一个很好的起点。在我的应用程序中,我们有一个半屏幕大小的图表,其中图例切断了较长的标签。
使用版本12.0.1。这在很大程度上依赖于DOM结构,所以如果在将来的版本中有什么变化,这可能不起作用。
图表的父容器中必须有额外的空间,否则图例将在图表下方换行。在此示例中,假设父容器宽度> 800px,并且图表使用宽度为620px的固定视图。您还必须关闭组件的视图封装。
在组件文件中:
@Component({
...
encapsulation: ViewEncapsulation.None
})在CSS文件中:
/* Makes the chart container expand to the full width of the parent container*/
.ngx-charts-outer {
width: 100% !important;
}
/* Sets the legend width to auto expand to fit labels, but caps at 200px */
.chart-legend > div {
width: auto !important;
max-width: 200px;
}
/* Some extra space is needed to offset the way the labels display */
.legend-labels {
width: calc(100% + 10px) !important;
}
/* Wrap the label text if it ends up being longer than the max width */
.legend-label-text {
white-space: normal !important;
width: 100% !important;
}发布于 2020-06-23 22:09:17
如果有人得到了@Maya Sol的帮助,但想知道和@Ollie一样的事情,这里是你可以做到的。我添加了一个答案,因为我没有足够的名气来添加评论。
修改legendLabelActivate函数以执行以下操作:
public legendLabelActivate(item: any): void {
// The if statement is here so both legend
// and chart element mouse over events will work.
if (item.value === undefined) {
this.activeEntries = [item];
} else {
this.activeEntries = [{ name: item.value.name }];
}
}然后,使用@Maya Sol中的示例html ...在ngx-charts-line-chart和ngx-charts-line中,执行以下操作:
<div fxLayout="row">
<div fxFlex="50%">
<ngx-charts-line-chart [legend]="false"
[activeEntries]="activeEntries"
(activate)="legendLabelActivate($event)"
(deactivate)="legendLabelDeactivate($event)"
[results]="chartData"
[scheme]="colorScheme">
</ngx-charts-line-chart>
</div>
<div fxFlex="50%">
<ngx-charts-legend fxFlex="100%"
class="chart-legend"
[data]="chartNames"
[title]="'Legend Title'"
[colors]="colors"
[activeEntries]="activeEntries"
(labelActivate)="legendLabelActivate($event)"
(labelDeactivate)="legendLabelDeactivate($event)">
</ngx-charts-legend>
</div>
</div>https://stackoverflow.com/questions/51946802
复制相似问题