如何用图例键添加数据表,在C#中的MS图表? [英] How to add data table with legend keys to a MS Chart in C#?

查看:1299
本文介绍了如何用图例键添加数据表,在C#中的MS图表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有2列出了名为 listversion &安培; MIN_list 。利用这些名单我已经创建了一个折线图的值。一切都做工精细。但我想知道是否有可能增加与传奇密钥的数据表像MS Excel的图表。

There are 2 lists called listversion & MIN_list. Using values of these list I have created a line chart. Everything is work fine. But I am wondering whether it is possible to add a data table with legend keys in to the chart like MS Excel.

chart.Series.Clear();
chart.ChartAreas[0].AxisX.Title = "Version";
chart.ChartAreas[0].AxisX.TitleFont = new System.Drawing.Font("Arial", 12, FontStyle.Regular);

chart.ChartAreas[0].AxisY.Title = "Time";
chart.ChartAreas[0].AxisY.TitleFont = new System.Drawing.Font("Arial", 12, FontStyle.Regular);

Series MIN = chart.Series.Add("Minimum");
MIN.Points.DataBindXY(listVersion, MIN_list[j]);
MIN.ChartType = SeriesChartType.Line;
MIN.Color = Color.Red;
MIN.BorderWidth = 3;



我很期待这样的事情

I am looking forward to something like this

在这里输入的形象描述

如果有可能,我该怎么办呢?

If it is possible How can I do it ?

感谢您

推荐答案

是的,你可以这样做:

在这里输入的形象描述

下面是我采取的步骤:

首先,我们停用原联想,因为它不能被操纵,我们需要..方式:

First we disable the original Legend as it can't be manipulated the way we need to..:

chart1.Legends[0].Enabled = false;

现在我们创建一个新的,快捷参考吧:

Now we create a new one and a shortcut reference to it:

chart1.Legends.Add(new Legend("customLegend"));
Legend L = chart1.Legends[1];



接下来,我们做了一些定位:

Next we do some positioning:

L.DockedToChartArea = chart1.ChartAreas[0].Name;  // the ca it refers to 
L.Docking = Docking.Bottom;
L.IsDockedInsideChartArea = false;
L.Alignment = StringAlignment.Center; 

现在我们想填补一个行头,每个系列一行。

Now we want to fill in one line for headers and one line per series.

我用一个共同的功能都和传递一个标志,指示是否标头(x值)或单元格数据(y值)应填写。下面是我调用该函数:

I use a common function for both and pass in a flag to indicate whether the headers (x-values) or the cell data (y-values) should be filled in. Here is how I call the function:

addValuesToLegend(L, chart1.Series[0], false);
foreach (Series s in chart1.Series) addValuesToLegend(L, s, true);

请注意,对于这个工作,我们需要在我们的系列<一些准备/ code>:

Note that for this to work we need a few preparations in our Series:


  • 我们需要设置 Series.Colors 明确地或否则我们不能引用他们。

  • 我添加了一个格式字符串到标签各系列;但也许你找到一个更好的解决方案,避免了硬编码的页眉格式。

  • We need to set the Series.Colors explicitly or else we can't refer to them.
  • I have added a format string to the Tag of each series; but maybe you find a better solution that avoids hard-coding the format for the header..

因此​​,这里是做所有功能灌装以及一些造型:

So here is the function that does all the filling and some styling:

void addValuesToLegend(Legend L, Series S, bool addYValues)
{
    // create a new row for the legend
    LegendItem newItem = new LegendItem();
    // if the series has a markerstyle we show it:
    newItem.MarkerStyle = S.MarkerStyle ;
    newItem.MarkerColor = S.Color; 
    newItem.MarkerSize *= 2;   // bump up the size
    if (S.MarkerStyle == MarkerStyle.None)
    {
        // no markerstyle so we just show a colored rectangle
        // you could add code to show a line for other chart types..
        newItem.ImageStyle = LegendImageStyle.Rectangle;
        newItem.BorderColor = Color.Transparent;
        newItem.Color = S.Color;
    }
    else newItem.ImageStyle =  LegendImageStyle.Marker;

    // the rowheader shows the marker or the series color
    newItem.Cells.Add(LegendCellType.SeriesSymbol, "", ContentAlignment.MiddleCenter);
    // add series name
    newItem.Cells.Add(LegendCellType.Text, addYValues ? S.Name : "",
                      ContentAlignment.MiddleLeft);
    // combine the 1st two cells:
    newItem.Cells[1].CellSpan = 2;

    // we hide the first cell of the header row
    if (!addYValues)
    {
        newItem.ImageStyle = LegendImageStyle.Line;
        newItem.Color = Color.Transparent;
        newItem.Cells[0].Tag = "*";  // we mark the 1st cell for not painting it
    }
    // now we loop over the points:
    foreach (DataPoint dp in S.Points)
    {
        // we format the y-value
        string t = dp.YValues[0].ToString(S.Tag.ToString());
        // or maybe the x-value. it is a datatime so we need to convert it!
        // note the escaping to work around my european locale!
        if (!addYValues) t = DateTime.FromOADate(dp.XValue).ToString("M\\/d\\/yyyy");
        newItem.Cells.Add(LegendCellType.Text, t, ContentAlignment.MiddleCenter);
    }
    // we can create some white space around the data:
    foreach (var cell in newItem.Cells)  cell.Margins = new Margins(25, 20, 25, 20);
    // finally add the row of cells:
    L.CustomItems.Add(newItem);
}

要绘制围绕我们的传奇表的单元格,我们需要编写的边界预制事件:

To draw the borders around the cells of our legend table we need to code the PrePaint event:

private void chart1_PrePaint(object sender, ChartPaintEventArgs e)
{
    LegendCell cell = e.ChartElement as LegendCell;
    if (cell != null && cell.Tag == null) 
    {
        RectangleF r = e.ChartGraphics.GetAbsoluteRectangle(e.Position.ToRectangleF());
        e.ChartGraphics.Graphics.DrawRectangle(Pens.DimGray,Rectangle.Round(r));
        // Let's hide the left border when there is a cell span!
        if (cell.CellSpan != 1) 
                e.ChartGraphics.Graphics.DrawLine(Pens.White, 
                                r.Left, r.Top+1, r.Left, r.Bottom-1);
    }
}

您可以添加更多的造型,虽然我不知道如果你能完全匹配的例子..

You can add more styling although I'm not sure if you can match the example perfectly..

这篇关于如何用图例键添加数据表,在C#中的MS图表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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