Axis Customization in ASP.NET MVC Chart Component
3 Jan 202324 minutes to read
Axis Crossing
An axis can be positioned in the chart area using CrossesAt
and CrossesInAxis
properties. The CrossesAt
property specifies the values (datetime, numeric, or logarithmic) at which the axis line has to be intersected with the vertical axis or vice-versa, and the CrossesInAxis
property specifies the axis name with which the axis line has to be crossed.
@Html.EJS().Chart("container").Series(series =>
{
series.Type(Syncfusion.EJ2.Charts.ChartSeriesType.Column).
Marker(ViewBag.Marker).
XName("x").
YName("yValue").
DataSource(ViewBag.dataSource).
Name("Gold").
Width(2).Add();
}).
PrimaryXAxis(px => px.Interval(1).ValueType(Syncfusion.EJ2.Charts.ValueType.Category). crossesAt(15)).
PrimaryXAxis(px => px.Interval(1).ValueType(Syncfusion.EJ2.Charts.ValueType.Category). crossesAt(5)).
Title("Olympic Medal Counts - RIO").Render()
public IActionResult Index()
{
List<ColumnChartData> chartData = new List<ColumnChartData>
{
new ColumnChartData { x= "USA", yValue= 46 },
new ColumnChartData { x= "GBR", yValue= 27 },
new ColumnChartData { x= "CHN", yValue= 26 },
new ColumnChartData { x= "UK", yValue= 26 },
new ColumnChartData { x= "AUS", yValue= 26 },
new ColumnChartData { x= "IND", yValue= 26 },
new ColumnChartData { x= "DEN", yValue= 26 },
new ColumnChartData { x= "MEX", yValue= 26 },
};
ViewBag.dataSource = chartData;
return View();
}
public class ColumnChartData
{
public string x;
public double yValue;
}
Title
You can add a title to the axis using Title
property to provide quick information to the user about the data plotted in the axis.
@Html.EJS().Chart("container").Series(series =>
{
series.Type(Syncfusion.EJ2.Charts.ChartSeriesType.Column).
Marker(ViewBag.Marker).
XName("x").
YName("yValue").
DataSource(ViewBag.dataSource).
Name("Gold").
Width(2).Add();
}).
PrimaryXAxis(px => px.Interval(1).ValueType(Syncfusion.EJ2.Charts.ValueType.Category).TitleStyle(ViewBag.titleStyle)).
Title("Olympic Medal Counts - RIO").Render()
public IActionResult Index()
{
List<ColumnChartData> chartData = new List<ColumnChartData>
{
new ColumnChartData { x= "USA", yValue= 46 },
new ColumnChartData { x= "GBR", yValue= 27 },
new ColumnChartData { x= "CHN", yValue= 26 },
new ColumnChartData { x= "UK", yValue= 26 },
new ColumnChartData { x= "AUS", yValue= 26 },
new ColumnChartData { x= "IND", yValue= 26 },
new ColumnChartData { x= "DEN", yValue= 26 },
new ColumnChartData { x= "MEX", yValue= 26 },
};
ViewBag.dataSource = chartData;
return View();
}
public class ColumnChartData
{
public string x;
public double yValue;
}
Title Rotation
By using the titleRotation
property, you can rotate the axis title from 0 to 360 degree.
@Html.EJS().Chart("container").Series(series =>
{
series.Type(Syncfusion.EJ2.Charts.ChartSeriesType.Column).
Marker(ViewBag.Marker).
XName("x").
YName("yValue").
DataSource(ViewBag.dataSource).
Name("Gold").
Width(2).Add();
}).
PrimaryXAxis(px => px.Interval(1).ValueType(Syncfusion.EJ2.Charts.ValueType.Category).TitleStyle(ViewBag.titleStyle).TitleRotation(90)).
Title("Olympic Medal Counts - RIO").Render()
public IActionResult Index()
{
List<ColumnChartData> chartData = new List<ColumnChartData>
{
new ColumnChartData { x= "USA", yValue= 46 },
new ColumnChartData { x= "GBR", yValue= 27 },
new ColumnChartData { x= "CHN", yValue= 26 },
new ColumnChartData { x= "UK", yValue= 26 },
new ColumnChartData { x= "AUS", yValue= 26 },
new ColumnChartData { x= "IND", yValue= 26 },
new ColumnChartData { x= "DEN", yValue= 26 },
new ColumnChartData { x= "MEX", yValue= 26 },
};
ViewBag.dataSource = chartData;
return View();
}
public class ColumnChartData
{
public string x;
public double yValue;
}
Tick Lines Customization
You can customize the Width
, Color
and Size
of the minor and major tick lines, using MajorTickLines
and MinorTickLines
properties in the axis.
@Html.EJS().Chart("container").PrimaryXAxis(px => px.Title("Years").
ValueType(Syncfusion.EJ2.Charts.ValueType.Category)
.MajorTickLines(mg => mg.Color("blue").Width(2))).Series(series =>
{
series.Type(
Syncfusion.EJ2.Charts.ChartSeriesType.Column
).DataSource(ViewBag.dataSource).Name("Product X").XName("x").YName("yValue").Add();
}
).PrimaryYAxis(py => py.Title("Profit ($)")
.MajorTickLines(mg => mg.Color("blue").Width(2))).Title("Sales History of Product X").Render()
public IActionResult Index()
{
List<ColumnChartData> chartData = new List<ColumnChartData>
{
new ColumnChartData { x= "John", yValue= 10000, yValue1=37, yValue2=38 },
new ColumnChartData { x= "Jake", yValue= 12000, yValue1=23, yValue2=17 },
new ColumnChartData { x= "Peter", yValue= 18000, yValue1=18, yValue2=26 },
new ColumnChartData { x= "James", yValue= 11000, yValue1=18, yValue2=26 }
};
ViewBag.dataSource = chartData;
return View();
}
public class ColumnChartData
{
public string x;
public double yValue;
public double yValue1;
public double yValue2;
}
Grid Lines Customization
You can customize the Width
, Color
and DashArray
of the minor and major grid lines, using MajorGridLines
and MinorGridLines
properties in the axis.
@Html.EJS().Chart("container").PrimaryXAxis(px => px.Title("Years").
ValueType(Syncfusion.EJ2.Charts.ValueType.Category)
.MajorGridLines(mg => mg.Color("blue").Width(2))).Series(series =>
{
series.Type(
Syncfusion.EJ2.Charts.ChartSeriesType.Column
).DataSource(ViewBag.dataSource).Name("Product X").XName("x").YName("yValue").Add();
}
).PrimaryYAxis(py => py.Title("Profit ($)")
.MajorGridLines(mg => mg.Color("blue").Width(2))).Title("Sales History of Product X").Render()
public IActionResult Index()
{
List<ColumnChartData> chartData = new List<ColumnChartData>
{
new ColumnChartData { x= "John", yValue= 10000, yValue1=37, yValue2=38 },
new ColumnChartData { x= "Jake", yValue= 12000, yValue1=23, yValue2=17 },
new ColumnChartData { x= "Peter", yValue= 18000, yValue1=18, yValue2=26 },
new ColumnChartData { x= "James", yValue= 11000, yValue1=18, yValue2=26 }
};
ViewBag.dataSource = chartData;
return View();
}
public class ColumnChartData
{
public string x;
public double yValue;
public double yValue1;
public double yValue2;
}
Multiple Axis
In addition to primary X and Y axis, we can add n number of axis to the chart. Series can be associated with this axis, by mapping with axis’s unique name.
@Html.EJS().Chart("container").Series(series =>
{
series.Type(Syncfusion.EJ2.Charts.ChartSeriesType.Column).XName("x").YName("y").DataSource(ViewBag.dataSource).Name("Germany").Add();
series.Type(Syncfusion.EJ2.Charts.ChartSeriesType.Line).YAxisName("yAxis").XName("x").Marker(ViewBag.marker).YName("y1").DataSource(ViewBag.dataSource).Name("Japan").Add();
}).PrimaryXAxis(px => px.ValueType(Syncfusion.EJ2.Charts.ValueType.Category)
.Interval(1).LabelIntersectAction(Syncfusion.EJ2.Charts.LabelIntersectAction.Rotate90)
).Axes(ax =>
{
ax.OpposedPosition(true).RowIndex(0).Minimum(24).Maximum(36).Interval(2).Name("yAxis").LabelFormat("{value}°C").Add();
}
)
).Title("Weather Condition JPN vs DEU").ChartArea(area => area.Border(ViewBag.ChartBorder)).Tooltip(tt => tt.Enable(true)).LegendSettings(lg=>lg.Visible(false)).Render()
public ActionResult Index()
{
List<MultipleAxesChartData> chartData = new List<MultipleAxesChartData>
{
new MultipleAxesChartData { x = "Sun", y = 35, y1 = 30 },
new MultipleAxesChartData { x = "Mon", y = 40, y1 = 28 },
new MultipleAxesChartData { x = "Tue", y = 80, y1 = 29 },
new MultipleAxesChartData { x = "Wed", y = 70, y1 = 30 },
new MultipleAxesChartData { x = "Thu", y = 65, y1 = 33 },
new MultipleAxesChartData { x = "Fri", y = 55, y1 = 32 },
new MultipleAxesChartData { x = "Sat", y = 50, y1 = 34 }
};
ViewBag.dataSource = chartData;
return View();
}
public class MultipleAxesChartData
{
public string x;
public double y;
public double y1;
}
Inversed Axis
When an axis is inversed, highest value of the axis comes closer to origin and vice versa. To place an axis in inversed manner set this property IsInversed
to true.
@Html.EJS().Chart("container").Series(series =>
{
series.Type(Syncfusion.EJ2.Charts.ChartSeriesType.Line).
Marker(ViewBag.Marker).
XName("x").
YName("yValue").
DataSource(ViewBag.dataSource).
Name("Gold").
Width(2).Add();
}).
PrimaryXAxis(px => px.Interval(1).ValueType(Syncfusion.EJ2.Charts.ValueType.Category).IsInversed(true)).
Title("Olympic Medal Counts - RIO").Render()
public ActionResult Index()
{
List<MultipleAxesChartData> chartData = new List<MultipleAxesChartData>
{
new MultipleAxesChartData { x = "Sun", y = 35, y1 = 30 },
new MultipleAxesChartData { x = "Mon", y = 40, y1 = 28 },
new MultipleAxesChartData { x = "Tue", y = 80, y1 = 29 },
new MultipleAxesChartData { x = "Wed", y = 70, y1 = 30 },
new MultipleAxesChartData { x = "Thu", y = 65, y1 = 33 },
new MultipleAxesChartData { x = "Fri", y = 55, y1 = 32 },
new MultipleAxesChartData { x = "Sat", y = 50, y1 = 34 }
};
ViewBag.dataSource = chartData;
return View();
}
public class MultipleAxesChartData
{
public string x;
public double y;
public double y1;
}
Opposed Position
To place an axis opposite from its original position, set OpposedPosition
property of the axis to true.
@Html.EJS().Chart("container").Series(series =>
{
series.Type(Syncfusion.EJ2.Charts.ChartSeriesType.Column).
Marker(ViewBag.Marker).
XName("x").
YName("yValue").
DataSource(ViewBag.dataSource).
Name("Gold").
Width(2).Add();
}).
PrimaryXAxis(px => px.Interval(1).ValueType(Syncfusion.EJ2.Charts.ValueType.Category).OpposedPosition(true)).
Title("Olympic Medal Counts - RIO").Render()
public ActionResult Index()
{
List<MultipleAxesChartData> chartData = new List<MultipleAxesChartData>
{
new MultipleAxesChartData { x = "Sun", y = 35, y1 = 30 },
new MultipleAxesChartData { x = "Mon", y = 40, y1 = 28 },
new MultipleAxesChartData { x = "Tue", y = 80, y1 = 29 },
new MultipleAxesChartData { x = "Wed", y = 70, y1 = 30 },
new MultipleAxesChartData { x = "Thu", y = 65, y1 = 33 },
new MultipleAxesChartData { x = "Fri", y = 55, y1 = 32 },
new MultipleAxesChartData { x = "Sat", y = 50, y1 = 34 }
};
ViewBag.dataSource = chartData;
return View();
}
public class MultipleAxesChartData
{
public string x;
public double y;
public double y1;
}