Mini Kabibi Habibi

Current Path : C:/Users/Public/Documents/DXperience 13.1 Demos/WPF/CS/ChartsDemo.Wpf/
Upload File :
Current File : C:/Users/Public/Documents/DXperience 13.1 Demos/WPF/CS/ChartsDemo.Wpf/Utils.cs

using System;
using System.Linq;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Data;
using System.Windows.Resources;
using System.Xml.Linq;
using DevExpress.Xpf.Charts;
using DevExpress.Xpf.Editors;
using System.Globalization;
using DevExpress.Utils;

namespace ChartsDemo {
    public static class DataLoader {
        public static XDocument LoadXmlFromResources(string fileName) {
            try {
                fileName = "/ChartsDemo;component" + fileName;
                Uri uri = new Uri(fileName, UriKind.RelativeOrAbsolute);
                StreamResourceInfo info = Application.GetResourceStream(uri);
                return XDocument.Load(info.Stream);
            }
            catch {
                return null;
            }
        }
    }

    public static class DemoModuleControlHelper {
        internal static void PrepareComboBox(ComboBoxEdit comboBox, params string[] items) {
            foreach (string item in items)
                comboBox.Items.Add(item);
            comboBox.SelectedIndex = 0;
        }
    }

    public static class ToolTipControlHelper {
        internal static void PrepareToolTipPositionComboBox(ComboBoxEdit comboBox) {
            comboBox.Items.Add("Mouse Pointer");
            comboBox.Items.Add("Relative");
            comboBox.Items.Add("Free");
            comboBox.SelectedIndex = 0;
        }
        internal static void PrepareToolTipLocationComboBox(ComboBoxEdit comboBox) {
            comboBox.Items.Add("Top Right");
            comboBox.Items.Add("Top Left");
            comboBox.Items.Add("Bottom Right");
            comboBox.Items.Add("Bottom Left");
            comboBox.SelectedIndex = 0;
        }
        internal static ToolTipLocation GetLocationFromComboBox(int selectedIndex) {
            switch (selectedIndex) {
                case 0:
                    return ToolTipLocation.TopRight;
                case 1:
                    return ToolTipLocation.TopLeft;
                case 2:
                    return ToolTipLocation.BottomRight;
                default:
                    return ToolTipLocation.BottomLeft;
            }
        }
    }

    public static class ResolveOverlappingModeHelper {
        public static void PrepareListBox(ListBoxEdit listBox, int index) {
            listBox.Items.Add("None");
            listBox.Items.Add("Default");
            listBox.Items.Add("Hide Overlapped");
            listBox.Items.Add("Justify Around Point");
            listBox.Items.Add("Justify All Around Point");
            listBox.SelectedIndex = index;
        }
        public static ResolveOverlappingMode GetMode(ListBoxEdit listBox) {
            switch(listBox.SelectedIndex) {
                case 0:
                    return ResolveOverlappingMode.None;
                case 1:
                    return ResolveOverlappingMode.Default;
                case 2:
                    return ResolveOverlappingMode.HideOverlapped;
                case 3:
                    return ResolveOverlappingMode.JustifyAroundPoint;
                case 4:
                    return ResolveOverlappingMode.JustifyAllAroundPoint;
                default:
                    return ResolveOverlappingMode.None;
            }
        }
    }

    public static class RangeArea2DHelper {
        public static void PrepareComboBox(ComboBoxEdit comboBox, int index) {
            comboBox.Items.Add("One Label");
            comboBox.Items.Add("Two Labels");
            comboBox.Items.Add("Min Value Label");
            comboBox.Items.Add("Max Value Label");
            comboBox.Items.Add("Value1 Label");
            comboBox.Items.Add("Value2 Label");
            comboBox.SelectedIndex = index;
        }
        public static  RangeAreaLabelKind GetMode(ComboBoxEdit comboBox) {
            switch (comboBox.SelectedIndex) {
                case 0:
                    return RangeAreaLabelKind.OneLabel;
                case 1:
                    return RangeAreaLabelKind.TwoLabels;
                case 2:
                    return RangeAreaLabelKind.MinValueLabel;
                case 3:
                    return RangeAreaLabelKind.MaxValueLabel;
                case 4:
                    return RangeAreaLabelKind.Value1Label;
                case 5:
                    return RangeAreaLabelKind.Value2Label;
                default:
                    return RangeAreaLabelKind.TwoLabels;
            }
        }
    }

