Skip to content

Commit 81a0b8a

Browse files
authored
Merge pull request #77 from apexcharts/add-goals
Add goals
2 parents 45f4149 + 0e4eb91 commit 81a0b8a

File tree

10 files changed

+148
-16
lines changed

10 files changed

+148
-16
lines changed

docs/BlazorApexCharts.Docs/Components/ChartTypes/BarCharts/BarCharts.razor

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,12 @@
22

33
<DocExamples Title="Bar Charts">
44

5+
<CodeSnippet Title=Markers ClassName=@typeof(Markers).ToString()>
6+
<Snippet>
7+
<Markers />
8+
</Snippet>
9+
</CodeSnippet>
10+
511
<CodeSnippet Title=Basic ClassName=@typeof(Basic).ToString()>
612
<Snippet>
713
<Basic />
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
<DemoContainer>
2+
<ApexChart TItem="Order"
3+
Title="Order Net Value">
4+
5+
<ApexPointSeries TItem="Order"
6+
Items="Orders"
7+
Name="Gross Value"
8+
XValue="@(e => e.Country)"
9+
YAggregate="@(e => e.Sum(e => e.GrossValue))"
10+
OrderByDescending="e=>e.Y"
11+
SeriesType="SeriesType.Bar"
12+
UpdateDataPoint=SetMarkers />
13+
14+
15+
</ApexChart>
16+
</DemoContainer>
17+
18+
@code {
19+
private List<Order> Orders { get; set; } = SampleData.GetOrders();
20+
21+
private void SetMarkers(DataPoint<Order> datapoint)
22+
{
23+
datapoint.Goals = new();
24+
var goal = new DataPointGoal { Name = "Target" };
25+
datapoint.Goals.Add(goal);
26+
27+
switch (datapoint.X.ToString())
28+
{
29+
case "France":
30+
goal.StrokeColor = "#e3001b";
31+
goal.StrokeDashArray = 5;
32+
goal.StrokeHeight = 5;
33+
goal.Value = 240000;
34+
break;
35+
36+
case "Brazil":
37+
goal.StrokeColor = "#145A32";
38+
goal.StrokeHeight = 10;
39+
goal.StrokeLineCap = StrokeLineCap.Butt;
40+
goal.Value = 290000;
41+
break;
42+
43+
default:
44+
goal.StrokeColor = "#ffd500";
45+
goal.StrokeHeight = 13;
46+
goal.StrokeWidth = 0;
47+
goal.StrokeLineCap = StrokeLineCap.Round;
48+
goal.Value = 200000;
49+
break;
50+
51+
}
52+
53+
//if (datapoint.X.ToString() == "France")
54+
//{
55+
// datapoint.Goals.Add(new DataPointGoal
56+
// {
57+
// Name = "Goal",
58+
// StrokeHeight = 5,
59+
// StrokeDashArray = 5,
60+
// Value = 250000,
61+
// StrokeColor = "#e3001b"
62+
// });
63+
//}
64+
//else
65+
//{
66+
// datapoint.Goals.Add(new DataPointGoal
67+
// {
68+
// Name = "Goal",
69+
// StrokeHeight = 20,
70+
// Value = 200000,
71+
// StrokeColor = "#ffd500"
72+
// });
73+
//}
74+
75+
76+
//var test = datapoint;
77+
}
78+
}

src/Blazor-ApexCharts/Extensions/GeneralExtensions.cs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Collections.Generic;
23

34
namespace ApexCharts
45
{
@@ -14,12 +15,24 @@ public static DateTimeOffset DayOnly(this DateTimeOffset value)
1415
return new DateTimeOffset(value.Year, value.Month, value.Day, 0, 0, 0, new TimeSpan());
1516
}
1617

17-
1818
public static long ToUnixTimeMilliseconds(this DateTime d)
1919
{
2020
DateTime epoch = DateTime.UnixEpoch;
2121
return (long)(d - epoch).TotalMilliseconds;
2222
}
2323

