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
{
2024-01-13 22:37:13 +08:00
/// <summary>
/// Heat map mainly use colors to represent values, which must be used along with visualMap component.
/// 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.
/// ||热力图主要通过颜色去表现数值的大小,必须要配合 visualMap 组件使用。
/// 可以应用在直角坐标系以及地理坐标系上,这两个坐标系上的表现形式相差很大,直角坐标系上必须要使用两个类目轴。
/// </summary>
2021-11-23 13:20:07 +08:00
[AddComponentMenu("XCharts/HeatmapChart", 18)]
[ExecuteInEditMode]
[RequireComponent(typeof(RectTransform))]
[DisallowMultipleComponent]
2023-06-04 21:52:23 +08:00
[HelpURL("https://xcharts-team.github.io/docs/configuration")]
2021-11-23 13:20:07 +08:00
public class HeatmapChart : BaseChart
{
2022-03-20 18:52:50 +08:00
protected override void DefaultChart ( )
2021-11-23 13:20:07 +08:00
{
2023-02-12 21:22:53 +08:00
var grid = EnsureChartComponent < GridCoord > ( ) ;
2024-01-13 22:37:13 +08:00
grid . UpdateRuntimeData ( this ) ;
2022-01-22 21:08:26 +08:00
grid . left = 0.12f ;
2021-11-23 13:20:07 +08:00
2024-01-13 22:37:13 +08:00
var heatmapGridWid = 18f ;
int xSplitNumber = ( int ) ( grid . context . width / heatmapGridWid ) ;
int ySplitNumber = ( int ) ( grid . context . height / heatmapGridWid ) ;
2023-02-12 21:22:53 +08:00
var xAxis = EnsureChartComponent < XAxis > ( ) ;
2021-11-23 13:20:07 +08:00
xAxis . type = Axis . AxisType . Category ;
2024-01-13 22:37:13 +08:00
xAxis . splitLine . show = false ;
2021-11-23 13:20:07 +08:00
xAxis . boundaryGap = true ;
2024-01-13 22:37:13 +08:00
xAxis . splitNumber = xSplitNumber / 2 ;
2021-11-23 13:20:07 +08:00
2023-02-12 21:22:53 +08:00
var yAxis = EnsureChartComponent < YAxis > ( ) ;
2021-11-23 13:20:07 +08:00
yAxis . type = Axis . AxisType . Category ;
2024-01-13 22:37:13 +08:00
yAxis . splitLine . show = false ;
2021-11-23 13:20:07 +08:00
yAxis . boundaryGap = true ;
2024-01-13 22:37:13 +08:00
yAxis . splitNumber = ySplitNumber ;
2021-11-23 13:20:07 +08:00
RemoveData ( ) ;
2021-12-19 20:53:55 +08:00
2021-11-23 13:20:07 +08:00
Heatmap . AddDefaultSerie ( this , GenerateDefaultSerieName ( ) ) ;
2023-02-12 21:22:53 +08:00
var visualMap = EnsureChartComponent < VisualMap > ( ) ;
2022-09-02 08:12:57 +08:00
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 + + )
{
2022-09-02 08:12:57 +08:00
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 ) ;
}
}
}
2024-01-13 22:37:13 +08:00
/// <summary>
/// default count heatmap chart.
/// || 默认计数热力图。
/// </summary>
public void DefaultCountHeatmapChart ( )
{
CheckChartInit ( ) ;
var serie = GetSerie < Heatmap > ( 0 ) ;
serie . heatmapType = HeatmapType . Count ;
var xAxis = GetChartComponent < XAxis > ( ) ;
xAxis . type = Axis . AxisType . Value ;
xAxis . splitNumber = 4 ;
var yAxis = GetChartComponent < YAxis > ( ) ;
yAxis . type = Axis . AxisType . Value ;
yAxis . splitNumber = 2 ;
serie . ClearData ( ) ;
for ( int i = 0 ; i < 100 ; i + + )
{
var x = UnityEngine . Random . Range ( 0 , 100 ) ;
var y = UnityEngine . Random . Range ( 0 , 100 ) ;
AddData ( 0 , x , y ) ;
}
}
2021-11-23 13:20:07 +08:00
}
2022-05-22 22:17:38 +08:00
}