Example60_Heatmap.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. using System.Collections.Generic;
  2. using UnityEngine;
  3. using XCharts.Runtime;
  4. namespace XCharts.Example
  5. {
  6. [DisallowMultipleComponent]
  7. [ExecuteInEditMode]
  8. public class Example60_Heatmap : MonoBehaviour
  9. {
  10. private HeatmapChart chart;
  11. void Awake()
  12. {
  13. chart = gameObject.GetComponent<HeatmapChart>();
  14. if (chart == null)
  15. {
  16. chart = gameObject.AddComponent<HeatmapChart>();
  17. chart.Init();
  18. }
  19. chart.GetChartComponent<Title>().text = "HeatmapChart";
  20. chart.GetChartComponent<Tooltip>().type = Tooltip.Type.None;
  21. var grid = chart.GetChartComponent<GridCoord>();
  22. grid.left = 100;
  23. grid.right = 60;
  24. grid.bottom = 60;
  25. var xAxis = chart.GetChartComponent<XAxis>();
  26. var yAxis = chart.GetChartComponent<YAxis>();
  27. //目前只支持Category
  28. xAxis.type = Axis.AxisType.Category;
  29. yAxis.type = Axis.AxisType.Category;
  30. xAxis.boundaryGap = true;
  31. xAxis.boundaryGap = true;
  32. xAxis.splitNumber = 10;
  33. yAxis.splitNumber = 10;
  34. //清空数据重新添加
  35. chart.RemoveData();
  36. var serie = chart.AddSerie<Heatmap>("serie1");
  37. //设置样式
  38. serie.itemStyle.show = true;
  39. serie.itemStyle.borderWidth = 1;
  40. serie.itemStyle.borderColor = Color.clear;
  41. //设置高亮样式
  42. var emphasisStyle = serie.EnsureComponent<EmphasisStyle>();
  43. emphasisStyle.itemStyle.show = true;
  44. emphasisStyle.itemStyle.borderWidth = 1;
  45. emphasisStyle.itemStyle.borderColor = Color.black;
  46. //设置视觉映射组件
  47. var visualMap = chart.GetChartComponent<VisualMap>();
  48. visualMap.max = 10;
  49. visualMap.range[0] = 0f;
  50. visualMap.range[1] = 10f;
  51. visualMap.orient = Orient.Vertical;
  52. visualMap.calculable = true;
  53. visualMap.location.align = Location.Align.BottomLeft;
  54. visualMap.location.bottom = 100;
  55. visualMap.location.left = 30;
  56. //清空颜色重新添加
  57. var heatmapGridWid = 10f;
  58. int xSplitNumber = (int) (grid.context.width / heatmapGridWid);
  59. int ySplitNumber = (int) (grid.context.height / heatmapGridWid);
  60. var colors = new List<string>
  61. {
  62. "#313695",
  63. "#4575b4",
  64. "#74add1",
  65. "#abd9e9",
  66. "#e0f3f8",
  67. "#ffffbf",
  68. "#fee090",
  69. "#fdae61",
  70. "#f46d43",
  71. "#d73027",
  72. "#a50026"
  73. };
  74. visualMap.AddColors(colors);
  75. //添加xAxis的数据
  76. for (int i = 0; i < xSplitNumber; i++)
  77. {
  78. chart.AddXAxisData((i + 1).ToString());
  79. }
  80. //添加yAxis的数据
  81. for (int i = 0; i < ySplitNumber; i++)
  82. {
  83. chart.AddYAxisData((i + 1).ToString());
  84. }
  85. for (int i = 0; i < xSplitNumber; i++)
  86. {
  87. for (int j = 0; j < ySplitNumber; j++)
  88. {
  89. var value = 0f;
  90. var rate = Random.Range(0, 101);
  91. if (rate > 70) value = Random.Range(8f, 10f);
  92. else value = Random.Range(1f, 8f);
  93. var list = new List<double> { i, j, value };
  94. //至少是一个三位数据:(x,y,value)
  95. chart.AddData(0, list);
  96. }
  97. }
  98. }
  99. }
  100. }