Skip to content

Commit

Permalink
Make everything nullable, remove hardcoded default values
Browse files Browse the repository at this point in the history
Chart.js will chose the correct default and even respect user-defined defaults when the property is not specified. Therefore we shouldn't serialize anything unless there's a non-default value assigned to it (aka make the properties nullable). This is also a performance improvement since less data has to be serialized and transferred but that gain is tiny.
This is of course a breaking change but shouldn't affect much user code.
  • Loading branch information
Joelius300 committed Oct 18, 2020
1 parent 6a9d58e commit 6c45391
Show file tree
Hide file tree
Showing 27 changed files with 205 additions and 207 deletions.
39 changes: 21 additions & 18 deletions src/ChartJs.Blazor/BarChart/Axes/BarCategoryAxis.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,28 +10,16 @@ namespace ChartJs.Blazor.BarChart.Axes
/// </summary>
public class BarCategoryAxis : CategoryAxis
{
/// <summary>
/// Creates a new instance of the <see cref="BarCategoryAxis"/> class.
/// </summary>
public BarCategoryAxis()
{
GridLines = new GridLines()
{
OffsetGridLines = true
};
Offset = true;
}

/// <summary>
/// Gets or sets the percentage (0-1) of the available width each bar should be within the category width.
/// 1.0 will take the whole category width and put the bars right next to each other.
/// </summary>
public double BarPercentage { get; set; } = 0.9;
public double? BarPercentage { get; set; }

/// <summary>
/// Gets or sets the percentage (0-1) of the available width each category should be within the sample width.
/// </summary>
public double CategoryPercentage { get; set; } = 0.8;
public double? CategoryPercentage { get; set; }

/// <summary>
/// Gets or sets the width of each bar in pixels.
Expand All @@ -58,10 +46,25 @@ public BarCategoryAxis()
/// If <see langword="false"/>, the grid line will go right down the middle of the bars.
/// <para>Changing this value will directly affect <see cref="GridLines.OffsetGridLines"/> of the property <see cref="CartesianAxis.GridLines"/> in this instance.</para>
/// </summary>
public bool OffsetGridLines
public bool? OffsetGridLines
{
get => GridLines.OffsetGridLines;
set => GridLines.OffsetGridLines = value;
get => GridLines?.OffsetGridLines;
set
{
if (GridLines == null)
{
if (value == null)
{
return;
}
else
{
GridLines = new GridLines();
}
}

GridLines.OffsetGridLines = value;
}
}

/// <summary>
Expand All @@ -70,7 +73,7 @@ public bool OffsetGridLines
/// Stacked bar charts can be used to show how one data series is made up of a number of smaller pieces.
/// <para>As per documentation <a href="https://www.chartjs.org/docs/latest/charts/bar.html#stacked-bar-chart">here (Chart.js)</a>.</para>
/// </summary>
public bool Stacked { get; set; } = false;
public bool? Stacked { get; set; }
}
}

39 changes: 21 additions & 18 deletions src/ChartJs.Blazor/BarChart/Axes/BarLinearCartesianAxis.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,28 +9,16 @@ namespace ChartJs.Blazor.BarChart.Axes
/// </summary>
public class BarLinearCartesianAxis : LinearCartesianAxis
{
/// <summary>
/// Creates a new instance of the <see cref="BarLinearCartesianAxis"/> class.
/// </summary>
public BarLinearCartesianAxis()
{
GridLines = new GridLines()
{
OffsetGridLines = false
};
Offset = false;
}

/// <summary>
/// Gets or sets the percentage (0-1) of the available width each bar should be within the category width.
/// 1.0 will take the whole category width and put the bars right next to each other.
/// </summary>
public double BarPercentage { get; set; } = 0.9;
public double? BarPercentage { get; set; }

/// <summary>
/// Gets or sets the percentage (0-1) of the available width each category should be within the sample width.
/// </summary>
public double CategoryPercentage { get; set; } = 0.8;
public double? CategoryPercentage { get; set; }

/// <summary>
/// Gets or sets the width of each bar in pixels.
Expand All @@ -57,10 +45,25 @@ public BarLinearCartesianAxis()
/// If <see langword="false"/>, the grid line will go right down the middle of the bars.
/// <para>Changing this value will directly affect <see cref="GridLines.OffsetGridLines"/> of the property <see cref="CartesianAxis.GridLines"/> in this instance.</para>
/// </summary>
public bool OffsetGridLines
public bool? OffsetGridLines
{
get => GridLines.OffsetGridLines;
set => GridLines.OffsetGridLines = value;
get => GridLines?.OffsetGridLines;
set
{
if (GridLines == null)
{
if (value == null)
{
return;
}
else
{
GridLines = new GridLines();
}
}

GridLines.OffsetGridLines = value;
}
}

/// <summary>
Expand All @@ -69,7 +72,7 @@ public bool OffsetGridLines
/// Stacked bar charts can be used to show how one data series is made up of a number of smaller pieces.
/// <para>As per documentation <a href="https://www.chartjs.org/docs/latest/charts/bar.html#stacked-bar-chart">here (Chart.js)</a>.</para>
/// </summary>
public bool Stacked { get; set; } = false;
public bool? Stacked { get; set; }
}
}

