增加雷达图radar

This commit is contained in:
monitor1394
2018-09-25 07:33:04 +08:00
parent 6828722efe
commit 22ef40f0b1
6 changed files with 1589 additions and 33 deletions

13
Demo.cs
View File

@@ -1,4 +1,5 @@
using UnityEngine;
using UnityEngine.UI;
using xcharts;
public class Demo : MonoBehaviour
@@ -10,6 +11,18 @@ public class Demo : MonoBehaviour
void Awake()
{
lineChart = transform.Find("xchart/line_chart").GetComponent<LineChart>();
var xchart = transform.Find("xchart");
GridLayoutGroup grid = xchart.GetComponent<GridLayoutGroup>();
RectTransform rect = xchart.GetComponent<RectTransform>();
var wid = rect.sizeDelta.x;
int childNum = xchart.childCount;
int numWid =(int) ((wid - grid.padding.left - grid.padding.right) / (grid.cellSize.x+grid.spacing.x));
int numHig = (childNum + numWid - 1) / numWid;
float hig = grid.padding.top + numHig * (grid.cellSize.y+ grid.spacing.y);
Debug.LogError("child:"+childNum+","+grid.cellSize.y+","+grid.spacing.y+","+grid.padding.top+","+ wid+","+ numWid+","+ numHig+","+ hig);
rect.sizeDelta = new Vector2(wid,hig);
}
void Update()

View File

