|
| 1 | +using Syncfusion.Maui.Toolkit.Charts; |
| 2 | +using System; |
| 3 | +using System.Collections.Generic; |
| 4 | +using System.Globalization; |
| 5 | +using System.Linq; |
| 6 | +using System.Text; |
| 7 | +using System.Threading.Tasks; |
| 8 | + |
| 9 | +namespace StackedAreaChart |
| 10 | +{ |
| 11 | + public class InteractionExt : ChartInteractiveBehavior |
| 12 | + { |
| 13 | + protected override void OnTouchMove(ChartBase chart, float pointX, float pointY) |
| 14 | + { |
| 15 | + base.OnTouchMove(chart, pointX, pointY); |
| 16 | + bool isSeriesTouched = false; |
| 17 | + |
| 18 | + if (chart is SfCartesianChart stackedAreaChart) |
| 19 | + { |
| 20 | + var series = stackedAreaChart.Series; |
| 21 | + |
| 22 | + for (int i = 0; i < series.Count; i++) |
| 23 | + { |
| 24 | + var seriesItem = series[i]; |
| 25 | + |
| 26 | + if (seriesItem != null) |
| 27 | + { |
| 28 | + var index = seriesItem.GetDataPointIndex(pointX, pointY); |
| 29 | + |
| 30 | + if (index != -1) |
| 31 | + { |
| 32 | + seriesItem.Opacity = 1; |
| 33 | + isSeriesTouched = true; |
| 34 | + } |
| 35 | + else |
| 36 | + { |
| 37 | + seriesItem.Opacity = 0.33; |
| 38 | + } |
| 39 | + } |
| 40 | + } |
| 41 | + |
| 42 | + if (!isSeriesTouched) |
| 43 | + { |
| 44 | + foreach (var item in series) |
| 45 | + { |
| 46 | + item.Opacity = 1; |
| 47 | + } |
| 48 | + } |
| 49 | + } |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + public class TrackballExt : ChartTrackballBehavior |
| 54 | + { |
| 55 | + protected override void LabelsGenerated(List<TrackballPointInfo> pointInfos) |
| 56 | + { |
| 57 | + foreach (var item in pointInfos) |
| 58 | + { |
| 59 | + var series = item.Series; |
| 60 | + |
| 61 | + if (series is StackingAreaSeries stackingArea && stackingArea.YBindingPath != "NorthAmerica") |
| 62 | + { |
| 63 | + item.Label = string.Empty; |
| 64 | + } |
| 65 | + |
| 66 | + item.MarkerSettings = new ChartMarkerSettings() { Fill = Colors.White, Stroke = series.Fill, StrokeWidth = 2 }; |
| 67 | + } |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + public class TrackballLabelConverter : IValueConverter |
| 72 | + { |
| 73 | + private static readonly Dictionary<string, Func<Model, double>> DataSelectors = new() |
| 74 | + { |
| 75 | + { "0", m => m.Oceania }, |
| 76 | + { "1", m => m.EasternEurope }, |
| 77 | + { "2", m => m.Africa }, |
| 78 | + { "3", m => m.SouthAmerica }, |
| 79 | + { "4", m => m.NorthAmerica }, |
| 80 | + { "5", m => m.WesternEurope }, |
| 81 | + { "6", m => m.Asia }, |
| 82 | + { "7", m => m.Total } |
| 83 | + }; |
| 84 | + |
| 85 | + public object? Convert(object? value, Type targetType, object? parameter, CultureInfo culture) |
| 86 | + { |
| 87 | + if (value is not TrackballPointInfo pointInfo || parameter is not string para) |
| 88 | + return null; |
| 89 | + |
| 90 | + if (pointInfo.DataItem is Model model && DataSelectors.TryGetValue(para, out var selector)) |
| 91 | + { |
| 92 | + double yValue = selector(model); |
| 93 | + return $"{Math.Round(yValue, 2)}%"; |
| 94 | + } |
| 95 | + |
| 96 | + return null; |
| 97 | + } |
| 98 | + |
| 99 | + public object? ConvertBack(object? value, Type targetType, object? parameter, CultureInfo culture) |
| 100 | + { |
| 101 | + throw new NotImplementedException(); |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + public class TrackballColorConverter : IValueConverter |
| 106 | + { |
| 107 | + public object? Convert(object? value, Type targetType, object? parameter, CultureInfo culture) |
| 108 | + { |
| 109 | + if (value is not TrackballPointInfo pointInfo || parameter is not string para) |
| 110 | + return null; |
| 111 | + |
| 112 | + if (pointInfo.Series.BindingContext is ViewModel viewModel |
| 113 | + && int.TryParse(para, out int index) |
| 114 | + && index >= 0 && index < viewModel.PaletteBrushes.Count) |
| 115 | + { |
| 116 | + return ((SolidColorBrush)viewModel.PaletteBrushes[index]).Color; |
| 117 | + } |
| 118 | + |
| 119 | + return null; |
| 120 | + } |
| 121 | + |
| 122 | + public object? ConvertBack(object? value, Type targetType, object? parameter, CultureInfo culture) |
| 123 | + { |
| 124 | + throw new NotImplementedException(); |
| 125 | + } |
| 126 | + } |
| 127 | +} |
0 commit comments