AirControl  1.3.0
Open Source, Modular, and Extensible Flight Simulator For Deep Learning Research
ChartTheme.cs
1 /************************************************/
2 /* */
3 /* Copyright (c) 2018 - 2021 monitor1394 */
4 /* https://github.com/monitor1394 */
5 /* */
6 /************************************************/
7 
8 using System.Collections.Generic;
9 using UnityEngine;
10 using System;
11 using System.Text;
12 #if dUI_TextMeshPro
13 using TMPro;
14 #endif
15 
16 namespace XCharts
17 {
21  public enum Theme
22  {
26  Default,
30  Light,
34  Dark,
38  Custom,
39  }
40 
41  [Serializable]
46  public class ChartTheme : MainComponent
47  {
48  [SerializeField] private Theme m_Theme = Theme.Default;
49  [SerializeField] private string m_ThemeName = Theme.Default.ToString();
50  [SerializeField] private string m_FontName;
51  [SerializeField] private string m_TMPFontName;
52  [SerializeField] private int m_FontInstacneId;
53  [SerializeField] private int m_TMPFontInstanceId;
54  [SerializeField] private Font m_Font;
55 #if dUI_TextMeshPro
56  [SerializeField] private TMP_FontAsset m_TMPFont;
57 #endif
58 
59  [SerializeField] private Color32 m_ContrastColor;
60  [SerializeField] private Color32 m_BackgroundColor;
61 
62 #if UNITY_2020_2
63  [NonReorderable]
64 #endif
65  [SerializeField] private List<Color32> m_ColorPalette = new List<Color32>(13);
66 
67  [SerializeField] private ComponentTheme m_Common;
68  [SerializeField] private TitleTheme m_Title;
69  [SerializeField] private SubTitleTheme m_SubTitle;
70  [SerializeField] private LegendTheme m_Legend;
71  [SerializeField] private AxisTheme m_Axis;
72  [SerializeField] private RadiusAxisTheme m_RadiusAxis;
73  [SerializeField] private AngleAxisTheme m_AngleAxis;
74  [SerializeField] private PolarAxisTheme m_Polar;
75  [SerializeField] private GaugeAxisTheme m_Gauge;
76  [SerializeField] private RadarAxisTheme m_Radar;
77  [SerializeField] private TooltipTheme m_Tooltip;
78  [SerializeField] private DataZoomTheme m_DataZoom;
79  [SerializeField] private VisualMapTheme m_VisualMap;
80  [SerializeField] private SerieTheme m_Serie;
81 
82  private ChartTheme()
83  {
84  m_FontName = "Arial";
85  m_TMPFontName = "LiberationSans SDF";
86  }
87 
92  public Theme theme
93  {
94  get { return m_Theme; }
95  set { if (PropertyUtil.SetStruct(ref m_Theme, value)) SetComponentDirty(); }
96  }
97  public string themeName
98  {
99  get { return m_ThemeName; }
100  set { if (PropertyUtil.SetClass(ref m_ThemeName, value)) SetComponentDirty(); }
101  }
102 
103 
108  public Color32 contrastColor
109  {
110  get { return m_ContrastColor; }
111  set { if (PropertyUtil.SetColor(ref m_ContrastColor, value)) SetVerticesDirty(); }
112  }
117  public Color32 backgroundColor
118  {
119  get { return m_BackgroundColor; }
120  set { if (PropertyUtil.SetColor(ref m_BackgroundColor, value)) SetVerticesDirty(); }
121  }
122 
123  public string fontName
124  {
125  get { return m_FontName; }
126  set { if (PropertyUtil.SetClass(ref m_FontName, value)) SetComponentDirty(); }
127  }
128  public int fontInstanceId
129  {
130  get { return m_FontInstacneId; }
131  set { if (PropertyUtil.SetStruct(ref m_FontInstacneId, value)) SetComponentDirty(); }
132  }
133  public string tmpFontName
134  {
135  get { return m_TMPFontName; }
136  set { if (PropertyUtil.SetClass(ref m_TMPFontName, value)) SetComponentDirty(); }
137  }
138  public int tmpFontInstanceId
139  {
140  get { return m_TMPFontInstanceId; }
141  set { if (PropertyUtil.SetStruct(ref m_TMPFontInstanceId, value)) SetComponentDirty(); }
142  }
143 
148  public List<Color32> colorPalette { get { return m_ColorPalette; } set { m_ColorPalette = value; SetVerticesDirty(); } }
149  public ComponentTheme common { get { return m_Common; } set { m_Common = value; SetComponentDirty(); } }
150  public TitleTheme title { get { return m_Title; } set { m_Title = value; SetComponentDirty(); } }
151  public SubTitleTheme subTitle { get { return m_SubTitle; } set { m_SubTitle = value; SetComponentDirty(); } }
152  public LegendTheme legend { get { return m_Legend; } set { m_Legend = value; SetComponentDirty(); } }
153  public AxisTheme axis { get { return m_Axis; } set { m_Axis = value; SetAllDirty(); } }
154  public RadiusAxisTheme radiusAxis { get { return m_RadiusAxis; } set { m_RadiusAxis = value; SetAllDirty(); } }
155  public AngleAxisTheme angleAxis { get { return m_AngleAxis; } set { m_AngleAxis = value; SetAllDirty(); } }
156  public PolarAxisTheme polar { get { return m_Polar; } set { m_Polar = value; SetAllDirty(); } }
157  public GaugeAxisTheme gauge { get { return m_Gauge; } set { m_Gauge = value; SetAllDirty(); } }
158  public RadarAxisTheme radar { get { return m_Radar; } set { m_Radar = value; SetAllDirty(); } }
159  public TooltipTheme tooltip { get { return m_Tooltip; } set { m_Tooltip = value; SetAllDirty(); } }
160  public DataZoomTheme dataZoom { get { return m_DataZoom; } set { m_DataZoom = value; SetAllDirty(); } }
161  public VisualMapTheme visualMap { get { return m_VisualMap; } set { m_VisualMap = value; SetAllDirty(); } }
162  public SerieTheme serie { get { return m_Serie; } set { m_Serie = value; SetVerticesDirty(); } }
163 #if dUI_TextMeshPro
164  public TMP_FontAsset tmpFont
169  {
170  get { return m_TMPFont; }
171  set
172  {
173  m_TMPFont = value;
174  if(value)
175  {
176  m_TMPFontName = value.name;
177  m_TMPFontInstanceId = value.GetInstanceID();
178  }
179  SetComponentDirty();
180  SyncTMPFontToSubComponent();
181  }
182  }
183 #endif
184  public Font font
189  {
190  get { return m_Font; }
191  set
192  {
193  m_Font = value;
194  if (value)
195  {
196  m_FontName = value.name;
197  m_FontInstacneId = value.GetInstanceID();
198  }
199  SetComponentDirty();
200  SyncFontToSubComponent();
201  }
202  }
203 
204  public void SetDefaultFont()
205  {
206 #if dUI_TextMeshPro
207  tmpFont = XChartsSettings.tmpFont;
208  SyncTMPFontToSubComponent();
209 #else
210  font = XChartsSettings.font;
211  SyncFontToSubComponent();
212 #endif
213  }
214 
221  public Color32 GetColor(int index)
222  {
223  if (index < 0) index = 0;
224  var newIndex = index < m_ColorPalette.Count ? index : index % m_ColorPalette.Count;
225  if (newIndex < m_ColorPalette.Count)
226  return m_ColorPalette[newIndex];
227  else return Color.clear;
228  }
229 
230  public void SetColorPalette(List<Color32> colorList)
231  {
232  m_ColorPalette = colorList;
233  SetVerticesDirty();
234  }
235  public void SetColorPalette(List<string> hexColorStringList)
236  {
237  m_ColorPalette.Clear();
238  foreach (var hexColor in hexColorStringList)
239  m_ColorPalette.Add(ColorUtil.GetColor(hexColor));
240  SetVerticesDirty();
241  }
242 
243  public void CheckWarning(StringBuilder sb)
244  {
245 #if dUI_TextMeshPro
246  if (m_TMPFont == null)
247  {
248  sb.AppendFormat("warning:theme->tmpFont is null\n");
249  }
250 #else
251  if (m_Font == null)
252  {
253  sb.AppendFormat("warning:theme->font is null\n");
254  }
255 #endif
256  if (m_ColorPalette.Count == 0)
257  {
258  sb.AppendFormat("warning:theme->colorPalette is empty\n");
259  }
260  for (int i = 0; i < m_ColorPalette.Count; i++)
261  {
262  if (!ChartHelper.IsClearColor(m_ColorPalette[i]) && m_ColorPalette[i].a == 0)
263  sb.AppendFormat("warning:theme->colorPalette[{0}] alpha = 0\n", i);
264  }
265  }
266 
267  Dictionary<int, string> _colorDic = new Dictionary<int, string>();
274  public string GetColorStr(int index)
275  {
276  if (index < 0)
277  {
278  index = 0;
279  }
280  index = index % m_ColorPalette.Count;
281  if (_colorDic.ContainsKey(index)) return _colorDic[index];
282  else
283  {
284  _colorDic[index] = ColorUtility.ToHtmlStringRGBA(GetColor(index));
285  return _colorDic[index];
286  }
287  }
288 
289  public void CopyTheme(Theme theme)
290  {
291  switch (theme)
292  {
293  case Theme.Dark:
294  CopyTheme(ChartTheme.Dark);
295  break;
296  case Theme.Default:
297  CopyTheme(ChartTheme.Default);
298  break;
299  case Theme.Light:
300  CopyTheme(ChartTheme.Light);
301  break;
302  }
303  }
304 
311  {
312  m_Theme = theme.theme;
313  m_ThemeName = theme.themeName;
314 #if dUI_TextMeshPro
315  tmpFont = theme.tmpFont;
316 #endif
317  font = theme.font;
318  m_FontName = theme.fontName;
319  m_FontInstacneId = theme.fontInstanceId;
320  m_TMPFontName = theme.tmpFontName;
321  m_TMPFontInstanceId = theme.tmpFontInstanceId;
322  m_ContrastColor = theme.contrastColor;
323  m_BackgroundColor = theme.m_BackgroundColor;
324  m_Common.Copy(theme.common);
325  m_Legend.Copy(theme.m_Legend);
326  m_Title.Copy(theme.m_Title);
327  m_SubTitle.Copy(theme.m_SubTitle);
328  m_Axis.Copy(theme.axis);
329  m_RadiusAxis.Copy(theme.radiusAxis);
330  m_AngleAxis.Copy(theme.angleAxis);
331  m_Polar.Copy(theme.polar);
332  m_Gauge.Copy(theme.gauge);
333  m_Radar.Copy(theme.radar);
334  m_Tooltip.Copy(theme.tooltip);
335  m_DataZoom.Copy(theme.dataZoom);
336  m_VisualMap.Copy(theme.visualMap);
337  m_Serie.Copy(theme.serie);
338  ChartHelper.CopyList(m_ColorPalette, theme.colorPalette);
339  SetAllDirty();
340  }
341 
346  public void ResetTheme()
347  {
348  switch (m_Theme)
349  {
350  case Theme.Default: CopyTheme(Default); break;
351  case Theme.Light: CopyTheme(Light); break;
352  case Theme.Dark: CopyTheme(Dark); break;
353  case Theme.Custom:
354  var sourTheme = XThemeMgr.GetTheme(themeName);
355  if (sourTheme != null)
356  {
357  CopyTheme(sourTheme);
358  }
359  else
360  {
361  Debug.LogWarning("ResetTheme:can't find theme:" + themeName + ", reset to Default theme");
362  CopyTheme(Default);
363  }
364  break;
365  }
366  }
367 
373  {
374  var theme = new ChartTheme();
375  InitChartComponentTheme(theme);
376  theme.CopyTheme(this);
377  return theme;
378  }
379 
380 
386  public static ChartTheme Default
387  {
388  get
389  {
390  var theme = new ChartTheme();
391  theme.theme = Theme.Default;
392  theme.themeName = Theme.Default.ToString();
393  theme.contrastColor = ColorUtil.GetColor("#514D4D");
394  theme.backgroundColor = new Color32(255, 255, 255, 255);
395  theme.colorPalette = new List<Color32>
396  {
397  new Color32(194, 53, 49, 255),
398  new Color32(47, 69, 84, 255),
399  new Color32(97, 160, 168, 255),
400  new Color32(212, 130, 101, 255),
401  new Color32(145, 199, 174, 255),
402  new Color32(116, 159, 131, 255),
403  new Color32(202, 134, 34, 255),
404  new Color32(189, 162, 154, 255),
405  new Color32(110, 112, 116, 255),
406  new Color32(84, 101, 112, 255),
407  new Color32(196, 204, 211, 255)
408  };
409  InitChartComponentTheme(theme);
410  return theme;
411  }
412  }
413 
419  public static ChartTheme Light
420  {
421  get
422  {
423  var theme = new ChartTheme();
424  theme.theme = Theme.Light;
425  theme.themeName = Theme.Light.ToString();
426  theme.contrastColor = ColorUtil.GetColor("#514D4D");
427  theme.backgroundColor = new Color32(255, 255, 255, 255);
428  theme.colorPalette = new List<Color32>
429  {
430  ColorUtil.GetColor("#37A2DA"),
431  ColorUtil.GetColor("#32C5E9"),
432  ColorUtil.GetColor("#67E0E3"),
433  ColorUtil.GetColor("#9FE6B8"),
434  ColorUtil.GetColor("#FFDB5C"),
435  ColorUtil.GetColor("#ff9f7f"),
436  ColorUtil.GetColor("#fb7293"),
437  ColorUtil.GetColor("#E062AE"),
438  ColorUtil.GetColor("#E690D1"),
439  ColorUtil.GetColor("#e7bcf3"),
440  ColorUtil.GetColor("#9d96f5"),
441  ColorUtil.GetColor("#8378EA"),
442  ColorUtil.GetColor("#96BFFF"),
443  };
444  InitChartComponentTheme(theme);
445  return theme;
446  }
447  }
448 
454  public static ChartTheme Dark
455  {
456  get
457  {
458  var theme = new ChartTheme();
459  theme.theme = Theme.Dark;
460  theme.themeName = Theme.Dark.ToString();
461  theme.contrastColor = ColorUtil.GetColor("#B9B8CE");
462  theme.backgroundColor = ColorUtil.GetColor("#100C2A");
463  theme.colorPalette = new List<Color32>
464  {
465  ColorUtil.GetColor("#4992ff"),
466  ColorUtil.GetColor("#7cffb2"),
467  ColorUtil.GetColor("#fddd60"),
468  ColorUtil.GetColor("#ff6e76"),
469  ColorUtil.GetColor("#58d9f9"),
470  ColorUtil.GetColor("#05c091"),
471  ColorUtil.GetColor("#ff8a45"),
472  ColorUtil.GetColor("#8d48e3"),
473  ColorUtil.GetColor("#dd79ff"),
474  };
475  InitChartComponentTheme(theme);
476  return theme;
477  }
478  }
479 
480  public static ChartTheme EmptyTheme
481  {
482  get
483  {
484  var theme = new ChartTheme();
485  theme.theme = Theme.Custom;
486  theme.themeName = Theme.Custom.ToString();
487  theme.contrastColor = Color.clear;
488  theme.backgroundColor = Color.clear;
489  theme.colorPalette = new List<Color32>();
490  InitChartComponentTheme(theme);
491  return theme;
492  }
493  }
494 
495  public void SyncFontName()
496  {
497  if (font)
498  {
499  m_FontName = font.name;
500  m_FontInstacneId = font.GetInstanceID();
501  }
502 #if dUI_TextMeshPro
503  if (tmpFont)
504  {
505  m_TMPFontName = tmpFont.name;
506  m_TMPFontInstanceId = tmpFont.GetInstanceID();
507  }
508 #endif
509  }
510 
511  public void SyncFontToSubComponent()
512  {
513  common.font = font;
514  title.font = font;
515  subTitle.font = font;
516  legend.font = font;
517  axis.font = font;
518  radiusAxis.font = font;
519  angleAxis.font = font;
520  polar.font = font;
521  gauge.font = font;
522  radar.font = font;
523  tooltip.font = font;
524  dataZoom.font = font;
525  visualMap.font = font;
526  }
527 
528 #if dUI_TextMeshPro
529  public void SyncTMPFontToSubComponent()
530  {
531  common.tmpFont = tmpFont;
532  title.tmpFont = tmpFont;
533  subTitle.tmpFont = tmpFont;
534  legend.tmpFont = tmpFont;
535  axis.tmpFont = tmpFont;
536  radiusAxis.tmpFont = tmpFont;
537  angleAxis.tmpFont = tmpFont;
538  polar.tmpFont = tmpFont;
539  gauge.tmpFont = tmpFont;
540  radar.tmpFont = tmpFont;
541  tooltip.tmpFont = tmpFont;
542  dataZoom.tmpFont = tmpFont;
543  visualMap.tmpFont = tmpFont;
544  }
545 #endif
546 
547  private static void InitChartComponentTheme(ChartTheme theme)
548  {
549  theme.common = new ComponentTheme(theme.theme);
550  theme.title = new TitleTheme(theme.theme);
551  theme.subTitle = new SubTitleTheme(theme.theme);
552  theme.legend = new LegendTheme(theme.theme);
553  theme.axis = new AxisTheme(theme.theme);
554  theme.radiusAxis = new RadiusAxisTheme(theme.theme);
555  theme.angleAxis = new AngleAxisTheme(theme.theme);
556  theme.polar = new PolarAxisTheme(theme.theme);
557  theme.gauge = new GaugeAxisTheme(theme.theme);
558  theme.radar = new RadarAxisTheme(theme.theme);
559  theme.tooltip = new TooltipTheme(theme.theme);
560  theme.dataZoom = new DataZoomTheme(theme.theme);
561  theme.visualMap = new VisualMapTheme(theme.theme);
562  theme.serie = new SerieTheme(theme.theme);
563  theme.SetDefaultFont();
564  }
565 
572  public static Color32 GetColor(string hexColorStr)
573  {
574  Color color;
575  ColorUtility.TryParseHtmlString(hexColorStr, out color);
576  return (Color32)color;
577  }
578 
579  public override int GetHashCode()
580  {
581  return base.GetHashCode();
582  }
583  }
584 }
XCharts.AxisTheme
Definition: AxisTheme.cs:201
XCharts.SerieType.Custom
@ Custom
自定义。
XCharts.AngleAxisTheme
Definition: AxisTheme.cs:217
XCharts.ChartTheme.font
Font font
the font of chart text。 字体。
Definition: ChartTheme.cs:189
XCharts.Theme
Theme
主题
Definition: ChartTheme.cs:21
XCharts.VisualMapTheme
Definition: VisualMapTheme.cs:14
XCharts.ChartTheme.GetColorStr
string GetColorStr(int index)
Gets the hexadecimal color string of the specified index from the palette. 获得指定索引的十六进制颜色值字符串。
Definition: ChartTheme.cs:274
XCharts.GaugeAxisTheme
Definition: GaugeAxisTheme.cs:15
XCharts.MainComponent
Definition: ChartComponent.cs:67
XCharts.ChartTheme.GetColor
Color32 GetColor(int index)
Gets the color of the specified index from the palette. 获得调色盘对应系列索引的颜色值。
Definition: ChartTheme.cs:221
XCharts.SubTitleTheme
Definition: SubTitleTheme.cs:13
XCharts
Definition: RewardChart.cs:14
XCharts.ChartTheme.contrastColor
Color32 contrastColor
the contrast color of chart. 对比色。
Definition: ChartTheme.cs:109
XCharts.ChartTheme.theme
Theme theme
the theme of chart. 主题类型。
Definition: ChartTheme.cs:93
XCharts.RadarAxisTheme
Definition: AxisTheme.cs:233
XCharts.ChartTheme.Default
static ChartTheme Default
default theme. 默认主题。
Definition: ChartTheme.cs:387
XCharts.DataZoomTheme
Definition: DataZoomTheme.cs:14
XCharts.LegendTheme
Definition: LegendTheme.cs:17
XCharts.Theme.Light
@ Light
亮主题。
XCharts.TooltipTheme
Definition: TooltipTheme.cs:14
XCharts.RadiusAxisTheme
Definition: AxisTheme.cs:209
XCharts.PolarAxisTheme
Definition: AxisTheme.cs:225
XCharts.SerieTheme
Definition: SerieTheme.cs:14
XCharts.Theme.Default
@ Default
默认主题。
XCharts.ChartTheme.Dark
static ChartTheme Dark
dark theme. 暗主题。
Definition: ChartTheme.cs:455
XCharts.ChartTheme
Theme. 主题相关配置。
Definition: ChartTheme.cs:46
XCharts.ChartTheme.GetColor
static Color32 GetColor(string hexColorStr)
Convert the html string to color. 将字符串颜色值转成Color。
Definition: ChartTheme.cs:572
XCharts.ChartTheme.colorPalette
List< Color32 > colorPalette
The color list of palette. If no color is set in series, the colors would be adopted sequentially and...
Definition: ChartTheme.cs:148
XCharts.ChartTheme.backgroundColor
Color32 backgroundColor
the background color of chart. 背景颜色。
Definition: ChartTheme.cs:118
XCharts.Theme.Dark
@ Dark
暗主题。
XCharts.TitleTheme
Definition: TitleTheme.cs:13
XCharts.ChartTheme.CopyTheme
void CopyTheme(ChartTheme theme)
copy all configurations from theme. 复制主题的所有配置。
Definition: ChartTheme.cs:310
XCharts.ChartTheme.ResetTheme
void ResetTheme()
Clear all custom configurations. 重置,清除所有自定义配置。
Definition: ChartTheme.cs:346
XCharts.ComponentTheme
Definition: ComponentTheme.cs:17
XCharts.ComponentTheme.font
Font font
the font of text. 字体。
Definition: ComponentTheme.cs:32
XCharts.ChartTheme.CloneTheme
ChartTheme CloneTheme()
克隆主题。
Definition: ChartTheme.cs:372
XCharts.ChartTheme.Light
static ChartTheme Light
light theme. 亮主题。
Definition: ChartTheme.cs:420
XCharts.XChartsSettings
Definition: XChartsSettings.cs:25