/// <summary>
        /// Creates a checkbox.
        /// </summary>
        /// <param name="value"></param>
        /// <param name="label">The checkbox label. Do not pass null. Pass an empty collection for no label.</param>
        /// <param name="setup">The setup object for the flow checkbox.</param>
        /// <param name="validationMethod">The validation method. Pass null if you’re only using the checkbox for page modification.</param>
        public FlowCheckbox(
            bool value, IReadOnlyCollection <PhrasingComponent> label, FlowCheckboxSetup setup = null,
            Action <PostBackValue <bool>, Validator> validationMethod = null)
        {
            setup = setup ?? FlowCheckboxSetup.Create();

            var checkbox = new Checkbox(value, label, setup: setup.CheckboxSetup, validationMethod: validationMethod);

            PageComponent = getComponent(
                setup.DisplaySetup,
                setup.Classes,
                setup.CheckboxSetup.PageModificationValue,
                checkbox,
                setup.HighlightedWhenChecked,
                setup.NestedContentGetter,
                setup.NestedContentAlwaysDisplayed);

            Validation = checkbox.Validation;
        }
예제 #2
0
        /// <summary>
        /// Creates a checkbox list.
        /// </summary>
        /// <param name="setup">The setup object for the checkbox list. Do not pass null.</param>
        /// <param name="value">The selected-item IDs.</param>
        /// <param name="validationMethod">The validation method. Pass null if you’re only using this control for page modification.</param>
        public CheckboxList(
            CheckboxListSetup <ItemIdType> setup, IEnumerable <ItemIdType> value, Action <IReadOnlyCollection <ItemIdType>, Validator> validationMethod = null)
        {
            var valueSet = value.ToImmutableHashSet();

            var selectedItemIdsInPostBack  = new List <ItemIdType>();
            var selectionChangedOnPostBack = false;
            var checkboxes = setup.Items.Select(
                i => new FlowCheckbox(
                    valueSet.Contains(i.Id),
                    i.Label.ToComponents(),
                    setup: FlowCheckboxSetup.Create(
                        highlightedWhenChecked: true,
                        action: new SpecifiedValue <FormAction>(setup.Action),
                        valueChangedAction: setup.SelectionChangedAction),
                    validationMethod: (postBackValue, validator) => {
                if (postBackValue.Value)
                {
                    selectedItemIdsInPostBack.Add(i.Id);
                }
                selectionChangedOnPostBack = selectionChangedOnPostBack || postBackValue.ChangedOnPostBack;
            }))
                             .Materialize();

            var contentContainerId = new ElementId();

            PageComponent = new GenericFlowContainer(
                (setup.IncludeSelectAndDeselectAllButtons
                                          ? new GenericFlowContainer(
                     new InlineList(
                         new EwfButton(
                             new StandardButtonStyle(
                                 "Select All",
                                 buttonSize: ButtonSize.ShrinkWrap,
                                 icon: new ActionComponentIcon(new FontAwesomeIcon("fa-check-square-o"))),
                             behavior: new CustomButtonBehavior(
                                 () => "$( '#{0}' ).find( 'input[type=checkbox]:not(:checked)' ).click();".FormatWith(contentContainerId.Id))).ToCollection()
                         .ToComponentListItem()
                         .ToCollection()
                         .Append(
                             new EwfButton(
                                 new StandardButtonStyle(
                                     "Deselect All",
                                     buttonSize: ButtonSize.ShrinkWrap,
                                     icon: new ActionComponentIcon(new FontAwesomeIcon("fa-square-o"))),
                                 behavior: new CustomButtonBehavior(
                                     () => "$( '#{0}' ).find( 'input[type=checkbox]:checked' ).click();".FormatWith(contentContainerId.Id))).ToCollection()
                             .ToComponentListItem())).ToCollection(),
                     classes: CheckboxListCssElementCreator.ActionContainerClass).ToCollection()
                                          : Enumerable.Empty <FlowComponent>()).Append(
                    new DisplayableElement(
                        context => {
                contentContainerId.AddId(context.Id);
                return(new DisplayableElementData(
                           null,
                           () => new DisplayableElementLocalData(
                               "div",
                               focusDependentData: new DisplayableElementFocusDependentData(
                                   attributes: setup.MinColumnWidth != null
                                                                                                    ? Tuple.Create("style", "column-width: {0}".FormatWith(((CssLength)setup.MinColumnWidth).Value)).ToCollection()
                                                                                                    : null,
                                   includeIdAttribute: true)),
                           classes: CheckboxListCssElementCreator.ContentContainerClass,
                           children: new RawList(from i in checkboxes select i.PageComponent.ToCollection().ToComponentListItem()).ToCollection()));
            }))
                .Materialize(),
                displaySetup: setup.DisplaySetup,
                classes: CheckboxListCssElementCreator.ListClass);

            if (validationMethod != null)
            {
                Validation = new EwfValidation(
                    validator => {
                    if (setup.ValidationPredicate != null && !setup.ValidationPredicate(selectionChangedOnPostBack))
                    {
                        return;
                    }
                    validationMethod(selectedItemIdsInPostBack, validator);
                });
            }
        }