    public static class Marker2DModelKindHelper {
        public static Marker2DKind FindActualMarker2DModelKind(Type modelType) {
            IEnumerable<Marker2DKind> marker2DKinds = Marker2DModel.GetPredefinedKinds();
            foreach (Marker2DKind marker2DKind in marker2DKinds) {
                if (Object.Equals(marker2DKind.Type, modelType))
                    return marker2DKind;
            }
            return null;
        }
    }

    public static class Bar3DModelKindHelper {
        public static void SetModel(ChartControl chart, Bar3DModel model) {
            foreach (BarSeries3D series in chart.Diagram.Series)
                series.Model = model;
        }
        public static Bar3DKind FindActualBar3DModelKind(Bar3DModel model) {
            if(model == null)
                return null;
            IEnumerable<Bar3DKind> bar3DKinds = Bar3DModel.GetPredefinedKinds();
            foreach(Bar3DKind bar3DKind in bar3DKinds) {
                if(Object.Equals(bar3DKind.Type, model.GetType()))
                    return bar3DKind;
            }
            return null;
        }
    }

    public static class Pie3DModelKindHelper {
        public static void SetModel(ChartControl chart, Pie3DModel model) {
            foreach(PieSeries3D series in chart.Diagram.Series)
                series.Model = model;
        }
        public static Pie3DKind FindActualPie3DModelKind(Pie3DModel model) {
            if(model == null)
                return null;
            IEnumerable<Pie3DKind> pie3DKinds = Pie3DModel.GetPredefinedKinds();
            foreach(Pie3DKind pie3DKind in pie3DKinds) {
                if(Object.Equals(pie3DKind.Type, model.GetType()))
                    return pie3DKind;
            }
            return null;
        }
    }

    public static class Marker3DModelKindHelper {
        public static void SetModel(ChartControl chart, Marker3DModel model) {
            foreach (MarkerSeries3D series in chart.Diagram.Series)
                series.Model = model;
        }
        public static Marker3DKind FindActualMarker3DModelKind(Marker3DModel model) {
            if (model == null)
                return null;
            IEnumerable<Marker3DKind> marker3DKinds = Marker3DModel.GetPredefinedKinds();
            foreach (Marker3DKind marker3DKind in marker3DKinds) {
                if (Object.Equals(marker3DKind.Type, model.GetType()))
                    return marker3DKind;
            }
            return null;
        }
    }
    public static class Pie2DModelKindHelper {
        public static Pie2DKind FindActualPie2DModelKind(Type modelType) {
            IEnumerable<Pie2DKind> pie2DKinds = Pie2DModel.GetPredefinedKinds();
            foreach (Pie2DKind pie2DType in pie2DKinds) {
                if (Object.Equals(pie2DType.Type, modelType))
                    return pie2DType;
            }
            return null;
        }
    }

    public static class Bar2DModelKindHelper {
        public static Bar2DKind FindActualBar2DModelKind(Type modelType) {
            IEnumerable<Bar2DKind> bar2DKinds = Bar2DModel.GetPredefinedKinds();
            foreach(Bar2DKind bar2DKind in bar2DKinds) {
                if (Object.Equals(bar2DKind.Type, modelType))
                    return bar2DKind;
            }
            return null;
        }
    }

    public static class RangeBar2DModelKindHelper {
        public static RangeBar2DKind FindActualRangeBar2DModelKind(Type modelType) {
            IEnumerable<RangeBar2DKind> bar2DKinds = RangeBar2DModel.GetPredefinedKinds();
            foreach (RangeBar2DKind bar2DKind in bar2DKinds) {
                if (Object.Equals(bar2DKind.Type, modelType))
                    return bar2DKind;
            }
            return null;
        }
    }

