Example60_Heatmap.cs 3.7 KB

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