Recent

Author Topic: Adding Text into a Chart  (Read 2040 times)

SandyG

  • Jr. Member
  • **
  • Posts: 94
Adding Text into a Chart
« on: February 29, 2024, 06:39:13 pm »
Might be simple, but haven't really found what I'm trying to do in the examples and poking around here.

Looking to be able to add a simple text into an existing chart. This is not quite the same as a hint which uses the data of that series to draw the hint.

For example say I have a chart with some areas that I would like to identify with some basic text, and place where I would like. This text still need it to scale (or position) with the chart as the user may pan or zoom.

I can calculate an xy value based on the series to come up with points myself to place. 

I was almost thinking of something like a line series with the line not drawn but the points and hints always drawn if that makes sense as a hack, but not sure.

Attached is a sample of what I'm trying to do.

Thanks

Sandy


Nicole

  • Hero Member
  • *****
  • Posts: 1009
Re: Adding Text into a Chart
« Reply #1 on: February 29, 2024, 07:08:39 pm »
This is everything else than simple.
There are a lot of ways to do this.

I would suggest to add a new series and to scale it alike the existing series. Where the text shall appear, you write something as "..AddXY(value,...'mytext');
And format the marks as you like.

This is 1 non debugged way of 100 ways to do it.

If you do not like it, please tell us:
What type of series do you use?
With which code line do you add your data?

SandyG

  • Jr. Member
  • **
  • Posts: 94
Re: Adding Text into a Chart
« Reply #2 on: February 29, 2024, 07:34:17 pm »
Thanks, I think we are thinking of the same idea.

I'll create a new series for each of the single points with the text I need. Will use the same scale as the main graph. Might make it easier to keep track and update if needed.

Will see how far I get.

Off hand any examples of owner draw labels?  I went through a bunch of the demos but didn't see one... or just missed it  :)

Thanks

Sandy

wp

  • Hero Member
  • *****
  • Posts: 12476
Re: Adding Text into a Chart
« Reply #3 on: February 29, 2024, 08:19:22 pm »
Yes, that's exactly the way I would do it: add a line series with the label texts added along with the x/y values, Marks.Style = smsLabel, ShowLines=false and ShowPoints= false. And set the series' Legend.Visible to false to avoid the "text series" to appear in the legend. There is also no way to center the text to the x/y coordinate specified in the AddXY method, it will always be above or below the point, depending on MarkPositions; the closest you can get is setting Marks.Distance to 0.

While writing this I had the idea that there is alway the OnDrawEvent which fires after the chart has been drawn completely, and you have a last chance to add something to it. And this works also when you are willing to write some code - see the attached demo. One tiny difference though: While in the "text series" method the position of the extra text labels is used for calculating the axis scale, this is not done in the event method.

SandyG

  • Jr. Member
  • **
  • Posts: 94
Re: Adding Text into a Chart
« Reply #4 on: February 29, 2024, 08:49:57 pm »
Thanks will take a look at the sample.

I created a simple line series, changed some options not to display the line, and Marks.style as smsLabel and worked like a champ.

Actually very simple with help from folks here!!

The text is always the same, so using the AddXY() with the fixed text at a correct location will do the job.

Just need to come up with a good way to compute X and Y so labels are where they should be. That's hopefully easy the way I'm thinking.

Thanks for the awesome and quick help!

Sandy

SandyG

  • Jr. Member
  • **
  • Posts: 94
Re: Adding Text into a Chart
« Reply #5 on: March 01, 2024, 02:01:47 am »
Yes, that's exactly the way I would do it: add a line series with the label texts added along with the x/y values, Marks.Style = smsLabel, ShowLines=false and ShowPoints= false. And set the series' Legend.Visible to false to avoid the "text series" to appear in the legend. There is also no way to center the text to the x/y coordinate specified in the AddXY method, it will always be above or below the point, depending on MarkPositions; the closest you can get is setting Marks.Distance to 0.

While writing this I had the idea that there is alway the OnDrawEvent which fires after the chart has been drawn completely, and you have a last chance to add something to it. And this works also when you are willing to write some code - see the attached demo. One tiny difference though: While in the "text series" method the position of the extra text labels is used for calculating the axis scale, this is not done in the event method.

Just tried your example and it works like a champ as well. This does have a nice feature that it draws OVER everything where when I tried way with a line series I would see some of the parts of the graph drawing over the text (I think this is fixable, but haven't looked).

Thanks again, likely spend a few minutes trying to figure out the other issue with graph drawing over the text, or just use your example!

Sandy

wp

  • Hero Member
  • *****
  • Posts: 12476
Re: Adding Text into a Chart
« Reply #6 on: March 01, 2024, 08:03:00 pm »
likely spend a few minutes trying to figure out the other issue with graph drawing over the text, or just use your example!
Change the ZPosition of the series. Default 0 means: drawing order = creation order "into" the screen. Series with higher values are drawn over the existing series. I.e, if you want the "label series" to be before all other series, give its ZPosition a high number

SandyG

  • Jr. Member
  • **
  • Posts: 94
Re: Adding Text into a Chart
« Reply #7 on: March 02, 2024, 06:26:34 am »
I think I tried that but didn't work. It may be my fault as I changed a bunch of the Z position but didn't move off the Edit field so may have thought I changed them but actually didn't.

I ended up going to the sample code and picked up font and the rest of the drawing data from the Marks on the series so I could easily change as needed.

The sample worked well with a few mods for my needs. Again THANKS!!!

Sandy

wp

  • Hero Member
  • *****
  • Posts: 12476
Re: Adding Text into a Chart
« Reply #8 on: March 02, 2024, 12:51:03 pm »
I think I tried that but didn't work.
For completeness, here is a working example for usage of the series ZPosition.

SandyG

  • Jr. Member
  • **
  • Posts: 94
Re: Adding Text into a Chart
« Reply #9 on: March 02, 2024, 07:35:02 pm »
Thanks, works.

I must have not really updated the 'Z' setting when I was playing with it. I found an issue that if you update a field in the property editor and run, it sometimes will not update until you move off it. I think that was my issue... or just operator error!

Sandy

 

TinyPortal © 2005-2018