-
Notifications
You must be signed in to change notification settings - Fork 105
fix(data-labels-style): fixed serializations of DataLabels.Style.Colors #632
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+219
−1
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
48 changes: 48 additions & 0 deletions
48
docs/BlazorApexCharts.Docs/Components/ChartTypes/HeatmapCharts/DataLabelColors.razor
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| <DemoContainer> | ||
| <ApexChart TItem="SupportIncident" | ||
| Title="Incident Severity" | ||
| Options="options"> | ||
|
|
||
| @foreach (var source in incidents.GroupBy(e => e.Source).OrderBy(e => e.Key)) | ||
| { | ||
| <ApexPointSeries TItem="SupportIncident" | ||
| Items="source.OrderBy(e=>e.WeekNumber)" | ||
| Name="@source.Key.ToString()" | ||
| SeriesType="SeriesType.Heatmap" | ||
| Color="@GetColor(source.Key)" | ||
| ShowDataLabels="true" | ||
| XValue="@(e => e.WeekName)" | ||
| YAggregate="@(e => (int)e.Average(a=>a.Severity))" /> | ||
| } | ||
|
|
||
| </ApexChart> | ||
| </DemoContainer> | ||
|
|
||
| @code { | ||
| private List<SupportIncident> incidents { get; set; } = SampleData.GetSupportIncidents(); | ||
| private ApexChartOptions<SupportIncident> options = new ApexChartOptions<SupportIncident> | ||
| { | ||
| DataLabels = new DataLabels | ||
| { | ||
| Style = new DataLabelsStyle | ||
| { | ||
| Colors = ["function(opts) { return (opts.series[opts.seriesIndex][opts.dataPointIndex] < 30) ? '#00f' : '#f00'; }"], | ||
| }, | ||
| }, | ||
| }; | ||
|
|
||
| private string GetColor(IncidentSource source) | ||
| { | ||
| switch (source) | ||
| { | ||
| case IncidentSource.Internal: | ||
| return "#FF0000"; | ||
| case IncidentSource.ThirdParty: | ||
| return "#0000FF"; | ||
| case IncidentSource.Integration: | ||
| return "#FF00FF"; | ||
| default: | ||
| return "#008FFB"; | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
peterboccia marked this conversation as resolved.
Show resolved
Hide resolved
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,65 @@ | ||
| using System; | ||
| using System.Text.RegularExpressions; | ||
|
|
||
| namespace ApexCharts.Internal; | ||
|
|
||
| /// <summary> | ||
| /// Provides JavaScript related helper utilities for ApexCharts internal processing. | ||
| /// </summary> | ||
| internal static class ChartUtilities | ||
| { | ||
| /// <summary> | ||
| /// Regex that matches the beginning of a JavaScript function or arrow function (including optional leading comments). | ||
| /// </summary> | ||
| private static readonly Regex JsFunctionStartRegex = new( | ||
| @"^\s*(?:/\*[\s\S]*?\*/\s*|//[^\n]*\n\s*)*(?:async\s+)?(?: | ||
| function\b | ||
| | \([^)]*\)\s*=> # (args) => | ||
| | [A-Za-z_$][\w$]*\s*=> # identifier => | ||
| )", | ||
| RegexOptions.IgnoreCase | RegexOptions.Compiled | RegexOptions.CultureInvariant | RegexOptions.IgnorePatternWhitespace); | ||
|
|
||
| /// <summary> | ||
| /// Regex that matches immediately-invoked function expressions (IIFE) using the traditional function keyword. | ||
| /// </summary> | ||
| private static readonly Regex IifeFunctionRegex = new( | ||
| @"^\s*\(\s*(?:async\s+)?function\b", | ||
| RegexOptions.IgnoreCase | RegexOptions.Compiled | RegexOptions.CultureInvariant); | ||
|
|
||
| /// <summary> | ||
| /// Regex that matches immediately-invoked arrow function expressions. | ||
| /// </summary> | ||
| private static readonly Regex IifeArrowRegex = new( | ||
| @"^\s*\(\s*(?:async\s+)?\([^)]*\)\s*=>", | ||
| RegexOptions.IgnoreCase | RegexOptions.Compiled | RegexOptions.CultureInvariant); | ||
|
|
||
| /// <summary> | ||
| /// Determines whether the provided string appears to represent a JavaScript function | ||
| /// (standard function declaration, arrow function, or IIFE). | ||
| /// </summary> | ||
| /// <param name="candidate">The string to inspect.</param> | ||
| /// <returns> | ||
| /// <see langword="true"/> if the string structurally resembles a JavaScript function; | ||
| /// otherwise, <see langword="false"/>. | ||
| /// </returns> | ||
| internal static bool IsJavaScriptFunction(string candidate) | ||
| { | ||
| if (string.IsNullOrWhiteSpace(candidate)) | ||
| return false; | ||
|
|
||
| // Quick cheap pre-filter: avoid regex if no plausible tokens | ||
| if (!(candidate.Contains("function", StringComparison.OrdinalIgnoreCase) || | ||
| candidate.Contains("=>", StringComparison.Ordinal))) | ||
| return false; | ||
|
|
||
| // Direct structural check at start | ||
| if (JsFunctionStartRegex.IsMatch(candidate)) | ||
| return true; | ||
|
|
||
| // IIFE patterns: (function(...) {...})(), (() => {...})(), etc. | ||
| if (IifeFunctionRegex.IsMatch(candidate) || IifeArrowRegex.IsMatch(candidate)) | ||
| return true; | ||
|
|
||
| return false; | ||
| } | ||
| } |
71 changes: 71 additions & 0 deletions
71
src/Blazor-ApexCharts/Internal/Converters/ListStringOrFunctionConverter.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,71 @@ | ||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.Text.Json; | ||
| using System.Text.Json.Serialization; | ||
|
|
||
| namespace ApexCharts.Internal; | ||
|
|
||
| /// <summary> | ||
| /// Converter for lists of strings that detects entries containing a JavaScript function definition | ||
| /// (case-insensitive search for the word "function") and serializes those as an object with the "@eval" key | ||
| /// so that ApexCharts interprets them as executable functions instead of plain strings. | ||
| /// | ||
| /// Output behavior: | ||
| /// - Normal strings are written as JSON string elements inside the array. | ||
| /// - Strings representing functions are written as: | ||
| /// { "@eval": "function(x) { return x; }" } | ||
| /// | ||
| /// Example: | ||
| /// C# input: | ||
| /// <code> | ||
| /// new List<string> | ||
| /// { | ||
| /// "Label 1", | ||
| /// "function(w) { return w; }", | ||
| /// "Another label" | ||
| /// }; | ||
| /// </code> | ||
| /// | ||
| /// JSON output: | ||
| /// <code> | ||
| /// [ | ||
| /// "Label 1", | ||
| /// { "@eval": "function(w) { return w; }" }, | ||
| /// "Another label" | ||
| /// ] | ||
| /// </code> | ||
| /// | ||
| /// Note: Deserialization (Read) is not implemented because this converter is intended only for outgoing | ||
| /// serialization to the client. | ||
| /// </summary> | ||
| internal class ListStringOrFunctionConverter : JsonConverter<List<string>> | ||
| { | ||
| public override bool CanConvert(Type typeToConvert) => typeToConvert == typeof(List<string>); | ||
|
|
||
| public override List<string> Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) | ||
| { | ||
| throw new NotImplementedException(); | ||
| } | ||
|
|
||
| public override void Write(Utf8JsonWriter writer, List<string> value, JsonSerializerOptions options) | ||
| { | ||
| writer.WriteStartArray(); | ||
|
|
||
| foreach (var item in value) | ||
| { | ||
| if (ChartUtilities.IsJavaScriptFunction(item)) | ||
| { | ||
| writer.WriteStartObject(); | ||
| writer.WritePropertyName("@eval"); | ||
| JsonSerializer.Serialize(writer, item, options); | ||
| writer.WriteEndObject(); | ||
| } | ||
| else | ||
| { | ||
| writer.WriteStringValue(item); | ||
| } | ||
| } | ||
|
|
||
| writer.WriteEndArray(); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.