using ScottPlot; using System; using System.Linq; using System.Windows; namespace LiveCharts2Test { /// /// MainWindow.xaml 的交互逻辑 /// public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); //double[] values = { 3, 2, 8, 4, 8, 10 }; //WpfPlot1.Plot.Add.Pie(values); //WpfPlot1.Refresh(); //double[] dataX = { 1, 2, 3, 4, 5 }; //double[] dataY = { 1, 4, 9, 16, 25 }; //WpfPlot1.Plot.Add.Scatter(dataX, dataY); //WpfPlot1.Refresh(); // 禁用所有交互功能 CreatePieChart2(); WpfPlot1.IsHitTestVisible = false; } private void CreatePieChart() { // 准备饼图数据 double[] values = { 25, 35, 30, 10 }; string[] labels = { "类别A", "类别B", "类别C", "类别D" }; Color[] colors = { Color.FromHex("#FF6B6B"), Color.FromHex("#4ECDC4"), Color.FromHex("#45B7D1"), Color.FromHex("#96CEB4") }; // 清除默认内容 WpfPlot1.Plot.Clear(); // 创建饼图 var pie = WpfPlot1.Plot.Add.Pie(values); // 隐藏坐标轴 WpfPlot1.Plot.Axes.Frameless(); // 移除整个坐标框架 WpfPlot1.Plot.HideGrid(); // 隐藏网格线 // 设置标题(可选) WpfPlot1.Plot.Title("数据分布饼状图"); // 刷新显示 WpfPlot1.Refresh(); } private void CreatePieChart2() { // create a pie chart double[] values = { 6, 8, 100 }; var pie = WpfPlot1.Plot.Add.Pie(values); //pie.ExplodeFraction = 0.1; pie.SliceLabelDistance = 0.8; // set different labels for slices and legend double total = pie.Slices.Select(x => x.Value).Sum(); for (int i = 0; i < pie.Slices.Count; i++) { pie.Slices[i].LabelFontSize = 20; pie.Slices[i].Label = $"{pie.Slices[i].Value}"; pie.Slices[i].LegendText = $"{pie.Slices[i].Value} " + $"({pie.Slices[i].Value / total:p1})"; } // hide unnecessary plot components WpfPlot1.Plot.Axes.Frameless(); WpfPlot1.Plot.HideGrid(); WpfPlot1.Refresh(); } } }