Click here to Skip to main content
15,896,063 members
Please Sign up or sign in to vote.
4.50/5 (2 votes)
See more:
I'm using the MS Chart component for .Net 3.5, and I'm using it to construct a metafile. (We're embedding it in an RTF report.) In order to work around the issue with WMF coordinates being poor precision, my virtual chart area is large (6000×4000) pixels.

I am rescaling everything with the following method:

C#
public void UpdateScaledElements()
{
    float scaleFactor = Chart.Height / 400f;
    foreach (Series s in Chart.Series)
    {
        s.Font = new Font(FONT_NAME, 8 * scaleFactor);
        s.BorderWidth = (int)(1.6 * scaleFactor);
        s.MarkerSize = (int)(6 * scaleFactor);
    }
    foreach (Legend l in Chart.Legends) {
        l.Font = new Font(FONT_NAME, 8 * scaleFactor);
    }
    foreach (Axis a in Chart.ChartAreas[0].Axes)
    {
        a.TitleFont = new Font(FONT_NAME, 10 * scaleFactor);
        a.LabelStyle.Font = new Font(FONT_NAME, 8 * scaleFactor);
        a.LineWidth = (int)(2 * scaleFactor);
    }
}


It's working pretty well. However, I can't work out how to make the line thickness of the lines drawn in the legend/key to scale up. I expected them to come through from the series like the marker scale and colour do. I've searched but I can't find a solution.

I have tried changing l.BorderWidth (it actually does change the border of the key, not lines within it), rebuilding the key in this method, and using the CustomizeLegend event to set BorderWidth on individual items. None of those changed the default behaviour, which seems to be 1 unit – which will be a hairline when this is printed, not a nice bold line like the series.

Edit: I have 'solved' this by painting the line myself in a PostPaint handler. This is a hack so I'm still in the market for an elegant solution.
Posted
Updated 12-Apr-11 23:14pm
v2

1 solution

Can you possibly use an image for the legend value (I assume you want a colored box or line)?

Or could you try using a custom legend item like this, which is taken from the MSCharts sample program:

MIDL
legendItem = new LegendItem();
legendItem.Name = "Marker Style Item";
legendItem.Style = LegendImageStyle.Marker;
legendItem.ShadowOffset = 1;
legendItem.Color = Color.Yellow;
legendItem.MarkerStyle = MarkerStyle.Cross;
legendItem.MarkerSize = 10;
legendItem.MarkerBorderColor = Color.Black;
chart1.Legends["Default"].CustomItems.Add(legendItem);
 
Share this answer
 
Comments
BobJanova 13-Apr-11 5:13am    
Thanks but your code is for a marker style key item, not a line style one. Marker key entries are working correctly already.
wizardzz 13-Apr-11 10:08am    
Do you have to use a line? Couldn't you just use a filled in box to represent the same thing?
BobJanova 13-Apr-11 10:53am    
It's a line graph, the key entry should also be a line (as it is by default for a line series).
wizardzz 13-Apr-11 11:46am    
Also, have you tried the MSCharts message boards as well?
wizardzz 13-Apr-11 11:44am    
Hmm, so using an image is out of the question too?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900