优化代码

This commit is contained in:
monitor1394
2018-10-13 07:03:54 +08:00
parent 481cf9ae68
commit c367caf5bd
3 changed files with 1369 additions and 178 deletions

View File

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

View File

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

1467
demo.unity

File diff suppressed because it is too large Load Diff