Customizing chart with multiple Legends
I'm adding a new feature in which each series in the chart has a color and a symbol (represented as a string shown in the cell). I need to create two legends, one to explain what the colors mean and another to explain what the symbols mean.
For example, lets say there are 4 series:
Blue - A
Red - A
Blue - B
Red - B
I need to create two legends:
Color Legend
Blue = Car
Red = Truck
Symbol Legend
A = 2wd
B = 4wd
The trouble is after I add the second legend to the chart, the two legends always have the same elements. When I add a series to the chart control it gets added to both legends. I can't find a way to access the Items collections in the legends independently.
- Karl
Hi Karl,
Thanks for using Syncfusion products.
We have analyzed your reported query; you can customize chart legend by using ChartLegend list collection. Series collections bind to legend items by using LegendName property.
Following code snippet illustrates how to add custom legends.
Code Snippet [WF]:Form1.cs
· Series Name added to series text.
series1.Name = "Car";
series1.Text = series1.Name;
series2.Name = "Truck";
series2.Text = series2.Name;
series3.Name = "2wd";
series3.Text = series3.Name;
series4.Name = "4wd";
series4.Text = series4.Name;
· Legend collection instance created as below
ChartLegend legend1 = new ChartLegend(chart);
ChartLegend legend2 = new ChartLegend(chart);
legend1.Name = "legend1";
legend2.Name = "legend2";
legend1.Text = "Color Legend";
legend2.Text = "Symbol Legend";
· Binding each series collection to legend collection by using LegendName property
series1.LegendName = "legend1";
series2.LegendName = "legend1";
series3.LegendName = "legend2";
series4.LegendName = "legend2";
ScreenShot:
We have prepared below sample, you can download it from below link
Sample Link:
WindowsFormsApplication11.zip
Please let us know if you have any concern.
Thanks,
Vinothkumar Arumugam.
I have a second issue that you might help with. My Series.Text is generated from input data and can sometimes be very long. I'd like to be able to have each item in the legend show a tooltip with the complete text of the Series. Is that possible?
- Karl
We have analyzed your query,
Currently there is no explicit support to the Legend Item tooltip, but we can achieve this by triggering the “Legend.MouseHover” event.
We have prepared a work around sample for legend tooltip .
You can achieve this by applying following code snippet
Code Snippet [WF]:
chart.Legend.MouseHover+=Legend_MouseHover;
void Legend_MouseHover(object sender, EventArgs e)
{
Point p1 =this.chart.Legend.PointToClient(new Point(Control.MousePosition.X, Control.MousePosition.Y));
ChartLegendItem item = chart.Legend.GetItemBy(p1);
if (item != null)
this.toolTip.Show(item.Text, chart.Legend, p1.X + 10, p1.Y + 10, 1000);
}
The Legend.GetItemBy method will let you get the reference to a legend item at a specific point .
Screenshot :
Please find the below sample link to download the sample
Sample Location:
WindowsFormsApplication11
Kindly refer our below online documentation link for more details
http://help.syncfusion.com/ug/windows%20forms/Documents/chartlegendandlegenditems.htm
Please let us know if you have any concern.
Thanks,
Vinothkumar Arumugam.
So, do you know a way to get the tooltip to respond when the mouse is hovered over the legend item text?
- Karl
We have analyzed your query,
Currently there is no explicit support to the Legend Item tooltip .But We have prepared a work around sample for legend tooltip .It is achieved by triggering the “Legend.MouseHover” event.
Find the following code snippet to achieve your requirements:
Code Snippet [WF]:
chart.Legend.MouseHover+=Legend_MouseHover;
void Legend_MouseHover(object sender, EventArgs e)
{
Point p1 =chart.Legend.PointToClient(new Point(Control.MousePosition.X, Control.MousePosition.Y));
foreach (var items in chart.Legend.Items)
{
if(p1.X > items.Bounds.X && p1.X<(items.Bounds.X+items.Bounds.Width+items.Bounds.Right))
this.toolTip.Show(items.Text, chart.Legend, p1.X + 10, p1.Y + 10, 1000);
}
}
Screenshot of the attached sample result:
Please find the below sample location to download the sample
Sample Location:
WindowsFormsApplication11
Please let us know if you have any concern.
Thanks,
Vinothkumar Arumugam.
- Karl
Hi Karl,
Thanks for the update.
Please let us know if you have any further query on this.
Thanks,
Vinothkumar Arumugam.
- 7 Replies
- 2 Participants
-
KT Karl Thorkildsen
- May 13, 2015 11:39 PM UTC
- Jun 4, 2015 05:13 AM UTC