{"id":1064,"date":"2011-02-27T15:04:09","date_gmt":"2011-02-27T14:04:09","guid":{"rendered":"http:\/\/www.itidea.nl\/?p=1064"},"modified":"2011-04-24T16:20:00","modified_gmt":"2011-04-24T14:20:00","slug":"search-refiners-part-3-chart-based","status":"publish","type":"post","link":"https:\/\/www.itidea.nl\/index.php\/search-refiners-part-3-chart-based\/","title":{"rendered":"Search Refiners (part 3) &#8211; Chart based"},"content":{"rendered":"<p>In this series:\u00a0\u00a0<\/p>\n<ol>\n<li>Search Refiners part 1 &#8211; <a href=\"https:\/\/www.itidea.nl\/index.php\/search-refiners-part-1-expanding-the-ootb-search-refinement-panel\/\">Expanding the OOTB search Refinement Panel <\/a><\/li>\n<li>Search Refiners part 2 &#8211; <a href=\"https:\/\/www.itidea.nl\/index.php\/search-refiners-part-2-use-of-customfilters\/\">Use of CustomFilters <\/a><\/li>\n<li>Search Refiners part 3 &#8211; Chart based (this post)<\/li>\n<li>Search Refiners part 4 &#8211; <a href=\"https:\/\/www.itidea.nl\/index.php\/search-refiners-part-4-user-selection-based\/\">User selection based<\/a><\/li>\n<\/ol>\n<p>\u00a0<\/p>\n<p>In the previous two posts about search refiners no code was written to adjust the OOTB Search Refinement Panel, just some XML modifications.<br \/>\nIn this post a chart based search refiner will be built and some code is needed to accomplish this. The refinement will be built on the Modified Date.\u00a0\u00a0<\/p>\n<h3>Create the refiner<\/h3>\n<p>To get the refinement categories and their values, the RefinementManager class will be used.<br \/>\nThe RefinementManager will get an instance of the refinement manager on the current page. This means the OOTB Search Refinement Panel has to be present on the current page. Not only it has to be present, it also has to have the filtercategory on which the refinement is going to perform on, in this case the Modified Date.\u00a0<\/p>\n<p>Get the instance of the refinement manager:<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nRefinementManager refinementManager = RefinementManager.GetInstance(this.Page);\r\n<\/pre>\n<p>To get the refinement values:<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nXmlDocument refinementXmlDoc = refinementManager.GetRefinementXml();\r\n<\/pre>\n<p>The GetRefinementXML method gets the filtered xml document of the refinement manager.<br \/>\nFor the example is this post only the Modified Date is of interest, so an XPath expression is used to get to the right filter category and values:<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nXmlNodeList filters = refXmlDocument.DocumentElement.SelectNodes(&quot;\/FilterPanel\/FilterCategory&#x5B;@ManagedProperty='&quot; + refiner + &quot;']\/Filters\/Filter&#x5B;Count&gt;0]&quot;);\r\n<\/pre>\n<p>The refiner variable is declared as:<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nprivate const string refiner = &quot;Write&quot;;\r\n<\/pre>\n<p>By looping through the XMLNodes in the XMLNodeList the neccesary values are available, just as in the XML which is rendered by the OOTB Search Refinement Panel. The elements of the childnodes (filter categories) are shown:<a href=\"https:\/\/www.itidea.nl\/wp-content\/uploads\/2011\/02\/SearchrefinerPart3ElementsOfFilterCategory.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-1066\" title=\"SearchrefinerPart3ElementsOfFilterCategory\" src=\"https:\/\/www.itidea.nl\/wp-content\/uploads\/2011\/02\/SearchrefinerPart3ElementsOfFilterCategory.png\" alt=\"\" width=\"641\" height=\"278\" srcset=\"https:\/\/www.itidea.nl\/wp-content\/uploads\/2011\/02\/SearchrefinerPart3ElementsOfFilterCategory.png 641w, https:\/\/www.itidea.nl\/wp-content\/uploads\/2011\/02\/SearchrefinerPart3ElementsOfFilterCategory-300x130.png 300w\" sizes=\"auto, (max-width: 641px) 100vw, 641px\" \/><\/a>\u00a0\u00a0<\/p>\n<p>Actually, this was the most interesting part. What&#8217;s left is displaying the values to the user, in this case: putting it in a chart; and binding it all together.\u00a0\u00a0<\/p>\n<h3>Putting it all together<\/h3>\n<p>In CreateChildControls the chart is setup:<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nprivate Microsoft.Office.Server.WebControls.Chart chart;\r\nprotected override void CreateChildControls()\r\n{\r\n\u00a0\u00a0\u00a0 ChartArea area = new ChartArea(&quot;Pie Chart&quot;);\r\n\r\n\u00a0\u00a0\u00a0 chart = new Microsoft.Office.Server.WebControls.Chart();\r\n\u00a0\u00a0\u00a0 chart.Width = 160;\r\n\u00a0\u00a0\u00a0 chart.Height = 160;\r\n\u00a0\u00a0\u00a0 chart.ChartAreas.Add(area);\r\n\r\n\u00a0\u00a0\u00a0 Controls.Add(chart);\r\n}\r\n<\/pre>\n<p>And in the OnPreRender the refiment manager, the values of the filter category Modified Date and the chart are put together.<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nprotected override void OnPreRender(EventArgs e)\r\n{\r\n\u00a0\u00a0\u00a0 RefinementManager refManager = RefinementManager.GetInstance(this.Page);\r\n\r\n\u00a0\u00a0\u00a0 XmlDocument refXmlDocument = refManager.GetRefinementXml();\r\n\r\n\u00a0\u00a0\u00a0 if (refXmlDocument != null)\r\n\u00a0\u00a0\u00a0 {\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 Series chartSeries = new Series();\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 chartSeries.ChartType = SeriesChartType.Pie;\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 XmlNodeList filters = refXmlDocument.DocumentElement.SelectNodes(&quot;\/FilterPanel\/FilterCategory&#x5B;@ManagedProperty='&quot; + refiner + &quot;']\/Filters\/Filter&#x5B;Count&gt;0]&quot;);\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 if (filters.Count != 0)\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 {\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 foreach (XmlNode filter in filters)\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 {\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 string xValue = filter.SelectSingleNode(&quot;Value&quot;).InnerText;\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 string yValue = filter.SelectSingleNode(&quot;Count&quot;).InnerText;\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 int i = chartSeries.Points.AddXY(xValue, yValue);\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/ display text in chart\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 chartSeries.Points&#x5B;i].Label = xValue;\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/use the url to make the pie parts clickable\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 chartSeries.Points&#x5B;i].Url = filter.SelectSingleNode(&quot;Url&quot;).InnerText;\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 }\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/add 'Any Modified Date' option\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 XmlNodeList xmlFilterNodesAllItems = refXmlDocument.DocumentElement.SelectNodes(&quot;\/FilterPanel\/FilterCategory&#x5B;@ManagedProperty='&quot; + refiner + &quot;']\/Filters\/Filter&#x5B;Count='']&quot;);\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 chart.Titles.Clear();\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 System.Web.UI.DataVisualization.Charting.Title allItemsTitle = new System.Web.UI.DataVisualization.Charting.Title();\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 allItemsTitle.Text = xmlFilterNodesAllItems&#x5B;0].SelectSingleNode(&quot;Value&quot;).InnerText;\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 allItemsTitle.Url = xmlFilterNodesAllItems&#x5B;0].SelectSingleNode(&quot;Url&quot;).InnerText;\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 allItemsTitle.ToolTip = &quot;Refine by: &quot; + allItemsTitle.Text;\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 allItemsTitle.ForeColor = Color.FromArgb(0, 114, 188);\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 chart.Titles.Add(allItemsTitle);\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 }\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 if (chartSeries.Points.Count &gt; 0)\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 chart.Series.Add(chartSeries);\r\n\u00a0\u00a0\u00a0 }\r\n}\r\n<\/pre>\n<p>In the XPath expressions used to get the filter values for Modified Date and &#8216;Any Modified Date&#8217; are slightly different. Look at the Count differences.<br \/>\nBy removing the Count in the first XPath expression the chart will display &#8216;Any Modified Date&#8217; and the chart kind of gets messed up:<br \/>\n<a href=\"https:\/\/www.itidea.nl\/wp-content\/uploads\/2011\/02\/SearchrefinerPart3PieChartWithAnyModifiedDate.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-1065\" title=\"SearchrefinerPart3PieChartWithAnyModifiedDate\" src=\"https:\/\/www.itidea.nl\/wp-content\/uploads\/2011\/02\/SearchrefinerPart3PieChartWithAnyModifiedDate.png\" alt=\"\" width=\"165\" height=\"157\" \/><\/a><br \/>\nand &#8216;Any Modified Date&#8217; is not clickable, because it get&#8217;s no colored part in the pie chart. The reason for this is that the Count value for Any Modified Date is empty.<br \/>\nThis is the reason to use Count&gt;0 for the values displayed in the pie chart and Count=&#8221; to get Any Modified Date.<br \/>\nThe pie chart now looks like this:<br \/>\n<a href=\"https:\/\/www.itidea.nl\/wp-content\/uploads\/2011\/02\/SearchrefinerPart3PieChart1.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-1067\" title=\"SearchrefinerPart3PieChart1\" src=\"https:\/\/www.itidea.nl\/wp-content\/uploads\/2011\/02\/SearchrefinerPart3PieChart1.png\" alt=\"\" width=\"336\" height=\"250\" srcset=\"https:\/\/www.itidea.nl\/wp-content\/uploads\/2011\/02\/SearchrefinerPart3PieChart1.png 336w, https:\/\/www.itidea.nl\/wp-content\/uploads\/2011\/02\/SearchrefinerPart3PieChart1-300x223.png 300w\" sizes=\"auto, (max-width: 336px) 100vw, 336px\" \/><\/a>\u00a0\u00a0<\/p>\n<p>And after selecting &#8216;Past Month&#8217;:\u00a0\u00a0<\/p>\n<p><a href=\"https:\/\/www.itidea.nl\/wp-content\/uploads\/2011\/02\/SearchrefinerPart3PieChart2.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-1068\" title=\"SearchrefinerPart3PieChart2\" src=\"https:\/\/www.itidea.nl\/wp-content\/uploads\/2011\/02\/SearchrefinerPart3PieChart2.png\" alt=\"\" width=\"333\" height=\"248\" srcset=\"https:\/\/www.itidea.nl\/wp-content\/uploads\/2011\/02\/SearchrefinerPart3PieChart2.png 333w, https:\/\/www.itidea.nl\/wp-content\/uploads\/2011\/02\/SearchrefinerPart3PieChart2-300x223.png 300w\" sizes=\"auto, (max-width: 333px) 100vw, 333px\" \/><\/a><br \/>\n\u00a0\u00a0<\/p>\n<h3>Summary<\/h3>\n<p>To transform a textual search refiner to a chart based search refiner is shown in this post.<br \/>\nAs long as the OOTB Search Refinement Panel is present on the page and the filter category used by the code is present in the Filter Category Definition (property of Search Refinement Panel web part), the code in this post can be used to visualize a search refiner.<br \/>\nIf it&#8217;s not present, you can extend the Refinement Panel webpart. This is not covered in this post, but maybe I&#8217;ll show you this in a future post.\u00a0\u00a0<\/p>\n<h3>\u00a0\u00a0\u00a0<\/h3>\n","protected":false},"excerpt":{"rendered":"<p>In this series:\u00a0\u00a0 Search Refiners part 1 &#8211; Expanding the OOTB search Refinement Panel Search Refiners part 2 &#8211; Use of CustomFilters Search Refiners part 3 &#8211; Chart based (this post) Search Refiners part 4 &#8211; User selection based \u00a0 &#8230; <a class=\"more-link\" href=\"https:\/\/www.itidea.nl\/index.php\/search-refiners-part-3-chart-based\/\">Read More &raquo;<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[8],"tags":[20,42,6],"class_list":["post-1064","post","type-post","status-publish","format-standard","hentry","category-sharepoint-2010","tag-c","tag-sharepoint-2010","tag-visual-studio"],"_links":{"self":[{"href":"https:\/\/www.itidea.nl\/index.php\/wp-json\/wp\/v2\/posts\/1064","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.itidea.nl\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.itidea.nl\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.itidea.nl\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.itidea.nl\/index.php\/wp-json\/wp\/v2\/comments?post=1064"}],"version-history":[{"count":9,"href":"https:\/\/www.itidea.nl\/index.php\/wp-json\/wp\/v2\/posts\/1064\/revisions"}],"predecessor-version":[{"id":1114,"href":"https:\/\/www.itidea.nl\/index.php\/wp-json\/wp\/v2\/posts\/1064\/revisions\/1114"}],"wp:attachment":[{"href":"https:\/\/www.itidea.nl\/index.php\/wp-json\/wp\/v2\/media?parent=1064"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.itidea.nl\/index.php\/wp-json\/wp\/v2\/categories?post=1064"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.itidea.nl\/index.php\/wp-json\/wp\/v2\/tags?post=1064"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}