Please use the following code snippet and click the button to print the chart:
public RadForm1()
{
InitializeComponent();
Random rand = new Random();
for (int i = 0; i < 50; i++)
{
LineSeries lineSeries = new LineSeries();
lineSeries.LegendTitle = "Series" + i;
lineSeries.DataPoints.Add(new CategoricalDataPoint(rand.Next(0,20), "Jan"));
lineSeries.DataPoints.Add(new CategoricalDataPoint(rand.Next(0,20), "Apr"));
lineSeries.DataPoints.Add(new CategoricalDataPoint(rand.Next(0,20), "Jul"));
lineSeries.DataPoints.Add(new CategoricalDataPoint(rand.Next(0,20), "Oct"));
this.radChartView1.Series.Add(lineSeries);
}
this.radChartView1.ShowLegend = true;
this.radChartView1.ChartElement.LegendPosition = LegendPosition.Bottom;
}
private void radButton1_Click(object sender, EventArgs e)
{
this.radChartView1.PrintPreview();
}
Expected:
Actual:
Workaround:
https://docs.telerik.com/devtools/winforms/knowledge-base/chartview-wrap-legend-items
internal class MyChart : RadChartView
{
protected override RadChartElement CreateChartElement()
{
return new MyChartElement();
}
}
internal class MyChartElement : RadChartElement
{
protected override ChartLegendElement CreateChartLegendElement()
{
return new MyLegendElement(this);
}
protected override Type ThemeEffectiveType
{
get
{
return typeof(RadChartElement);
}
}
}
internal class MyLegendElement : ChartLegendElement
{
private RadListViewElement panel;
public MyLegendElement(RadChartElement chartElement) : base(chartElement)
{
}
protected override void OnLegendInfosCollectionChanged(Telerik.WinControls.Data.NotifyCollectionChangedEventArgs e, bool providerChange)
{
base.OnLegendInfosCollectionChanged(e, providerChange);
if (e.Action == Telerik.WinControls.Data.NotifyCollectionChangedAction.Add)
{
LegendItem li = e.NewItems[0] as LegendItem;
panel.Items.Add(li.Title);
panel.Items.Last().Tag = li.Element;
}
panel.SelectedIndex = -1;
}
protected override void CreateChildElements()
{
base.CreateChildElements();
panel = new RadListViewElement();
panel.VisualItemFormatting += ListView_VisualItemFormatting;
panel.ShowCheckBoxes = true;
panel.StretchHorizontally = true;
panel.StretchVertically = false;
panel.ViewType = ListViewType.IconsView;
panel.ItemSize = new System.Drawing.Size(100, 20);
panel.ShouldHandleMouseInput = true;
panel.NotifyParentOnMouseInput = false;
panel.MaxSize = new System.Drawing.Size(int.MaxValue, 200);
this.StackElement.Visibility = Telerik.WinControls.ElementVisibility.Collapsed;
this.Children.Add(panel);
}
protected override Type ThemeEffectiveType
{
get
{
return typeof(ChartLegendElement);
}
}
private void ListView_VisualItemFormatting(object sender, ListViewVisualItemEventArgs e)
{
ListViewItemCheckbox checkBox = e.VisualItem.ToggleElement as ListViewItemCheckbox;
if (checkBox != null)
{
e.VisualItem.ToggleElement.ShouldHandleMouseInput = false;
e.VisualItem.ToggleElement.NotifyParentOnMouseInput = false;
checkBox.CheckMarkPrimitive.Fill.BackColor = ((LineSeries)(e.VisualItem.Data.Tag)).BorderColor;
checkBox.CheckMarkPrimitive.Fill.GradientStyle = Telerik.WinControls.GradientStyles.Solid;
checkBox.CheckMarkPrimitive.Border.Visibility = Telerik.WinControls.ElementVisibility.Collapsed;
}
}
}