//////////////////////////////////////////////////////////////////////////////// // RadChartBuilder - A microDSL for Telerik's RadChart // // Copyright (c) 2009 Jose Simas (josesimas at gepsoft.com). All Rights Reserved. // // Permission to use, copy, modify, and distribute this software and its // documentation for any purpose, without fee, and without a written // agreement, is hereby granted, provided that the above copyright notice, // this paragraph and the following two paragraphs appear in all copies, // modifications, and distributions. // // IN NO EVENT SHALL THE AUTHOR BE LIABLE TO ANY PARTY FOR DIRECT, // INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING LOST // PROFITS, ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, // EVEN IF THE AUTHOR HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. // // THE AUTHOR SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, BUT NOT // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A // PARTICULAR PURPOSE. THE SOFTWARE AND ACCOMPANYING DOCUMENTATION, IF // ANY, PROVIDED HEREUNDER IS PROVIDED "AS IS". THE AUTHOR HAS NO OBLIGATION // TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS. // // V0.1 11/10/2009 - Untested sample code. //////////////////////////////////////////////////////////////////////////////// namespace GeneXproTools.Wpf.Utility.Helpers { using Telerik.Windows.Controls; using Telerik.Windows.Controls.Charting; /// /// MiniDSL for Telerik chart configuration /// public class RadChartBuilder : IAddMapping { private readonly RadChart _chart; private SeriesMapping _currentSeriesMapping; private RadChartBuilder(RadChart chart) { _chart = chart; } /// /// Start here. /// /// /// public static RadChartBuilder Configure(RadChart chart) { chart.SeriesMappings.Clear(); return new RadChartBuilder(chart); } /// /// Add the chart type of the series /// /// /// public IAddMapping AddSeriesDefinition(SeriesDefinition seriesDefinition) { _currentSeriesMapping = new SeriesMapping { SeriesDefinition = seriesDefinition }; _chart.SeriesMappings.Add(_currentSeriesMapping); return this; } /// /// Finalize the configuration and bind the chart to the data source /// /// public void BindTo(object source) { _chart.ItemsSource = source; } #region IAddMapping implementation /// /// Add a mapping between a source column and an element of the chart. /// /// The name of the column with the data for this member /// The chart item /// IAddMapping IAddMapping.AddMapping(string memberSourceName, DataPointMember member) { var mapping = new ItemMapping { DataPointMember = member, FieldName = memberSourceName }; _currentSeriesMapping.ItemMappings.Add(mapping); return this; } /// /// Stop adding mappings and return to the main branch. /// /// public RadChartBuilder CloseMappings() { return this; } #endregion } /// /// Interface for the add mapping branch of the above DSL. /// public interface IAddMapping { /// /// Add a mapping between a source column and an element of the chart. /// /// The name of the column with the data for this member /// The chart item IAddMapping AddMapping(string memberSourceName, DataPointMember member); /// /// Stop adding mappings and return to the main branch. /// RadChartBuilder CloseMappings(); } }