amcharts / amcharts5

The newest, fastest, and most advanced amCharts charting library for JavaScript and TypeScript apps.
Other
345 stars 92 forks source link

How I can get the DateAxis label as HTML? #1703

Closed sergiu-os closed 3 weeks ago

sergiu-os commented 3 weeks ago

Hello, I'm trying to move the DateAxis labels outside the chart.

I was wondering if there is any way to get the HTML of the dateAxis labels ?

martynasma commented 3 weeks ago

Yes, via adapters:

xAxis.get("renderer").labels.template.adapters.add("html", function(html, target) {
  if (target.dataItem) {
    return "<strong>" + new Date(target.dataItem.get("value")).toLocaleDateString() + "</strong>";
  }
  return html;
})
sergiu-os commented 3 weeks ago

I've tried with that but the problem is that is not the same number of labels that I see at the bottom. It got additional dates in there.

https://codepen.io/sergiu-os/pen/xxoQRXR

Is there anyway to just copy the html in the DateAxis label ? thanks

martynasma commented 3 weeks ago

I'm not entirely sure what you mean.

An adapter for html will kick in whenever label needs to be drawn. Whatever it returns it will be displayed as HTML.

There's no guarantee that adapter will kick in exactly number of times there are labels, and that it will occur in exact order.

You need to base the content on the actual timestamp.

sergiu-os commented 3 weeks ago

I meant that it's not exactly the same HTML. I can get the dates from the axis but when I move them anywhere else I lose the position on them relative to the chart.

Basically what I want is to move the axis label HTML outside of the canvas. I could do this in Amcharts 4 by just copying the html of the axis and move it somewhere else.

martynasma commented 3 weeks ago

I'm afraid there's not way to display axis labels outside of the chart container.