.NET 图表图例标记大小 [英] .NET Charting Legend Marker Size

查看:18
本文介绍了.NET 图表图例标记大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 .NET Windows 窗体项目上使用 DataVisualizations Charting 控件.我遇到的问题是,当我打印图表时,图例没有显示系列标记(实际上它是一种显示,但它看起来像是线上的一个较暗的像素).当在表单上查看图表时,标记是可见的,尽管它们不是很大,并且似乎与系列的 MarkerSize 值没有变化.但是当图表被打印出来(在纸上或到 PDF 上)时,标记就不存在了.

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.

此图显示了同一图表的 PDF 版本.如果你用力眯眼,你会在图例线的中心看到更暗的像素.

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.

如何修复图例标记,使其在打印时实际显示并使其尺寸更大?

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

推荐答案

由于似乎无法控制图例标记,因此您可能需要创建自定义图例.以下是它在 FormPDF 中的外观示例:

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:

我不得不缩小 PDF,所以它看起来更薄/更轻.

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

这是一个返回 CustomLegend 的函数:

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;
}

我是这样称呼它的:

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

一些注意事项:

  • 代码使用chart.ApplyPaletteColors().这是访问 Series 颜色所必需的.
  • 它还利用了鲜为人知的类NamedImageChart.Images.这是必要的,因为在 Chart 中设置任何图像都需要一个字符串!
  • 如果您想放大图像,您可能需要使用 LegendCells.例如见这里
  • 我只为一个 ChartType (Line) 和两个 MarkerStyles 编写了图像绘图.
  • 有很多方法可以自定义这些CustomItems.有关详细信息,请参阅此处..
  • 我确实使用了 Series.MarkerSize 但通过在循环内设置 ir = S.MarkerSize; 等很容易调整代码!
  • 与我所做的 3 个设置相比,您可能需要从原始图例中复制一些设置到自定义图例.我刚刚注意到您设置了 Font..
  • 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..

这篇关于.NET 图表图例标记大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