MainWindow.xaml.cs 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. using ScottPlot;
  2. using System;
  3. using System.Linq;
  4. using System.Windows;
  5. namespace LiveCharts2Test
  6. {
  7. /// <summary>
  8. /// MainWindow.xaml 的交互逻辑
  9. /// </summary>
  10. public partial class MainWindow : Window
  11. {
  12. public MainWindow()
  13. {
  14. InitializeComponent();
  15. //double[] values = { 3, 2, 8, 4, 8, 10 };
  16. //WpfPlot1.Plot.Add.Pie(values);
  17. //WpfPlot1.Refresh();
  18. //double[] dataX = { 1, 2, 3, 4, 5 };
  19. //double[] dataY = { 1, 4, 9, 16, 25 };
  20. //WpfPlot1.Plot.Add.Scatter(dataX, dataY);
  21. //WpfPlot1.Refresh();
  22. // 禁用所有交互功能
  23. CreatePieChart2();
  24. WpfPlot1.IsHitTestVisible = false;
  25. }
  26. private void CreatePieChart()
  27. {
  28. // 准备饼图数据
  29. double[] values = { 25, 35, 30, 10 };
  30. string[] labels = { "类别A", "类别B", "类别C", "类别D" };
  31. Color[] colors = {
  32. Color.FromHex("#FF6B6B"),
  33. Color.FromHex("#4ECDC4"),
  34. Color.FromHex("#45B7D1"),
  35. Color.FromHex("#96CEB4")
  36. };
  37. // 清除默认内容
  38. WpfPlot1.Plot.Clear();
  39. // 创建饼图
  40. var pie = WpfPlot1.Plot.Add.Pie(values);
  41. // 隐藏坐标轴
  42. WpfPlot1.Plot.Axes.Frameless(); // 移除整个坐标框架
  43. WpfPlot1.Plot.HideGrid(); // 隐藏网格线
  44. // 设置标题(可选)
  45. WpfPlot1.Plot.Title("数据分布饼状图");
  46. // 刷新显示
  47. WpfPlot1.Refresh();
  48. }
  49. private void CreatePieChart2()
  50. {
  51. // create a pie chart
  52. double[] values = { 6, 8, 100 };
  53. var pie = WpfPlot1.Plot.Add.Pie(values);
  54. //pie.ExplodeFraction = 0.1;
  55. pie.SliceLabelDistance = 0.8;
  56. // set different labels for slices and legend
  57. double total = pie.Slices.Select(x => x.Value).Sum();
  58. for (int i = 0; i < pie.Slices.Count; i++)
  59. {
  60. pie.Slices[i].LabelFontSize = 20;
  61. pie.Slices[i].Label = $"{pie.Slices[i].Value}";
  62. pie.Slices[i].LegendText = $"{pie.Slices[i].Value} " +
  63. $"({pie.Slices[i].Value / total:p1})";
  64. }
  65. // hide unnecessary plot components
  66. WpfPlot1.Plot.Axes.Frameless();
  67. WpfPlot1.Plot.HideGrid();
  68. WpfPlot1.Refresh();
  69. }
  70. }
  71. }