Page 1 of 1

How to control chart's legend behavior?

Posted: Wed Aug 12, 2009 3:53 pm
by 15053374
Hi,

If I understand correctly, legend behavior is controlled through its LegendStyle property. By default it is set to lsAuto, which makes it behave differently when there is a single data series and multiple data series.

As expected, when there are multiple data series, the legend uses lsSeries style and displays each series' title (or name if title is not set).

When there is a single data series, the legend uses lsValues style and displays points/values from the data series.

What I would like to display in case of a single data series is not points/values but their corresponding axis labels. (I'm setting these labels myself, for each point/value.) Is there a way to do that?

So far I have tried to access Legendtems collection and manually override each item text (calling item's SetText method) but that did not work (I get an empty legend).

Btw, I'm manipulating the chart externally and I can't use the callbacks such as OnGetLegendText mentioned in a few places in this forum.

Thanks in advance for help!

Re: How to control chart's legend behavior?

Posted: Thu Aug 13, 2009 9:37 am
by yeray
Hi wss,

Yes, that works as you understood. Only one consideration: note that the axis labels work in a similar way than the legend; you can also change the axis labels style to show the series labels or the series values.

And also note the legend's TextStyle property (that applies with LegendStyle lsLastValues and lsValues) and can take the values: ltsLeftPercent, ltsLeftValue, ltsPercent, ltsPlain, ltsRightPercent, ltsRightValue, ltsValue, ltsXAndPercent, ltsXAndValue and ltsXValue.

Look at this simple example:

Code: Select all

Private Sub Form_Load()
  TChart1.AddSeries scBar
  
  Dim i As Integer
  For i = 0 To 5
    TChart1.Series(0).Add Rnd * 100, "Point Number" & Str$(i + 1), clTeeColor 'adding points with labels
  Next i
  
  TChart1.Series(0).PointLabel(4) = "custom string" 'changing an individual point label
  
  TChart1.Legend.LegendStyle = lsValues
  TChart1.Legend.TextStyle = ltsPlain
End Sub

Re: How to control chart's legend behavior?

Posted: Thu Aug 13, 2009 12:54 pm
by 15053374
Thank you for the prompt answer!

Here's what I have done after I read your response:

1. Stopped explicitly seting labels for the (bottom) axis
2. Set the point label for each point that I add to the series
3. Set the legend's TextStyle to ltsPlain. (In this particular scenario I have only one data series so I did not bother setting LegendStyle, knowing it will default to lsValues.)

As a result, I do get point labels shown in the legend instead of point values, which is what I wanted. Unfortunately, now the point values themselves are not shown anywhere in the chart.

What would be the way to have values displayed in the chart, labels displayed on the (bottom) axis, and again labels (not values) displayed in the legend?

Thanks!

Re: How to control chart's legend behavior?

Posted: Thu Aug 13, 2009 1:19 pm
by yeray
Hi wss,

I think that the problem is still to set correctly the style of the different parts: Legend labels, Axis labels and Series marks. If I understood well, that could be an example of how you could do it:

Code: Select all

Private Sub Form_Load()
  TChart1.AddSeries scBar
 
  Dim i As Integer
  For i = 0 To 5
    TChart1.Series(0).Add Rnd * 100, "Point Number" & Str$(i + 1), clTeeColor 'adding points with labels
  Next i
 
  TChart1.Series(0).PointLabel(4) = "custom string" 'changing an individual point label
 
  TChart1.Legend.LegendStyle = lsValues
  TChart1.Legend.TextStyle = ltsPlain
  
  TChart1.Axis.Bottom.Labels.Style = talText
  TChart1.Series(0).Marks.Style = smsValue
End Sub

Re: How to control chart's legend behavior?

Posted: Thu Aug 13, 2009 1:47 pm
by 15053374
Yeray wrote:I think that the problem is still to set correctly the style of the different parts: Legend labels, Axis labels and Series marks.

Code: Select all

TChart1.Series(0).Marks.Style = smsValue
This was the last remaining piece of the puzzle. Now I have values in the chart, and their corresponding labels in the legend.

Thank you for your time (and informative responses)!

Re: How to control chart's legend behavior?

Posted: Thu Aug 13, 2009 1:58 pm
by yeray
Hi wss,

You're welcome!