vojtamolda / Plotly.swift

Interactive data visualization library for Swift
https://vojtamolda.github.io/Plotly.swift/
MIT License
82 stars 8 forks source link

Built-in Support for TabularData Framework #20

Open vojtamolda opened 2 years ago

vojtamolda commented 2 years ago

Apple recently published TabularData framework. Basically, it is a typed clone of Pandas from Python.

I'm a little disappointed that the framework isn't open sourced and available on Linux. I think it may hinder adoption, but hey that's life. There isn't much documentation or examples but there's a nice tech talk video that describes the API on a nice example.

It would be nice to add support for the DataFrame and Column types directly into the library. Best way to support the framework is add conformance to the Plotable protocol to a few types. This would allow out of the box dataset exploration and manipulation in Xcode Playgrounds.

Here's an example conformance extension and the generated figure:

import TabularData
import Plotly

let dataFrame: TabularData.DataFrame = [
    "id": [1, 2, 3],
    "name": ["alice", "bob", "charlie"]
]

let barPlot = Plotly.Bar(
    x: dataFrame[column: 1].assumingType(String.self),
    y: dataFrame[column: 0].assumingType(Int.self)
)

let figure = Plotly.Figure(data: [barPlot])
try figure.show()

extension TabularData.Column: Plotly.Plotable where WrappedElement: Encodable {
    public func encode(toPlotly encoder: Encoder) throws {
        var container = encoder.unkeyedContainer()
        for element in self {
            try container.encode(element)
        }
    }
}
Screen Shot 2021-12-08 at 23 21 23