    public static class Stock2DModelKindHelper {
        public static Stock2DKind FindActualStock2DModelKind(Type modelType) {
            IEnumerable<Stock2DKind> stock2DKinds = Stock2DModel.GetPredefinedKinds();
            foreach (Stock2DKind stock2DKind in stock2DKinds) {
                if (Object.Equals(stock2DKind.Type, modelType))
                    return stock2DKind;
            }
            return null;
        }
    }

    public static class CandleStick2DModelKindHelper {
        public static CandleStick2DKind FindActualCandleStick2DModelKind(Type modelType) {
            IEnumerable<CandleStick2DKind> candleStick2DKinds = CandleStick2DModel.GetPredefinedKinds();
            foreach (CandleStick2DKind candleStick2DKind in candleStick2DKinds) {
                if (Object.Equals(candleStick2DKind.Type, modelType))
                    return candleStick2DKind;
            }
            return null;
        }
    }

    public class FinancialPoint : DependencyObject {
        string argument;
        double highValue;
        double lowValue;
        double openValue;
        double closeValue;

        public string Argument { get { return argument; } set { argument = value; } }
        public double HighValue { get { return highValue; } set { highValue = value; } }
        public double LowValue { get { return lowValue; } set { lowValue = value; } }
        public double OpenValue { get { return openValue; } set { openValue = value; } }
        public double CloseValue { get { return closeValue; } set { closeValue = value; } }
    }

    public class IndustryBubblePoint : DependencyObject {
        public static readonly DependencyProperty NameProperty ;
        public static readonly DependencyProperty NumberOfCasesProperty;
        public static readonly DependencyProperty RateProperty;

        static IndustryBubblePoint() {
            Type ownerType = typeof(IndustryBubblePoint);
            NameProperty = DependencyProperty.Register("Name", typeof(string), ownerType, new PropertyMetadata(String.Empty));
            NumberOfCasesProperty = DependencyProperty.Register("NumberOfCases", typeof(int), ownerType, new PropertyMetadata(0));
            RateProperty = DependencyProperty.Register("Rate", typeof(double), ownerType, new PropertyMetadata(0.0));
        }

        public string Name {
            get { return (string)GetValue(NameProperty); }
            set { SetValue(NameProperty, value); }
        }
        public int NumberOfCases {
            get { return (int)GetValue(NumberOfCasesProperty); }
            set { SetValue(NumberOfCasesProperty, value); }
        }
        public double Rate {
            get { return (double)GetValue(RateProperty); }
            set { SetValue(RateProperty, value); }
        }
    }

    public class SeriesTypeItem {
        readonly Type diagramType;
        readonly Type seriesType;
        readonly string seriesName;
        readonly int seriesCount;

        public Type DiagramType { get { return diagramType; } }
        public Type SeriesType { get { return seriesType; } }
        public int SeriesCount { get { return seriesCount; } }

        public SeriesTypeItem(Type diagramType, Type seriesType, string seriesName) : this(diagramType, seriesType, seriesName, 1) { }
        public SeriesTypeItem(Type diagramType, Type seriesType, string seriesName, int seriesCount) {
            this.diagramType = diagramType;
            this.seriesType = seriesType;
            this.seriesName = seriesName;
            this.seriesCount = seriesCount;
        }
        public override string ToString() {
            return seriesName;
        }
    }

