mirror of
https://github.com/XCharts-Team/XCharts.git
synced 2026-05-21 07:50:16 +00:00
重构代码,整理图例和数据来源
This commit is contained in:
@@ -57,11 +57,11 @@ namespace xcharts
|
||||
}
|
||||
for (int i = startIndex; i < series.dataList.Count; i++)
|
||||
{
|
||||
SeriesData data = series.dataList[i];
|
||||
float data = series.dataList[i];
|
||||
float pX = zeroX + coordinate.tickness;
|
||||
float pY = zeroY + i * coordinateHig / (yAxis.splitNumber - 1);
|
||||
if (!yAxis.boundaryGap) pY -= scaleWid / 2;
|
||||
float barHig = data.value / max * coordinateWid;
|
||||
float barHig = data / max * coordinateWid;
|
||||
float space = offset + j * (barWid + barInfo.space);
|
||||
Vector3 p1 = new Vector3(pX, pY + space + barWid);
|
||||
Vector3 p2 = new Vector3(pX + barHig, pY + space + barWid);
|
||||
@@ -100,11 +100,11 @@ namespace xcharts
|
||||
}
|
||||
for (int i = startIndex; i < series.dataList.Count; i++)
|
||||
{
|
||||
SeriesData data = series.dataList[i];
|
||||
float data = series.dataList[i];
|
||||
float pX = zeroX + i * coordinateWid / (xAxis.splitNumber - 1);
|
||||
if (!xAxis.boundaryGap) pX -= scaleWid / 2;
|
||||
float pY = zeroY + coordinate.tickness;
|
||||
float barHig = data.value / max * coordinateHig;
|
||||
float barHig = data / max * coordinateHig;
|
||||
float space = offset + j * (barWid + barInfo.space);
|
||||
Vector3 p1 = new Vector3(pX + space, pY);
|
||||
Vector3 p2 = new Vector3(pX + space, pY + barHig);
|
||||
|
||||
@@ -209,7 +209,7 @@ namespace xcharts
|
||||
tooltip.SetActive(true);
|
||||
if (seriesList.Count == 1)
|
||||
{
|
||||
string txt = tempAxis.data[index] + ": " + seriesList[0].dataList[index].value;
|
||||
string txt = tempAxis.data[index] + ": " + seriesList[0].dataList[index];
|
||||
tooltip.UpdateTooltipText(txt);
|
||||
}
|
||||
else
|
||||
@@ -218,8 +218,8 @@ namespace xcharts
|
||||
for(int i=0; i<seriesList.Count;i++)
|
||||
{
|
||||
string strColor = ColorUtility.ToHtmlStringRGBA(themeInfo.GetColor(i));
|
||||
string key = seriesList[i].legendKey;
|
||||
float value = seriesList[i].dataList[index].value;
|
||||
string key = seriesList[i].name;
|
||||
float value = seriesList[i].dataList[index];
|
||||
sb.Append("\n");
|
||||
sb.AppendFormat("<color=#{0}>● </color>", strColor);
|
||||
sb.AppendFormat("{0}: {1}", key, value);
|
||||
|
||||
@@ -44,15 +44,6 @@ namespace xcharts
|
||||
end,
|
||||
}
|
||||
|
||||
[System.Serializable]
|
||||
public class LegendData
|
||||
{
|
||||
public bool show = true;
|
||||
public string key;
|
||||
public string text;
|
||||
public Button button { get; set; }
|
||||
}
|
||||
|
||||
[System.Serializable]
|
||||
public class Legend
|
||||
{
|
||||
@@ -65,14 +56,58 @@ namespace xcharts
|
||||
public float right = 5;
|
||||
public float top;
|
||||
public float bottom;
|
||||
public List<LegendData> dataList = new List<LegendData>();
|
||||
public List<string> dataList = new List<string>();
|
||||
public int checkDataListCount { get; set; }
|
||||
|
||||
private List<bool> dataShowList = new List<bool>();
|
||||
private List<Button> dataBtnList = new List<Button>();
|
||||
|
||||
public bool IsShowSeries(int seriesIndex)
|
||||
{
|
||||
if (seriesIndex < 0 || seriesIndex >= dataList.Count) seriesIndex = 0;
|
||||
if (seriesIndex < 0 || seriesIndex > dataList.Count - 1) seriesIndex = 0;
|
||||
if (seriesIndex >= dataList.Count) return false;
|
||||
return dataList[seriesIndex].show;
|
||||
if (seriesIndex < 0 || seriesIndex > dataShowList.Count - 1)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
else
|
||||
{
|
||||
return dataShowList[seriesIndex];
|
||||
}
|
||||
}
|
||||
|
||||
public void SetShowData(int index,bool flag)
|
||||
{
|
||||
dataShowList[index] = flag;
|
||||
}
|
||||
|
||||
public void SetDataButton(int index,Button btn)
|
||||
{
|
||||
if (index < 0 || index > dataBtnList.Count - 1)
|
||||
{
|
||||
dataBtnList.Add(btn);
|
||||
}
|
||||
else
|
||||
{
|
||||
dataBtnList[index] = btn;
|
||||
}
|
||||
}
|
||||
|
||||
public void SetShowData(string name,bool flag)
|
||||
{
|
||||
for(int i = 0; i < dataList.Count; i++)
|
||||
{
|
||||
if (dataList[i].Equals(name))
|
||||
{
|
||||
dataShowList[i] = flag;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public Button GetButton(int index)
|
||||
{
|
||||
return dataBtnList[index];
|
||||
}
|
||||
}
|
||||
|
||||
@@ -130,25 +165,12 @@ namespace xcharts
|
||||
}
|
||||
}
|
||||
|
||||
[System.Serializable]
|
||||
public class SeriesData
|
||||
{
|
||||
public string key;
|
||||
public float value;
|
||||
|
||||
public SeriesData(string key, float value)
|
||||
{
|
||||
this.key = key;
|
||||
this.value = value;
|
||||
}
|
||||
}
|
||||
|
||||
[System.Serializable]
|
||||
public class Series
|
||||
{
|
||||
public string legendKey;
|
||||
public string name;
|
||||
public int showDataNumber = 0;
|
||||
public List<SeriesData> dataList = new List<SeriesData>();
|
||||
public List<float> dataList = new List<float>();
|
||||
|
||||
public float Max
|
||||
{
|
||||
@@ -157,9 +179,9 @@ namespace xcharts
|
||||
float max = 0;
|
||||
foreach (var data in dataList)
|
||||
{
|
||||
if (data.value > max)
|
||||
if (data > max)
|
||||
{
|
||||
max = data.value;
|
||||
max = data;
|
||||
}
|
||||
}
|
||||
return max;
|
||||
@@ -173,19 +195,28 @@ namespace xcharts
|
||||
float total = 0;
|
||||
foreach (var data in dataList)
|
||||
{
|
||||
total += data.value;
|
||||
total += data;
|
||||
}
|
||||
return total;
|
||||
}
|
||||
}
|
||||
|
||||
public void AddData(string key, float value)
|
||||
public void AddData(float value)
|
||||
{
|
||||
if (dataList.Count >= showDataNumber && showDataNumber != 0)
|
||||
{
|
||||
dataList.RemoveAt(0);
|
||||
}
|
||||
dataList.Add(new SeriesData(key, value));
|
||||
dataList.Add(value);
|
||||
}
|
||||
|
||||
public float GetData(int index)
|
||||
{
|
||||
if (index >=0 && index <= dataList.Count - 1)
|
||||
{
|
||||
return dataList[index];
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -242,13 +273,13 @@ namespace xcharts
|
||||
}
|
||||
}
|
||||
|
||||
public void AddData(string legend, string key, float value)
|
||||
public void AddData(string legend, float value)
|
||||
{
|
||||
for (int i = 0; i < seriesList.Count; i++)
|
||||
{
|
||||
if (seriesList[i].legendKey == legend)
|
||||
if (seriesList[i].name == legend)
|
||||
{
|
||||
seriesList[i].AddData(key, value);
|
||||
seriesList[i].AddData(value);
|
||||
break;
|
||||
}
|
||||
}
|
||||
@@ -320,20 +351,20 @@ namespace xcharts
|
||||
{
|
||||
for (int i = 0; i < legend.dataList.Count; i++)
|
||||
{
|
||||
LegendData data = legend.dataList[i];
|
||||
//LegendData data = legend.dataList[i];
|
||||
Button btn = ChartUtils.AddButtonObject(LEGEND_TEXT + i, transform, themeInfo.font,
|
||||
themeInfo.textColor, Vector2.zero, Vector2.zero, Vector2.zero,
|
||||
new Vector2(legend.itemWidth, legend.itemHeight));
|
||||
legend.dataList[i].button = btn;
|
||||
Color bcolor = data.show ? themeInfo.GetColor(i) : themeInfo.unableColor;
|
||||
legend.SetDataButton(i,btn);
|
||||
Color bcolor = themeInfo.GetColor(i);
|
||||
btn.gameObject.SetActive(legend.show);
|
||||
btn.transform.localPosition = GetLegendPosition(i);
|
||||
btn.GetComponent<Image>().color = bcolor;
|
||||
btn.GetComponentInChildren<Text>().text = data.text;
|
||||
btn.GetComponentInChildren<Text>().text = legend.dataList[i];
|
||||
btn.onClick.AddListener(delegate ()
|
||||
{
|
||||
data.show = !data.show;
|
||||
btn.GetComponent<Image>().color = data.show ?
|
||||
legend.SetShowData(i,!legend.IsShowSeries(i));
|
||||
btn.GetComponent<Image>().color = legend.IsShowSeries(i) ?
|
||||
themeInfo.GetColor(i) : themeInfo.unableColor;
|
||||
OnYMaxValueChanged();
|
||||
OnLegendButtonClicked();
|
||||
@@ -504,7 +535,7 @@ namespace xcharts
|
||||
{
|
||||
for (int i = 0; i < legend.dataList.Count; i++)
|
||||
{
|
||||
Button btn = legend.dataList[i].button;
|
||||
Button btn = legend.GetButton(i);
|
||||
btn.GetComponent<RectTransform>().sizeDelta =
|
||||
new Vector2(legend.itemWidth, legend.itemHeight);
|
||||
Text txt = btn.GetComponentInChildren<Text>();
|
||||
|
||||
@@ -60,9 +60,9 @@ namespace xcharts
|
||||
}
|
||||
for (int i = startIndex; i < series.dataList.Count; i++)
|
||||
{
|
||||
SeriesData data = series.dataList[i];
|
||||
float value = series.dataList[i];
|
||||
|
||||
np = new Vector3(startX + i * scaleWid, zeroY + data.value * coordinateHig / max);
|
||||
np = new Vector3(startX + i * scaleWid, zeroY + value * coordinateHig / max);
|
||||
if (i > 0)
|
||||
{
|
||||
if (lineInfo.smooth)
|
||||
@@ -95,10 +95,10 @@ namespace xcharts
|
||||
{
|
||||
for (int i = 0; i < series.dataList.Count; i++)
|
||||
{
|
||||
SeriesData data = series.dataList[i];
|
||||
float value = series.dataList[i];
|
||||
|
||||
Vector3 p = new Vector3(startX + i * scaleWid,
|
||||
zeroY + data.value * coordinateHig / max);
|
||||
zeroY + value * coordinateHig / max);
|
||||
float pointWid = lineInfo.pointWid;
|
||||
if (tooltip.show && i == tooltip.DataIndex - 1)
|
||||
{
|
||||
|
||||
@@ -4,16 +4,10 @@ using UnityEngine.UI;
|
||||
|
||||
namespace xcharts
|
||||
{
|
||||
[System.Serializable]
|
||||
public class PieData
|
||||
{
|
||||
public string text;
|
||||
public float value;
|
||||
}
|
||||
|
||||
[System.Serializable]
|
||||
public class PieInfo
|
||||
{
|
||||
public string name;
|
||||
public float insideRadius = 0f;
|
||||
public float outsideRadius = 80f;
|
||||
public bool outsideRadiusDynamic = false;
|
||||
@@ -22,7 +16,6 @@ namespace xcharts
|
||||
public float right;
|
||||
public float top;
|
||||
public float bottom;
|
||||
public List<PieData> dataList = new List<PieData>();
|
||||
}
|
||||
|
||||
public class PieChart : BaseChart
|
||||
@@ -52,10 +45,10 @@ namespace xcharts
|
||||
float startDegree = 0;
|
||||
float dataTotal = GetDataTotal();
|
||||
float dataMax = GetDataMax();
|
||||
for (int i = 0; i < pieInfo.dataList.Count; i++)
|
||||
for (int i = 0; i < seriesList.Count; i++)
|
||||
{
|
||||
if (!legend.IsShowSeries(i)) continue;
|
||||
float value = pieInfo.dataList[i].value;
|
||||
float value = seriesList[i].dataList[0];
|
||||
float degree = totalDegree * value / dataTotal;
|
||||
float toDegree = startDegree + degree;
|
||||
|
||||
@@ -77,11 +70,11 @@ namespace xcharts
|
||||
private float GetDataTotal()
|
||||
{
|
||||
float total = 0;
|
||||
for(int i = 0; i < pieInfo.dataList.Count; i++)
|
||||
for(int i = 0; i < seriesList.Count; i++)
|
||||
{
|
||||
if (legend.IsShowSeries(i))
|
||||
{
|
||||
total += pieInfo.dataList[i].value;
|
||||
total += seriesList[i].GetData(0);
|
||||
}
|
||||
}
|
||||
return total;
|
||||
@@ -90,11 +83,11 @@ namespace xcharts
|
||||
private float GetDataMax()
|
||||
{
|
||||
float max = 0;
|
||||
for(int i = 0; i < pieInfo.dataList.Count; i++)
|
||||
for(int i = 0; i < seriesList.Count; i++)
|
||||
{
|
||||
if(legend.IsShowSeries(i) && pieInfo.dataList[i].value > max)
|
||||
if(legend.IsShowSeries(i) && seriesList[i].GetData(0) > max)
|
||||
{
|
||||
max = pieInfo.dataList[i].value;
|
||||
max = seriesList[i].GetData(0);
|
||||
}
|
||||
}
|
||||
return max;
|
||||
|
||||
@@ -194,7 +194,7 @@ namespace xcharts
|
||||
List<Vector3> pointList = new List<Vector3>();
|
||||
for (int j = 0; j < dataList.Count; j++)
|
||||
{
|
||||
var radius = radarInfo.radius * dataList[j].value / max;
|
||||
var radius = radarInfo.radius * dataList[j] / max;
|
||||
var currAngle = j * angle;
|
||||
if (j == 0)
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user