mirror of
https://github.com/XCharts-Team/XCharts.git
synced 2026-05-21 07:50:16 +00:00
增加JsonUtil工具类
This commit is contained in:
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
{
|
||||
|
||||
@@ -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();
|
||||
|
||||
73
Runtime/Utilities/JsonUtil.cs
Normal file
73
Runtime/Utilities/JsonUtil.cs
Normal 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
|
||||
}
|
||||
}
|
||||
}
|
||||
11
Runtime/Utilities/JsonUtil.cs.meta
Normal file
11
Runtime/Utilities/JsonUtil.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 88e9115d32af34a3dae0d5c3e32de41c
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
Reference in New Issue
Block a user