    public class DemoValuesProvider {
        public IEnumerable<Bubble2DLabelPosition> Bubble2DLabelPositions { get { return DevExpress.Data.Mask.EnumHelper.GetValues(typeof(Bubble2DLabelPosition)).Cast<Bubble2DLabelPosition>(); } }
        public IEnumerable<Bar2DLabelPosition> Bar2DLabelPositions { get { return DevExpress.Data.Mask.EnumHelper.GetValues(typeof(Bar2DLabelPosition)).Cast<Bar2DLabelPosition>(); } }
        public IEnumerable<RangeAreaLabelKind> RangeAreaLabelKinds { get { return DevExpress.Data.Mask.EnumHelper.GetValues(typeof(RangeAreaLabelKind)).Cast<RangeAreaLabelKind>(); } }
        public IEnumerable<Bar2DKind> PredefinedBar2DKinds { get { return Bar2DModel.GetPredefinedKinds(); } }
        public IEnumerable<Marker2DKind> PredefinedMarker2DKinds { get { return Marker2DModel.GetPredefinedKinds(); } }
        public IEnumerable<CandleStick2DKind> PredefinedCandleStick2DKinds { get { return CandleStick2DModel.GetPredefinedKinds(); } }
        public IEnumerable<Stock2DKind> PredefinedStock2DKinds { get { return Stock2DModel.GetPredefinedKinds(); } }
        public IEnumerable<Pie2DKind> PredefinedPie2DKinds { get { return Pie2DModel.GetPredefinedKinds(); } }
        public IEnumerable<RangeBar2DKind> PredefinedRangeBar2DKinds { get { return RangeBar2DModel.GetPredefinedKinds(); } }
        public IEnumerable<ScrollBarAlignment> ScrollBarAlignments { get { return DevExpress.Data.Mask.EnumHelper.GetValues(typeof(ScrollBarAlignment)).Cast<ScrollBarAlignment>(); } }
    }

    public enum CircularFunction {
        TaubinsHeart,
        Cardioid,
        Lemniskate
    }

    public class FunctionsPointGenerator {
        public static List<Point> GeneratePoints(CircularFunction f) {
            switch (f) {
                case CircularFunction.TaubinsHeart:
                    return GeneratePointsOfTaubinsHeart();
                case CircularFunction.Cardioid:
                    return GeneratePointsOfCardioid();
                case CircularFunction.Lemniskate:
                    return GeneratePointsOfLemniskate();
                default:
                    return null;
            }
        }

        static List<Point> GeneratePointsOfLemniskate() {
            List<Point> list = new List<Point>();
            for (double x = 0; x < 360; x += 5) {
                double xRadian = DegreeToRadian(x);
                double cos = Math.Cos(2 * xRadian);
                double y = Math.Pow(Math.Abs(cos), 2);
                list.Add(new Point(x, y));
            }
            return list;
        }

        static List<Point> GeneratePointsOfCardioid() {
            List<Point> list = new List<Point>();
            const double a = 200;
            for (double x = 0; x < 360; x += 15) {
                double y = 2 * a * Math.Cos(DegreeToRadian(x));
                list.Add(new Point(x, y));
            }
            return list;
        }

        static List<Point> GeneratePointsOfTaubinsHeart() {
            List<Point> list = new List<Point>();
            for (double x = 0; x < 360; x += 15) {
                double xRadian = DegreeToRadian(x);
                double y = 2 - 2 * Math.Sin(xRadian) + Math.Sin(xRadian) * Math.Sqrt(Math.Abs(Math.Cos(xRadian))) / (Math.Sin(xRadian) + 1.4);
                list.Add(new Point(x, y));
            }
            return list;
        }

        static double DegreeToRadian(double degree) {
            return 2 * Math.PI / 360 * degree;
        }
    }