@@ -6,6 +6,7 @@ namespace xcharts
{
public static class ChartUtils
{
private static float CRICLE_SMOOTHNESS = 1f;
public static Text AddTextObject(string name, Transform parent, Font font, TextAnchor anchor,
Vector2 anchorMin, Vector2 anchorMax, Vector2 pivot, Vector2 sizeDelta, int fontSize = 14)
{
@@ -160,9 +161,9 @@ namespace xcharts
{
if(segments <= 0)
{
segments = (int)((2 * Mathf.PI * radius) / 10f);
segments = (int)((2 * Mathf.PI * radius) / CRICLE_SMOOTHNESS);
}
DrawSector(vh, p, radius, color, segments, 0, 360);
DrawSector(vh, p, radius, color, 0, 360, segments);
}
public static void DrawSector(VertexHelper vh, Vector3 p, float radius, Color color,
@@ -170,7 +171,7 @@ namespace xcharts
{
if (segments <= 0)
{
segments = (int)((2 * Mathf.PI * radius) / 10f);
segments = (int)((2 * Mathf.PI * radius) / CRICLE_SMOOTHNESS);
}
List<UIVertex> vertexs = new List<UIVertex>();
vh.GetUIVertexStream(vertexs);
@@ -197,10 +198,8 @@ namespace xcharts
}
if (segments <= 0)
{
segments = (int)((2 * Mathf.PI * outsideRadius) / 10f);
segments = (int)((2 * Mathf.PI * outsideRadius) / CRICLE_SMOOTHNESS);
}
List<UIVertex> vertexs = new List<UIVertex>();
vh.GetUIVertexStream(vertexs);
Vector3 p1, p2, p3, p4;
float startAngle = startDegree * Mathf.Deg2Rad;
float angle = (toDegree - startDegree) * Mathf.Deg2Rad / segments;

View File

@@ -23,16 +23,6 @@ namespace xcharts
public float top;
public float bottom;
public List<PieData> dataList;
public float GetDataTotal()
{
float total = 0;
foreach(var d in dataList)
{
total += d.value;
}
return total;
}
}
public class PieChart : BaseChart

171
Scripts/RadarChart.cs Normal file
View File

@@ -0,0 +1,171 @@
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
namespace xcharts
{
[System.Serializable]
public class RadarIndicator
{
public string name;
public float max;
}
[System.Serializable]
public class RadarInfo
{
public float radius = 100;
public int splitNumber = 5;
public float space;
public float left;
public float right;
public float top;
public float bottom;
public float lineTickness = 1f;
public float linePointSize = 5f;
public Color lineColor = Color.grey;
public List<Color> backgroundColorList;
public List<RadarIndicator> indicatorList;
}
public class RadarChart : BaseChart
{
[SerializeField]
private RadarInfo radarInfo;
private float radarCenterX = 0f;
private float radarCenterY = 0f;
private float radarRadius = 0;
protected override void Awake()
{
base.Awake();
}
protected override void Update()
{
base.Update();
}
protected override void OnPopulateMesh(VertexHelper vh)
{
base.OnPopulateMesh(vh);
UpdateRadarCenter();
DrawRadar(vh);
DrawData(vh);
}
protected override void OnLegendButtonClicked()
{
base.OnLegendButtonClicked();
}
private void DrawData(VertexHelper vh)
{
int indicatorNum = radarInfo.indicatorList.Count;
var angle = 2 * Mathf.PI / indicatorNum;
var p = new Vector3(radarCenterX, radarCenterY);
Vector3 startPoint = Vector3.zero;
Vector3 toPoint = Vector3.zero;
Vector3 firstPoint = Vector3.zero;
for (int i = 0; i < seriesList.Count; i++)
{
if (!legend.IsShowSeries(i)) continue;
var dataList = seriesList[i].dataList;
var color = legend.GetColor(i);
var max = radarInfo.indicatorList[i].max > 0 ?
radarInfo.indicatorList[i].max :
GetMaxValue();
List<Vector3> pointList = new List<Vector3>();
for (int j = 0; j < dataList.Count; j++)
{
var radius = radarInfo.radius * dataList[j].value / max;
var currAngle = j * angle;
if (j == 0)
{
startPoint = new Vector3(p.x + radius * Mathf.Sin(currAngle),
p.y + radius * Mathf.Cos(currAngle));
firstPoint = startPoint;
}
else
{
toPoint = new Vector3(p.x + radius * Mathf.Sin(currAngle),
p.y + radius * Mathf.Cos(currAngle));
ChartUtils.DrawLine(vh, startPoint, toPoint, radarInfo.lineTickness, color);
startPoint = toPoint;
}
pointList.Add(startPoint);
}
ChartUtils.DrawLine(vh, startPoint, firstPoint, radarInfo.lineTickness, color);
foreach (var point in pointList)
{
float radius = radarInfo.linePointSize - radarInfo.lineTickness * 2;
ChartUtils.DrawCricle(vh, point, radius, Color.white);
ChartUtils.DrawDoughnut(vh, point, radius, radarInfo.linePointSize, 0, 360, color);
}
}
}
private void DrawRadar(VertexHelper vh)
{
float insideRadius = 0, outsideRadius = 0;
float block = radarInfo.radius / radarInfo.splitNumber;
int indicatorNum = radarInfo.indicatorList.Count;
Vector3 p1, p2, p3, p4;
Vector3 p = new Vector3(radarCenterX,radarCenterY);
float angle = 2 * Mathf.PI / indicatorNum;
for (int i = 0; i < radarInfo.splitNumber; i++)
{
Color color = radarInfo.backgroundColorList[i% radarInfo.backgroundColorList.Count];
outsideRadius = insideRadius + block;
p1 = new Vector3(p.x + insideRadius * Mathf.Sin(0), p.y + insideRadius * Mathf.Cos(0));
p2 = new Vector3(p.x + outsideRadius * Mathf.Sin(0), p.y + outsideRadius * Mathf.Cos(0));
for (int j = 0; j <= indicatorNum; j++)
{
float currAngle = j * angle;
p3 = new Vector3(p.x + outsideRadius * Mathf.Sin(currAngle), p.y + outsideRadius * Mathf.Cos(currAngle));
p4 = new Vector3(p.x + insideRadius * Mathf.Sin(currAngle), p.y + insideRadius * Mathf.Cos(currAngle));
ChartUtils.DrawPolygon(vh, p1, p2, p3, p4, color);
ChartUtils.DrawLine(vh, p2, p3, radarInfo.lineTickness, radarInfo.lineColor);
p1 = p4;
p2 = p3;
}
insideRadius = outsideRadius;
}
for (int j = 0; j <= indicatorNum; j++)
{
float currAngle = j * angle;
p3 = new Vector3(p.x + outsideRadius * Mathf.Sin(currAngle), p.y + outsideRadius * Mathf.Cos(currAngle));
ChartUtils.DrawLine(vh, p, p3, radarInfo.lineTickness/2, radarInfo.lineColor);
}
}
private void UpdateRadarCenter()
{
float diffX = chartWid - radarInfo.left - radarInfo.right;
float diffY = chartHig - radarInfo.top - radarInfo.bottom;
float diff = Mathf.Min(diffX, diffY);
if (radarInfo.radius <= 0)
{
radarRadius = diff / 3 * 2;
radarCenterX = radarInfo.left + radarRadius;
radarCenterY = radarInfo.bottom + radarRadius;
}
else
{
radarRadius = radarInfo.radius;
radarCenterX = chartWid / 2;
radarCenterY = chartHig / 2;
if (radarInfo.left > 0) radarCenterX = radarInfo.left + radarRadius;
if (radarInfo.right > 0) radarCenterX = chartWid - radarInfo.right - radarRadius;
if (radarInfo.top > 0) radarCenterY = chartHig - radarInfo.top - radarRadius;
if (radarInfo.bottom > 0) radarCenterY = radarInfo.bottom + radarRadius;
}
}
}
}

View File

@@ -0,0 +1,13 @@
fileFormatVersion: 2
guid: d2231a0d3e3a5b043b074f6739be4a86
timeCreated: 1537742341
licenseType: Free
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

1404
demo.unity

File diff suppressed because it is too large Load Diff