In my chart I need to clean up and trim the text in the legend. I’m using data from a SQL query and I need to shorten the text string so it will fit on the chart legend for a cleaner look. The SQL query returns data like ‘Microsoft Windows Server 2008
R2 Enterprise Edition’, I want to shorten this to ‘Server 2008 R2’ .
I’m using a VB sub routine with GridViewRowEventArgs to clean the data for output to the grid view but I can’t find a similar method that I can use for the chart. What have I overlooked?
Try something like this. The code can be optimized further as needed.
var oldName1 = "Old name 1";
var oldName2 = "Old name 2";
var newName1 = "New name 1";
var newName2 = "New name 2";
var chart = new Chart();
chart.Legends.Add(new Legend { Docking = Docking.Top, Alignment = StringAlignment.Far });
// Create and add the Series to Chart
// Now loop through all series and change name
foreach (Series series in chart.Series)
{
series.Name.Replace(oldName1, newName1);
series.Name.Replace(oldName2, newName2);
}
dft277
Member
2 Points
2 Posts
Manipulating legend text in chart
Feb 24, 2012 08:21 PM|LINK
In my chart I need to clean up and trim the text in the legend. I’m using data from a SQL query and I need to shorten the text string so it will fit on the chart legend for a cleaner look. The SQL query returns data like ‘Microsoft Windows Server 2008 R2 Enterprise Edition’, I want to shorten this to ‘Server 2008 R2’ .
I’m using a VB sub routine with GridViewRowEventArgs to clean the data for output to the grid view but I can’t find a similar method that I can use for the chart. What have I overlooked?
jerryjoseph
Contributor
6740 Points
1257 Posts
Re: Manipulating legend text in chart
Feb 24, 2012 09:04 PM|LINK
You can iterate through all the Series in the Chart and modify their Name property as needed.
linkedin | twitter | www.jerryjoseph.net
dft277
Member
2 Points
2 Posts
Re: Manipulating legend text in chart
Feb 27, 2012 06:45 PM|LINK
that's my goal, I'm just having trouble getting the syntax right to return the names to me.
jerryjoseph
Contributor
6740 Points
1257 Posts
Re: Manipulating legend text in chart
Feb 27, 2012 07:42 PM|LINK
Try something like this. The code can be optimized further as needed.
var oldName1 = "Old name 1"; var oldName2 = "Old name 2"; var newName1 = "New name 1"; var newName2 = "New name 2"; var chart = new Chart(); chart.Legends.Add(new Legend { Docking = Docking.Top, Alignment = StringAlignment.Far }); // Create and add the Series to Chart // Now loop through all series and change name foreach (Series series in chart.Series) { series.Name.Replace(oldName1, newName1); series.Name.Replace(oldName2, newName2); }Or paste your code.
linkedin | twitter | www.jerryjoseph.net