From 9e2f111af111894ffacceb268453fb85e80429c4 Mon Sep 17 00:00:00 2001 From: Brian Lagunas <835562+brianlagunas@users.noreply.github.com> Date: Thu, 5 Sep 2024 13:32:54 -0600 Subject: [PATCH 1/3] Simplifying dashboard filter API --- e2e/Sandbox/Factories/CampaignsDashboard.cs | 52 +++++++++-------- e2e/Sandbox/Factories/SalesDashboard.cs | 56 +++++++++---------- e2e/Sandbox/MainWindow.xaml.cs | 13 +++-- e2e/Sandbox/Sandbox.csproj | 36 ++++++------ src/Reveal.Sdk.Dom/Filters/FilterItem.cs | 14 ++++- .../Extensions/IFilterBindingsExtensions.cs | 18 ++++-- .../Visualizations/Visualization.cs | 4 ++ 7 files changed, 110 insertions(+), 83 deletions(-) diff --git a/e2e/Sandbox/Factories/CampaignsDashboard.cs b/e2e/Sandbox/Factories/CampaignsDashboard.cs index d6ee0cd0..87be6ae8 100644 --- a/e2e/Sandbox/Factories/CampaignsDashboard.cs +++ b/e2e/Sandbox/Factories/CampaignsDashboard.cs @@ -1,4 +1,5 @@ -using Reveal.Sdk.Dom; +using DocumentFormat.OpenXml.Spreadsheet; +using Reveal.Sdk.Dom; using Reveal.Sdk.Dom.Data; using Reveal.Sdk.Dom.Filters; using Reveal.Sdk.Dom.Visualizations; @@ -21,10 +22,11 @@ internal static RdashDocument CreateDashboard() UseAutoLayout = false, }; - document.Filters.Add(new DashboardDateFilter() + var dateFilter = new DashboardDateFilter() { RuleType = DateRuleType.TrailingTwelveMonths - }); + }; + document.Filters.Add(dateFilter); var campaignIdFilter = new DashboardDataFilter(excelDataSourceItem) { @@ -35,23 +37,19 @@ internal static RdashDocument CreateDashboard() }; document.Filters.Add(campaignIdFilter); - - var globalDateFilterBinding = new DashboardDateFilterBinding("Date"); - var territoryFilterBinding = new DashboardDataFilterBinding(campaignIdFilter); - - document.Visualizations.Add(CreateKpiTargetVisualization(excelDataSourceItem, territoryFilterBinding)); - document.Visualizations.Add(CreateIndicatorVisualization("Website Traffic", "Traffic", excelDataSourceItem, territoryFilterBinding)); - document.Visualizations.Add(CreateIndicatorVisualization("Conversions", "Conversions", excelDataSourceItem, territoryFilterBinding)); - document.Visualizations.Add(CreateIndicatorVisualization("New Seats", "New Seats", excelDataSourceItem, territoryFilterBinding)); - document.Visualizations.Add(CreateSplineAreaChartVisualization(excelDataSourceItem, globalDateFilterBinding, territoryFilterBinding)); - document.Visualizations.Add(CreateStackedColumnChartVisualization(excelDataSourceItem, globalDateFilterBinding, territoryFilterBinding)); - document.Visualizations.Add(CreateLineChartVisualization(excelDataSourceItem, globalDateFilterBinding, territoryFilterBinding)); - document.Visualizations.Add(CreateDoughnutChartVisualization(excelDataSourceItem, globalDateFilterBinding, territoryFilterBinding)); + document.Visualizations.Add(CreateKpiTargetVisualization(excelDataSourceItem, campaignIdFilter)); + document.Visualizations.Add(CreateIndicatorVisualization("Website Traffic", "Traffic", excelDataSourceItem, campaignIdFilter)); + document.Visualizations.Add(CreateIndicatorVisualization("Conversions", "Conversions", excelDataSourceItem, campaignIdFilter)); + document.Visualizations.Add(CreateIndicatorVisualization("New Seats", "New Seats", excelDataSourceItem, campaignIdFilter)); + document.Visualizations.Add(CreateSplineAreaChartVisualization(excelDataSourceItem, dateFilter, campaignIdFilter)); + document.Visualizations.Add(CreateStackedColumnChartVisualization(excelDataSourceItem, dateFilter, campaignIdFilter)); + document.Visualizations.Add(CreateLineChartVisualization(excelDataSourceItem, dateFilter, campaignIdFilter)); + document.Visualizations.Add(CreateDoughnutChartVisualization(excelDataSourceItem, dateFilter, campaignIdFilter)); return document; } - private static Visualization CreateKpiTargetVisualization(DataSourceItem excelDataSourceItem, Binding territoryFilterBinding) + private static Visualization CreateKpiTargetVisualization(DataSourceItem excelDataSourceItem, DashboardDataFilter filter) { var visualization = new KpiTargetVisualization(excelDataSourceItem) { @@ -60,7 +58,7 @@ private static Visualization CreateKpiTargetVisualization(DataSourceItem excelDa RowSpan = 13, }; - visualization.FilterBindings.Add(territoryFilterBinding); + visualization.ConnectDashboardFilter(filter); visualization.Date = new DimensionColumn() { @@ -83,7 +81,7 @@ private static Visualization CreateKpiTargetVisualization(DataSourceItem excelDa return visualization; } - private static Visualization CreateIndicatorVisualization(string title, string field, DataSourceItem excelDataSourceItem, Binding territoryFilterBinding) + private static Visualization CreateIndicatorVisualization(string title, string field, DataSourceItem excelDataSourceItem, DashboardDataFilter filter) { var visualization = new KpiTimeVisualization(excelDataSourceItem) { @@ -92,7 +90,7 @@ private static Visualization CreateIndicatorVisualization(string title, string f RowSpan = 13, }; - visualization.FilterBindings.Add(territoryFilterBinding); + visualization.ConnectDashboardFilter(filter); visualization.Date = new DimensionColumn() { @@ -110,7 +108,7 @@ private static Visualization CreateIndicatorVisualization(string title, string f return visualization; } - private static Visualization CreateSplineAreaChartVisualization(DataSourceItem excelDataSourceItem, params Binding[] filterBindings) + private static Visualization CreateSplineAreaChartVisualization(DataSourceItem excelDataSourceItem, params DashboardFilter[] filters) { var visualization = new SplineAreaChartVisualization(excelDataSourceItem) { @@ -119,7 +117,7 @@ private static Visualization CreateSplineAreaChartVisualization(DataSourceItem e RowSpan = 28, }; - visualization.FilterBindings.AddRange(filterBindings); + visualization.ConnectDashboardFilters(filters); visualization.Labels.Add(new DimensionColumn() { @@ -141,7 +139,7 @@ private static Visualization CreateSplineAreaChartVisualization(DataSourceItem e return visualization; } - private static Visualization CreateStackedColumnChartVisualization(DataSourceItem excelDataSourceItem, params Binding[] filterBindings) + private static Visualization CreateStackedColumnChartVisualization(DataSourceItem excelDataSourceItem, params DashboardFilter[] filters) { var visualization = new StackedColumnChartVisualization(excelDataSourceItem) { @@ -150,7 +148,7 @@ private static Visualization CreateStackedColumnChartVisualization(DataSourceIte RowSpan = 28, }; - visualization.FilterBindings.AddRange(filterBindings); + visualization.ConnectDashboardFilters(filters); visualization.Labels.Add(new DimensionColumn() { @@ -176,7 +174,7 @@ private static Visualization CreateStackedColumnChartVisualization(DataSourceIte return visualization; } - private static Visualization CreateLineChartVisualization(DataSourceItem excelDataSourceItem, params Binding[] filterBindings) + private static Visualization CreateLineChartVisualization(DataSourceItem excelDataSourceItem, params DashboardFilter[] filters) { var visualization = new LineChartVisualization(excelDataSourceItem) { @@ -185,7 +183,7 @@ private static Visualization CreateLineChartVisualization(DataSourceItem excelDa RowSpan = 19, }; - visualization.FilterBindings.AddRange(filterBindings); + visualization.ConnectDashboardFilters(filters); visualization.Labels.Add(new DimensionColumn() { @@ -203,7 +201,7 @@ private static Visualization CreateLineChartVisualization(DataSourceItem excelDa return visualization; } - private static Visualization CreateDoughnutChartVisualization(DataSourceItem excelDataSourceItem, params Binding[] filterBindings) + private static Visualization CreateDoughnutChartVisualization(DataSourceItem excelDataSourceItem, params DashboardFilter[] filters) { var visualization = new DoughnutChartVisualization(excelDataSourceItem) { @@ -212,7 +210,7 @@ private static Visualization CreateDoughnutChartVisualization(DataSourceItem exc RowSpan = 19, }; - visualization.FilterBindings.AddRange(filterBindings); + visualization.ConnectDashboardFilters(filters); visualization.Labels.Add(new DimensionColumn() { diff --git a/e2e/Sandbox/Factories/SalesDashboard.cs b/e2e/Sandbox/Factories/SalesDashboard.cs index bd7aec53..ae6c88ab 100644 --- a/e2e/Sandbox/Factories/SalesDashboard.cs +++ b/e2e/Sandbox/Factories/SalesDashboard.cs @@ -21,10 +21,11 @@ internal static RdashDocument CreateDashboard() UseAutoLayout = false, }; - document.Filters.Add(new DashboardDateFilter() + var dateFilter = new DashboardDateFilter() { Title = "My Date Filter" - }); + }; + document.Filters.Add(dateFilter); var territoryFilter = new DashboardDataFilter(excelDataSourceItem) { @@ -35,22 +36,19 @@ internal static RdashDocument CreateDashboard() }; document.Filters.Add(territoryFilter); - var globalDateFilterBinding = new DashboardDateFilterBinding("Date"); - var territoryFilterBinding = new DashboardDataFilterBinding(territoryFilter); - - document.Visualizations.Add(CreateKpiTargetVisualization(excelDataSourceItem, territoryFilterBinding)); - document.Visualizations.Add(CreateSplineAreaChartVisualization(excelDataSourceItem, globalDateFilterBinding, territoryFilterBinding)); - document.Visualizations.Add(CreateStackedColumnChartVisualization(excelDataSourceItem, globalDateFilterBinding, territoryFilterBinding)); - document.Visualizations.Add(CreateIndicatorVisualization(excelDataSourceItem, territoryFilterBinding)); - document.Visualizations.Add(CreateSparklineVisualization(excelDataSourceItem, globalDateFilterBinding, territoryFilterBinding)); - document.Visualizations.Add(CreateBarChartVisualization(excelDataSourceItem, globalDateFilterBinding, territoryFilterBinding)); - document.Visualizations.Add(CreateColumnChartVisualization(excelDataSourceItem, globalDateFilterBinding, territoryFilterBinding)); - document.Visualizations.Add(CreateGaugeVisualization(excelDataSourceItem, globalDateFilterBinding, territoryFilterBinding)); + document.Visualizations.Add(CreateKpiTargetVisualization(excelDataSourceItem, territoryFilter)); + document.Visualizations.Add(CreateSplineAreaChartVisualization(excelDataSourceItem, dateFilter, territoryFilter)); + document.Visualizations.Add(CreateStackedColumnChartVisualization(excelDataSourceItem, dateFilter, territoryFilter)); + document.Visualizations.Add(CreateIndicatorVisualization(excelDataSourceItem, territoryFilter)); + document.Visualizations.Add(CreateSparklineVisualization(excelDataSourceItem, dateFilter, territoryFilter)); + document.Visualizations.Add(CreateBarChartVisualization(excelDataSourceItem, dateFilter, territoryFilter)); + document.Visualizations.Add(CreateColumnChartVisualization(excelDataSourceItem, dateFilter, territoryFilter)); + document.Visualizations.Add(CreateGaugeVisualization(excelDataSourceItem, dateFilter, territoryFilter)); return document; } - private static Visualization CreateKpiTargetVisualization(DataSourceItem excelDataSourceItem, params Binding[] filterBindings) + private static Visualization CreateKpiTargetVisualization(DataSourceItem excelDataSourceItem, params DashboardFilter[] filters) { return new KpiTargetVisualization("Sales", excelDataSourceItem) .SetDate("Date") @@ -69,38 +67,38 @@ private static Visualization CreateKpiTargetVisualization(DataSourceItem excelDa } }) .SetTarget("Forecasted") - .AddFilterBindings(filterBindings) + .ConnectDashboardFilters(filters) .SetPosition(20, 11); } - private static Visualization CreateSplineAreaChartVisualization(DataSourceItem excelDataSourceItem, params Binding[] filterBindings) + private static Visualization CreateSplineAreaChartVisualization(DataSourceItem excelDataSourceItem, params DashboardFilter[] filters) { return new SplineAreaChartVisualization("New vs Renewal Sales", excelDataSourceItem) .SetLabel(new DateDataField("Date") { AggregationType = DateAggregationType.Month }) .SetValues("New Sales", "Renewal Sales ") - .AddFilterBindings(filterBindings) + .ConnectDashboardFilters(filters) .SetPosition(39, 31); } - private static Visualization CreateStackedColumnChartVisualization(DataSourceItem excelDataSourceItem, params Binding[] filterBindings) + private static Visualization CreateStackedColumnChartVisualization(DataSourceItem excelDataSourceItem, params DashboardFilter[] filters) { return new StackedColumnChartVisualization("Sales by Product", excelDataSourceItem) .SetLabel("Product") .SetValues("New Sales", "Renewal Sales ") - .AddFilterBindings(filterBindings) + .ConnectDashboardFilters(filters) .SetPosition(39, 18); } - private static Visualization CreateIndicatorVisualization(DataSourceItem excelDataSourceItem, params Binding[] filterBindings) + private static Visualization CreateIndicatorVisualization(DataSourceItem excelDataSourceItem, params DashboardFilter[] filters) { return new KpiTimeVisualization("Total Opportunities", excelDataSourceItem) .SetDate(new DateDataField("Date") { AggregationType = DateAggregationType.Year }) .SetValue("Total Opportunites") - .AddFilterBindings(filterBindings) + .ConnectDashboardFilters(filters) .SetPosition(19, 11); } - private static Visualization CreateSparklineVisualization(DataSourceItem excelDataSourceItem, params Binding[] filterBindings) + private static Visualization CreateSparklineVisualization(DataSourceItem excelDataSourceItem, params DashboardFilter[] filters) { return new SparklineVisualization("New Seats by Product", excelDataSourceItem) .SetDate(new DateDataField("Date") { AggregationType = DateAggregationType.Month }) @@ -124,11 +122,11 @@ private static Visualization CreateSparklineVisualization(DataSourceItem excelDa settings.DateFieldAlignment = Alignment.Left; settings.AggregationType = SparklineAggregationType.Months; }) - .AddFilterBindings(filterBindings) + .ConnectDashboardFilters(filters) .SetPosition(30, 31); } - private static Visualization CreateBarChartVisualization(DataSourceItem excelDataSourceItem, params Binding[] filterBindings) + private static Visualization CreateBarChartVisualization(DataSourceItem excelDataSourceItem, params DashboardFilter[] filters) { return new BarChartVisualization("Sales", excelDataSourceItem) .SetLabel("Employee") @@ -143,20 +141,20 @@ private static Visualization CreateBarChartVisualization(DataSourceItem excelDat ApplyMkFormat = true, } }) - .AddFilterBindings(filterBindings) + .ConnectDashboardFilters(filters) .SetPosition(43, 29); } - private static Visualization CreateColumnChartVisualization(DataSourceItem excelDataSourceItem, params Binding[] filterBindings) + private static Visualization CreateColumnChartVisualization(DataSourceItem excelDataSourceItem, params DashboardFilter[] filters) { return new ColumnChartVisualization("", excelDataSourceItem) .SetLabel(new DateDataField("Date") { AggregationType = DateAggregationType.Month }) .SetValues("Leads", "Hot Leads") - .AddFilterBindings(filterBindings) + .ConnectDashboardFilters(filters) .SetPosition(46, 31); } - private static Visualization CreateGaugeVisualization(DataSourceItem excelDataSourceItem, params Binding[] filterBindings) + private static Visualization CreateGaugeVisualization(DataSourceItem excelDataSourceItem, params DashboardFilter[] filters) { var visualization = new BulletGraphVisualization("Quotas by Sales Rep", excelDataSourceItem) .SetLabel("Employee") @@ -185,7 +183,7 @@ private static Visualization CreateGaugeVisualization(DataSourceItem excelDataSo RuleType = NumberRuleType.TopItems, Value = 10.0 }) - .AddFilterBindings(filterBindings) + .ConnectDashboardFilters(filters) .SetPosition(33, 29); return visualization; diff --git a/e2e/Sandbox/MainWindow.xaml.cs b/e2e/Sandbox/MainWindow.xaml.cs index 0bacecfe..400982b2 100644 --- a/e2e/Sandbox/MainWindow.xaml.cs +++ b/e2e/Sandbox/MainWindow.xaml.cs @@ -23,6 +23,7 @@ using Reveal.Sdk.Data.Snowflake; using Reveal.Sdk.Dom; using Reveal.Sdk.Dom.Data; +using Reveal.Sdk.Dom.Filters; using Reveal.Sdk.Dom.Visualizations; using Sandbox.Factories; using Sandbox.Helpers; @@ -34,6 +35,7 @@ using System.Threading.Tasks; using System.Windows; using Windows.Management.Deployment; +using Windows.Storage.Streams; namespace Sandbox { @@ -44,8 +46,8 @@ public partial class MainWindow : Window { static readonly string _dashboardFilePath = Path.Combine(Environment.CurrentDirectory, "Dashboards"); - //readonly string _readFilePath = Path.Combine(_dashboardFilePath, DashboardFileNames.Marketing); - readonly string _readFilePath = Path.Combine(_dashboardFilePath, "My Dashboard.rdash"); + readonly string _readFilePath = Path.Combine(_dashboardFilePath, DashboardFileNames.Sales); + //readonly string _readFilePath = Path.Combine(_dashboardFilePath, "My Dashboard.rdash"); readonly string _saveJsonToPath = Path.Combine(_dashboardFilePath, "MyDashboard.json"); readonly string _saveRdashToPath = Path.Combine(_dashboardFilePath, DashboardFileNames.MyDashboard); @@ -209,6 +211,9 @@ private void Clear_Dashboard(object sender, RoutedEventArgs e) private async void Read_Dashboard(object sender, RoutedEventArgs e) { var document = RdashDocument.Load(_readFilePath); + var filter = document.Filters[1] as DashboardDataFilter; + filter.SelectedItems.Add(new FilterItem("CampaignID", "Ruby")); + var json = document.ToJsonString(); _revealView.Dashboard = await RVDashboard.LoadFromJsonAsync(json); } @@ -216,13 +221,13 @@ private async void Read_Dashboard(object sender, RoutedEventArgs e) private async void Create_Dashboard(object sender, RoutedEventArgs e) { //var document = MarketingDashboard.CreateDashboard(); - //var document = SalesDashboard.CreateDashboard(); + var document = SalesDashboard.CreateDashboard(); //var document = CampaignsDashboard.CreateDashboard(); //var document = HealthcareDashboard.CreateDashboard(); //var document = ManufacturingDashboard.CreateDashboard(); //var document = CustomDashboard.CreateDashboard(); //var document = RestDataSourceDashboards.CreateDashboard(); - var document = SqlServerDataSourceDashboards.CreateDashboard(); + //var document = SqlServerDataSourceDashboards.CreateDashboard(); //var document = DashboardLinkingDashboard.CreateDashboard(); var json = document.ToJsonString(); diff --git a/e2e/Sandbox/Sandbox.csproj b/e2e/Sandbox/Sandbox.csproj index ec1c5914..ee5a18d5 100644 --- a/e2e/Sandbox/Sandbox.csproj +++ b/e2e/Sandbox/Sandbox.csproj @@ -8,24 +8,24 @@ - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + diff --git a/src/Reveal.Sdk.Dom/Filters/FilterItem.cs b/src/Reveal.Sdk.Dom/Filters/FilterItem.cs index b867c1ab..9777ea2c 100644 --- a/src/Reveal.Sdk.Dom/Filters/FilterItem.cs +++ b/src/Reveal.Sdk.Dom/Filters/FilterItem.cs @@ -4,7 +4,19 @@ namespace Reveal.Sdk.Dom.Filters { public sealed class FilterItem { - public Dictionary FieldValues { get; set; } + public FilterItem() { } + public FilterItem(string fieldName, object value) + { + FieldValues.Add(fieldName, value); + } + public FilterItem(string fieldName, params object[] value) + { + foreach (var v in value) + { + FieldValues.Add(fieldName, v); + } + } + public Dictionary FieldValues { get; set; } = new Dictionary(); public List ExpansionPath { get; set; } = new List(); } } diff --git a/src/Reveal.Sdk.Dom/Visualizations/Extensions/IFilterBindingsExtensions.cs b/src/Reveal.Sdk.Dom/Visualizations/Extensions/IFilterBindingsExtensions.cs index d4b7659a..57abae7a 100644 --- a/src/Reveal.Sdk.Dom/Visualizations/Extensions/IFilterBindingsExtensions.cs +++ b/src/Reveal.Sdk.Dom/Visualizations/Extensions/IFilterBindingsExtensions.cs @@ -4,17 +4,27 @@ namespace Reveal.Sdk.Dom.Visualizations { public static class IFilterBindingsExtensions { - public static T AddFilterBinding(this T visualization, Binding filterBinding) + public static T ConnectDashboardFilter(this T visualization, DashboardFilter dashboardFilter) where T : IFilterBindings { - visualization.FilterBindings.Add(filterBinding); + if (dashboardFilter is DashboardDateFilter) + { + visualization.FilterBindings.Add(new DashboardDateFilterBinding("Date")); + } + else + { + visualization.FilterBindings.Add(new DashboardDataFilterBinding(dashboardFilter as DashboardDataFilter)); + } return visualization; } - public static T AddFilterBindings(this T visualization, params Binding[] filterBindings) + public static T ConnectDashboardFilters(this T visualization, params DashboardFilter[] dashboardFilter) where T : IFilterBindings { - visualization.FilterBindings.AddRange(filterBindings); + foreach (var filter in dashboardFilter) + { + visualization.ConnectDashboardFilter(filter); + } return visualization; } } diff --git a/src/Reveal.Sdk.Dom/Visualizations/Visualization.cs b/src/Reveal.Sdk.Dom/Visualizations/Visualization.cs index dc0df469..004d64d9 100644 --- a/src/Reveal.Sdk.Dom/Visualizations/Visualization.cs +++ b/src/Reveal.Sdk.Dom/Visualizations/Visualization.cs @@ -103,6 +103,10 @@ public void UpdateDataSourceItem(DataSourceItem dataSourceItem) return; ((DataDefinitionBase)DataDefinition).DataSourceItem = dataSourceItem; + if (DataDefinition is TabularDataDefinition tdd) + { + tdd.Fields = dataSourceItem.Fields.Clone(); + } } protected virtual void InitializeDataDefinition(DataSourceItem dataSourceItem) From 65dfa1f30b38651d6f1ab1fc3cd04eaba3fb9ae6 Mon Sep 17 00:00:00 2001 From: Brian Lagunas <835562+brianlagunas@users.noreply.github.com> Date: Thu, 5 Sep 2024 14:18:06 -0600 Subject: [PATCH 2/3] simplified API to select filter values --- e2e/Sandbox/Helpers/DataSourceFactory.cs | 1 + e2e/Sandbox/MainWindow.xaml.cs | 6 ++---- src/Reveal.Sdk.Dom/Filters/DashboardDataFilter.cs | 9 +++++++++ src/Reveal.Sdk.Dom/Filters/FilterItem.cs | 8 +------- 4 files changed, 13 insertions(+), 11 deletions(-) diff --git a/e2e/Sandbox/Helpers/DataSourceFactory.cs b/e2e/Sandbox/Helpers/DataSourceFactory.cs index 55d22199..67711bcf 100644 --- a/e2e/Sandbox/Helpers/DataSourceFactory.cs +++ b/e2e/Sandbox/Helpers/DataSourceFactory.cs @@ -67,6 +67,7 @@ internal static DataSourceItem GetSalesDataSourceItem() IsAnonymous = true, Fields = GetSalesDataSourceFields(), }; + excelDataSourceItem.UseExcel("Sales"); return excelDataSourceItem; } diff --git a/e2e/Sandbox/MainWindow.xaml.cs b/e2e/Sandbox/MainWindow.xaml.cs index 400982b2..c0278658 100644 --- a/e2e/Sandbox/MainWindow.xaml.cs +++ b/e2e/Sandbox/MainWindow.xaml.cs @@ -210,10 +210,8 @@ private void Clear_Dashboard(object sender, RoutedEventArgs e) private async void Read_Dashboard(object sender, RoutedEventArgs e) { - var document = RdashDocument.Load(_readFilePath); - var filter = document.Filters[1] as DashboardDataFilter; - filter.SelectedItems.Add(new FilterItem("CampaignID", "Ruby")); - + var document = RdashDocument.Load(_readFilePath); + var json = document.ToJsonString(); _revealView.Dashboard = await RVDashboard.LoadFromJsonAsync(json); } diff --git a/src/Reveal.Sdk.Dom/Filters/DashboardDataFilter.cs b/src/Reveal.Sdk.Dom/Filters/DashboardDataFilter.cs index c70bf6f9..72921006 100644 --- a/src/Reveal.Sdk.Dom/Filters/DashboardDataFilter.cs +++ b/src/Reveal.Sdk.Dom/Filters/DashboardDataFilter.cs @@ -21,5 +21,14 @@ public DashboardDataFilter(DataSourceItem dataSourceItem) DataDefinition.DataSourceItem = dataSourceItem; DataDefinition.Fields = dataSourceItem?.Fields.Clone(); } + + public void SelectValues(params object[] values) + { + SelectedItems.Clear(); + foreach (var value in values) + { + SelectedItems.Add(new FilterItem(SelectedFieldName, value)); + } + } } } diff --git a/src/Reveal.Sdk.Dom/Filters/FilterItem.cs b/src/Reveal.Sdk.Dom/Filters/FilterItem.cs index 9777ea2c..1bf23988 100644 --- a/src/Reveal.Sdk.Dom/Filters/FilterItem.cs +++ b/src/Reveal.Sdk.Dom/Filters/FilterItem.cs @@ -9,13 +9,7 @@ public FilterItem(string fieldName, object value) { FieldValues.Add(fieldName, value); } - public FilterItem(string fieldName, params object[] value) - { - foreach (var v in value) - { - FieldValues.Add(fieldName, v); - } - } + public Dictionary FieldValues { get; set; } = new Dictionary(); public List ExpansionPath { get; set; } = new List(); } From 334df14556d901feb01c608139a152fdb0e105ff Mon Sep 17 00:00:00 2001 From: Brian Lagunas <835562+brianlagunas@users.noreply.github.com> Date: Thu, 5 Sep 2024 15:04:33 -0600 Subject: [PATCH 3/3] fixed tests --- .../RdashDocumentValidatorFixture.cs | 32 ++++++++++++++++--- .../Core/Utilities/RdashDocumentValidator.cs | 26 ++++----------- 2 files changed, 35 insertions(+), 23 deletions(-) diff --git a/src/Reveal.Sdk.Dom.Tests/Core/Utilities/RdashDocumentValidatorFixture.cs b/src/Reveal.Sdk.Dom.Tests/Core/Utilities/RdashDocumentValidatorFixture.cs index b3d9c923..ea46dce8 100644 --- a/src/Reveal.Sdk.Dom.Tests/Core/Utilities/RdashDocumentValidatorFixture.cs +++ b/src/Reveal.Sdk.Dom.Tests/Core/Utilities/RdashDocumentValidatorFixture.cs @@ -80,18 +80,44 @@ public void Validate_Adds_Fields() var document = new RdashDocument(); document.Visualizations.Add(new GridVisualization(dataSourceItem)); - Assert.Empty(document.Visualizations[0].DataDefinition.AsTabular().Fields); - RdashDocumentValidator.Validate(document); Assert.Single(document.Visualizations[0].DataDefinition.AsTabular().Fields); } + [Fact] + public void Validate_ThrowsException_When_Fields_Are_Null() + { + var dataSourceItem = new DataSourceItem("Test", new DataSource()); + dataSourceItem.Fields = null; + + var document = new RdashDocument(); + document.Visualizations.Add(new GridVisualization(dataSourceItem)); + + var exception = Assert.Throws(() => RdashDocumentValidator.Validate(document)); + + Assert.Equal("Fields for DataSourceItem Test is null or empty.", exception.Message); + } + + [Fact] + public void Validate_ThrowsException_When_Fields_Are_Empty() + { + var dataSourceItem = new DataSourceItem("Test", new DataSource()).SetFields(new List()); + + var document = new RdashDocument(); + document.Visualizations.Add(new GridVisualization(dataSourceItem)); + + var exception = Assert.Throws(() => RdashDocumentValidator.Validate(document)); + + Assert.Equal("Fields for DataSourceItem Test is null or empty.", exception.Message); + } + [Fact] public void Validate_PreventsDuplicate_Fields() { var dataSourceItem = new DataSourceItem("Test", new DataSource()).SetFields(new List { + new TextField("Test"), new TextField("Test"), new TextField("Test") }); @@ -99,8 +125,6 @@ public void Validate_PreventsDuplicate_Fields() var document = new RdashDocument(); document.Visualizations.Add(new GridVisualization(dataSourceItem)); - Assert.Empty(document.Visualizations[0].DataDefinition.AsTabular().Fields); - RdashDocumentValidator.Validate(document); Assert.Single(document.Visualizations[0].DataDefinition.AsTabular().Fields); diff --git a/src/Reveal.Sdk.Dom/Core/Utilities/RdashDocumentValidator.cs b/src/Reveal.Sdk.Dom/Core/Utilities/RdashDocumentValidator.cs index be8acaa1..99dd10d1 100644 --- a/src/Reveal.Sdk.Dom/Core/Utilities/RdashDocumentValidator.cs +++ b/src/Reveal.Sdk.Dom/Core/Utilities/RdashDocumentValidator.cs @@ -36,27 +36,15 @@ static void FixVisualizations(RdashDocument document) private static void FixFields(TabularDataDefinition tdd) { - if (tdd.DataSourceItem.Fields?.Count != 0) - { - // Create a HashSet to track added field names - HashSet fieldNames = new HashSet(tdd.Fields.Select(f => f.FieldName)); - - foreach (var field in tdd.DataSourceItem.Fields.Clone()) - { - if (field == null) - throw new Exception($"Field for DataSourceItem {tdd.DataSourceItem.Title} is null."); + if (tdd.Fields?.Count == 0) + throw new Exception($"Fields for DataSourceItem {tdd.DataSourceItem.Title} is null or empty."); - //prevent adding duplicate fields - if (!fieldNames.Contains(field.FieldName)) - { - tdd.Fields.Add(field); - fieldNames.Add(field.FieldName); - } - } + // Check if there are duplicates + var hasDuplicates = tdd.Fields.GroupBy(f => f.FieldName).Any(g => g.Count() > 1); + if (hasDuplicates) + { + tdd.Fields = tdd.Fields.GroupBy(f => f.FieldName).Select(g => g.First()).ToList(); } - - if (tdd.Fields?.Count == 0) - throw new Exception($"Fields for DataSourceItem {tdd.DataSourceItem.Title} is null."); } static void FixJoinedTables(TabularDataDefinition tdd)