Legend in ASP.NET MVC Accumulation Chart Component
4 Oct 202224 minutes to read
As like a chart, the legend is also available for accumulation charts, which gives information about the points. By default, the legend will be placed on the right, if the width of the chart is high or will be placed on the bottom, if the height of the chart is high. Other customization features regarding the legend element are same as the chart legend
. Here, the legend for a point can be collapsed by giving the empty string to the x value of the point.
@Html.EJS().AccumulationChart("container").Series(series =>
{
series.DataSource(ViewBag.dataSource)
.XName("xValue")
.YName("yValue")
.Name("Browser")
.Type(Syncfusion.EJ2.Charts.AccumulationType.Pie)
.ExplodeIndex(0).Add();
})
.EnableSmartLabels(true)
.Title("Mobile Browser Statistics")
.LegendSettings(ls => ls.Visible(true)).Render()
public ActionResult Index()
{
List<PieChartData> chartData = new List<PieChartData>
{
new PieChartData { xValue = "Chrome", yValue = 37 },
new PieChartData { xValue = "UC Browser", yValue = 17 },
new PieChartData { xValue = "iPhone", yValue = 19 },
new PieChartData { xValue = "Others", yValue = 4 },
new PieChartData { xValue = "Opera", yValue = 11 },
new PieChartData { xValue = "Android", yValue = 12 },
};
ViewBag.dataSource = chartData;
return View();
}
public class PieChartData
{
public string xValue;
public double yValue;
}
Position and Alignment
By using the position property, you can position the legend at the Left
, Right
, Top
or Bottom
of the chart. You can also align the legend to Center
, Far
or Near
of the chart using the alignment property.
@Html.EJS().AccumulationChart("container").Series(series =>
{
series.DataSource(ViewBag.dataSource)
.XName("xValue")
.YName("yValue")
.Name("Browser")
.Type(Syncfusion.EJ2.Charts.AccumulationType.Pie)
.ExplodeIndex(0).Add();
})
.EnableSmartLabels(true)
.Title("Mobile Browser Statistics")
.LegendSettings(ls => ls.Visible(true)
.Position(Syncfusion.EJ2.Charts.LegendPosition.Top)).Render()
public ActionResult Index()
{
List<PieChartData> chartData = new List<PieChartData>
{
new PieChartData { xValue = "Chrome", yValue = 37 },
new PieChartData { xValue = "UC Browser", yValue = 17 },
new PieChartData { xValue = "iPhone", yValue = 19 },
new PieChartData { xValue = "Others", yValue = 4 },
new PieChartData { xValue = "Opera", yValue = 11 },
new PieChartData { xValue = "Android", yValue = 12 },
};
ViewBag.dataSource = chartData;
return View();
}
public class PieChartData
{
public string xValue;
public double yValue;
}
Legend Reverse
You can reverse the order of the legend items by using the Reverse
property. By default, legend for the first series in the collection will be placed first.
@Html.EJS().Chart("container").Series(series =>
{
series.Type(Syncfusion.EJ2.Charts.ChartSeriesType.Column)
.XName("x").YName("yValue").DataSource(ViewBag.dataSource)
.Name("Gold").Width(2).Add();
series.Type(Syncfusion.EJ2.Charts.ChartSeriesType.Column)
.XName("x").YName("yValue1").DataSource(ViewBag.dataSource)
.Name("Silver").Width(2).Add();
series.Type(Syncfusion.EJ2.Charts.ChartSeriesType.Column)
.XName("x").YName("yValue2").DataSource(ViewBag.dataSource)
.Name("Brozen").Width(2).Add();
}).PrimaryYAxis(px => px.LabelStyle(ls=>ls.Color("transparent")).LineStyle(ls=>ls.Width(0)).MajorTickLines(mg=>mg.Width(0))
.MajorGridLines(mg=>mg.Width(0))
).PrimaryXAxis(px => px.Interval(1)
.ValueType(Syncfusion.EJ2.Charts.ValueType.Category).MajorGridLines(mg=>mg.Width(0))
).ChartArea(area => area.Border(br=>br.Color("transparent"))
).LegendSettings(lg => lg.reverse(true)).Title("Olympic Medal Counts - RIO").Load("load").Render()
public IActionResult Index()
{
List<ColumnChartData> chartData = new List<ColumnChartData>
{
new ColumnChartData { x= "USA", yValue= 46, yValue1=37, yValue2=38 },
new ColumnChartData { x= "GBR", yValue= 27, yValue1=23, yValue2=17 },
new ColumnChartData { x= "CHN", yValue= 26, yValue1=18, yValue2=26 }
};
ViewBag.dataSource = chartData;
return View();
}
public class ColumnChartData
{
public string x;
public double yValue;
public double yValue1;
public double yValue2;
}
Legend Shape
To change the legend icon shape, use the LegendShape
property in the Series
. By default, legend icon shape is SeriesType
.
@Html.EJS().AccumulationChart("container").Series(series =>
{
series.DataSource(ViewBag.dataSource)
.XName("xValue")
.YName("yValue")
.Name("Browser")
.Type(Syncfusion.EJ2.Charts.AccumulationType.Pie)
.LegendShape("Rectangle")
.ExplodeIndex(0).Add();
})
.EnableSmartLabels(true)
.Title("Mobile Browser Statistics")
.LegendSettings(ls => ls.Visible(true)).Render()
public ActionResult Index()
{
List<PieChartData> chartData = new List<PieChartData>
{
new PieChartData { xValue = "Chrome", yValue = 37 },
new PieChartData { xValue = "UC Browser", yValue = 17 },
new PieChartData { xValue = "iPhone", yValue = 19 },
new PieChartData { xValue = "Others", yValue = 4 },
new PieChartData { xValue = "Opera", yValue = 11 },
new PieChartData { xValue = "Android", yValue = 12 },
};
ViewBag.dataSource = chartData;
return View();
}
public class PieChartData
{
public string xValue;
public double yValue;
}
Legend Size
The legend size can be changed by using the Width
and Height
properties of the LegendSettings
.
@Html.EJS().AccumulationChart("container").Series(series =>
{
series.DataSource(ViewBag.dataSource)
.XName("xValue")
.YName("yValue")
.Name("Browser")
.Type(Syncfusion.EJ2.Charts.AccumulationType.Pie)
.ExplodeIndex(0).Add();
})
.EnableSmartLabels(true)
.Title("Mobile Browser Statistics")
.LegendSettings(ls => ls.Visible(true)
.Height("28%")
.Width("50%")
Legend.Border(ViewBag.border)).Render()
public ActionResult Index()
{
List<PieChartData> chartData = new List<PieChartData>
{
new PieChartData { xValue = "Chrome", yValue = 37 },
new PieChartData { xValue = "UC Browser", yValue = 17 },
new PieChartData { xValue = "iPhone", yValue = 19 },
new PieChartData { xValue = "Others", yValue = 4 },
new PieChartData { xValue = "Opera", yValue = 11 },
new PieChartData { xValue = "Android", yValue = 12 },
};
ViewBag.dataSource = chartData;
return View();
}
public class PieChartData
{
public string xValue;
public double yValue;
}
Legend Item Size
You can customize the size of the legend items by using the ShapeHeight
and ShapeWidth
properties.
@Html.EJS().AccumulationChart("container").Series(series =>
{
series.DataSource(ViewBag.dataSource)
.XName("xValue")
.YName("yValue")
.Name("Browser")
.Type(Syncfusion.EJ2.Charts.AccumulationType.Pie)
.ExplodeIndex(0).Add();
})
.EnableSmartLabels(true)
.Title("Mobile Browser Statistics")
.LegendSettings(ls => ls.Visible(true)
.ShapeHeight(15)
.ShapeWidth(15)).Render()
public ActionResult Index()
{
List<PieChartData> chartData = new List<PieChartData>
{
new PieChartData { xValue = "Chrome", yValue = 37 },
new PieChartData { xValue = "UC Browser", yValue = 17 },
new PieChartData { xValue = "iPhone", yValue = 19 },
new PieChartData { xValue = "Others", yValue = 4 },
new PieChartData { xValue = "Opera", yValue = 11 },
new PieChartData { xValue = "Android", yValue = 12 },
};
ViewBag.dataSource = chartData;
return View();
}
public class PieChartData
{
public string xValue;
public double yValue;
}
Paging for Legend
Paging will be enabled by default, when the legend items exceeds the legend bounds. You can view the each legend item by navigating between the pages using the navigation buttons.
@Html.EJS().AccumulationChart("container").Series(series =>
{
series.DataSource(ViewBag.dataSource)
.XName("xValue")
.YName("yValue")
.Name("Browser")
.Type(Syncfusion.EJ2.Charts.AccumulationType.Pie)
.ExplodeIndex(0).Add();
})
.EnableSmartLabels(true)
.Title("Mobile Browser Statistics")
.LegendSettings(ls => ls.Visible(true)
.Height("150")
.Width("80")).Render()
)
public ActionResult Index()
{
List<PieChartData> chartData = new List<PieChartData>
{
new PieChartData { xValue = "Chrome", yValue = 37 },
new PieChartData { xValue = "UC Browser", yValue = 17 },
new PieChartData { xValue = "iPhone", yValue = 19 },
new PieChartData { xValue = "Others", yValue = 4 },
new PieChartData { xValue = "Opera", yValue = 11 },
new PieChartData { xValue = "Android", yValue = 12 },
};
ViewBag.dataSource = chartData;
return View();
}
public class PieChartData
{
public string xValue;
public double yValue;
}
Legend Text Wrap
When the legend text exceeds the container, the text can be wrapped by using TextWrap Property. End user can also wrap the legend text based on the MaximumLabelWidth property.
@Html.EJS().AccumulationChart("container").Series(series =>
{
series.DataSource(ViewBag.dataSource)
.XName("xValue")
.YName("yValue")
.Name("Browser")
.Type(Syncfusion.EJ2.Charts.AccumulationType.Pie).Add();
})
.Title("Mobile Browser Statistics")
.LegendSettings(ls => ls.Visible(true)
.MaximumLabelWidth(50).TextWrap(Syncfusion.EJ2.Charts.TextWrap.Wrap)).Render()
)
public IActionResult Index()
{
List<PieChartData> chartData = new List<PieChartData>
{
new PieChartData { xValue = "Chrome", yValue = 37 },
new PieChartData { xValue = "UC Browser", yValue = 17 },
new PieChartData { xValue = "iPhone", yValue = 19 },
new PieChartData { xValue = "Others", yValue = 4 },
new PieChartData { xValue = "Opera", yValue = 11 },
new PieChartData { xValue = "Android", yValue = 12 },
};
ViewBag.dataSource = chartData;
return View();
}
public class PieChartData
{
public string xValue;
public double yValue;
}
Enable Animation
You can customize the animation while clicking legend by setting enableAnimation as true or false using EnableAnimation
property in Accumulation Chart.
@(Html.EJS().AccumulationChart("container")
.Series(sr =>
{
sr.Type(Syncfusion.EJ2.Charts.AccumulationType.Pie)
.XName("xValue")
.YName("yValue")
.Name("RIO")
.Radius("70%")
.StartAngle(0)
.EndAngle(360)
.InnerRadius("0%")
.DataSource(ViewBag.dataSource).Add();
})
.Tooltip(tp => tp.Enable(true))
.LegendSettings(leg => leg.Visible(true))
.EnableAnimation(true)
.Title("RIO Olympics Gold").Render()
)
public IActionResult Index()
{
List<GroupingChartData> chartData = new List<GroupingChartData>
{
new GroupingChartData { xValue = "China", yValue = 26, text = "China: 26" },
new GroupingChartData { xValue = "Russia", yValue = 19, text = "Russia: 19" },
new GroupingChartData { xValue = "Germany", yValue = 17, text = "Germany: 17" },
new GroupingChartData { xValue = "Japan", yValue = 12, text = "Japan: 12" },
new GroupingChartData { xValue = "France", yValue = 10, text = "France: 10" },
new GroupingChartData { xValue = "South Korea", yValue = 9, text = "South Korea: 9" },
new GroupingChartData { xValue = "Great Britain", yValue = 27, text = "Great Britain: 27" },
new GroupingChartData { xValue = "Italy", yValue = 8, text = "Italy: 8" },
new GroupingChartData { xValue = "Australia", yValue = 8, text = "Australia: 8" },
new GroupingChartData { xValue = "Netherlands", yValue = 8, text = "Netherlands: 8" },
new GroupingChartData { xValue = "Hungary", yValue = 8, text = "Hungary: 8" },
new GroupingChartData { xValue = "Brazil", yValue = 7, text = "Brazil: 7" },
new GroupingChartData { xValue = "Spain", yValue = 7, text = "Spain: 7" },
new GroupingChartData { xValue = "Kenya", yValue = 6, text = "Kenya: 6" }
};
ViewBag.dataSource = chartData;
return View();
}
public class GroupingChartData
{
public string xValue;
public double yValue;
public string text;
}
Legend Title
You can set title for legend using Title
property in LegendSettings
. You can also customize the FontStyle
, Size
, FontWeight
, Color
, TextAlignment
, FontFamily
, Opacity
and TextOverflow
of legend title. TitlePosition
is used to set the legend position in Top
, Left
and Right
position. MaximumTitleWidth
is used to set the width of the legend title. By default, it will be 100px
.
@Html.EJS().AccumulationChart("container").Series(series =>
{
series.DataSource(ViewBag.dataSource)
.XName("xValue")
.YName("yValue")
.Type(Syncfusion.EJ2.Charts.AccumulationType.Pie)
.Add();
})
.EnableSmartLabels(true)
.Title("Mobile Browser Statistics")
.LegendSettings(ls => ls.Visible(true)
.Position(Syncfusion.EJ2.Charts.LegendPosition.Bottom).Title("Browsers"))
.Render()
public ActionResult Index()
{
List<PieChartData> chartData = new List<PieChartData>
{
new PieChartData { xValue = "Chrome", yValue = 37 },
new PieChartData { xValue = "UC Browser", yValue = 17 },
new PieChartData { xValue = "iPhone", yValue = 19 },
new PieChartData { xValue = "Others", yValue = 4 },
new PieChartData { xValue = "Opera", yValue = 11 },
new PieChartData { xValue = "Android", yValue = 12 },
};
ViewBag.dataSource = chartData;
return View();
}
public class PieChartData
{
public string xValue;
public double yValue;
}
Arrow Page Navigation
By default, the page number will be enabled while legend paging. Now, you can disable that page number and also you can get left and right arrows for page navigation. You have to set false
value to EnablePages
to get this support.
@Html.EJS().AccumulationChart("container").Series(series =>
{
series.DataSource(ViewBag.dataSource)
.XName("xValue")
.YName("yValue")
.Type(Syncfusion.EJ2.Charts.AccumulationType.Pie)
.Add();
})
.EnableSmartLabels(true)
.Title("Mobile Browser Statistics")
.LegendSettings(ls => ls.Visible(true)
.Position(Syncfusion.EJ2.Charts.LegendPosition.Bottom).EnablePages(false))
.Render()
public ActionResult Index()
{
List<PieChartData> chartData = new List<PieChartData>
{
new PieChartData { xValue = "Chrome", yValue = 37 },
new PieChartData { xValue = "UC Browser", yValue = 17 },
new PieChartData { xValue = "iPhone", yValue = 19 },
new PieChartData { xValue = "Others", yValue = 4 },
new PieChartData { xValue = "Opera", yValue = 11 },
new PieChartData { xValue = "Android", yValue = 12 },
};
ViewBag.dataSource = chartData;
return View();
}
public class PieChartData
{
public string xValue;
public double yValue;
}
Legend Item Padding
The ItemPadding
property can be used to adjust the space between the legend items.
@Html.EJS().AccumulationChart("container").Series(series =>
{
series.DataSource(ViewBag.dataSource)
.XName("xValue")
.YName("yValue")
.Type(Syncfusion.EJ2.Charts.AccumulationType.Pie)
.Add();
})
.EnableSmartLabels(true)
.Title("Mobile Browser Statistics")
.LegendSettings(ls => ls.Visible(true)
.Position(Syncfusion.EJ2.Charts.LegendPosition.Bottom).EnablePages(false).ItemPadding(30))
.Render()
public ActionResult Index()
{
List<PieChartData> chartData = new List<PieChartData>
{
new PieChartData { xValue = "Chrome", yValue = 37 },
new PieChartData { xValue = "UC Browser", yValue = 17 },
new PieChartData { xValue = "iPhone", yValue = 19 },
new PieChartData { xValue = "Others", yValue = 4 },
new PieChartData { xValue = "Opera", yValue = 11 },
new PieChartData { xValue = "Android", yValue = 12 },
};
ViewBag.dataSource = chartData;
return View();
}
public class PieChartData
{
public string xValue;
public double yValue;
}