예제 #3
0
        /// <summary>
        /// Creates a chart displaying a supported <see cref="ChartType"/> with the given data. Includes a chart and a table, and allows exporting the data to
        /// Excel. Assuming <paramref name="dataSets"/> has multiple elements, draws multiple sets of Y values on the same chart.
        /// </summary>
        /// <param name="setup">The setup object for the chart.</param>
        /// <param name="dataSets">The data sets.</param>
        /// <param name="colors">The colors to use for the data sets. Pass null for default colors. If you specify your own colors, the number of colors does not
        /// need to match the number of data sets. If you pass fewer colors than data sets, the chart will use random colors for the remaining data sets.</param>
        public Chart(ChartSetup setup, [NotNull] IReadOnlyCollection <ChartDataSet> dataSets, IEnumerable <Color> colors = null)
        {
            var rand = new Random();

            colors = (colors ?? getDefaultColors()).Take(dataSets.Count)
                     .Pad(dataSets.Count, () => Color.FromArgb(rand.Next(256), rand.Next(256), rand.Next(256)));

            string chartType;

            string toRgbaString(Color color, string opacity) => "rgba( {0}, {1}, {2}, {3} )".FormatWith(color.R, color.G, color.B, opacity);

            Func <ChartDataSet, Color, JObject> datasetSelector;
            var yAxisTicksCallbackProperty = setup.YAxisLabelFormatOptions != null
                                                                 ? new JProperty(
                "callback",
                new JRaw(
                    "function( value, index, values ) {{ return new Intl.NumberFormat( '{0}', {1} ).format( value ); }}".FormatWith(
                        Cultures.EnglishUnitedStates.Name,
                        setup.YAxisLabelFormatOptions.ToString(Formatting.None)))).ToCollection()
                                                                 : Enumerable.Empty <JProperty>();
            JObject options;

            switch (setup.ChartType)
            {
            case ChartType.Line:
                chartType       = "line";
                datasetSelector = (set, color) => new JObject(
                    new JProperty("label", set.Label),
                    new JProperty("data", new JArray(set.Values.TakeLast(setup.MaxXValues))),
                    new JProperty("pointBackgroundColor", toRgbaString(color, "1")),
                    new JProperty("backgroundColor", toRgbaString(color, "0.25")),
                    new JProperty("borderColor", toRgbaString(color, "1")));
                options = new JObject(
                    new JProperty("aspectRatio", setup.AspectRatio),
                    new JProperty("legend", new JObject(new JProperty("display", dataSets.Count > 1))),
                    new JProperty(
                        "scales",
                        new JObject(
                            new JProperty(
                                "xAxes",
                                new JArray(
                                    new JObject(
                                        new JProperty(
                                            "scaleLabel",
                                            new JObject(new JProperty("display", setup.XAxisTitle.Any()), new JProperty("labelString", setup.XAxisTitle)))))),
                            new JProperty(
                                "yAxes",
                                new JArray(
                                    new JObject(
                                        new JProperty(
                                            "scaleLabel",
                                            new JObject(new JProperty("display", setup.YAxisTitle.Any()), new JProperty("labelString", setup.YAxisTitle))),
                                        new JProperty(
                                            "ticks",
                                            new JObject(new JProperty("beginAtZero", true).ToCollection().Concat(yAxisTicksCallbackProperty)))))))));
                break;

            case ChartType.Bar:
            case ChartType.StackedBar:
            case ChartType.HorizontalBar:
            case ChartType.HorizontalStackedBar:
                var horizontal = setup.ChartType == ChartType.HorizontalBar || setup.ChartType == ChartType.HorizontalStackedBar;
                chartType = horizontal ? "horizontalBar" : "bar";

                var stacked = setup.ChartType == ChartType.StackedBar || setup.ChartType == ChartType.HorizontalStackedBar;
                datasetSelector = (set, color) => new JObject(
                    new JProperty("label", set.Label).ToCollection()
                    .Append(new JProperty("data", new JArray(set.Values.TakeLast(setup.MaxXValues))))
                    .Append(new JProperty("backgroundColor", toRgbaString(color, "1")))
                    .Concat(stacked ? new JProperty("stack", set.StackedGroupName).ToCollection() : Enumerable.Empty <JProperty>()));

                var xAxis = new JObject(
                    new JProperty("stacked", stacked),
                    new JProperty(
                        "scaleLabel",
                        new JObject(new JProperty("display", setup.XAxisTitle.Any()), new JProperty("labelString", setup.XAxisTitle))));
                var yAxis = new JObject(
                    new JProperty("stacked", stacked),
                    new JProperty("scaleLabel", new JObject(new JProperty("display", setup.YAxisTitle.Any()), new JProperty("labelString", setup.YAxisTitle))),
                    new JProperty("ticks", new JObject(new JProperty("beginAtZero", true).ToCollection().Concat(yAxisTicksCallbackProperty))));
                options = new JObject(
                    new JProperty("aspectRatio", setup.AspectRatio),
                    new JProperty("legend", new JObject(new JProperty("display", dataSets.Count > 1))),
                    new JProperty(
                        "scales",
                        new JObject(
                            new JProperty("xAxes", new JArray(horizontal ? yAxis : xAxis)),
                            new JProperty("yAxes", new JArray(horizontal ? xAxis : yAxis)))));

                break;

            default:
                throw new UnexpectedValueException(setup.ChartType);
            }

            var canvas = new GenericFlowContainer(
                new ElementComponent(
                    context => new ElementData(
                        () => {
                var jsInitStatement = "new Chart( '{0}', {{ type: '{1}', data: {2}, options: {3} }} );".FormatWith(
                    context.Id,
                    chartType,
                    new JObject(
                        new JProperty("labels", new JArray(setup.Labels.TakeLast(setup.MaxXValues))),
                        new JProperty("datasets", new JArray(dataSets.Zip(colors, (set, color) => datasetSelector(set, color))))).ToString(
                        Formatting.None),
                    options.ToString(Formatting.None));

                return(new ElementLocalData(
                           "canvas",
                           focusDependentData: new ElementFocusDependentData(includeIdAttribute: true, jsInitStatements: jsInitStatement)));
            })).ToCollection());

            var table = setup.OmitTable
                                            ? Enumerable.Empty <FlowComponent>()
                                            : new FlowCheckbox(
                false,
                "Show underlying data".ToComponents(),
                setup: FlowCheckboxSetup.Create(
                    nestedContentGetter: () =>
                    ColumnPrimaryTable.Create(postBackIdBase: setup.PostBackIdBase, allowExportToExcel: true, firstDataFieldIndex: 1)
                    .AddItems(
                        (setup.XAxisTitle.Any() || setup.Labels.Any(i => i.Any())
                                                                                                      ? EwfTableItem.Create(setup.XAxisTitle.ToCollection().Concat(setup.Labels).Select(i => i.ToCell()).Materialize())
                         .ToCollection()
                                                                                                      : Enumerable.Empty <EwfTableItem>()).Concat(
                            dataSets.Select(
                                dataSet => EwfTableItem.Create(
                                    dataSet.Label.ToCell().Concat(from i in dataSet.Values select i.ToString().ToCell()).Materialize())))
                        .Materialize())
                    .ToCollection())).ToFormItem()
                        .ToComponentCollection();

            children = new GenericFlowContainer(canvas.Concat(table).Materialize(), classes: elementClass).ToCollection();
        }