diff --git a/BlazorBootstrap.Demo.RCL/Components/Pages/Charts/LineCharts/LineChartDocumentation.razor b/BlazorBootstrap.Demo.RCL/Components/Pages/Charts/LineCharts/LineChartDocumentation.razor index 42c108642..fb34e2eb5 100644 --- a/BlazorBootstrap.Demo.RCL/Components/Pages/Charts/LineCharts/LineChartDocumentation.razor +++ b/BlazorBootstrap.Demo.RCL/Components/Pages/Charts/LineCharts/LineChartDocumentation.razor @@ -6,24 +6,24 @@

Blazor Line Chart

- A Blazor Bootstrap line chart component is a graphical representation of data that uses a series of connected points to show how the data changes over time. - It is a type of x-y chart, where the x-axis represents the independent variable, such as time, and the y-axis represents the dependent variable, such as the value. + A Blazor Bootstrap line chart component is a graphical representation of data that uses a series of connected points to show how the data changes over time. + It is a type of x-y chart, where the x-axis represents the independent variable, such as time, and the y-axis represents the dependent variable, such as the value.
- Refer to the getting started guide for setting up charts. + Refer to the getting started guide for setting up charts.
- In the following example, a categorical 12-color palette is used. + In the following example, a categorical 12-color palette is used.
- For data visualization, you can use the predefined palettes ColorUtility.CategoricalTwelveColors for a 12-color palette and ColorUtility.CategoricalSixColors for a 6-color palette. - These palettes offer a range of distinct and visually appealing colors that can be applied to represent different categories or data elements in your visualizations. + For data visualization, you can use the predefined palettes ColorUtility.CategoricalTwelveColors for a 12-color palette and ColorUtility.CategoricalSixColors for a 6-color palette. + These palettes offer a range of distinct and visually appealing colors that can be applied to represent different categories or data elements in your visualizations.
@@ -31,8 +31,8 @@
- By default, the chart is using the default locale of the platform on which it is running. - In the following example, you will see the chart in the German locale (de_DE). + By default, the chart is using the default locale of the platform on which it is running. + In the following example, you will see the chart in the German locale (de_DE).
@@ -45,12 +45,15 @@ + + + @code { - private readonly string pageUrl = "/charts/line-chart"; - private readonly string title = "Blazor Line Chart"; - private readonly string description = "A Blazor Bootstrap line chart component is a graphical representation of data that uses a series of connected points to show how the data changes over time. It is a type of x-y chart, where the x-axis represents the independent variable, such as time, and the y-axis represents the dependent variable, such as the value."; - private readonly string imageUrl = "https://i.imgur.com/8b7jH0D.png"; + private readonly string pageUrl = "/charts/line-chart"; + private readonly string title = "Blazor Line Chart"; + private readonly string description = "A Blazor Bootstrap line chart component is a graphical representation of data that uses a series of connected points to show how the data changes over time. It is a type of x-y chart, where the x-axis represents the independent variable, such as time, and the y-axis represents the dependent variable, such as the value."; + private readonly string imageUrl = "https://i.imgur.com/8b7jH0D.png"; } \ No newline at end of file diff --git a/BlazorBootstrap.Demo.RCL/Components/Pages/Charts/LineCharts/LineChart_Demo_06_Secondary_Axis.razor b/BlazorBootstrap.Demo.RCL/Components/Pages/Charts/LineCharts/LineChart_Demo_06_Secondary_Axis.razor new file mode 100644 index 000000000..7c8b6af8c --- /dev/null +++ b/BlazorBootstrap.Demo.RCL/Components/Pages/Charts/LineCharts/LineChart_Demo_06_Secondary_Axis.razor @@ -0,0 +1,102 @@ + + +@code { + private LineChart lineChart = default!; + private LineChartOptions lineChartOptions = default!; + private ChartData chartData = default!; + + private int datasetsCount; + private int labelsCount; + + private Random random = new(); + + protected override void OnInitialized() + { + chartData = new ChartData { Labels = GetDefaultDataLabels(6), Datasets = GetDefaultDataSets() }; + lineChartOptions = new() { Responsive = true, Interaction = new Interaction { Mode = InteractionMode.Index } }; + + lineChartOptions.Scales.Y!.Title = new ChartAxesTitle() { Display = true, Text = "Primary y-axis" }; + lineChartOptions.Scales.Y!.Max = 100; + // Set secondary y-axis + lineChartOptions.Scales.SecondaryY!.Display = true; + lineChartOptions.Scales.SecondaryY!.Title = new ChartAxesTitle() { Display = true, Text = "Secondary y-axis" }; + lineChartOptions.Scales.SecondaryY!.Max = 500; + } + + protected override async Task OnAfterRenderAsync(bool firstRender) + { + if (firstRender) + { + await lineChart.InitializeAsync(chartData, lineChartOptions); + } + await base.OnAfterRenderAsync(firstRender); + } + + #region Data Preparation + + private List GetDefaultDataSets() + { + var datasets = new List(); + var primaryDataset = GetRandomLineChartDataset(100); + var secondaryDataset = GetRandomLineChartDataset(500); + + // Move the dataset to the secondary y-axis + secondaryDataset.OnSecondaryYAxis = true; + + datasets.Add(primaryDataset); + datasets.Add(secondaryDataset); + + return datasets; + } + + private LineChartDataset GetRandomLineChartDataset(int max) + { + var c = ColorUtility.CategoricalTwelveColors[datasetsCount].ToColor(); + + datasetsCount += 1; + + return new LineChartDataset + { + Label = $"Team {datasetsCount}", + Data = GetRandomData(max), + BackgroundColor = c.ToRgbString(), + BorderColor = c.ToRgbString(), + BorderWidth = 2, + HoverBorderWidth = 4, + // PointBackgroundColor = c.ToRgbString(), + // PointRadius = 0, // hide points + // PointHoverRadius = 4, + }; + } + + private List GetRandomData(int max) + { + var data = new List(); + for (var index = 0; index < labelsCount; index++) + { + data.Add(random.Next(max)); + } + + return data; + } + + private List GetDefaultDataLabels(int numberOfLabels) + { + var labels = new List(); + for (var index = 0; index < numberOfLabels; index++) + { + labels.Add(GetNextDataLabel()); + } + + return labels; + } + + private string GetNextDataLabel() + { + labelsCount += 1; + return $"Day {labelsCount}"; + } + + #endregion Data Preparation + +} \ No newline at end of file diff --git a/blazorbootstrap/Models/Charts/ChartDataset/LineChart/LineChartDataset.cs b/blazorbootstrap/Models/Charts/ChartDataset/LineChart/LineChartDataset.cs index 8c035851b..0c49e89b7 100644 --- a/blazorbootstrap/Models/Charts/ChartDataset/LineChart/LineChartDataset.cs +++ b/blazorbootstrap/Models/Charts/ChartDataset/LineChart/LineChartDataset.cs @@ -282,6 +282,12 @@ public LineChartDataset FillToValue(double value) [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] public string? IndexAxis { get; set; } + /// + /// Determines if the dataset is plotted on the secondary y-axis. + /// + [JsonIgnore] + public bool OnSecondaryYAxis { get; set; } + /// /// The fill color for points. /// @@ -438,12 +444,13 @@ public LineChartDataset FillToValue(double value) /// /// The ID of the y axis to plot this dataset on. + /// Change this through the property. /// /// /// Default value is 'first y axis'. /// [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] - public string? YAxisID { get; set; } + public string? YAxisID => OnSecondaryYAxis ? "Y1" : null; #endregion } diff --git a/blazorbootstrap/Models/Charts/ChartOptions/ChartOptions.cs b/blazorbootstrap/Models/Charts/ChartOptions/ChartOptions.cs index 686eebea3..10275d1d7 100644 --- a/blazorbootstrap/Models/Charts/ChartOptions/ChartOptions.cs +++ b/blazorbootstrap/Models/Charts/ChartOptions/ChartOptions.cs @@ -3,7 +3,7 @@ public interface IChartOptions { } /// -/// +/// /// public class ChartOptions : IChartOptions { @@ -183,9 +183,13 @@ public class Scales { #region Properties, Indexers - [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] public ChartAxes? X { get; set; } = new(); + [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] + [JsonPropertyOrder(0)] + public ChartAxes? X { get; set; } = new(); - [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] public ChartAxes? Y { get; set; } = new(); + [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] + [JsonPropertyOrder(1)] + public ChartAxes? Y { get; set; } = new(); #endregion } @@ -215,6 +219,11 @@ public class ChartAxes [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] public ChartAxesBorder? Border { get; set; } + /// + /// Gets or sets whether the axis is displayed. + /// + public bool Display { get; set; } = true; + /// /// Gets or sets the grid configuration. /// @@ -233,6 +242,11 @@ public class ChartAxes [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] public double? Min { get; set; } + /// + /// Gets or sets the position of the axis. + /// + public string Position { get; set; } = "left"; + /// /// Should the data be stacked. /// By default data is not stacked. diff --git a/blazorbootstrap/Models/Charts/ChartOptions/LineChartOptions.cs b/blazorbootstrap/Models/Charts/ChartOptions/LineChartOptions.cs index 58091b12a..df2784ec1 100644 --- a/blazorbootstrap/Models/Charts/ChartOptions/LineChartOptions.cs +++ b/blazorbootstrap/Models/Charts/ChartOptions/LineChartOptions.cs @@ -12,7 +12,7 @@ public class LineChartOptions : ChartOptions /// The base axis of the dataset. 'x' for horizontal lines and 'y' for vertical lines. /// /// - /// Default value is . + /// Default value is . /// [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] public string? IndexAxis { get; set; } @@ -23,7 +23,7 @@ public class LineChartOptions : ChartOptions public LineChartPlugins Plugins { get; set; } = new(); - public Scales Scales { get; set; } = new(); + public LineChartScales Scales { get; set; } = new(); #endregion diff --git a/blazorbootstrap/Models/Charts/ChartOptions/LineChartScales.cs b/blazorbootstrap/Models/Charts/ChartOptions/LineChartScales.cs new file mode 100644 index 000000000..b321ec059 --- /dev/null +++ b/blazorbootstrap/Models/Charts/ChartOptions/LineChartScales.cs @@ -0,0 +1,13 @@ +namespace BlazorBootstrap; + +public class LineChartScales : Scales +{ + #region Properties, Indexers + + [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] + [JsonPropertyOrder(2)] + [JsonPropertyName("Y1")] + public ChartAxes? SecondaryY { get; set; } = new() { Display = false, Position = "right" }; + + #endregion +}