39 changes: 21 additions & 18 deletions src/ChartJs.Blazor/BarChart/Axes/BarLogarithmicAxis.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,28 +9,16 @@ namespace ChartJs.Blazor.BarChart.Axes
/// </summary>
public class BarLogarithmicAxis : LogarithmicAxis
{
/// <summary>
/// Creates a new instance of the <see cref="BarLogarithmicAxis"/> class.
/// </summary>
public BarLogarithmicAxis()
{
GridLines = new GridLines()
{
OffsetGridLines = false
};
Offset = false;
}

/// <summary>
/// Gets or sets the percentage (0-1) of the available width each bar should be within the category width.
/// 1.0 will take the whole category width and put the bars right next to each other.
/// </summary>
public double BarPercentage { get; set; } = 0.9;
public double? BarPercentage { get; set; }

/// <summary>
/// Gets or sets the percentage (0-1) of the available width each category should be within the sample width.
/// </summary>
public double CategoryPercentage { get; set; } = 0.8;
public double? CategoryPercentage { get; set; }

/// <summary>
/// Gets or sets the width of each bar in pixels.
Expand All @@ -57,10 +45,25 @@ public BarLogarithmicAxis()
/// If <see langword="false"/>, the grid line will go right down the middle of the bars.
/// <para>Changing this value will directly affect <see cref="GridLines.OffsetGridLines"/> of the property <see cref="CartesianAxis.GridLines"/> in this instance.</para>
/// </summary>
public bool OffsetGridLines
public bool? OffsetGridLines
{
get => GridLines.OffsetGridLines;
set => GridLines.OffsetGridLines = value;
get => GridLines?.OffsetGridLines;
set
{
if (GridLines == null)
{
if (value == null)
{
return;
}
else
{
GridLines = new GridLines();
}
}

GridLines.OffsetGridLines = value;
}
}

/// <summary>
Expand All @@ -69,7 +72,7 @@ public bool OffsetGridLines
/// Stacked bar charts can be used to show how one data series is made up of a number of smaller pieces.
/// <para>As per documentation <a href="https://www.chartjs.org/docs/latest/charts/bar.html#stacked-bar-chart">here (Chart.js)</a>.</para>
/// </summary>
public bool Stacked { get; set; } = false;
public bool? Stacked { get; set; }
}
}

39 changes: 21 additions & 18 deletions src/ChartJs.Blazor/BarChart/Axes/BarTimeAxis.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,28 +9,16 @@ namespace ChartJs.Blazor.BarChart.Axes
/// </summary>
public class BarTimeAxis : TimeAxis
{
/// <summary>
/// Creates a new instance of the <see cref="BarTimeAxis"/> class.
/// </summary>
public BarTimeAxis()
{
GridLines = new GridLines()
{
OffsetGridLines = false
};
Offset = false;
}

/// <summary>
/// Gets or sets the percentage (0-1) of the available width each bar should be within the category width.
/// 1.0 will take the whole category width and put the bars right next to each other.
/// </summary>
public double BarPercentage { get; set; } = 0.9;
public double? BarPercentage { get; set; }

/// <summary>
/// Gets or sets the percentage (0-1) of the available width each category should be within the sample width.
/// </summary>
public double CategoryPercentage { get; set; } = 0.8;
public double? CategoryPercentage { get; set; }

/// <summary>
/// Gets or sets the width of each bar in pixels.
Expand All @@ -57,10 +45,25 @@ public BarTimeAxis()
/// If <see langword="false"/>, the grid line will go right down the middle of the bars.
/// <para>Changing this value will directly affect <see cref="GridLines.OffsetGridLines"/> of the property <see cref="CartesianAxis.GridLines"/> in this instance.</para>
/// </summary>
public bool OffsetGridLines
public bool? OffsetGridLines
{
get => GridLines.OffsetGridLines;
set => GridLines.OffsetGridLines = value;
get => GridLines?.OffsetGridLines;
set
{
if (GridLines == null)
{
if (value == null)
{
return;
}
else
{
GridLines = new GridLines();
}
}

GridLines.OffsetGridLines = value;
}
}

/// <summary>
Expand All @@ -69,7 +72,7 @@ public bool OffsetGridLines
/// Stacked bar charts can be used to show how one data series is made up of a number of smaller pieces.
/// <para>As per documentation <a href="https://www.chartjs.org/docs/latest/charts/bar.html#stacked-bar-chart">here (Chart.js)</a>.</para>
/// </summary>
public bool Stacked { get; set; } = false;
public bool? Stacked { get; set; }
}
}

