I am currently using ngx-charts for Angular2, and I would like to customize some legends, e.g. place the legend below the chart, or rename the legend fields, or make the legend wider (parts of the text just gets cut off..) etc.
Are there any good options to customize the legend? Or is it generally not possible, or what is the best way to do it? Currently, my chart legends look like this:
Some legends are too wide, some are cut off, and I'd like to position the legend e.g below the chart...
If you are looking for a pure CSS solution this should be good jumping off point. In my application, we have a half-screen sized chart where the legend was cutting off longer labels.
Using version 12.0.1. This is heavily dependent on the DOM structure so if something changes in future releases this may not work.
The parent container of the chart must have extra space in it or the legend will wrap below the chart. In this example, assume the parent container width is > 800px, and the chart is using a fixed view of width 620px. You will have to turn off view encapsulation for your component as well.
In component file:
In CSS file:
If anyone was helped by @Maya Sol but was wondering the same thing as @Ollie, here's how you can do it. I'm adding an answer since I don't have enough reputation to add comments.
Modify the the legendLabelActivate function to do the following:
Then, using the example html from @Maya Sol ... in the ngx-charts-line-chart and ngx-charts-legend do the following:
Legend Position
To position the legend below the chart, you can use the
legendPosition
input:The only options for
legendPosition
are 'right' (default) and 'below'.Rename Legend Fields
There don't seem to be options for customizing the legend fields (there's a more customizable legend in the advanced pie chart, but your examples are other kinds of charts.) I think the best workaround would be to pass the field titles into your chartData's
chartData[i].name
values exactly as you want them to appear in the legend, and then customize your tooltip(s) to show a different field name there.This answer gives an overview of how to customize tooltips. From there, I put together an example with a line chart that will look like the default tooltips except with a different field name:
Change Legend Width
The legend width is calculated in the
ngx-charts-chart
component that the other charts extend. It will set the width to about 1/6th or 1/12th of the chart container, depending on your data type. There is no way to input a different width to this legend, so your easiest solution is to setlegendPosition
to 'below' when the automatic width doesn't work for you.However, you don't need to use the legend built into the chart! Here's a more complex (but more fine-tune-able) alternative: set
[legend]="false"
in your chart, and then add a newngx-charts-legend
component outside of the chart.You can either input width and height to this external legend, or wrap it in a div that handles sizing more responsively. I had to set the legend's width to 100% of its container when using the latter method.
To get this solution working, you have to bind the external legend activation events to the chart, and set some of the legend input properties onInit. In the component that contains your chart, you'll need something like this:
Then my template (with chart and legend each taking half of the width) looks like: