Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
685 views
in Technique[技术] by (71.8m points)

c# - .NET Charting Legend Marker Size

I'm using the DataVisualizations Charting control on a .NET Windows form project. The problem I'm having is that when I print the chart the legend is not showing the series marker (actually it is kind of showing but it just looks like a darker pixel on the line). When the chart is viewed on the form the markers are visible although they are not very large and do not seem to change in relation to the MarkerSize value for the series. But when the chart is printed (on paper or to a PDF) the markers are not there.

This picture shows the view of the chart when viewed on the form. As you can see the legend markers are sort of visible but still no where near what the actual series markers are.

Viewing chart on form

This image shows a PDF version of the same chart. If you squint real hard you can see darker pixel in the center of the legend line.

PDF printed chart

How can I fix the legend markers so they actually show when printed and to make them larger in size?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Since there seems to be no way to control the Legend Markers you may need to create a custom Legend. Here is an example of how it may look in the Form and in a PDF:

enter image description hereenter image description here

I had to zoom out the PDF, so it looks a little thinner/lighter.

Here is a function that returns a CustomLegend:

Legend CustomCloneLegend(Chart chart, Legend oLeg)
{
    Legend newL = new Legend();
    newL.Position = oLeg.Position;  // copy a few settings:
    newL.Docking = oLeg.Docking;
    newL.Alignment = oLeg.Alignment;
    // a few numbers for the drawing to play with; you may want to use floats..
    int iw = 32; int iw2 = iw / 2;    int ih = 18; int ih2 = ih / 2;
    int ir = 12;  int ir2 = ir / 2;   int lw = 3;
    // we want to access the series' colors!
    chart.ApplyPaletteColors();
    foreach (Series S in chart.Series)
    {
        // the drawing code is only for linechart and markerstyles circle or square:
        Bitmap bmp = new Bitmap(iw, ih);
        using (Graphics G = Graphics.FromImage(bmp))
        using (Pen pen = new Pen(S.Color, lw))
        using (SolidBrush brush = new SolidBrush(S.Color))
        {
            G.DrawLine(pen, 0, ih2, iw, ih2);
            if (S.MarkerStyle == MarkerStyle.Circle)
                G.FillEllipse(brush, iw2 - ir2, ih2 - ir2, ir, ir);
            else if (S.MarkerStyle == MarkerStyle.Square)
                G.FillRectangle(brush, iw2 - ir2, ih2 - ir2, ir, ir);
        }
        // add a new NamesImage
        NamedImage ni = new NamedImage(S.Name, bmp);
        chart.Images.Add(ni);
        // create and add the custom legend item
        LegendItem lit = new LegendItem( S.Name, Color.Red, S.Name);
        newL.CustomItems.Add(lit);
    }
    oLeg.Enabled = false;
    return newL;
}

Here is how I call it:

Legend LC = CustomCloneLegend(chart3, L);
chart1.Legends.Add(LC);

A few notes:

  • The code uses the chart.ApplyPaletteColors(). This is necessary to access the Series color.
  • It also makes use of the little know classes NamedImage and Chart.Images. This is neccessary since setting any images in a Chart requires a string!
  • If you want to enlarge the image you may need to use LegendCells. For an example see here!
  • I have coded the image drawing only for one ChartType (Line) and only two MarkerStyles.
  • There are many ways to customize those CustomItems. See here for more info..
  • I did use the Series.MarkerSize but it is easy to adapt the code by setting ir = S.MarkerSize; etc inside the loop!
  • You may need to copy a few more settings from your original legend to the custom legend than the 3 I did. I just noticed that you have set a Font..

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...