39 changes: 21 additions & 18 deletions src/ChartJs.Blazor/BarChart/Axes/ExtendedCartesianAxes.tt
Original file line number Diff line number Diff line change
Expand Up @@ -21,28 +21,16 @@ namespace ChartJs.Blazor.BarChart.Axes
/// </summary>
public class <#= className #> : <#= baseName #>
{
/// <summary>
/// Creates a new instance of the <see cref="<#= className #>"/> class.
/// </summary>
public <#= className #>()
{
GridLines = new GridLines()
{
OffsetGridLines = <#= className.Contains("Category").ToString().ToLower() #>
};
Offset = <#= className.Contains("Category").ToString().ToLower() #>;
}

/// <summary>
/// Gets or sets the percentage (0-1) of the available width each bar should be within the category width.
/// 1.0 will take the whole category width and put the bars right next to each other.
/// </summary>
public double BarPercentage { get; set; } = 0.9;
public double? BarPercentage { get; set; }

/// <summary>
/// Gets or sets the percentage (0-1) of the available width each category should be within the sample width.
/// </summary>
public double CategoryPercentage { get; set; } = 0.8;
public double? CategoryPercentage { get; set; }

/// <summary>
/// Gets or sets the width of each bar in pixels.
Expand All @@ -69,10 +57,25 @@ namespace ChartJs.Blazor.BarChart.Axes
/// If <see langword="false"/>, the grid line will go right down the middle of the bars.
/// <para>Changing this value will directly affect <see cref="GridLines.OffsetGridLines"/> of the property <see cref="CartesianAxis.GridLines"/> in this instance.</para>
/// </summary>
public bool OffsetGridLines
public bool? OffsetGridLines
{
get => GridLines.OffsetGridLines;
set => GridLines.OffsetGridLines = value;
get => GridLines?.OffsetGridLines;
set
{
if (GridLines == null)
{
if (value == null)
{
return;
}
else
{
GridLines = new GridLines();
}
}

GridLines.OffsetGridLines = value;
}
}

/// <summary>
Expand All @@ -81,7 +84,7 @@ namespace ChartJs.Blazor.BarChart.Axes
/// Stacked bar charts can be used to show how one data series is made up of a number of smaller pieces.
/// <para>As per documentation <a href="https://www.chartjs.org/docs/latest/charts/bar.html#stacked-bar-chart">here (Chart.js)</a>.</para>
/// </summary>
public bool Stacked { get; set; } = false;
public bool? Stacked { get; set; }
}
}

Expand Down
6 changes: 3 additions & 3 deletions src/ChartJs.Blazor/Common/Axes/AngleLines.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public class AngleLines
/// <summary>
/// Gets or sets the value indicating whether the angle line is displayed or not.
/// </summary>
public bool Display { get; set; } = true;
public bool? Display { get; set; }

/// <summary>
/// Gets or sets the color of the angled lines.
Expand All @@ -22,7 +22,7 @@ public class AngleLines
/// <summary>
/// Gets or sets the width of the angled lines.
/// </summary>
public int LineWidth { get; set; } = 1;
public int? LineWidth { get; set; }

/// <summary>
/// Gets or sets the length and spacing of dashes of the angled lines.
Expand All @@ -34,6 +34,6 @@ public class AngleLines
/// Gets or sets the offset for line dashes.
/// <para>As per documentation <a href="https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/lineDashOffset">here (MDN)</a>.</para>
/// </summary>
public double BorderDashOffset { get; set; }
public double? BorderDashOffset { get; set; }
}
}
2 changes: 1 addition & 1 deletion src/ChartJs.Blazor/Common/Axes/Axis.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public abstract class Axis
/// Controls the axis global visibility (visible when <see cref="AxisDisplay.True"/>, hidden when <see cref="AxisDisplay.False"/>).
/// When display: <see cref="AxisDisplay.Auto"/>, the axis is visible only if at least one associated dataset is visible.
/// </summary>
public AxisDisplay Display { get; set; } = AxisDisplay.True;
public AxisDisplay Display { get; set; }

/// <summary>
/// The weight used to sort the axis. Higher weights are further away from the chart area.
Expand Down
Loading

0 comments on commit 6c45391

Please sign in to comment.