示例#1
0
        /// <summary>
        /// You can supply any of the parameters you like. You must supply either 'percentage' (as a whole number out of 100) or 'amount' and 'outOfTotal' parameters.
        /// </summary>
        /// <example>
        ///     <%=Beweb.Html.BarGraph(percentage:project.PercentOfBudget.ToInt(0), amount:actualHours, showAmount:true, decimalPlaces:1) %>
        ///     <%=Beweb.Html.BarGraph(amount:totalHoursUsed, outOfTotal:totalHoursPurchased, showPercentage:true) %>
        /// </example>
        public static string BarGraph(decimal?amount = null, decimal?outOfTotal = null, bool showPercentage = false, bool showAmount = false, bool showOutOf = false, decimal?percentage = null, string label = null, int decimalPlaces = 0)
        {
            /* You need to add the following CSS to your stylesheet.
             * .svyBarGraph { width: 150px; }
             * .svyBarGraphLabel { position: absolute;color:white;margin-left: 5px; }
             * .svyBarGraphOuter { width:50px;background:#333;float:left;margin-right:10px;height:18px;border-radius: 5px; }
             * .svyBarGraphInner { background:green;height:14px;margin-top:2px;margin-left:2px;border-radius: 4px; }
             */
            if (percentage == null && amount != null && outOfTotal != null)
            {
                percentage = Numbers.Floor(Numbers.SafeDivide(amount.Value, outOfTotal.Value) * 100);
            }
            else if (percentage == null)
            {
                percentage = 0;
            }
            if (showAmount && amount == null)
            {
                throw new BewebException("Html.BarGraph: You need to supply 'amount' parameter if you set 'showAmount' to true.");
            }
            if (showOutOf && outOfTotal == null)
            {
                throw new BewebException("Html.BarGraph: You need to supply 'outOfTotal' parameter if you set 'showOutOf' to true.");
            }

            if (label.IsBlank())
            {
                if (showAmount)
                {
                    label = Fmt.Number(amount, decimalPlaces);
                    if (showOutOf)
                    {
                        label += " of " + Fmt.Number(outOfTotal, decimalPlaces);
                    }
                    if (showPercentage)
                    {
                        label += " (" + Fmt.Percent(percentage, decimalPlaces) + ")";
                    }
                }
                else if (showPercentage)
                {
                    label = Fmt.Percent(percentage, decimalPlaces);
                }
            }

            var html      = new HtmlTag("div").Add("class", "svyBarGraph");
            var labelSpan = new HtmlTag("span").Add("class", "svyBarGraphLabel").SetInnerText(label);
            var barOuter  = new HtmlTag("div").Add("class", "svyBarGraphOuter");
            var barInner  = new HtmlTag("div").Add("class", "svyBarGraphInner").Add("style", "width:" + Fmt.Number(percentage, 0, false) + "%");

            barOuter.AddTag(barInner);
            html.AddTag(labelSpan);
            html.AddTag(barOuter);

            return(html.ToString());
        }