tdwright / contabs

Simple yet flexible tables for console apps.
MIT License
54 stars 20 forks source link

Add an option to plot simple bar charts in a generated column #75

Open tdwright opened 2 years ago

tdwright commented 2 years ago

For numeric columns, it would be cool to be able to visualize these as a simple bar chart in a new column.

For instance:

╭─────────┬────────┬────────────────────────────────────────────────────────────╮
│         │        │                                                            │
│ Name    │ Radius │ Radius chart                                               │
│         │        │                                                            │
├─────────┼────────┼────────────────────────────────────────────────────────────┤
│         │        │                                                            │
│ Mercury │ 2439   │ ##                                                         │
│         │        │                                                            │
│ Venus   │ 6052   │ #####                                                      │
│         │        │                                                            │
│ Earth   │ 6371   │ #####                                                      │
│         │        │                                                            │
│ Mars    │ 3389   │ ##                                                         │
│         │        │                                                            │
│ Jupiter │ 69911  │ ########################################################## │
│         │        │                                                            │
│ Saturn  │ 58232  │ ################################################           │
│         │        │                                                            │
│ Uranus  │ 25362  │ #####################                                      │
│         │        │                                                            │
│ Neptune │ 24622  │ ####################                                       │
│         │        │                                                            │
╰─────────┴────────┴────────────────────────────────────────────────────────────╯

This gives a nice quick visual representation of the data in the column. The above example can be achieved with existing functionality by doing something like:

table.Columns.AddGeneratedColumn<int, string>(
    d => {
        var normalized = d / 2400;
        return new string('#', normalized);
    },
    "Radius chart",
    table.Columns["Diameter"]
);

But, since the scope of the lambda here is limited to data from individual rows, we have no way of automagically scaling the chart to an appropriate scale. In the above example, I've hard coded the fact that each unit of width of the bars corresponds to 2400km, but this is not ideal.

In a proper implementation, there would be a quick way of adding a chart column that didn't need the lambda writing out, nor any prior knowledge of the correct scale.

In terms of API design, I imagine users would want the ability to:

  1. override scale
  2. set the bar character
  3. define overall width.

NB: 1 and 3 may not be compatible - what's the most intuitive way of handling this?

tdwright commented 2 years ago

Another question - should we enforce a particular LongStringBehaviour for the generated column?