ChartHelper.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. using LiveCharts.Wpf;
  2. using LiveCharts;
  3. using System.Windows.Media.Imaging;
  4. using System.Windows.Media;
  5. using System.Windows.Controls;
  6. using System.Windows;
  7. using System.Collections.Generic;
  8. using System.IO;
  9. using shjxCamera;
  10. namespace SHJX.Service.Common.Utils
  11. {
  12. public class ChartHelper
  13. {
  14. //private static readonly ILogger logger = LogFactory.Build("ChartHelper");
  15. private static LineSeries NewLineSeries(string title, Color color)
  16. {
  17. LineSeries series = new()
  18. {
  19. Title = title,
  20. Values = new ChartValues<int>(),
  21. Stroke = new SolidColorBrush(color),
  22. StrokeThickness = 1,
  23. Fill = Brushes.Transparent,
  24. PointGeometrySize = 0,
  25. LineSmoothness = 1,
  26. Foreground = new SolidColorBrush(color),
  27. Visibility = Visibility.Visible
  28. };
  29. return series;
  30. }
  31. public static void SaveGraph(List<ColorPoint> cps, string name)
  32. {
  33. SaveGraph(cps, name, name);
  34. }
  35. public static void SaveGraph(List<ColorPoint> cps, string name, string title)
  36. {
  37. //logger.LogInformation($"SaveGraph: {name}");
  38. LineSeries seriesH = NewLineSeries("A", Colors.White);
  39. LineSeries seriesR = NewLineSeries("R", Colors.Red);
  40. LineSeries seriesG = NewLineSeries("G", Colors.Green);
  41. LineSeries seriesB = NewLineSeries("B", Colors.Blue);
  42. LineSeries seriesP = NewLineSeries("P", Colors.Gray);
  43. for (int i = 0; i < cps.Count; i++)
  44. {
  45. seriesH.Values.Add(cps[i].Hue);
  46. seriesR.Values.Add(cps[i].R);
  47. seriesG.Values.Add(cps[i].G);
  48. seriesB.Values.Add(cps[i].B);
  49. seriesP.Values.Add(cps[i].Gray);
  50. }
  51. CartesianChart chart = new()
  52. {
  53. DisableAnimations = true,
  54. Name = "Chart",
  55. Width = 1024,
  56. Height = 768,
  57. Series = new SeriesCollection {
  58. seriesH,
  59. seriesR,
  60. seriesG,
  61. seriesB,
  62. seriesP
  63. },
  64. Background = new SolidColorBrush(Color.FromRgb(0x64, 0x52, 0xa4)),
  65. //LegendLocation = LegendLocation.Right,
  66. };
  67. chart.AxisX.Add(new LiveCharts.Wpf.Axis
  68. {
  69. IsMerged = true,
  70. Title = title,
  71. //IsEnabled = false,
  72. ShowLabels = false,
  73. FontSize = 24,
  74. });
  75. chart.AxisY.Add(new LiveCharts.Wpf.Axis
  76. {
  77. IsMerged= true,
  78. //Title = "color",
  79. MinValue = -5,
  80. MaxValue = 365,
  81. Separator = new LiveCharts.Wpf.Separator
  82. {
  83. Step = 50,
  84. StrokeDashArray = new DoubleCollection { 4, 8 },
  85. }
  86. });
  87. saveChart(chart, name);
  88. }
  89. public static void saveChart(CartesianChart chart, string name)
  90. {
  91. var viewbox = new Viewbox();
  92. viewbox.Child = chart;
  93. viewbox.Measure(chart.RenderSize);
  94. viewbox.Arrange(new Rect(new System.Windows.Point(0, 0), chart.RenderSize));
  95. chart.Update(false, true); //force chart redraw
  96. viewbox.UpdateLayout();
  97. //对象转换成位图
  98. RenderTargetBitmap bmp = new((int)chart.ActualWidth, (int)chart.ActualHeight, 96, 96, PixelFormats.Pbgra32);
  99. bmp.Render(chart);
  100. //对象的集合编码转成图像流
  101. BitmapEncoder encoder = new PngBitmapEncoder();
  102. encoder.Frames.Add(BitmapFrame.Create(bmp));
  103. //保存到文件中
  104. FileStream fs = new FileStream($"Data/{name}.png", FileMode.OpenOrCreate);
  105. encoder.Save(fs);
  106. fs.Close();
  107. fs.Dispose();
  108. }
  109. }
  110. }