Files
XCharts/Runtime/Chart/HeatmapChart.cs

79 lines
2.5 KiB
C#
Raw Normal View History

2021-11-23 13:20:07 +08:00
using System.Collections.Generic;
using UnityEngine;
2022-02-19 22:37:57 +08:00
namespace XCharts.Runtime
2021-11-23 13:20:07 +08:00
{
[AddComponentMenu("XCharts/HeatmapChart", 18)]
[ExecuteInEditMode]
[RequireComponent(typeof(RectTransform))]
[DisallowMultipleComponent]
public class HeatmapChart : BaseChart
{
2022-03-20 18:52:50 +08:00
protected override void DefaultChart()
2021-11-23 13:20:07 +08:00
{
2021-12-19 20:53:55 +08:00
var tooltip = GetChartComponent<Tooltip>();
tooltip.type = Tooltip.Type.None;
tooltip.trigger = Tooltip.Trigger.Axis;
2021-11-23 13:20:07 +08:00
var grid = GetOrAddChartComponent<GridCoord>();
2022-01-22 21:08:26 +08:00
grid.left = 0.12f;
2021-11-23 13:20:07 +08:00
var xAxis = GetOrAddChartComponent<XAxis>();
xAxis.type = Axis.AxisType.Category;
xAxis.boundaryGap = true;
xAxis.splitNumber = 10;
var yAxis = GetOrAddChartComponent<YAxis>();
yAxis.type = Axis.AxisType.Category;
yAxis.boundaryGap = true;
yAxis.splitNumber = 10;
RemoveData();
2021-12-19 20:53:55 +08:00
2021-11-23 13:20:07 +08:00
var heatmapGridWid = 10f;
2022-05-22 22:17:38 +08:00
int xSplitNumber = (int) (grid.context.width / heatmapGridWid);
int ySplitNumber = (int) (grid.context.height / heatmapGridWid);
2021-11-23 13:20:07 +08:00
Heatmap.AddDefaultSerie(this, GenerateDefaultSerieName());
var visualMap = GetOrAddChartComponent<VisualMap>();
visualMap.autoMinMax = true;
2021-11-23 13:20:07 +08:00
visualMap.orient = Orient.Vertical;
visualMap.calculable = true;
visualMap.location.align = Location.Align.BottomLeft;
visualMap.location.bottom = 100;
visualMap.location.left = 30;
2022-05-22 22:17:38 +08:00
var colors = new List<string>
2021-11-23 13:20:07 +08:00
{
2022-05-22 22:17:38 +08:00
"#313695",
"#4575b4",
"#74add1",
"#abd9e9",
"#e0f3f8",
"#ffffbf",
"#fee090",
"#fdae61",
"#f46d43",
"#d73027",
"#a50026"
};
visualMap.AddColors(colors);
2021-11-23 13:20:07 +08:00
for (int i = 0; i < xSplitNumber; i++)
{
xAxis.data.Add((i + 1).ToString());
}
for (int i = 0; i < ySplitNumber; i++)
{
yAxis.data.Add((i + 1).ToString());
}
for (int i = 0; i < xSplitNumber; i++)
{
for (int j = 0; j < ySplitNumber; j++)
{
var value = Random.Range(0, 150);
2021-11-23 13:20:07 +08:00
var list = new List<double> { i, j, value };
AddData(0, list);
}
}
}
}
2022-05-22 22:17:38 +08:00
}