mirror of
https://github.com/XCharts-Team/XCharts.git
synced 2026-05-26 02:40:13 +00:00
3.0 - unitypackage
This commit is contained in:
116
Runtime/Coord/Grid/GridCoord.cs
Normal file
116
Runtime/Coord/Grid/GridCoord.cs
Normal file
@@ -0,0 +1,116 @@
|
||||
|
||||
using System;
|
||||
using UnityEngine;
|
||||
|
||||
namespace XCharts
|
||||
{
|
||||
/// <summary>
|
||||
/// Grid component.
|
||||
/// Drawing grid in rectangular coordinate. Line chart, bar chart, and scatter chart can be drawn in grid.
|
||||
/// <para>
|
||||
/// 网格组件。
|
||||
/// 直角坐标系内绘图网格。可以在网格上绘制折线图,柱状图,散点图。
|
||||
/// </para>
|
||||
/// </summary>
|
||||
[Serializable]
|
||||
[ComponentHandler(typeof(GridCoordHandler), true)]
|
||||
public class GridCoord : CoordSystem, IUpdateRuntimeData, ISerieContainer
|
||||
{
|
||||
[SerializeField] private bool m_Show = true;
|
||||
[SerializeField] private float m_Left = 0.1f;
|
||||
[SerializeField] private float m_Right = 0.08f;
|
||||
[SerializeField] private float m_Top = 0.22f;
|
||||
[SerializeField] private float m_Bottom = 0.12f;
|
||||
[SerializeField] private Color m_BackgroundColor;
|
||||
|
||||
public GridCoordContext context = new GridCoordContext();
|
||||
|
||||
/// <summary>
|
||||
/// Whether to show the grid in rectangular coordinate.
|
||||
/// 是否显示直角坐标系网格。
|
||||
/// </summary>
|
||||
public bool show
|
||||
{
|
||||
get { return m_Show; }
|
||||
set { if (PropertyUtil.SetStruct(ref m_Show, value)) SetVerticesDirty(); }
|
||||
}
|
||||
/// <summary>
|
||||
/// Distance between grid component and the left side of the container.
|
||||
/// grid 组件离容器左侧的距离。
|
||||
/// </summary>
|
||||
public float left
|
||||
{
|
||||
get { return m_Left; }
|
||||
set { if (PropertyUtil.SetStruct(ref m_Left, value)) SetAllDirty(); }
|
||||
}
|
||||
/// <summary>
|
||||
/// Distance between grid component and the right side of the container.
|
||||
/// grid 组件离容器右侧的距离。
|
||||
/// </summary>
|
||||
public float right
|
||||
{
|
||||
get { return m_Right; }
|
||||
set { if (PropertyUtil.SetStruct(ref m_Right, value)) SetAllDirty(); }
|
||||
}
|
||||
/// <summary>
|
||||
/// Distance between grid component and the top side of the container.
|
||||
/// grid 组件离容器上侧的距离。
|
||||
/// </summary>
|
||||
public float top
|
||||
{
|
||||
get { return m_Top; }
|
||||
set { if (PropertyUtil.SetStruct(ref m_Top, value)) SetAllDirty(); }
|
||||
}
|
||||
/// <summary>
|
||||
/// Distance between grid component and the bottom side of the container.
|
||||
/// grid 组件离容器下侧的距离。
|
||||
/// </summary>
|
||||
public float bottom
|
||||
{
|
||||
get { return m_Bottom; }
|
||||
set { if (PropertyUtil.SetStruct(ref m_Bottom, value)) SetAllDirty(); }
|
||||
}
|
||||
/// <summary>
|
||||
/// Background color of grid, which is transparent by default.
|
||||
/// 网格背景色,默认透明。
|
||||
/// </summary>
|
||||
public Color backgroundColor
|
||||
{
|
||||
get { return m_BackgroundColor; }
|
||||
set { if (PropertyUtil.SetColor(ref m_BackgroundColor, value)) SetVerticesDirty(); }
|
||||
}
|
||||
|
||||
public bool IsPointerEnter()
|
||||
{
|
||||
return context.isPointerEnter;
|
||||
}
|
||||
|
||||
public void UpdateRuntimeData(float chartX, float chartY, float chartWidth, float chartHeight)
|
||||
{
|
||||
context.left = left <= 1 ? left * chartWidth : left;
|
||||
context.bottom = bottom <= 1 ? bottom * chartHeight : bottom;
|
||||
context.top = top <= 1 ? top * chartHeight : top;
|
||||
context.right = right <= 1 ? right * chartWidth : right;
|
||||
context.x = chartX + context.left;
|
||||
context.y = chartY + context.bottom;
|
||||
context.width = chartWidth - context.left - context.right;
|
||||
context.height = chartHeight - context.top - context.bottom;
|
||||
context.position = new Vector3(context.x, context.y);
|
||||
}
|
||||
|
||||
public bool Contains(Vector3 pos)
|
||||
{
|
||||
return Contains(pos.x, pos.y);
|
||||
}
|
||||
|
||||
public bool Contains(float x, float y)
|
||||
{
|
||||
if (x < context.x - 1 || x > context.x + context.width + 1 ||
|
||||
y < context.y - 1 || y > context.y + context.height + 1)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
11
Runtime/Coord/Grid/GridCoord.cs.meta
Normal file
11
Runtime/Coord/Grid/GridCoord.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: f99c74cc7f2c44bfcae9f5c40e6b7c46
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
19
Runtime/Coord/Grid/GridCoordContext.cs
Normal file
19
Runtime/Coord/Grid/GridCoordContext.cs
Normal file
@@ -0,0 +1,19 @@
|
||||
|
||||
using UnityEngine;
|
||||
|
||||
namespace XCharts
|
||||
{
|
||||
public class GridCoordContext : MainComponentContext, IRectContext
|
||||
{
|
||||
public float x { get; internal set; }
|
||||
public float y { get; internal set; }
|
||||
public float width { get; internal set; }
|
||||
public float height { get; internal set; }
|
||||
public Vector3 position { get; internal set; }
|
||||
public float left { get; internal set; }
|
||||
public float right { get; internal set; }
|
||||
public float bottom { get; internal set; }
|
||||
public float top { get; internal set; }
|
||||
public bool isPointerEnter { get; set; }
|
||||
}
|
||||
}
|
||||
11
Runtime/Coord/Grid/GridCoordContext.cs.meta
Normal file
11
Runtime/Coord/Grid/GridCoordContext.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 6a7c139761fe64d93be4c65619a0fd38
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
82
Runtime/Coord/Grid/GridCoordHandler.cs
Normal file
82
Runtime/Coord/Grid/GridCoordHandler.cs
Normal file
@@ -0,0 +1,82 @@
|
||||
|
||||
using System;
|
||||
using System.Text;
|
||||
using UnityEngine;
|
||||
using UnityEngine.UI;
|
||||
using XUGL;
|
||||
|
||||
namespace XCharts
|
||||
{
|
||||
[UnityEngine.Scripting.Preserve]
|
||||
internal sealed class GridCoordHandler : MainComponentHandler<GridCoord>
|
||||
{
|
||||
public override void InitComponent()
|
||||
{
|
||||
var grid = component;
|
||||
grid.painter = chart.painter;
|
||||
grid.refreshComponent = delegate ()
|
||||
{
|
||||
grid.UpdateRuntimeData(chart.chartX, chart.chartY, chart.chartWidth, chart.chartHeight);
|
||||
chart.OnCoordinateChanged();
|
||||
};
|
||||
grid.refreshComponent();
|
||||
}
|
||||
|
||||
public override void CheckComponent(StringBuilder sb)
|
||||
{
|
||||
var grid = component;
|
||||
if (grid.left >= chart.chartWidth)
|
||||
sb.Append("warning:grid->left > chartWidth\n");
|
||||
if (grid.right >= chart.chartWidth)
|
||||
sb.Append("warning:grid->right > chartWidth\n");
|
||||
if (grid.top >= chart.chartHeight)
|
||||
sb.Append("warning:grid->top > chartHeight\n");
|
||||
if (grid.bottom >= chart.chartHeight)
|
||||
sb.Append("warning:grid->bottom > chartHeight\n");
|
||||
if (grid.left + grid.right >= chart.chartWidth)
|
||||
sb.Append("warning:grid.left + grid.right > chartWidth\n");
|
||||
if (grid.top + grid.bottom >= chart.chartHeight)
|
||||
sb.Append("warning:grid.top + grid.bottom > chartHeight\n");
|
||||
}
|
||||
|
||||
public override void Update()
|
||||
{
|
||||
if (chart.isPointerInChart)
|
||||
{
|
||||
component.context.isPointerEnter = component.Contains(chart.pointerPos);
|
||||
}
|
||||
else
|
||||
{
|
||||
component.context.isPointerEnter = false;
|
||||
}
|
||||
}
|
||||
|
||||
public override void DrawBase(VertexHelper vh)
|
||||
{
|
||||
if (!SeriesHelper.IsAnyClipSerie(chart.series))
|
||||
{
|
||||
DrawCoord(vh);
|
||||
}
|
||||
}
|
||||
public override void DrawTop(VertexHelper vh)
|
||||
{
|
||||
if (SeriesHelper.IsAnyClipSerie(chart.series))
|
||||
{
|
||||
DrawCoord(vh);
|
||||
}
|
||||
}
|
||||
|
||||
private void DrawCoord(VertexHelper vh)
|
||||
{
|
||||
var grid = component;
|
||||
if (grid.show && !ChartHelper.IsClearColor(grid.backgroundColor))
|
||||
{
|
||||
var p1 = new Vector2(grid.context.x, grid.context.y);
|
||||
var p2 = new Vector2(grid.context.x, grid.context.y + grid.context.height);
|
||||
var p3 = new Vector2(grid.context.x + grid.context.width, grid.context.y + grid.context.height);
|
||||
var p4 = new Vector2(grid.context.x + grid.context.width, grid.context.y);
|
||||
UGL.DrawQuadrilateral(vh, p1, p2, p3, p4, grid.backgroundColor);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
11
Runtime/Coord/Grid/GridCoordHandler.cs.meta
Normal file
11
Runtime/Coord/Grid/GridCoordHandler.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 7e22a9b603e57459f97040b285f3936a
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
Reference in New Issue
Block a user