Пример #1
0
        /// <summary>
        /// Redraws the whole barcode
        /// </summary>
        public override void RedrawAll()
        {
            Children.Clear();

            double actualWidth  = ActualWidth;
            double actualHeight = ActualHeight;

            if ((double.IsNaN(actualWidth)) || (actualWidth <= 0))
            {
                actualWidth = Width;
            }
            if ((double.IsNaN(actualHeight)) || (actualHeight <= 0))
            {
                actualHeight = Height;
            }
            if ((double.IsNaN(actualWidth)) || (actualWidth <= 0))
            {
                return;
            }
            if ((double.IsNaN(actualHeight)) || (actualHeight <= 0))
            {
                return;
            }

            Rect rectClient = new Rect(0, 0, actualWidth, actualHeight);

            if (Value == null)
            {
                return;
            }

            string text = InlineHasValue.FormatValue(Value, Format);

            if (String.IsNullOrEmpty(text))
            {
                return;
            }

            List <BarcodeCharInfo> charInfo;
            List <int>             codeSequence = GenerateCodeSequence(text, out charInfo);

            if (codeSequence.Count <= 0)
            {
                return;
            }

            // override shown text
            if ((Text != null) && (!String.IsNullOrEmpty(Text)))
            {
                text = Text;
            }

            int    checkSum = 0;
            double left     = 0;

            // measure barcode first
            double startCharWidth = 0;

            for (int i = 0; i < codeSequence.Count; i++)
            {
                double charWidth = DrawCharByIndex(null, left, 0, 1, codeSequence[i]);
                if (i == 0)
                {
                    startCharWidth = charWidth;
                }
                left += charWidth;

                if (i == 0)
                {
                    checkSum = codeSequence[0];
                }
                else
                {
                    checkSum += codeSequence[i] * i;
                }
            }
            double barcodeDataWidth = left - startCharWidth;

            // draw check sum
            left += DrawCharByIndex(null, left, 0, 1, (checkSum % 103)); // Check Sum

            // draw stop char
            left += DrawCharByIndex(null, left, 0, 1, 106); // Stop

            double barcodeWidth = left;

            Canvas canvas = new Canvas();

            // show barcode text
            Label          labelText  = null;
            double         textHeight = 0;
            decimal        labelScale = 1;
            TransformGroup tgl        = null;

            if (ShowText)
            {
                labelText             = new Label();
                labelText.Content     = text;
                labelText.Padding     = new Thickness(0.25); // HACK: something is not right here
                labelText.FontFamily  = FontFamily;
                labelText.FontStretch = FontStretch;
                labelText.FontStyle   = FontStyle;
                labelText.FontWeight  = FontWeight;
                labelText.FontSize    = 1;
                labelText.Measure(new Size(actualWidth, actualHeight));

                labelScale = (decimal)rectClient.Width / (decimal)barcodeWidth * (decimal)barcodeDataWidth / (decimal)labelText.DesiredSize.Width;
                textHeight = labelText.DesiredSize.Height * (double)labelScale;

                // set new size of barcode text
                tgl = new TransformGroup();
                tgl.Children.Add(new ScaleTransform((double)labelScale, (double)labelScale));
                labelText.RenderTransform = tgl;
            }

            // draw barcode now
            left = 0;
            for (int i = 0; i < codeSequence.Count; i++)
            {
                double lineHeight = 1;
                double th         = 0;
                if (labelText != null)
                {
                    th = labelText.DesiredSize.Height;
                }
                if ((ShowText) && (i > 0))
                {
                    lineHeight = (rectClient.Height - (double)labelScale * th / 2) / rectClient.Height;
                }

                double charWidth = DrawCharByIndex(canvas, left, 0, lineHeight, codeSequence[i]);
                left += charWidth;
            }

            // draw check sum
            left += DrawCharByIndex(canvas, left, 0, 1, (checkSum % 103)); // Check Sum

            // draw stop char
            left += DrawCharByIndex(canvas, left, 0, 1, 106); // Stop

            // move text to right place
            if ((ShowText) && (labelText != null))
            {
                tgl.Children.Add(new TranslateTransform(startCharWidth * rectClient.Width / left, rectClient.Height - textHeight));
                Children.Add(labelText);
            }

            TransformGroup tg = new TransformGroup();

            tg.Children.Add(new ScaleTransform(rectClient.Width / left, rectClient.Height - textHeight / 2));
            tg.Children.Add(new TranslateTransform(rectClient.Left, rectClient.Top));
            canvas.RenderTransform = tg;
            Children.Add(canvas);
        }
Пример #2
0
        /// <summary>
        /// Computes an aggregate value and formats it
        /// </summary>
        /// <param name="values">list of values</param>
        /// <returns>calculated and formatted value</returns>
        /// <exception cref="NotSupportedException">The aggregate value type {0} is not supported yet!</exception>
        public string ComputeAndFormat(Dictionary <string, List <object> > values)
        {
            if ((values == null) || (values.Count <= 0))
            {
                return(_emptyValue);
            }
            if (!values.ContainsKey(_aggregateGroup))
            {
                return(_emptyValue);
            }

            decimal?result     = null;
            bool    isTimeSpan = false;
            long    count      = 0;

            foreach (object value in values[_aggregateGroup])
            {
                count++;
                if (_aggregateValueType == ReportAggregateValueType.Count)
                {
                    continue;                                                        // count needs no real calculation
                }
                decimal thisValue;
                if (value == null)
                {
                    return(_errorValue);
                }
                if (value is TimeSpan)
                {
                    thisValue  = Convert.ToDecimal(((TimeSpan)value).Ticks);
                    isTimeSpan = true;
                }
                else
                {
                    if (!Decimal.TryParse(value.ToString(), out thisValue))
                    {
                        return(_errorValue);
                    }
                }
                switch (_aggregateValueType)
                {
                case ReportAggregateValueType.Average:
                case ReportAggregateValueType.Sum:
                    if (result == null)
                    {
                        result = thisValue; break;
                    }
                    result += thisValue;
                    break;

                case ReportAggregateValueType.Maximum:
                    if (result == null)
                    {
                        result = thisValue; break;
                    }
                    if (thisValue > result)
                    {
                        result = thisValue;
                    }
                    break;

                case ReportAggregateValueType.Minimum:
                    if (result == null)
                    {
                        result = thisValue; break;
                    }
                    if (thisValue < result)
                    {
                        result = thisValue;
                    }
                    break;

                default:
                    throw new NotSupportedException(String.Format("The aggregate value type {0} is not supported yet!", _aggregateValueType.ToString()));
                }
            }
            if (_aggregateValueType == ReportAggregateValueType.Count)
            {
                result = count;
            }
            if (result == null)
            {
                return(_emptyValue);
            }

            if (_aggregateValueType == ReportAggregateValueType.Average)
            {
                result /= count;                                                          // calculate average
            }
            if (isTimeSpan)
            {
                return(TimeSpan.FromTicks(Convert.ToInt64(result)).ToString());             //for timespans
            }
            return(InlineHasValue.FormatValue(result, Format));
        }