| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120 |
- using LiveCharts.Wpf;
- using LiveCharts;
- using System.Windows.Media.Imaging;
- using System.Windows.Media;
- using System.Windows.Controls;
- using System.Windows;
- using System.Collections.Generic;
- using System.IO;
- using shjxCamera;
- namespace SHJX.Service.Common.Utils
- {
- public class ChartHelper
- {
- //private static readonly ILogger logger = LogFactory.Build("ChartHelper");
- private static LineSeries NewLineSeries(string title, Color color)
- {
- LineSeries series = new()
- {
- Title = title,
- Values = new ChartValues<int>(),
- Stroke = new SolidColorBrush(color),
- StrokeThickness = 1,
- Fill = Brushes.Transparent,
- PointGeometrySize = 0,
- LineSmoothness = 1,
- Foreground = new SolidColorBrush(color),
- Visibility = Visibility.Visible
- };
- return series;
- }
- public static void SaveGraph(List<ColorPoint> cps, string name)
- {
- SaveGraph(cps, name, name);
- }
- public static void SaveGraph(List<ColorPoint> cps, string name, string title)
- {
- //logger.LogInformation($"SaveGraph: {name}");
- LineSeries seriesH = NewLineSeries("A", Colors.White);
- LineSeries seriesR = NewLineSeries("R", Colors.Red);
- LineSeries seriesG = NewLineSeries("G", Colors.Green);
- LineSeries seriesB = NewLineSeries("B", Colors.Blue);
- LineSeries seriesP = NewLineSeries("P", Colors.Gray);
- for (int i = 0; i < cps.Count; i++)
- {
- seriesH.Values.Add(cps[i].Hue);
- seriesR.Values.Add(cps[i].R);
- seriesG.Values.Add(cps[i].G);
- seriesB.Values.Add(cps[i].B);
- seriesP.Values.Add(cps[i].Gray);
- }
- CartesianChart chart = new()
- {
- DisableAnimations = true,
- Name = "Chart",
- Width = 1024,
- Height = 768,
- Series = new SeriesCollection {
- seriesH,
- seriesR,
- seriesG,
- seriesB,
- seriesP
- },
- Background = new SolidColorBrush(Color.FromRgb(0x64, 0x52, 0xa4)),
- //LegendLocation = LegendLocation.Right,
- };
- chart.AxisX.Add(new LiveCharts.Wpf.Axis
- {
- IsMerged = true,
- Title = title,
- //IsEnabled = false,
- ShowLabels = false,
- FontSize = 24,
- });
- chart.AxisY.Add(new LiveCharts.Wpf.Axis
- {
- IsMerged= true,
- //Title = "color",
- MinValue = -5,
- MaxValue = 365,
- Separator = new LiveCharts.Wpf.Separator
- {
- Step = 50,
- StrokeDashArray = new DoubleCollection { 4, 8 },
- }
- });
- saveChart(chart, name);
- }
- public static void saveChart(CartesianChart chart, string name)
- {
- var viewbox = new Viewbox();
- viewbox.Child = chart;
- viewbox.Measure(chart.RenderSize);
- viewbox.Arrange(new Rect(new System.Windows.Point(0, 0), chart.RenderSize));
- chart.Update(false, true); //force chart redraw
- viewbox.UpdateLayout();
- //对象转换成位图
- RenderTargetBitmap bmp = new((int)chart.ActualWidth, (int)chart.ActualHeight, 96, 96, PixelFormats.Pbgra32);
- bmp.Render(chart);
- //对象的集合编码转成图像流
- BitmapEncoder encoder = new PngBitmapEncoder();
- encoder.Frames.Add(BitmapFrame.Create(bmp));
- //保存到文件中
- FileStream fs = new FileStream($"Data/{name}.png", FileMode.OpenOrCreate);
- encoder.Save(fs);
- fs.Close();
- fs.Dispose();
- }
- }
- }
|