Example #1
0
        private void Unregister()
        {
            CharItemEffectController control = Controller;

            if (control != null)
            {
                control.Unregister(this);
            }
        }
Example #2
0
        private void GeneratePie(bool update)
        {
            if (mFixPositionPie == null)
            {
                update = false;
            }
            if (update == false)
            {
                ClearChart();
            }
            else
            {
                EnsureTextController();
            }
            if (((IInternalPieData)Data).InternalDataSource == null)
            {
                return;
            }

            double[,] data = ((IInternalPieData)Data).InternalDataSource.getRawData();
            int rowCount    = data.GetLength(0);
            int columnCount = data.GetLength(1);

            if (rowCount != 1) // row count for pie must be 1
            {
                return;
            }

            double total = 0.0;

            for (int i = 0; i < columnCount; ++i)
            {
                double val = Math.Max(data[0, i], 0);
                total += val;
            }

            float start = startAngle;

            if (clockWise)
            {
                start -= angleSpan;
            }
            float totalGaps       = columnCount * spacingAngle;
            float spanWithoutGaps = angleSpan - totalGaps;

            if (spanWithoutGaps < 0f)
            {
                spanWithoutGaps = 0f;
            }

            if (mFixPositionPie == null)
            {
                mFixPositionPie = new GameObject("FixPositionPie", typeof(ChartItem));
                ChartCommon.HideObject(mFixPositionPie, hideHierarchy);
                mFixPositionPie.transform.SetParent(transform, false);
                if (IsCanvas)
                {
                    var rectTrans = mFixPositionPie.AddComponent <RectTransform>();
                    rectTrans.anchorMax        = new Vector2(0.5f, 0.5f);
                    rectTrans.anchorMin        = new Vector2(0.5f, 0.5f);
                    rectTrans.pivot            = new Vector2(0.5f, 0.5f);
                    rectTrans.anchoredPosition = new Vector2(0.5f, 0.5f);
                }
            }

            for (int i = 0; i < columnCount; ++i)
            {
                object userData    = ((IInternalPieData)Data).InternalDataSource.Columns[i].UserData;
                float  radiusScale = 1f;
                float  depthScale  = 1f;
                float  depthOffset = 0f;
                if (userData != null && userData is PieData.CategoryData)
                {
                    radiusScale = ((PieData.CategoryData)userData).RadiusScale;
                    depthScale  = ((PieData.CategoryData)userData).DepthScale;
                    depthOffset = ((PieData.CategoryData)userData).DepthOffset;
                }
                if (radiusScale <= 0.001f)
                {
                    radiusScale = 1f;
                }
                if (depthScale <= 0.001f)
                {
                    depthScale = 1f;
                }
                string name   = ((IInternalPieData)Data).InternalDataSource.Columns[i].Name;
                double amount = Math.Max(data[0, i], 0);
                if (amount == 0f)
                {
                    continue;
                }
                float                    weight      = (float)(amount / total);
                float                    currentSpan = spanWithoutGaps * weight;
                GameObject               pieObject   = null;
                IPieGenerator            generator   = null;
                PieObject                dataObject;
                CanvasLines.LineSegement line;
                float                    modifiedRadius = Mathf.Max(radius * radiusScale, torusRadius);
                //  float modifiedDepth = d
                float lineAngle = start + currentSpan * 0.5f;
                if (mPies.TryGetValue(name, out dataObject))
                {
                    dataObject.StartAngle = start;
                    dataObject.AngleSpan  = currentSpan;
                    generator             = dataObject.Generator;
                    if (dataObject.ItemLabel)
                    {
                        Vector3 labelPos = AlignTextPosition(mItemLabels, dataObject, out line, modifiedRadius);
                        dataObject.ItemLabel.transform.localPosition = labelPos;
                        string toSet = ChartAdancedSettings.Instance.FormatFractionDigits(mItemLabels.FractionDigits, amount, CustomNumberFormat);
                        toSet = mItemLabels.TextFormat.Format(toSet, name, "");
                        ChartCommon.UpdateTextParams(dataObject.ItemLabel.UIText, toSet);
                        if (dataObject.ItemLine != null)
                        {
                            var lst = new List <CanvasLines.LineSegement>();
                            lst.Add(line);
                            dataObject.ItemLine.SetLines(lst);
                        }
                    }
                    if (dataObject.CategoryLabel != null)
                    {
                        Vector3 labelPos = AlignTextPosition(mCategoryLabels, dataObject, out line, modifiedRadius);
                        dataObject.CategoryLabel.transform.localPosition = labelPos;
                        if (dataObject.CategoryLine != null)
                        {
                            var lst = new List <CanvasLines.LineSegement>();
                            lst.Add(line);
                            dataObject.CategoryLine.SetLines(lst);
                        }
                    }
                    Vector2 add = ChartCommon.FromPolar(start + currentSpan * 0.5f, Extrusion);
                    dataObject.TopObject.transform.localPosition = new Vector3(add.x, add.y, 0f);
                }
                else
                {
                    GameObject topObject = new GameObject();
                    if (IsCanvas)
                    {
                        topObject.AddComponent <RectTransform>();
                    }
                    ChartCommon.HideObject(topObject, hideHierarchy);
                    topObject.AddComponent <ChartItem>();
                    topObject.transform.SetParent(mFixPositionPie.transform);
                    topObject.transform.localPosition = new Vector3();
                    topObject.transform.localRotation = Quaternion.identity;
                    topObject.transform.localScale    = new Vector3(1f, 1f, 1f);

                    generator = PreparePieObject(out pieObject);

                    ChartCommon.EnsureComponent <ChartItem>(pieObject);
                    ChartMaterialController control = ChartCommon.EnsureComponent <ChartMaterialController>(pieObject);
                    control.Materials = Data.GetMaterial(name);
                    control.Refresh();
                    dataObject            = new PieObject();
                    dataObject.StartAngle = start;
                    dataObject.AngleSpan  = currentSpan;
                    dataObject.TopObject  = topObject;
                    dataObject.Generator  = generator;
                    dataObject.category   = name;
                    var pieInfo = pieObject.AddComponent <PieInfo>();
                    pieInfo.pieObject = dataObject;
                    pieObject.transform.SetParent(topObject.transform);
                    Vector2 add = ChartCommon.FromPolar(start + currentSpan * 0.5f, Extrusion);
                    pieObject.transform.localPosition = new Vector3(0f, 0f, 0f);
                    pieObject.transform.localScale    = new Vector3(1f, 1f, 1f);
                    pieObject.transform.localRotation = Quaternion.identity;
                    mPies.Add(name, dataObject);

                    topObject.transform.localPosition = new Vector3(add.x, add.y, 0f);
                    CharItemEffectController effect = ChartCommon.EnsureComponent <CharItemEffectController>(pieObject);
                    effect.WorkOnParent = true;
                    effect.InitialScale = false;

                    ChartItemEvents[] events = pieObject.GetComponentsInChildren <ChartItemEvents>();
                    for (int j = 0; j < events.Length; ++j)
                    {
                        if (events[j] == null)
                        {
                            continue;
                        }
                        InternalItemEvents comp = (InternalItemEvents)events[j];
                        comp.Parent   = this;
                        comp.UserData = dataObject;
                    }

                    if (mItemLabels != null)
                    {
                        Vector3 labelPos = AlignTextPosition(mItemLabels, dataObject, out line, modifiedRadius);
                        if (line != null && IsUnderCanvas)
                        {
                            dataObject.ItemLine = AddLineRenderer(topObject, line);
                        }
                        string toSet = ChartAdancedSettings.Instance.FormatFractionDigits(mItemLabels.FractionDigits, amount, CustomNumberFormat);
                        toSet = mItemLabels.TextFormat.Format(toSet, name, "");
                        BillboardText billboard = ChartCommon.CreateBillboardText(null, mItemLabels.TextPrefab, topObject.transform, toSet, labelPos.x, labelPos.y, labelPos.z, lineAngle, topObject.transform, hideHierarchy, mItemLabels.FontSize, mItemLabels.FontSharpness);
                        dataObject.ItemLabel = billboard;
                        TextController.AddText(billboard);
                    }

                    if (mCategoryLabels != null)
                    {
                        Vector3 labelPos = AlignTextPosition(mCategoryLabels, dataObject, out line, modifiedRadius);
                        if (line != null && IsUnderCanvas)
                        {
                            dataObject.CategoryLine = AddLineRenderer(topObject, line);
                        }
                        string toSet = name;
                        toSet = mCategoryLabels.TextFormat.Format(toSet, "", "");
                        BillboardText billboard = ChartCommon.CreateBillboardText(null, mCategoryLabels.TextPrefab, topObject.transform, toSet, labelPos.x, labelPos.y, labelPos.z, lineAngle, topObject.transform, hideHierarchy, mCategoryLabels.FontSize, mCategoryLabels.FontSharpness);
                        dataObject.CategoryLabel = billboard;
                        TextController.AddText(billboard);
                    }
                }
                float maxDepth  = Mathf.Max(OuterDepthLink, InnerDepthLink);
                float depthSize = maxDepth * depthScale;
                if (pieObject != null)
                {
                    float depthStart = (maxDepth - depthSize) * 0.5f;
                    pieObject.transform.localPosition = new Vector3(0f, 0f, depthStart - depthSize * depthOffset);
                }
                dataObject.Value = (float)data[0, i];
                generator.Generate(Mathf.Deg2Rad * start, Mathf.Deg2Rad * currentSpan, modifiedRadius, torusRadius, meshSegements, OuterDepthLink * depthScale, InnerDepthLink * depthScale);
                start += spacingAngle + currentSpan;
            }
        }
        /// <summary>
        /// Creates a single bar game object using the chart parameters and the bar prefab
        /// </summary>
        /// <param name="innerPosition">the local position of the bar in the chart</param>
        /// <returns>the new bar game object</returns>
        private GameObject CreateBar(Vector3 innerPosition, double value, float size, float elevation, float normalizedSize, string category, string group, int index, int categoryIndex)
        {
            if (BarPrefabLink == null)
            {
                GameObject dummy = new GameObject();
                dummy.AddComponent <ChartItem>();
                dummy.transform.parent = transform;
                return(dummy);
            }

            GameObject topLevel = new GameObject();

            topLevel.AddComponent <ChartItem>();
            topLevel.layer = gameObject.layer;
            topLevel.transform.SetParent(transform, false);
            topLevel.transform.localScale    = new Vector3(1f, 1f, 1f);
            topLevel.transform.localPosition = innerPosition;
            GameObject obj = (GameObject)GameObject.Instantiate(BarPrefabLink);

            Vector3 initialScale = obj.transform.localScale;

            obj.transform.SetParent(topLevel.transform, false);
            obj.transform.localScale = new Vector3(1f, 1f, 1f);

            CharItemEffectController effect = obj.GetComponent <CharItemEffectController>();

            if (effect == null)
            {
                effect = obj.AddComponent <CharItemEffectController>();
            }
            effect.WorkOnParent = true;
            effect.InitialScale = false;
            BarInfo inf = obj.AddComponent <BarInfo>();

            obj.AddComponent <ChartItem>();
            topLevel.transform.localRotation = Quaternion.identity;
            obj.transform.localRotation      = Quaternion.identity;
            obj.transform.localPosition      = Vector3.zero;
            ChartCommon.HideObject(obj, hideHierarchy);
            IBarGenerator generator = obj.GetComponent <IBarGenerator>();

            obj.layer = gameObject.layer;   // put the bar on the same layer as this object
            BarObject barObj = new BarObject();

            ChartCommon.HideObject(topLevel, hideHierarchy);
            barObj.Bar           = generator;
            barObj.BarGameObject = obj;
            barObj.InitialScale  = initialScale;
            barObj.TopObject     = topLevel;
            barObj.InnerPosition = innerPosition;
            barObj.Size          = size;
            barObj.Category      = category;
            barObj.Group         = group;
            barObj.Value         = value;
            barObj.Elevation     = elevation;
            inf.BarObject        = barObj;
            mBars.Add(new ChartItemIndex(index, categoryIndex), barObj);
            ChartItemEvents[] events = obj.GetComponentsInChildren <ChartItemEvents>();
            for (int i = 0; i < events.Length; ++i)
            {
                if (events[i] == null)
                {
                    continue;
                }
                InternalItemEvents comp = (InternalItemEvents)events[i];
                comp.Parent   = this;
                comp.UserData = barObj;
            }

            ChartMaterialController[] materialController = obj.GetComponentsInChildren <ChartMaterialController>();

            for (int i = 0; i < materialController.Length; i++)
            {
                ChartMaterialController m   = materialController[i];
                ChartDynamicMaterial    mat = Data.GetMaterial(category);
                if (mat != null)
                {
                    m.Materials = mat;
                    m.Refresh();
                }
            }

            float ySize = 1f * size * initialScale.y;

            if (generator != null)
            {
                generator.Generate(normalizedSize, ySize);
            }

            SetBarSize(obj, new Vector3(BarSizeLink.Breadth * initialScale.x, ySize, BarSizeLink.Depth * initialScale.z), elevation);

            if (mItemLabels != null && mItemLabels.isActiveAndEnabled)
            {
                string toSet = mItemLabels.TextFormat.Format(ChartAdancedSettings.Instance.FormatFractionDigits(mItemLabels.FractionDigits, value),
                                                             category, group);
                Vector3 labelPos = AlignLabel(mItemLabels, innerPosition, size + elevation);
                float   angle    = 45f;
                if (mItemLabels.Alignment == ChartLabelAlignment.Base)
                {
                    angle = -45f;
                }
                BillboardText billboard = ChartCommon.CreateBillboardText(null, mItemLabels.TextPrefab, topLevel.transform, toSet, labelPos.x, labelPos.y, labelPos.z, angle, obj.transform, hideHierarchy, mItemLabels.FontSize, mItemLabels.FontSharpness);
                //                billboard.UserData =
                //                billboard.UIText.fontSize = ItemLabels.FontSize;
                // billboard.transform.parent =;
                barObj.ItemLabel = billboard;
                TextController.AddText(billboard);
            }

            if (mGroupLabels != null && mGroupLabels.isActiveAndEnabled)
            {
                if (mGroupLabels.Alignment == GroupLabelAlignment.BarBottom || mGroupLabels.Alignment == GroupLabelAlignment.BarTop || (mGroupLabels.Alignment == GroupLabelAlignment.FirstBar && index == 0))
                {
                    Vector3 labelPos = AlignLabel(mGroupLabels, innerPosition, size + elevation);
                    string  toSet    = mGroupLabels.TextFormat.Format(group, category, group);
                    // float angle = 45f;
                    BillboardText billboard = ChartCommon.CreateBillboardText(null, mGroupLabels.TextPrefab, topLevel.transform, toSet, labelPos.x, labelPos.y, labelPos.z, 0f, obj.transform, hideHierarchy, mGroupLabels.FontSize, mGroupLabels.FontSharpness);
                    barObj.GroupLabel = billboard;
                    TextController.AddText(billboard);
                }
            }
            if (mCategoryLabels != null && mCategoryLabels.isActiveAndEnabled)
            {
                if (mCategoryLabels.VisibleLabels != CategoryLabels.ChartCategoryLabelOptions.FirstOnly ||
                    index == 0)
                {
                    Vector3 labelPos = AlignLabel(mCategoryLabels, innerPosition, size + elevation);
                    string  toSet    = mCategoryLabels.TextFormat.Format(category, category, group);
                    float   angle    = 45f;
                    if (mCategoryLabels.Alignment == ChartLabelAlignment.Base)
                    {
                        angle = -45f;
                    }
                    BillboardText billboard = ChartCommon.CreateBillboardText(null, mCategoryLabels.TextPrefab, topLevel.transform, toSet, labelPos.x, labelPos.y, labelPos.z, angle, obj.transform, hideHierarchy, mCategoryLabels.FontSize, mCategoryLabels.FontSharpness);
                    barObj.CategoryLabel = billboard;
                    TextController.AddText(billboard);
                }
            }

            if (Orientation == ChartOrientation.Horizontal)
            {
                obj.transform.localRotation = Quaternion.Euler(0f, 0, -90f);
            }
            return(obj);
        }
        private void GeneratePyramid(bool update)
        {
            if (update == false)
            {
                ClearChart();
            }
            else
            {
                EnsureTextController();
            }
            if (((IInternalPyramidData)Data).InternalDataSource == null)
            {
                return;
            }

            double[,] data = ((IInternalPyramidData)Data).InternalDataSource.getRawData();
            int rowCount    = data.GetLength(0);
            int columnCount = data.GetLength(1);

            if (rowCount != 1) // row count for pie must be 1
            {
                return;
            }

            double total = 0.0;

            for (int i = 0; i < columnCount; ++i)
            {
                double val = Math.Max(data[0, i], 0);
                total += val;
            }

            var rectTrans = GetComponent <RectTransform>();

            totalHeight = rectTrans.rect.height;
            totalWidth  = rectTrans.rect.width;

            float baseX1            = 0;
            float baseX2            = totalWidth;
            float accumilatedHeight = 0;
            float?firstCenterHeight = null;
            float acummilatedWeight = 0;

            for (int i = 0; i < columnCount; ++i)
            {
                object userData     = ((IInternalPyramidData)Data).InternalDataSource.Columns[i].UserData;
                var    categoryData = ((PyramidData.CategoryData)userData);

                string name   = ((IInternalPyramidData)Data).InternalDataSource.Columns[i].Name;
                double amount = Math.Max(data[0, i], 0);
                if (amount == 0f)
                {
                    continue;
                }

                float weight       = (float)(amount / total);
                float actualHeight = totalHeight * weight;

                float slopeRight = categoryData.RightSlope;
                float slopeLeft  = categoryData.LeftSlope;
                float atan;
                switch (slope)
                {
                case SlopeType.Center:
                    atan       = -Mathf.Atan2(totalHeight, totalWidth * 0.5f) * Mathf.Rad2Deg + 90;
                    slopeRight = atan;
                    slopeLeft  = atan;
                    break;

                case SlopeType.Left:
                    atan       = -Mathf.Atan2(totalHeight, totalWidth) * Mathf.Rad2Deg + 90;
                    slopeLeft  = 0;
                    slopeRight = atan;
                    break;

                case SlopeType.Right:
                    atan       = -Mathf.Atan2(totalHeight, totalWidth) * Mathf.Rad2Deg + 90;
                    slopeLeft  = atan;
                    slopeRight = 0;
                    break;

                default:
                    break;
                }
                GameObject        pyramidObject     = null;
                GameObject        pyramidBackObject = null;
                IPyramidGenerator generator         = null;
                IPyramidGenerator backgenerator     = null;
                PyramidObject     dataObject;
                float             centerHeight    = actualHeight * 0.5f + accumilatedHeight;
                float             unblendedHeight = centerHeight;
                if (firstCenterHeight.HasValue == false)
                {
                    firstCenterHeight = centerHeight;
                }
                centerHeight = Mathf.Lerp(firstCenterHeight.Value, centerHeight, categoryData.PositionBlend);

                if (mPyramids.TryGetValue(name, out dataObject))
                {
                    pyramidBackObject = dataObject.backObject;
                    pyramidObject     = dataObject.pyramidObject;
                    backgenerator     = dataObject.BackGenerator;
                    generator         = dataObject.Generator;
                    generator.SetParams(baseX1, baseX2, totalWidth, slopeLeft, slopeRight, actualHeight, inset, 0f, 1f);
                    if (backgenerator != null)
                    {
                        backgenerator.SetParams(baseX1, baseX2, totalWidth, slopeLeft, slopeRight, actualHeight, 0f, acummilatedWeight, acummilatedWeight + weight);
                    }
                    if (dataObject.ItemLabel)
                    {
                        Vector3 labelPos = AlignTextPosition(mItemLabels, dataObject, generator, centerHeight);
                        dataObject.ItemLabel.transform.localPosition = labelPos;
                        ChartCommon.UpdateTextParams(dataObject.ItemLabel.UIText, categoryData.Title);
                    }
                }
                else
                {
                    dataObject = new PyramidObject();

                    if (backMaterial != null)
                    {
                        var backGenerator = PreparePyramidObject(out pyramidBackObject);
                        backGenerator.SetParams(baseX1, baseX2, totalWidth, slopeLeft, slopeRight, actualHeight, 0f, acummilatedWeight, acummilatedWeight + weight);
                        dataObject.backObject    = pyramidBackObject;
                        dataObject.BackGenerator = backGenerator;
                        ChartCommon.HideObject(pyramidBackObject, hideHierarchy);
                        pyramidBackObject.transform.SetParent(transform, false);
                        ChartCommon.EnsureComponent <ChartItem>(pyramidBackObject);
                        ChartMaterialController backcontrol = ChartCommon.EnsureComponent <ChartMaterialController>(pyramidBackObject);
                        backcontrol.Materials = new ChartDynamicMaterial(backMaterial);
                        foreach (var itemEffect  in pyramidBackObject.GetComponents <ChartItemEffect>())
                        {
                            ChartCommon.SafeDestroy(itemEffect);
                        }
                        ChartCommon.SafeDestroy(backGenerator.ContainerObject);
                    }

                    generator = PreparePyramidObject(out pyramidObject);
                    generator.SetParams(baseX1, baseX2, totalWidth, slopeLeft, slopeRight, actualHeight, inset, 0f, 1f);
                    ChartCommon.HideObject(pyramidObject, hideHierarchy);
                    pyramidObject.transform.SetParent(transform, false);
                    ChartCommon.EnsureComponent <ChartItem>(pyramidObject);



                    ChartMaterialController control = ChartCommon.EnsureComponent <ChartMaterialController>(pyramidObject);
                    control.Materials = Data.GetMaterial(name);
                    control.Refresh();


                    dataObject.Generator     = generator;
                    dataObject.category      = name;
                    dataObject.pyramidObject = pyramidObject;
                    mPyramids.Add(name, dataObject);

                    CharItemEffectController effect = ChartCommon.EnsureComponent <CharItemEffectController>(pyramidObject);
                    effect.WorkOnParent = false;
                    effect.InitialScale = false;

                    ChartItemEvents[] events = pyramidObject.GetComponentsInChildren <ChartItemEvents>();
                    for (int j = 0; j < events.Length; ++j)
                    {
                        if (events[j] == null)
                        {
                            continue;
                        }
                        InternalItemEvents comp = (InternalItemEvents)events[j];
                        comp.Parent   = this;
                        comp.UserData = dataObject;
                    }


                    if (mItemLabels != null)
                    {
                        Vector3       labelPos  = AlignTextPosition(mItemLabels, dataObject, generator, 0f);
                        float         angle     = justification == JustificationType.LeftAligned ? -180f : 180f;
                        BillboardText billboard = ChartCommon.CreateBillboardText(null, mItemLabels.TextPrefab, dataObject.pyramidObject.transform, categoryData.Title, labelPos.x, labelPos.y, labelPos.z, angle, null, hideHierarchy, mItemLabels.FontSize, mItemLabels.FontSharpness);
                        dataObject.ItemLabel = billboard;
                        dataObject.ItemLabel.transform.localPosition = labelPos;
                        TextController.AddText(billboard);
                    }
                }

                dataObject.Text  = categoryData.Text;
                dataObject.Title = categoryData.Title;

                if (IsCanvas)
                {
                    if (pyramidObject != null)
                    {
                        Vector2 actualPosition = new Vector2(0.5f, centerHeight) + categoryData.Shift;
                        actualPosition = new Vector2(actualPosition.x, actualPosition.y / TotalHeight);
                        var objectRect = pyramidObject.GetComponent <RectTransform>();
                        objectRect.pivot            = new Vector2(0.5f, 0.5f);
                        objectRect.anchorMin        = actualPosition;
                        objectRect.anchorMax        = actualPosition;
                        objectRect.anchoredPosition = new Vector2();
                        objectRect.sizeDelta        = new Vector2(totalWidth, actualHeight);
                    }
                    if (pyramidBackObject != null)
                    {
                        Vector2 actualPosition = new Vector2(0.5f, unblendedHeight);
                        actualPosition = new Vector2(actualPosition.x, actualPosition.y / TotalHeight);
                        var objectRect = pyramidBackObject.GetComponent <RectTransform>();
                        objectRect.pivot            = new Vector2(0f, 0f);
                        objectRect.anchorMin        = actualPosition;
                        objectRect.anchorMax        = actualPosition;
                        objectRect.anchoredPosition = new Vector2();
                    }
                }
                accumilatedHeight += actualHeight;
                acummilatedWeight += weight;
                if (backgenerator != null)
                {
                    backgenerator.Generate();
                }
                generator.Generate();
                generator.GetUpperBase(out baseX1, out baseX2);
                generator.ApplyInfo(categoryData.Title, categoryData.Text, categoryData.Image, categoryData.Scale);
                generator.SetAlpha(categoryData.Alpha);
            }
        }