0

我试图通过使用 DataBindCrossTable() 将 LegendText 作为参数传递给 otherFields 来更改柱形图中的图例文本。

比如我的数据如下:

MyData {运行时间:日期时间位置:字符串项目:int}

然后我打电话

chart.DataBindCrossTable(data, "RunTime", "Location", "Projects", "LegendText=RunTime{MM/dd/yyyy}");

结果图表没问题,除了显示如下的图例:

运行时间 - 2013-01-01 12:00 AM

我希望图例只显示:

2013-01-01

看起来设置 LegendText 什么都不做。

谢谢您的帮助。

4

2 回答 2

1

我已经找到了解决方案。使用 DataBindCrossTable 时,otherFields 参数不适用于 Series 对象。通过设置它,结果将应用于系列中的每个数据点。

chart.DataBindCrossTable(data, "RunTime", "Location", "Projects", "LegendText=RunTime{MM/dd/yyyy}");

调用上述方法时,Series 中的点将正确设置其 LegendText 属性。我所做的是用第一点的 LegendText 设置我的系列名称,如下所示:

foreach (Series s in chart.Series)
        {
            s.ChartType = SeriesChartType.Column;
            s.Name = s.Points.First().LegendText;
            chart.Legends.Add(GetLegend(s.Name));
        }

然后它工作得很好。

于 2013-10-31T16:15:05.963 回答
0

我只找到了一个肮脏的方法,使用图表控件的 oncustomizelegend="Chart1_CustomizeLegend" 事件

    protected void Chart1_CustomizeLegend(object sender, System.Web.UI.DataVisualization.Charting.CustomizeLegendEventArgs e)
    {
        if (e.LegendItems.Count > 0)
        {
            for (int i = 0; i < e.LegendItems.Count; i++)
            {
                if (e.LegendItems[0].Cells.Count > 1)
                {
                    e.LegendItems[i].Cells[1].Text = e.LegendItems[i].Cells[1].Text.Replace("RunTime - ", "").Replace(" 12:00 AM", "");
                }
            }
        }
    }
于 2013-10-31T08:42:11.327 回答