Exemple #1
0
        private void RecalculatePositionsAutomaticDatetime(PlotDimensions dims, float labelWidth, float labelHeight, int?forcedTickCount)
        {
            double low, high;
            int    tickCount;

            if (MinimumTickSpacing > 0)
            {
                throw new InvalidOperationException("minimum tick spacing does not support DateTime ticks");
            }

            if (Orientation == AxisOrientation.Vertical)
            {
                low       = dims.YMin - dims.UnitsPerPxY; // add an extra pixel to capture the edge tick
                high      = dims.YMax + dims.UnitsPerPxY; // add an extra pixel to capture the edge tick
                tickCount = (int)(dims.DataHeight / labelHeight * TickDensity);
                tickCount = forcedTickCount ?? tickCount;
            }
            else
            {
                low       = dims.XMin - dims.UnitsPerPxX; // add an extra pixel to capture the edge tick
                high      = dims.XMax + dims.UnitsPerPxX; // add an extra pixel to capture the edge tick
                tickCount = (int)(dims.DataWidth / labelWidth * TickDensity);
                tickCount = forcedTickCount ?? tickCount;
            }

            if (low < high)
            {
                low  = Math.Max(low, new DateTime(0100, 1, 1, 0, 0, 0).ToOADate()); // minimum OADate value
                high = Math.Min(high, DateTime.MaxValue.ToOADate());

                var dtManualUnits   = (Orientation == AxisOrientation.Vertical) ? manualDateTimeSpacingUnitY : manualDateTimeSpacingUnitX;
                var dtManualSpacing = (Orientation == AxisOrientation.Vertical) ? manualSpacingY : manualSpacingX;

                try
                {
                    DateTime from = DateTime.FromOADate(low);
                    DateTime to   = DateTime.FromOADate(high);

                    var           unitFactory = new DateTimeUnitFactory();
                    IDateTimeUnit tickUnit    = unitFactory.CreateUnit(from, to, Culture, tickCount, dtManualUnits, (int)dtManualSpacing);
                    (tickPositionsMajor, tickLabels) = tickUnit.GetTicksAndLabels(from, to, dateTimeFormatString);
                    tickLabels = tickLabels.Select(x => x.Trim()).ToArray();
                }
                catch
                {
                    tickPositionsMajor = new double[] { }; // far zoom out can produce FromOADate() exception
                }
            }
            else
            {
                tickPositionsMajor = new double[] { };
            }

            // dont forget to set all the things
            tickPositionsMinor = null;
            CornerLabel        = null;
        }
        private void RecalculatePositionsAutomaticDatetime(Settings settings)
        {
            // the goal of this function is to set tickPositionsMajor, tickLabels, tickPositionsMinor, cornerLabel, and maxLabelSize
            double low, high;
            int    tickCount;

            // predict maxLabelSize up front using predetermined label sizes
            maxLabelSize = Drawing.GDI.MeasureString(settings.gfxData, "2019-08-20\n8:42:17 PM", settings.ticks.font);

            if (verticalAxis)
            {
                low       = settings.axes.y.min - settings.yAxisUnitsPerPixel; // add an extra pixel to capture the edge tick
                high      = settings.axes.y.max + settings.yAxisUnitsPerPixel; // add an extra pixel to capture the edge tick
                tickCount = (int)(settings.dataSize.Height / maxLabelSize.Height);
            }
            else
            {
                low       = settings.axes.x.min - settings.xAxisUnitsPerPixel; // add an extra pixel to capture the edge tick
                high      = settings.axes.x.max + settings.xAxisUnitsPerPixel; // add an extra pixel to capture the edge tick
                tickCount = (int)(settings.dataSize.Width / maxLabelSize.Width);
            }

            if (low < high)
            {
                low  = Math.Max(low, DateTime.MinValue.ToOADate());
                high = Math.Min(high, DateTime.MaxValue.ToOADate());

                var dtManualUnits   = (verticalAxis) ? settings.ticks.manualDateTimeSpacingUnitY : settings.ticks.manualDateTimeSpacingUnitX;
                var dtManualSpacing = (verticalAxis) ? settings.ticks.manualSpacingY : settings.ticks.manualSpacingX;

                try
                {
                    DateTime from = DateTime.FromOADate(low);
                    DateTime to   = DateTime.FromOADate(high);

                    var           unitFactory = new DateTimeUnitFactory();
                    IDateTimeUnit tickUnit    = unitFactory.CreateUnit(from, to, settings.culture, tickCount, dtManualUnits, (int)dtManualSpacing);
                    (tickPositionsMajor, tickLabels) = tickUnit.GetTicksAndLabels(from, to, dateTimeFormatString);
                }
                catch
                {
                    tickPositionsMajor = new double[] { }; // far zoom out can produce FromOADate() exception
                }
            }
            else
            {
                tickPositionsMajor = new double[] { };
            }

            // dont forget to set all the things
            tickPositionsMinor = null;
            cornerLabel        = null;
            maxLabelSize       = LargestLabel(settings, tickLabels);
        }