增加JsonUtil工具类

This commit is contained in:
monitor1394
2024-05-09 13:13:51 +08:00
parent b86cae6717
commit 4e3182edfc
10 changed files with 161 additions and 24 deletions

View File

@@ -107,6 +107,7 @@ slug: /api
- [ISimplifiedSerie](#isimplifiedserie)
- [ItemStyle](#itemstyle)
- [IUpdateRuntimeData](#iupdateruntimedata)
- [JsonUtil](#jsonutil)
- [LabelLine](#labelline)
- [LabelStyle](#labelstyle)
- [Lang](#lang)
@@ -236,6 +237,7 @@ slug: /api
- [VisualMapHelper](#visualmaphelper)
- [VisualMapRange](#visualmaprange)
- [VisualMapTheme](#visualmaptheme)
- [Wrapper<T>](#wrappert)
- [XAxis](#xaxis)
- [XAxis3D](#xaxis3d)
- [XChartsMgr](#xchartsmgr)
@@ -461,6 +463,7 @@ The axis in rectangular coordinate.
|IsTop()||public bool IsTop()|
|IsValue()||public bool IsValue()<br/>是否为数值轴。 |
|RemoveData()||public void RemoveData(int dataIndex)|
|ResetStatus()||public override void ResetStatus()<br/>重置状态。 |
|SetComponentDirty()||public override void SetComponentDirty()|
|SetNeedUpdateFilterData()||public void SetNeedUpdateFilterData()|
|UpdateData()||public void UpdateData(int index, string category)<br/>更新类目数据 |
@@ -730,7 +733,7 @@ Bar chart shows different data through the height of a bar, which is used in rec
|AddData()||public SerieData AddData(string serieName, double xValue, double yValue, string dataName = null, string dataId = null)<br/>Add a (x,y) data to serie. |
|AddData()||public SerieData AddData(string serieName, List&lt;double&gt; multidimensionalData, string dataName = null, string dataId = null)<br/>Add an arbitray dimension data to serie,such as (x,y,z,...). |
|AddData()||public SerieData AddData(string serieName, params double[] multidimensionalData)<br/>Add an arbitray dimension data to serie,such as (x,y,z,...). |
|AddLink()||public SerieDataLink AddLink(int serieIndex, string sourceName, string targetName, double value)<br/>Add a link data to serie. |
|AddLink()||public SerieDataLink AddLink(int serieIndex, string sourceId, string targetId, double value = 0)<br/>Add a link data to serie. |
|AddSerie&lt;T&gt;()||public T AddSerie&lt;T&gt;(string serieName = null, bool show = true, bool addToHead = false) where T : Serie|
|AddXAxisData()||public void AddXAxisData(string category, int xAxisIndex = 0)<br/>Add a category data to xAxis. |
|AddXAxisIcon()||public void AddXAxisIcon(Sprite icon, int xAxisIndex = 0)<br/>Add an icon to xAxis. |
@@ -1446,7 +1449,7 @@ the data struct of graph.
|public method|since|description|
|--|--|--|
|AddEdge()||public GraphEdge AddEdge(string nodeId1, string nodeId2, double value)|
|AddNode()||public GraphNode AddNode(string nodeId, string nodeName, int dataIndex)|
|AddNode()||public GraphNode AddNode(string nodeId, string nodeName, int dataIndex, double value)|
|BreadthFirstTraverse()||public void BreadthFirstTraverse(GraphNode startNode, System.Action&lt;GraphNode&gt; onTraverse)|
|Clear()||public void Clear()|
|DeepFirstTraverse()||public void DeepFirstTraverse(GraphNode startNode, System.Action&lt;GraphNode&gt; onTraverse)|
@@ -1723,6 +1726,18 @@ The interface for serie data component.
> class in XCharts.Runtime / Subclasses: [SingleAxis](#singleaxis),[DataZoom](#datazoom),[CalendarCoord](#calendarcoord),[GridCoord](#gridcoord),[GridLayout](#gridlayout),[GridCoord3D](#gridcoord3d),[ParallelCoord](#parallelcoord)
## JsonUtil
> class in XCharts.Runtime
|public method|since|description|
|--|--|--|
|GetJsonArray&lt;T&gt;()||public static T[] GetJsonArray&lt;T&gt;(string json)|
|GetJsonObject&lt;T&gt;()||public static T GetJsonObject&lt;T&gt;(string json)|
|GetWebJson&lt;T&gt;()||public static IEnumerator GetWebJson&lt;T&gt;(string url, Action&lt;T[]&gt; callback)|
|GetWebJson&lt;T&gt;()||public static IEnumerator GetWebJson&lt;T&gt;(string url, Action&lt;T&gt; callback)|
## LabelLine
> class in XCharts.Runtime / Inherits from: [ChildComponent](#childcomponent),[ISerieComponent](#iseriecomponent),[ISerieDataComponent](#iseriedatacomponent)
@@ -2019,6 +2034,7 @@ Location type. Quick to set the general location.
|CompareTo()||public int CompareTo(object obj)|
|OnRemove()||public virtual void OnRemove()|
|Reset()||public virtual void Reset() { }|
|ResetStatus()||public virtual void ResetStatus() { }|
|SetAllDirty()||public virtual void SetAllDirty()|
|SetComponentDirty()||public virtual void SetComponentDirty()|
|SetDefaultValue()||public virtual void SetDefaultValue() { }|
@@ -2458,8 +2474,8 @@ Configurations of select state.
|AddData()||public SerieData AddData(List&lt;double&gt; valueList, string dataName = null, string dataId = null)<br/>将一组数据添加到系列中。 如果数据只有一个默认添加到维度Y中。 |
|AddData()||public SerieData AddData(params double[] values)<br/>添加任意维数据到系列中。 |
|AddExtraComponent&lt;T&gt;()||public T AddExtraComponent&lt;T&gt;() where T : ChildComponent, ISerieComponent|
|AddLink()||public SerieDataLink AddLink(string sourceName, string targetName, double value)<br/>Add a link data. |
|AddSerieData()||public void AddSerieData(SerieData serieData)|
|AddLink()||public virtual SerieDataLink AddLink(string sourceId, string targetId, double value = 0)<br/>Add a link data. |
|AddSerieData()||public virtual void AddSerieData(SerieData serieData)|
|AddXYData()||public SerieData AddXYData(double xValue, double yValue, string dataName = null, string dataId = null)<br/>添加xy数据到维度X和维度Y |
|AddYData()||public SerieData AddYData(double value, string dataName = null, string dataId = null)<br/>添加一个数据到维度Y此时维度X对应的数据是索引 |
|AnimationEnable()||public void AnimationEnable(bool flag)<br/>启用或取消初始动画 |
@@ -3388,6 +3404,11 @@ VisualMap component. Mapping data to visual elements such as colors.
|Copy()||public void Copy(VisualMapTheme theme)|
|VisualMapTheme()||public VisualMapTheme(ThemeType theme) : base(theme)|
## Wrapper&lt;T&gt;
> class in XCharts.Runtime
## XAxis
> class in XCharts.Runtime / Inherits from: [Axis](#axis)

View File

@@ -199,6 +199,7 @@ import APITable from '@site/src/components/APITable';
- [LangCandlestick](#langcandlestick)
- [LangTime](#langtime)
- [MainComponent](#maincomponent)
- [Wrapper&lt;T&gt;](#wrappert)
- [XCResourcesImporter](#xcresourcesimporter)
- [XCSettings](#xcsettings)
@@ -2520,6 +2521,10 @@ VisualMap component. Mapping data to visual elements such as colors.
</APITable>
```
## Wrapper&lt;T&gt;
> class in XCharts.Runtime
## XAxis
> class in XCharts.Runtime / Inherits from: [Axis](#axis)

View File

@@ -107,6 +107,7 @@ slug: /api
- [ISimplifiedSerie](#isimplifiedserie)
- [ItemStyle](#itemstyle)
- [IUpdateRuntimeData](#iupdateruntimedata)
- [JsonUtil](#jsonutil)
- [LabelLine](#labelline)
- [LabelStyle](#labelstyle)
- [Lang](#lang)
@@ -236,6 +237,7 @@ slug: /api
- [VisualMapHelper](#visualmaphelper)
- [VisualMapRange](#visualmaprange)
- [VisualMapTheme](#visualmaptheme)
- [Wrapper&lt;T&gt;](#wrappert)
- [XAxis](#xaxis)
- [XAxis3D](#xaxis3d)
- [XChartsMgr](#xchartsmgr)
@@ -461,6 +463,7 @@ slug: /api
|IsTop()||public bool IsTop()|
|IsValue()||public bool IsValue()<br/>是否为数值轴。 |
|RemoveData()||public void RemoveData(int dataIndex)|
|ResetStatus()||public override void ResetStatus()<br/>重置状态。 |
|SetComponentDirty()||public override void SetComponentDirty()|
|SetNeedUpdateFilterData()||public void SetNeedUpdateFilterData()|
|UpdateData()||public void UpdateData(int index, string category)<br/>更新类目数据 |
@@ -730,7 +733,7 @@ slug: /api
|AddData()||public SerieData AddData(string serieName, double xValue, double yValue, string dataName = null, string dataId = null)<br/>添加x,y数据到指定系列中。 |
|AddData()||public SerieData AddData(string serieName, List&lt;double&gt; multidimensionalData, string dataName = null, string dataId = null)<br/>添加多维数据x,y,z...)到指定的系列中。 |
|AddData()||public SerieData AddData(string serieName, params double[] multidimensionalData)<br/>添加多维数据x,y,z...)到指定的系列中。 |
|AddLink()||public SerieDataLink AddLink(int serieIndex, string sourceName, string targetName, double value)<br/>添加一个关系图的关系数据。 |
|AddLink()||public SerieDataLink AddLink(int serieIndex, string sourceId, string targetId, double value = 0)<br/>添加一个关系图的关系数据。 |
|AddSerie&lt;T&gt;()||public T AddSerie&lt;T&gt;(string serieName = null, bool show = true, bool addToHead = false) where T : Serie|
|AddXAxisData()||public void AddXAxisData(string category, int xAxisIndex = 0)<br/>添加一个类目数据到指定的x轴。 |
|AddXAxisIcon()||public void AddXAxisIcon(Sprite icon, int xAxisIndex = 0)<br/>添加一个图标到指定的x轴。 |
@@ -1446,7 +1449,7 @@ DataZoom 组件 用于区域缩放,从而能自由关注细节的数据信息
|API|版本|描述|
|--|--|--|
|AddEdge()||public GraphEdge AddEdge(string nodeId1, string nodeId2, double value)|
|AddNode()||public GraphNode AddNode(string nodeId, string nodeName, int dataIndex)|
|AddNode()||public GraphNode AddNode(string nodeId, string nodeName, int dataIndex, double value)|
|BreadthFirstTraverse()||public void BreadthFirstTraverse(GraphNode startNode, System.Action&lt;GraphNode&gt; onTraverse)|
|Clear()||public void Clear()|
|DeepFirstTraverse()||public void DeepFirstTraverse(GraphNode startNode, System.Action&lt;GraphNode&gt; onTraverse)|
@@ -1723,6 +1726,18 @@ Drawing grid in rectangular coordinate. Line chart, bar chart, and scatter chart
> class in XCharts.Runtime / 子类: [SingleAxis](#singleaxis),[DataZoom](#datazoom),[CalendarCoord](#calendarcoord),[GridCoord](#gridcoord),[GridLayout](#gridlayout),[GridCoord3D](#gridcoord3d),[ParallelCoord](#parallelcoord)
## JsonUtil
> class in XCharts.Runtime
|API|版本|描述|
|--|--|--|
|GetJsonArray&lt;T&gt;()||public static T[] GetJsonArray&lt;T&gt;(string json)|
|GetJsonObject&lt;T&gt;()||public static T GetJsonObject&lt;T&gt;(string json)|
|GetWebJson&lt;T&gt;()||public static IEnumerator GetWebJson&lt;T&gt;(string url, Action&lt;T[]&gt; callback)|
|GetWebJson&lt;T&gt;()||public static IEnumerator GetWebJson&lt;T&gt;(string url, Action&lt;T&gt; callback)|
## LabelLine
> class in XCharts.Runtime / 继承自: [ChildComponent](#childcomponent),[ISerieComponent](#iseriecomponent),[ISerieDataComponent](#iseriedatacomponent)
@@ -2019,6 +2034,7 @@ Drawing grid in rectangular coordinate. Line chart, bar chart, and scatter chart
|CompareTo()||public int CompareTo(object obj)|
|OnRemove()||public virtual void OnRemove()|
|Reset()||public virtual void Reset() { }|
|ResetStatus()||public virtual void ResetStatus() { }|
|SetAllDirty()||public virtual void SetAllDirty()|
|SetComponentDirty()||public virtual void SetComponentDirty()|
|SetDefaultValue()||public virtual void SetDefaultValue() { }|
@@ -2458,8 +2474,8 @@ Radar coordinate conponnet for radar charts. 雷达图坐标系组件,只适
|AddData()||public SerieData AddData(List&lt;double&gt; valueList, string dataName = null, string dataId = null)<br/>将一组数据添加到系列中。 如果数据只有一个默认添加到维度Y中。 |
|AddData()||public SerieData AddData(params double[] values)<br/>添加任意维数据到系列中。 |
|AddExtraComponent&lt;T&gt;()||public T AddExtraComponent&lt;T&gt;() where T : ChildComponent, ISerieComponent|
|AddLink()||public SerieDataLink AddLink(string sourceName, string targetName, double value)<br/>添加一个关系图的关系数据。 |
|AddSerieData()||public void AddSerieData(SerieData serieData)|
|AddLink()||public virtual SerieDataLink AddLink(string sourceId, string targetId, double value = 0)<br/>添加一个关系图的关系数据。 |
|AddSerieData()||public virtual void AddSerieData(SerieData serieData)|
|AddXYData()||public SerieData AddXYData(double xValue, double yValue, string dataName = null, string dataId = null)<br/>添加xy数据到维度X和维度Y |
|AddYData()||public SerieData AddYData(double value, string dataName = null, string dataId = null)<br/>添加一个数据到维度Y此时维度X对应的数据是索引 |
|AnimationEnable()||public void AnimationEnable(bool flag)<br/>启用或取消初始动画 |
@@ -3388,6 +3404,11 @@ UI帮助类。
|Copy()||public void Copy(VisualMapTheme theme)|
|VisualMapTheme()||public VisualMapTheme(ThemeType theme) : base(theme)|
## Wrapper&lt;T&gt;
> class in XCharts.Runtime
## XAxis
> class in XCharts.Runtime / 继承自: [Axis](#axis)

View File

@@ -73,6 +73,7 @@ slug: /changelog
## master
* (2024.05.09) 增加`JsonUtil`工具类
* (2024.04.23) 修复`ConvertXYAxis()`多次调用后图表异常的问题
* (2024.04.22) 修复`DataZoom`控制多个轴时的`GridCoord`获取可能不正确的问题 (#317)
* (2024.04.22) 增加`GridCoord3D`3D坐标系

View File

@@ -199,6 +199,7 @@ import APITable from '@site/src/components/APITable';
- [LangCandlestick](#langcandlestick)
- [LangTime](#langtime)
- [MainComponent](#maincomponent)
- [Wrapper&lt;T&gt;](#wrappert)
- [XCResourcesImporter](#xcresourcesimporter)
- [XCSettings](#xcsettings)
@@ -2442,6 +2443,10 @@ Serie的状态样式。Serie的状态有正常高亮淡出选中四种
</APITable>
```
## Wrapper&lt;T&gt;
> class in XCharts.Runtime
## XAxis
> class in XCharts.Runtime / 继承自: [Axis](#axis)

View File

@@ -447,16 +447,16 @@ namespace XCharts.Runtime
/// ||添加一个关系图的关系数据。
/// </summary>
/// <param name="serieIndex">the index of serie</param>
/// <param name="sourceName">the source name of link</param>
/// <param name="targetName">the target name of link</param>
/// <param name="sourceId">the source id of link</param>
/// <param name="targetId">the target id of link</param>
/// <param name="value">the value of link</param>
/// <returns></returns>
public SerieDataLink AddLink(int serieIndex, string sourceName, string targetName, double value)
public SerieDataLink AddLink(int serieIndex, string sourceId, string targetId, double value = 0)
{
var serie = GetSerie(serieIndex);
if (serie != null)
{
var link = serie.AddLink(sourceName, targetName, value);
var link = serie.AddLink(sourceId, targetId, value);
RefreshPainter(serie.painter);
return link;
}

View File

@@ -209,7 +209,7 @@ namespace XCharts.Runtime
}
}
public GraphNode AddNode(string nodeId, string nodeName, int dataIndex)
public GraphNode AddNode(string nodeId, string nodeName, int dataIndex, double value)
{
if (nodeMap.ContainsKey(nodeId))
{
@@ -248,9 +248,9 @@ namespace XCharts.Runtime
XLog.Warning("GraphData.AddEdge(): node2 is null");
return null;
}
if (node1 == node2)
if (directed && node1 == node2)
{
XLog.Warning("GraphData.AddEdge(): node1 == node2");
XLog.Warning("GraphData.AddEdge(): node1 == node2:" + node1);
return null;
}
string edgeKey = nodeId1 + "_" + nodeId2;
@@ -377,6 +377,7 @@ namespace XCharts.Runtime
{
public string id;
public string name;
public double value;
public List<GraphEdge> edges = new List<GraphEdge>();
public List<GraphEdge> inEdges = new List<GraphEdge>();
public List<GraphEdge> outEdges = new List<GraphEdge>();
@@ -386,11 +387,10 @@ namespace XCharts.Runtime
public int depth = -1;
public bool expand = true;
public int level = 0;
public Vector3 position;
public Vector3 delta;
public Vector3 position = Vector3.zero;
public Vector3 pp = Vector3.zero;
public float weight;
public float repulsion;
public Vector3 pp;
public GraphNode(string id, string name, int dataIndex)
{

View File

@@ -1357,7 +1357,7 @@ namespace XCharts.Runtime
return serieData;
}
public void AddSerieData(SerieData serieData)
public virtual void AddSerieData(SerieData serieData)
{
if (m_InsertDataToHead)
m_Data.Insert(0, serieData);
@@ -1547,15 +1547,15 @@ namespace XCharts.Runtime
/// Add a link data.
/// ||添加一个关系图的关系数据。
/// </summary>
/// <param name="sourceName"></param>
/// <param name="targetName"></param>
/// <param name="sourceId"></param>
/// <param name="targetId"></param>
/// <param name="value"></param>
/// <returns></returns>
public SerieDataLink AddLink(string sourceName, string targetName, double value)
public virtual SerieDataLink AddLink(string sourceId, string targetId, double value = 0)
{
var link = new SerieDataLink();
link.source = sourceName;
link.target = targetName;
link.source = sourceId;
link.target = targetId;
link.value = value;
m_Links.Add(link);
SetVerticesDirty();

View File

@@ -0,0 +1,73 @@
using System;
using System.Collections;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.Networking;
namespace XCharts.Runtime
{
public static class JsonUtil
{
public static IEnumerator GetWebJson<T>(string url, Action<T[]> callback)
{
var www = UnityWebRequest.Get(url);
yield return www;
#if UNITY_2020_1_OR_NEWER
if (www.result != UnityWebRequest.Result.Success)
#else
if (www.isNetworkError || www.isHttpError)
#endif
{
Debug.LogError("GetWebJson Error: " + www.error);
}
else
{
var json = www.downloadHandler.text.Trim();
callback(GetJsonArray<T>(json));
www.Dispose();
}
}
public static IEnumerator GetWebJson<T>(string url, Action<T> callback)
{
var www = UnityWebRequest.Get(url);
yield return www;
#if UNITY_2020_1_OR_NEWER
if (www.result != UnityWebRequest.Result.Success)
#else
if (www.isNetworkError || www.isHttpError)
#endif
{
Debug.LogError("GetWebJson Error: " + www.error);
}
else
{
var json = www.downloadHandler.text.Trim();
callback(GetJsonObject<T>(json));
www.Dispose();
}
}
public static T GetJsonObject<T>(string json)
{
return JsonUtility.FromJson<T>(json);
}
public static T[] GetJsonArray<T>(string json)
{
string newJson = "{ \"array\": " + json + "}";
Wrapper<T> wrapper = JsonUtility.FromJson<Wrapper<T>>(newJson);
return wrapper.array;
}
[Serializable]
private class Wrapper<T>
{
#pragma warning disable 0649
public T[] array;
#pragma warning restore 0649
}
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 88e9115d32af34a3dae0d5c3e32de41c
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant: