ASE Home Page Products Download Purchase Support About ASE
ChartDirector Support
Forum HomeForum Home   SearchSearch

Message ListMessage List     Post MessagePost Message

  Finding X/Y coordinates of each bar in a bar chart.
Posted by chrish on Oct-01-2024 02:41
Hi,

I am trying to annotate a bar chart and want to see if there is a way to access each bar in a bar chart to find its X/Y coordinate in the screen space so that I can add graphics to particular bars. For example, I want to find the mid-point of each X coordinate for each bar in the chart. I can somewhat find the Y elements ok but if there is a way to also grab each Y coordinate, that would be great. Thank you.

Best,
  Chris

  Re: Finding X/Y coordinates of each bar in a bar chart.
Posted by Peter Kwan on Oct-01-2024 12:37
Hi Chris,

Yes, you can use XYChart.getXCoor and XYChart.getYCoor to obtain the (x, y) pixel coordinates of a point given the (x, y) data values. Your code already has the (x, y) data values of the bars (they are provided by your code to ChartDirector). You can use them to determine the (x, y) pixel coordinates of the bars.

Note that to use getXCoor/getYCoor, you must call XYChart.layoutAxes first. This tells ChartDirector your code has added all the layers and data sets to the chart, and it can determine final the axis scale. ChartDirector can only convert data values to pixel coordinates after the axis scale has been finalized.

(Note: As I am not sure of your programming language, I just randomly use the .NET version of the documentation below.)

https://www.advsofteng.com/doc/cdnet.htm#XYChart.getXCoor.htm
https://www.advsofteng.com/doc/cdnet.htm#XYChart.getYCoor.htm
https://www.advsofteng.com/doc/cdnet.htm#XYChart.layoutAxes.htm

If the bar chart has no x data values (Layer.setXData is not used), the x data values will be automatically set to the array index of the bars (0, 1, 2, 3, ....). For example, c.getXCoor(3) is the x pixel coordinate of the center of the 4th bar. c.getXCoor(1.5) is the x-pixel coordinate in between the 2nd and 3rd bar.

Note that you can add custom text boxes, symbols, images or lines to the chart without using pixel coordinates. The bar itself has pre-defined positions to add labels. For example:

https://www.advsofteng.com/doc/cdnet.htm#stackedbar.htm

The labels can be customized by using Layer.addCustomDataLabel or Layer.addCustomAggregateLabel. You can control the label alignment relative to the bar or apply offsets to the labels to shift them.

In ChartDirector, with CDML, a label can also includes shapes and images, so you can add an image with the same API that adds labels:

https://www.advsofteng.com/doc/cdnet.htm#cdml.htm

If you need to add text or images at some custom position, you can add a scatter layer with transparent scatter symbols at the label positions and assign labels to the invisible symbols:

https://www.advsofteng.com/doc/cdnet.htm#scatterlabels.htm
https://www.advsofteng.com/doc/cdnet.htm#scattersymbols.htm

If you have a custom image that is drawn dynamically at runtime (eg. using the DrawArea), you can set the DrawArea as a resource and use it the same way as if it is an image file on disk.

https://www.advsofteng.com/doc/cdnet.htm#BaseChart.setResource2.htm
https://www.advsofteng.com/doc/cdnet.htm#BaseChart.setResource.htm

Regards
Peter Kwan