Results 1 to 11 of 11

Thread: [VB6] Tabulator, Crosstab Class

Threaded View

  1. #1

    Thread Starter
    PowerPoster dilettante's Avatar
    Join Date
    Feb 2006
    Posts
    24,487

    [VB6] Tabulator, Crosstab Class

    If you know what cross tabulation is you may have a need for this. If you've never heard of it the chances are this may just be a really confusing bit of code.

    Quite often we're dealing with data from a database, and many DBMSs offer a built-in way to do this. For example for Jet/ACE SQL look up the TRANSFORM... PIVOT syntax. Normally this makes use of some aggregation function (SUM(), AVERAGE(), etc.).

    This Tabulator class can be used with any data source. This version does not include aggregation support, but instead assumes you have simple unique value to collect for each intersection of Column and Row (i.e. "cell").

    It can return rows in ascending order (default) or descending order by RowId value. Columns are always in ascending order by ColName values.


    You might add aggregation a number of ways. You could hard-code that into Tabulator.cls or the TabulatorValue.cls (a helper class for value storage).

    Or you might modify Tabulator.cls to accept an object reference that offers fixed-named methods such as Accumulate() and Report(). For SUM() aggregation Accumulate() might just add new values to the current sum in a cell, and Report() would jsut return the value without modification. For something like AVERAGE() you might have to add values and increment a count in Accumulate() and then divide in the Report() method.


    An illustration may help. This is Project1 in the attached archive. Here we have data like:

    Code:
    GOLD 01-JAN-2010 70.19
    OIL 01-JAN-2010 16.70
    SUGAR 01-JAN-2010 44.51
    COPPER 01-JAN-2010 2.57
    GOLD 02-JAN-2010 68.30
    OIL 02-JAN-2010 15.11
    SUGAR 02-JAN-2010 49.23
    COPPER 02-JAN-2010 5.58
    GOLD 03-JAN-2010 70.78
    OIL 03-JAN-2010 15.69
    SUGAR 03-JAN-2010 48.71
    COPPER 03-JAN-2010 9.29
    GOLD 04-JAN-2010 69.87
    OIL 04-JAN-2010 8.52
    SUGAR 04-JAN-2010 43.70
    We cross tabulate Price by Date and Commodity and display that (descending) in an MSHFlexGrid control:

    Name:  sshot1.gif
Views: 3113
Size:  23.9 KB


    Project2 is another example, showing how Tabulate can handle complex values which can be arrays or even objects. Here each cell Value is an instance of a two-value class (Price and Volume).

    The raw data looks like:

    Code:
    GOLD 15-APR-2014 74.70 42551
    OIL 15-APR-2014 9.69 70748
    SUGAR 15-APR-2014 49.28 109303
    COPPER 15-APR-2014 12.02 28024
    GOLD 01-JAN-2011 67.72 45741
    OIL 01-JAN-2011 9.91 72771
    SUGAR 01-JAN-2011 40.25 36548
    COPPER 01-JAN-2011 6.92 94342
    GOLD 02-JAN-2011 72.42 111129
    OIL 02-JAN-2011 12.99 29290
    SUGAR 02-JAN-2011 41.81 91619
    COPPER 02-JAN-2011 2.63 93288
    GOLD 03-JAN-2011 70.49 96250
    OIL 03-JAN-2011 11.10 76063
    SUGAR 03-JAN-2011 48.44 87550
    COPPER 03-JAN-2011 11.76 90176
    OIL 04-JAN-2011 16.53 107546
    We'll tabulate this and report it in another MSHFlexGrid control:

    Name:  sshot2.jpg
Views: 3190
Size:  88.5 KB


    Tabulate works by storing row data as a Collection of row Collection objects, RowId values as a Variant array, and "cell" values as TabulateValue.cls instances, each of which have a Variant property.

    Peformance was improved by adding a binary search for doing row insertion. Since there are normally far fewer columns, a linear search is still being used to insert new columns as they are "put" into Tabulator. At this point Tabulator is reasonably fast, and the demo programs spend most of their time populating the grid after tabulation has completed.

    It seems to be working properly, but if you find bugs please let everyone know by posting a reply here.

    Note:

    Bug fixed, reposted attachment.
    Attached Files Attached Files
    Last edited by dilettante; Apr 27th, 2014 at 10:06 AM. Reason: Fixed bug, reposted attachment

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width