HeatmapChart.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. using System.Collections.Generic;
  2. using UnityEngine;
  3. namespace XCharts.Runtime
  4. {
  5. /// <summary>
  6. /// Heat map mainly use colors to represent values, which must be used along with visualMap component.
  7. /// It can be used in either rectangular coordinate or geographic coordinate. But the behaviour on them are quite different. Rectangular coordinate must have two categories to use it.
  8. /// ||热力图主要通过颜色去表现数值的大小,必须要配合 visualMap 组件使用。
  9. /// 可以应用在直角坐标系以及地理坐标系上,这两个坐标系上的表现形式相差很大,直角坐标系上必须要使用两个类目轴。
  10. /// </summary>
  11. [AddComponentMenu("XCharts/HeatmapChart", 18)]
  12. [ExecuteInEditMode]
  13. [RequireComponent(typeof(RectTransform))]
  14. [DisallowMultipleComponent]
  15. [HelpURL("https://xcharts-team.github.io/docs/configuration")]
  16. public class HeatmapChart : BaseChart
  17. {
  18. protected override void DefaultChart()
  19. {
  20. var grid = EnsureChartComponent<GridCoord>();
  21. grid.UpdateRuntimeData(this);
  22. grid.left = 0.12f;
  23. var heatmapGridWid = 18f;
  24. int xSplitNumber = (int)(grid.context.width / heatmapGridWid);
  25. int ySplitNumber = (int)(grid.context.height / heatmapGridWid);
  26. var xAxis = EnsureChartComponent<XAxis>();
  27. xAxis.type = Axis.AxisType.Category;
  28. xAxis.splitLine.show = false;
  29. xAxis.boundaryGap = true;
  30. xAxis.splitNumber = xSplitNumber / 2;
  31. var yAxis = EnsureChartComponent<YAxis>();
  32. yAxis.type = Axis.AxisType.Category;
  33. yAxis.splitLine.show = false;
  34. yAxis.boundaryGap = true;
  35. yAxis.splitNumber = ySplitNumber;
  36. RemoveData();
  37. Heatmap.AddDefaultSerie(this, GenerateDefaultSerieName());
  38. var visualMap = EnsureChartComponent<VisualMap>();
  39. visualMap.autoMinMax = true;
  40. visualMap.orient = Orient.Vertical;
  41. visualMap.calculable = true;
  42. visualMap.location.align = Location.Align.BottomLeft;
  43. visualMap.location.bottom = 100;
  44. visualMap.location.left = 30;
  45. var colors = new List<string>
  46. {
  47. "#313695",
  48. "#4575b4",
  49. "#74add1",
  50. "#abd9e9",
  51. "#e0f3f8",
  52. "#ffffbf",
  53. "#fee090",
  54. "#fdae61",
  55. "#f46d43",
  56. "#d73027",
  57. "#a50026"
  58. };
  59. visualMap.AddColors(colors);
  60. for (int i = 0; i < xSplitNumber; i++)
  61. {
  62. xAxis.data.Add((i + 1).ToString());
  63. }
  64. for (int i = 0; i < ySplitNumber; i++)
  65. {
  66. yAxis.data.Add((i + 1).ToString());
  67. }
  68. for (int i = 0; i < xSplitNumber; i++)
  69. {
  70. for (int j = 0; j < ySplitNumber; j++)
  71. {
  72. var value = Random.Range(0, 150);
  73. var list = new List<double> { i, j, value };
  74. AddData(0, list);
  75. }
  76. }
  77. }
  78. /// <summary>
  79. /// default count heatmap chart.
  80. /// || 默认计数热力图。
  81. /// </summary>
  82. public void DefaultCountHeatmapChart()
  83. {
  84. CheckChartInit();
  85. var serie = GetSerie<Heatmap>(0);
  86. serie.heatmapType = HeatmapType.Count;
  87. var xAxis = GetChartComponent<XAxis>();
  88. xAxis.type = Axis.AxisType.Value;
  89. xAxis.splitNumber = 4;
  90. var yAxis = GetChartComponent<YAxis>();
  91. yAxis.type = Axis.AxisType.Value;
  92. yAxis.splitNumber = 2;
  93. serie.ClearData();
  94. for (int i = 0; i < 100; i++)
  95. {
  96. var x = UnityEngine.Random.Range(0, 100);
  97. var y = UnityEngine.Random.Range(0, 100);
  98. AddData(0, x, y);
  99. }
  100. }
  101. }
  102. }