| ||
Chart Samples Style Examples SharpLeaf Tutorials Document Layout Tutorials Text Flow Tutorials Table Tutorials Visual Glossaries SharpPlot Reference SharpPlot Class SharpPlot Properties SharpPlot Methods SharpPlot Structures SharpPlot Enumerations PageMap Class SharpLeaf Reference SharpLeaf Class SharpLeaf Properties SharpLeaf Methods Table Class Table Properties Table Methods SharpLeaf Structures FontType Structure ParagraphStyle Structure BoxStyle Structure SharpLeaf Enumerations DocumentLayout Classes DocumentLayout Class PageLayout Class PageElement Abstract Class Frame : PageElement Class TextBlock : PageElement Class ImageBlock : PageElement Class Box : PageElement Class Rule : PageElement Class Common Reference Document Class VectorMath Class DbUtil Class Download Release Notes Licensing |
SharpPlot Tutorials > General Tutorials > Grouping and Splitting database data Grouping and Splitting database dataMany aggregations can be done very easily in SQL, and SharpPlot simply used to plot the group totals. The methods explored in this tutorial show you how to make more complex summaries and cross-tabs directly from the raw data series. Grouped Barchart of Quarterly SalesThis example makes use of the DbUtil class to extract entire columns from a sample dataset (supplied as XML) and make a simple summary using grouping and splitting. Here is the finished chart and the complete source code to generate it: // Simple example to show how we make a chart from a dataset // Uses sample Sales.xml shipped with SharpPlot and the DbUtil class using System; using System.Data; using Causeway; class charttest { static void Main() { SharpPlot sp = new SharpPlot(); DataSet ds = new DataSet(); // Initialise the dataset from the XML (schema is included) ds.ReadXml("Sales.xml"); DataTable summary = ds.Tables[1]; DataRow[] sel = summary.Select(); // All rows are fine here int[] col_sales = DbUtil.GetIntegers(sel,"Sales"); string[] col_prodid = DbUtil.GetStrings(sel,"Product"); string[] col_qtr = DbUtil.GetStrings(sel,"Quarter"); sp.SplitBy(col_prodid); // One bar for each product sp.GroupBy(col_qtr); // Accumulate quarters sp.BarChartStyle = BarChartStyles.ForceZero|BarChartStyles.StackedBars; sp.Heading = "Quarterly Sales Summary"; sp.YCaption = "Tonnes"; sp.YAxisStyle = YAxisStyles.AtEndCaption; // Use the product names for the keys (tables have the same ordering) sp.SetKeyText(DbUtil.GetStrings(ds.Tables[0].Select(),"Product Name")); sp.DrawBarChart(col_sales); // Save to SVG file sp.SaveSvg("dbdemo1.svg",SvgMode.FixedAspect); } } Some things to note is the example:
With real data, you will probably have to take care to have the selections ordered correctly, but the basic principles should be very similar. Plotting a Simple Timeseries (Share price data)This example makes a comparative plot of two popular UK shares to get an idea which has performed better over the last three years. Note the formatting of the date column to obtain well-structured X-axis labelling. The Date column should be retrieved as integers, which forces a conversion to OLEDate values |