amcharts / amcharts5

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

Get width of xAxis #1340

Closed canesen7 closed 8 months ago

canesen7 commented 9 months ago

Hi. I've a single bar chart like this.

image

As you can see, it has one xAxis and two series, first is opposite. When the values are different each other, they has different x axis width. And I need to each serie's max width.

image

(I need to each line's width.)

Thanks

martynasma commented 9 months ago

Do I understand you correctly that the left part of the X axis should end at -600,000,000?

If so, you would need to set min setting to 600000000 on the X axis: https://www.amcharts.com/docs/v5/charts/xy-chart/axes/value-axis/#Restricting_to_exact_min_and_max_values

canesen7 commented 9 months ago

No, I need to know width between -200 000 000 to 0 and 0 to 600 000 000

martynasma commented 8 months ago

I suppose you can do it in a two-step process:

1) Determine axis position of -200,000,000, 0, and 600,000,000 using axis method valueToPosition(). 2) Then use axis renderer's method positionToCoordinate() to convert it to a screen coordinates, do the math to calculate width of each segment.

Does that make sense?

canesen7 commented 8 months ago

Yeah, that make sense but I search the easy way to do it bc in every render, this method must run and calculate label's position by label font-size and current bar width.

martynasma commented 8 months ago

What's the purpose of calculating those widths? Maybe there's a better approach.

canesen7 commented 8 months ago

I need these for calculate values on bar labels.

martynasma commented 8 months ago

I'm a bit confused. There are no labels on bars in your screenshots. Are you going to have labels on bars as a bullets?

canesen7 commented 8 months ago

image Yes like this

martynasma commented 8 months ago

OK, so you actually need a width of the related bar, not the axis width?

canesen7 commented 8 months ago

No, I can reach related bar's width. I need to related bar's max width from -200 000 000 to 0 and 0 to 600 000 000. Chart has 2 yAxis (negative and positive) and one xAxis. I need each yAxis's maxWidth.

martynasma commented 8 months ago

OK, so you can do something like this:

var fromPos = xAxis.valueToPosition(xAxis.getPrivate("min"));
var toPos = xAxis.valueToPosition(0);
var fromPoint = xAxis.get("renderer").positionToCoordinate(fromPos);
var toPoint = xAxis.get("renderer").positionToCoordinate(toPos);
console.log("Lenght:", toPoint - fromPoint);
canesen7 commented 8 months ago

Thanks, this method not solve my issue but gives me a idea that solve my issue.