24+
public static IList<T> SetValue<T>(this IList<T> items, Action<T> updateMethod)
25+
{
26+
if(updateMethod == null)
27+
{
28+
return items;
29+
}
30+
31+
foreach (T item in items)
32+
{
33+
updateMethod(item);
34+
}
35+
return items;
36+
}
2437
}
2538
}
Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,39 @@
11
using System.Collections.Generic;
2-
using System.Diagnostics.CodeAnalysis;
32
using System.Text.Json.Serialization;
43

54
namespace ApexCharts
65
{
7-
6+
87
public class DataPoint<TItem> : IDataPoint<TItem>
98
{
109
public string FillColor { get; set; }
1110
public object X { get; set; }
1211

12+
public List<DataPointGoal> Goals { get; set; }
13+
1314
[JsonIgnore(Condition = JsonIgnoreCondition.Never)]
1415
public decimal? Y { get; set; }
1516

16-
1717
[JsonIgnore]
1818
public IEnumerable<TItem> Items { get; set; }
1919

2020
}
21-
21+
22+
public class DataPointGoal
23+
{
24+
public string Name { get; set; }
25+
public decimal Value { get; set; }
26+
public int? StrokeWidth { get; set; }
27+
public int? StrokeHeight { get; set; }
28+
public int? StrokeDashArray { get; set; }
29+
public string StrokeColor { get; set; }
30+
public StrokeLineCap? StrokeLineCap { get; set; }
31+
}
32+
33+
public enum StrokeLineCap
34+
{
35+
Butt,
36+
Round,
37+
Square
38+
}
2239
}

src/Blazor-ApexCharts/Series/ApexBaseSeries.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,5 +46,20 @@ public string GetPointColor(IEnumerable<TItem> items)
4646
return PointColor.Invoke(items.First());
4747
}
4848

49+
50+
public List<T> UpdateDataPoints<T>(IEnumerable<T> items, Action<T> updateMethod)
51+
{
52+
var data = items.ToList();
53+
if(updateMethod == null)
54+
{
55+
return data;
56+
}
57+
58+
foreach (T item in data)
59+
{
60+
updateMethod(item);
61+
}
62+
return data;
63+
}
4964
}
5065
}

src/Blazor-ApexCharts/Series/ApexBoxPlotSeries.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ public class ApexBoxPlotSeries<TItem> : ApexBaseSeries<TItem>, IApexSeries<TItem
1212
[Parameter] public Func<TItem, decimal?> YValue { get; set; }
1313
[Parameter] public Func<ListPoint<TItem>, object> OrderBy { get; set; }
1414
[Parameter] public Func<ListPoint<TItem>, object> OrderByDescending { get; set; }
15+
[Parameter] public Action<ListPoint<TItem>> UpdateDataPoint { get; set; }
16+
1517

1618
protected override void OnInitialized()
1719
{
@@ -44,7 +46,7 @@ public IEnumerable<IDataPoint<TItem>> GenerateDataPoints(IEnumerable<TItem> item
4446
data = data.OrderByDescending(OrderByDescending);
4547
}
4648

47-
return data;
49+
return UpdateDataPoints(data, UpdateDataPoint);
4850
}
4951

5052
public void Dispose()

src/Blazor-ApexCharts/Series/ApexBubbleSeries.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ public class ApexBubbleSeries<TItem> : ApexBaseSeries<TItem>, IApexSeries<TItem>
1313
[Parameter] public Func<IEnumerable<TItem>, decimal> ZAggregate { get; set; }
1414
[Parameter] public Func<BubblePoint<TItem>, object> OrderBy { get; set; }
1515
[Parameter] public Func<BubblePoint<TItem>, object> OrderByDescending { get; set; }
16+
[Parameter] public Action<BubblePoint<TItem>> UpdateDataPoint { get; set; }
17+
1618

