优化代码

This commit is contained in:
monitor1394
2018-10-13 07:03:54 +08:00
parent 4c5a2628d4
commit 72a0eb1e54
3 changed files with 1369 additions and 178 deletions

View File

@@ -15,7 +15,7 @@ namespace xcharts
public float top = 40;
public float bottom = 25f;
public float tickness = 0.6f;
public float scaleLen = 5.0f;
public float splitWidth = 5.0f;
}
[System.Serializable]
@@ -47,7 +47,7 @@ namespace xcharts
public void AddCategory(string category)
{
if (data.Count >= maxSplitNumber)
if (data.Count >= maxSplitNumber && maxSplitNumber != 0)
{
data.RemoveAt(0);
}
@@ -100,7 +100,7 @@ namespace xcharts
base.Awake();
lastCoordinateHig = chartHig;
lastCoordinateWid = chartWid;
lastCoordinateScaleLen = coordinate.scaleLen;
lastCoordinateScaleLen = coordinate.splitWidth;
InitXScale();
InitYScale();
}
@@ -220,19 +220,19 @@ namespace xcharts
float scaleWid = coordinateHig / (yAxis.splitNumber - 1);
if (yAxis.type == AxisType.value)
{
return new Vector3(zeroX - coordinate.scaleLen - 2f,
return new Vector3(zeroX - coordinate.splitWidth - 2f,
zeroY + i * scaleWid, 0);
}
else
{
if (yAxis.boundaryGap)
{
return new Vector3(zeroX - coordinate.scaleLen - 2f,
return new Vector3(zeroX - coordinate.splitWidth - 2f,
zeroY + (i + 0.5f) * scaleWid, 0);
}
else
{
return new Vector3(zeroX - coordinate.scaleLen - 2f,
return new Vector3(zeroX - coordinate.splitWidth - 2f,
zeroY + i * scaleWid, 0);
}
@@ -245,18 +245,18 @@ namespace xcharts
if (xAxis.type == AxisType.value)
{
return new Vector3(zeroX + (i + 1 - 0.5f) * scaleWid,
zeroY - coordinate.scaleLen - 10, 0);
zeroY - coordinate.splitWidth - 10, 0);
}
else
{
if (xAxis.boundaryGap)
{
return new Vector3(zeroX + (i + 1) * scaleWid, zeroY - coordinate.scaleLen - 5, 0);
return new Vector3(zeroX + (i + 1) * scaleWid, zeroY - coordinate.splitWidth - 5, 0);
}
else
{
return new Vector3(zeroX + (i + 1 - 0.5f) * scaleWid,
zeroY - coordinate.scaleLen - 5, 0);
zeroY - coordinate.splitWidth - 5, 0);
}
}
}
@@ -265,11 +265,11 @@ namespace xcharts
{
if (lastCoordinateHig != coordinateHig
|| lastCoordinateWid != coordinateWid
|| lastCoordinateScaleLen != coordinate.scaleLen)
|| lastCoordinateScaleLen != coordinate.splitWidth)
{
lastCoordinateWid = coordinateWid;
lastCoordinateHig = coordinateHig;
lastCoordinateScaleLen = coordinate.scaleLen;
lastCoordinateScaleLen = coordinate.splitWidth;
OnCoordinateSize();
}
if (checkCoordinate.show != coordinate.show)
@@ -388,7 +388,7 @@ namespace xcharts
// draw splitline
for (int i = 1; i < yAxis.splitNumber; i++)
{
float pX = zeroX - coordinate.scaleLen;
float pX = zeroX - coordinate.splitWidth;
float pY = zeroY + i * coordinateHig / (yAxis.splitNumber - 1);
ChartUtils.DrawLine(vh, new Vector3(pX, pY), new Vector3(zeroX, pY), coordinate.tickness,
themeInfo.axisLineColor);
@@ -401,7 +401,7 @@ namespace xcharts
for (int i = 1; i < xAxis.splitNumber; i++)
{
float pX = zeroX + i * coordinateWid / (xAxis.splitNumber - 1);
float pY = zeroY - coordinate.scaleLen - 2;
float pY = zeroY - coordinate.splitWidth - 2;
ChartUtils.DrawLine(vh, new Vector3(pX, zeroY), new Vector3(pX, pY), coordinate.tickness,
themeInfo.axisLineColor);
if (xAxis.showSplitLine)
@@ -411,10 +411,10 @@ namespace xcharts
}
}
//draw x,y axis
ChartUtils.DrawLine(vh, new Vector3(zeroX, zeroY - coordinate.scaleLen),
ChartUtils.DrawLine(vh, new Vector3(zeroX, zeroY - coordinate.splitWidth),
new Vector3(zeroX, zeroY + coordinateHig + 2), coordinate.tickness,
themeInfo.axisLineColor);
ChartUtils.DrawLine(vh, new Vector3(zeroX - coordinate.scaleLen, zeroY),
ChartUtils.DrawLine(vh, new Vector3(zeroX - coordinate.splitWidth, zeroY),
new Vector3(zeroX + coordinateWid + 2, zeroY), coordinate.tickness,
themeInfo.axisLineColor);
}

View File

@@ -48,7 +48,6 @@ namespace xcharts
public class LegendData
{
public bool show = true;
public ChartType type;
public string key;
public string text;
public Button button { get; set; }
@@ -59,9 +58,9 @@ namespace xcharts
{
public bool show = true;
public Location location = Location.right;
public float dataWid = 50.0f;
public float dataHig = 20.0f;
public float dataSpace = 5;
public float itemWidth = 50.0f;
public float itemHeight = 20.0f;
public float itemGap = 5;
public float left;
public float right = 5;
public float top;
@@ -77,6 +76,13 @@ namespace xcharts
}
}
[System.Serializable]
public class Tooltip
{
public bool show;
}
[System.Serializable]
public class SeriesData
{
@@ -266,7 +272,7 @@ namespace xcharts
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.dataWid, legend.dataHig));
new Vector2(legend.itemWidth, legend.itemHeight));
legend.dataList[i].button = btn;
Color bcolor = data.show ? themeInfo.GetColor(i) : themeInfo.unableColor;
btn.gameObject.SetActive(legend.show);
@@ -294,29 +300,29 @@ namespace xcharts
float startX = legend.left;
if (startX <= 0)
{
startX = (chartWid - (legendCount * legend.dataWid -
(legendCount - 1) * legend.dataSpace)) / 2;
startX = (chartWid - (legendCount * legend.itemWidth -
(legendCount - 1) * legend.itemGap)) / 2;
}
float posY = legend.location == Location.bottom ?
legend.bottom : chartHig - legend.top - legend.dataHig;
return new Vector3(startX + i * (legend.dataWid + legend.dataSpace), posY, 0);
legend.bottom : chartHig - legend.top - legend.itemHeight;
return new Vector3(startX + i * (legend.itemWidth + legend.itemGap), posY, 0);
case Location.left:
case Location.right:
float startY = 0;
if (legend.top > 0)
{
startY = chartHig - legend.top - legend.dataHig;
startY = chartHig - legend.top - legend.itemHeight;
}
else if (startY <= 0)
{
float legendHig = legendCount * legend.dataHig - (legendCount - 1) * legend.dataSpace;
float legendHig = legendCount * legend.itemHeight - (legendCount - 1) * legend.itemGap;
float offset = (chartHig - legendHig) / 2;
startY = chartHig - offset - legend.dataHig;
startY = chartHig - offset - legend.itemHeight;
}
float posX = legend.location == Location.left ?
legend.left :
chartWid - legend.right - legend.dataWid;
return new Vector3(posX, startY - i * (legend.dataHig + legend.dataSpace), 0);
chartWid - legend.right - legend.itemWidth;
return new Vector3(posX, startY - i * (legend.itemHeight + legend.itemGap), 0);
default: break;
}
return Vector3.zero;
@@ -365,9 +371,9 @@ namespace xcharts
OnLegendDataListChanged();
}
if (checkLegend.dataWid != legend.dataWid ||
checkLegend.dataHig != legend.dataHig ||
checkLegend.dataSpace != legend.dataSpace ||
if (checkLegend.itemWidth != legend.itemWidth ||
checkLegend.itemHeight != legend.itemHeight ||
checkLegend.itemGap != legend.itemGap ||
checkLegend.left != legend.left ||
checkLegend.right != legend.right ||
checkLegend.bottom != legend.bottom ||
@@ -375,9 +381,9 @@ namespace xcharts
checkLegend.location != legend.location ||
checkLegend.show != legend.show)
{
checkLegend.dataWid = legend.dataWid;
checkLegend.dataHig = legend.dataHig;
checkLegend.dataSpace = legend.dataSpace;
checkLegend.itemWidth = legend.itemWidth;
checkLegend.itemHeight = legend.itemHeight;
checkLegend.itemGap = legend.itemGap;
checkLegend.left = legend.left;
checkLegend.right = legend.right;
checkLegend.bottom = legend.bottom;
@@ -417,10 +423,10 @@ namespace xcharts
{
Button btn = legend.dataList[i].button;
btn.GetComponent<RectTransform>().sizeDelta =
new Vector2(legend.dataWid, legend.dataHig);
new Vector2(legend.itemWidth, legend.itemHeight);
Text txt = btn.GetComponentInChildren<Text>();
txt.transform.GetComponent<RectTransform>().sizeDelta =
new Vector2(legend.dataWid, legend.dataHig);
new Vector2(legend.itemWidth, legend.itemHeight);
txt.transform.localPosition = Vector3.zero;
btn.transform.localPosition = GetLegendPosition(i);
btn.gameObject.SetActive(legend.show);

File diff suppressed because it is too large Load Diff