C# 使用Chart.DataBindCrossTable时更改LegendText
我试图通过使用DataBindCrossTable()将LegendText作为参数传递给其他字段来更改柱状图中的图例文本 例如,我的数据如下: 我的数据{ 运行时:日期时间 位置:字符串 项目:int } 那我就给你打电话C# 使用Chart.DataBindCrossTable时更改LegendText,c#,mschart,C#,Mschart,我试图通过使用DataBindCrossTable()将LegendText作为参数传递给其他字段来更改柱状图中的图例文本 例如,我的数据如下: 我的数据{ 运行时:日期时间 位置:字符串 项目:int } 那我就给你打电话 chart.DataBindCrossTable(data, "RunTime", "Location", "Projects", "LegendText=RunTime{MM/dd/yyyy}"); 结果图表正常,但图例显示如下内容: 运行时间-2013-01-01上午
chart.DataBindCrossTable(data, "RunTime", "Location", "Projects", "LegendText=RunTime{MM/dd/yyyy}");
结果图表正常,但图例显示如下内容:
运行时间-2013-01-01上午12:00
我希望图例只显示:
2013-01-01
看起来设置LegendText没有任何作用
谢谢您的帮助。我只找到了一个不好的方法,请使用图表控件的事件oncustomizelegend=“Chart1\u 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", "");
}
}
}
}
protectedvoid Chart1\u customizeElegend(对象发送者,System.Web.UI.DataVisualization.Charting.customizeElegendEventArgs e)
{
如果(e.LegendItems.Count>0)
{
对于(int i=0;i1)
{
e、 LegendItems[i]。单元格[1]。Text=e.LegendItems[i]。单元格[1]。Text.Replace(“运行时-”,“”)。Replace(“上午12:00,”);
}
}
}
}
我找到了解决这个问题的方法。
使用DataBindCrossTable时,otherFields参数不适用于Series对象。通过设置,结果将应用于序列中的每个数据点
chart.DataBindCrossTable(data, "RunTime", "Location", "Projects", "LegendText=RunTime{MM/dd/yyyy}");
调用上述方法时,序列中的点将正确设置其LegendText属性。我所做的是用第一个point的LegendText设置我的系列名称,如下所示:
foreach (Series s in chart.Series)
{
s.ChartType = SeriesChartType.Column;
s.Name = s.Points.First().LegendText;
chart.Legends.Add(GetLegend(s.Name));
}
然后它工作得很好。您可以试试Chart1.DataBindCrossTable(mylist,“运行时”、“位置”、“项目”、“标签=运行时”);标签只是在图表区域中放置一个标签。我要的是图例,不是标签。。。真脏。其实我已经找到了答案。它取决于数据点。