1719
protected override void OnInitialized()
1820
{
@@ -45,7 +47,7 @@ public IEnumerable<IDataPoint<TItem>> GenerateDataPoints(IEnumerable<TItem> item
4547
data = data.OrderByDescending(OrderByDescending);
4648
}
4749

48-
return data;
50+
return UpdateDataPoints(data, UpdateDataPoint);
4951
}
5052

5153
public void Dispose()

src/Blazor-ApexCharts/Series/ApexCandleSeries.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ public class ApexCandleSeries<TItem> : ApexBaseSeries<TItem>, IApexSeries<TItem>
1515
[Parameter] public Func<TItem, decimal> Close { get; set; }
1616
[Parameter] public Func<ListPoint<TItem>, object> OrderBy { get; set; }
1717
[Parameter] public Func<ListPoint<TItem>, object> OrderByDescending { get; set; }
18+
[Parameter] public Action<ListPoint<TItem>> UpdateDataPoint { get; set; }
19+
1820

1921
protected override void OnInitialized()
2022
{
@@ -52,7 +54,7 @@ public IEnumerable<IDataPoint<TItem>> GenerateDataPoints(IEnumerable<TItem> item
5254
data = data.OrderByDescending(OrderByDescending);
5355
}
5456

55-
return data;
57+
return UpdateDataPoints(data, UpdateDataPoint);
5658

5759
}
5860

src/Blazor-ApexCharts/Series/ApexPointSeries.cs

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,13 @@ namespace ApexCharts
1010
{
1111
public class ApexPointSeries<TItem> : ApexBaseSeries<TItem>, IApexSeries<TItem> where TItem : class
1212
{
13-
//[Parameter] public IEnumerable<IDataPoint<TItem>> Data { get; set; }
1413
[Parameter] public Func<TItem, decimal?> YValue { get; set; }
1514
[Parameter] public Func<IEnumerable<TItem>, decimal?> YAggregate { get; set; }
1615
[Parameter] public Func<DataPoint<TItem>, object> OrderBy { get; set; }
1716
[Parameter] public Func<DataPoint<TItem>, object> OrderByDescending { get; set; }
1817
[Parameter] public SeriesType SeriesType { get; set; }
18+
[Parameter] public Action<DataPoint<TItem>> UpdateDataPoint { get; set; }
19+
1920

2021
protected override void OnInitialized()
2122
{
@@ -97,12 +98,7 @@ public IEnumerable<IDataPoint<TItem>> GenerateDataPoints(IEnumerable<TItem> item
9798
data = data.OrderByDescending(OrderByDescending);
9899
}
99100

100-
return data;
101-
}
102-
103-
public IEnumerable<IDataPoint<TItem>> GetData()
104-
{
105-
return GenerateDataPoints(Items);
101+
return UpdateDataPoints(data, UpdateDataPoint);
106102
}
107103

108104
public void Dispose()

src/Blazor-ApexCharts/Series/ApexRangeSeries.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ public class ApexRangeSeries<TItem> : ApexBaseSeries<TItem>, IApexSeries<TItem>
1616

1717
[Parameter] public Func<TItem, decimal> YMinValue { get; set; }
1818
[Parameter] public Func<TItem, decimal> YMaxValue { get; set; }
19+
[Parameter] public Action<ListPoint<TItem>> UpdateDataPoint { get; set; }
1920

2021
protected override void OnInitialized()
2122
{
@@ -74,7 +75,7 @@ public IEnumerable<IDataPoint<TItem>> GenerateDataPoints(IEnumerable<TItem> item
7475
data = data.OrderByDescending(OrderByDescending);
7576
}
7677

77-
return data;
78+
return UpdateDataPoints(data, UpdateDataPoint);
7879
}
7980

8081
public IEnumerable<IDataPoint<TItem>> GetData()

0 commit comments

Comments
 (0)