    public class Bar2DKindToTickmarksLengthConverter : IValueConverter {
        #region IValueConverter Members
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture) {
            Bar2DKind bar2DKind = value as Bar2DKind;
            if (bar2DKind != null) {
                switch (bar2DKind.Name) {
                    case "Glass Cylinder":
                        return 18;
                    case "Quasi-3D Bar":
                        return 9;
                }
            }
            return 5;
        }
        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) {
            return null;
        }
        #endregion
    }

    public class Bar2DKindToBar2DModelConverter : IValueConverter {
        #region IValueConverter Members
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture) {
            Bar2DKind bar2DKind = value as Bar2DKind;
            if (bar2DKind != null)
                return Activator.CreateInstance(bar2DKind.Type);
            return value;
        }
        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) {
            return null;
        }
        #endregion
    }

    public class RangeBar2DKindToRangeBar2DModelConverter : IValueConverter {
        #region IValueConverter Members
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture) {
            RangeBar2DKind bar2DKind = value as RangeBar2DKind;
            if (bar2DKind != null)
                return Activator.CreateInstance(bar2DKind.Type);
            return value;
        }
        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) {
            return null;
        }
        #endregion
    }

    public class Marker2DKindToMarker2DModelConverter : IValueConverter {
        #region IValueConverter Members
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture) {
            Marker2DKind marker2DKind = value as Marker2DKind;
            if (marker2DKind != null)
                return Activator.CreateInstance(marker2DKind.Type);
            return value;
        }
        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) {
            return null;
        }
        #endregion
    }

    public class CandleStick2DKindToCandleStick2DModelConverter : IValueConverter {
        #region IValueConverter Members
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture) {
            CandleStick2DKind candleStick2DKind = value as CandleStick2DKind;
            if (candleStick2DKind != null)
                return Activator.CreateInstance(candleStick2DKind.Type);
            return value;
        }
        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) {
            return null;
        }
        #endregion
    }

    public class Stock2DKindToStock2DModelConverter : IValueConverter {
        #region IValueConverter Members
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture) {
            Stock2DKind stock2DKind = value as Stock2DKind;
            if (stock2DKind != null)
                return Activator.CreateInstance(stock2DKind.Type);
            return value;
        }
        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) {
            return null;
        }
        #endregion
    }

    public class Pie2DKindToPie2DModelConverter : IValueConverter {
        #region IValueConverter Members
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture) {
            Pie2DKind pie2DKind = value as Pie2DKind;
            if (pie2DKind != null)
                return Activator.CreateInstance(pie2DKind.Type);
            return value;
        }
        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) {
            return null;
        }
        #endregion
    }

    public class MarkerSizeToLabelIndentConverter : IValueConverter {
        #region IValueConverter Members
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture) {
            return ((double)value) / 2;
        }
        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) {
            return null;
        }
        #endregion
    }

    public class IsCheckedToVisibilityConverter : IValueConverter {
        #region IValueConverter Members
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture) {
            if ((bool)value)
                return Visibility.Visible;
            return Visibility.Collapsed;
        }
        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) {
            return null;
        }
        #endregion
    }

    public class BoolToResolveOverlappingModeConverter : IValueConverter {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture) {
            bool booleanValue = (bool)value;
            if (booleanValue == true)
                return ResolveOverlappingMode.Default;
            else
                return ResolveOverlappingMode.None;
        }
        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) {
            return null;
        }
    }

    public class StringToRotationDirectionConverter : IValueConverter {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture) {
            string str = value as String;
            if (str == null || targetType != typeof(CircularDiagramRotationDirection))
                return null;
            if (str == "Clockwise")
                return CircularDiagramRotationDirection.Clockwise;
            else
                return CircularDiagramRotationDirection.Counterclockwise;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) {
            return null;
        }
    }

    public class StringToCircularDiagramShapeStyleConverter : IValueConverter {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture) {
            string str = value as string;
            if (str == null || targetType != typeof(CircularDiagramShapeStyle))
                return null;
            if (str == "Circle")
                return CircularDiagramShapeStyle.Circle;
            else
                return CircularDiagramShapeStyle.Polygon;
        }
        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) {
            return null;
        }
    }

    public class StringToCrosshairSnapModeConverter : IValueConverter {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture) {
            string str = value as string;
            if (str == null || targetType != typeof(CrosshairSnapMode))
                return null;
            if (str == "Nearest Argument")
                return CrosshairSnapMode.NearestArgument;
            else
                return CrosshairSnapMode.NearestValue;
        }
        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) {
            return null;
        }
    }

    public class NullableBooleanToInvertedBoolConverter : IValueConverter {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture) {
            if (value is bool? && targetType == typeof(bool?)) {
                bool? boolValue = (bool?)value;
                if (boolValue == true)
                    return false;
                else
                    return true;
            }
            return null;
        }
        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) {
            return null;
        }
    }

    public static class PaletteSelectorHelper {
        static Palette actualPalette = new OfficePalette();

        public static Palette ActualPalette {
            get { return actualPalette; }
            set { actualPalette = value; }
        }
    }
}