更新时间:2023-12-05 21:01:22
由于似乎没有办法控制Legend Markers,你可能需要创建一个自定义Legend。下面是一个在表单
和 PDF
中的示例:
我不得不
这里是一个返回 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()
。这是访问系列
颜色所必需的。 NamedImage
和 Chart.Images
。这是必要的,因为在图表
中设置任何图像都需要一个字符串! LegendCells
。有关示例,请参见此处! ChartType
(行
)和只有两个 MarkerStyles
。 CustomItems
。有关详情,请参见此处。 。 c $> c $> c $>
Font
.. chart.ApplyPaletteColors()
. This is necessary to access the Series
color.NamedImage
and Chart.Images
. This is neccessary since setting any images in a Chart
requires a string!LegendCells
. For an example see here!ChartType
(Line
) and only two MarkerStyles
. CustomItems
. See here for more info..Series.MarkerSize
but it is easy to adapt the code by setting ir = S.MarkerSize;
etc inside the loop!Font
..