When driving JS libraries from Seaside it is often necessary to build a tree of dictionaries to encode settings - for example, using vega-lite to display charts & graphs.
I currently have a very simple case that ends up rather verbose - |barchart| barchart := Dictionary new. barchart at: #'$schema' put: 'https://vega.github.io/schema/vega-lite/v5.json'; at: #description put: 'A simple bar chart'; at: #width put: 'container'; at: #usermeta put: (Dictionary new at: #embedOptions put: (Dictionary new at: #export put: true; at: #compiled put: false; at: #editor put: false; yourself); yourself); at: #mark put: (Dictionary new at: #type put: #bar; at: #tooltip put: true; yourself); at: #encoding put: (Dictionary new at: #x put: (Dictionary new at: #field put: 'label'; at: #type put: #nominal; at: #axis put: (Dictionary new at: #labelAngle put: -45; yourself); at: #title put: #Group; yourself); at: #y put: (Dictionary new at: #field put: 'value'; at: #type put: #quantitative; at: #title put: #Value; yourself); yourself); at: #height put:'container'. ^barchart
... and that's without any actual data!
If I want to do something like change the label angle I end up with ugliness like (((chartSpec at: #encoding) at: #x) at: #axis) at:#labelAngle put: axisLabellingAngle.
I feel sure I've seen this done better but I can't find anything with my current searches. It wouldn't be too hard to implement a method to do #atPath: #(encoding x axis labelAngle) put: thingy but if it's been done cleanly by somebody I'd much rather just load it.
Somebody please point me to the ludicrously obvious answer any idiot should have spotted immediately...
tim -- tim Rowledge; tim@rowledge.org; http://www.rowledge.org/tim Strange OpCodes: AG: Add Gibberish
What about
#(encoding x axis labelAngle) inject: barchart into: [:dict :segment | dict at: segment].
or even shorter
#(encoding x axis labelAngle) inject: barchart into: #at:.
?
Best,
Christoph
________________________________ Von: Squeak-dev squeak-dev-bounces@lists.squeakfoundation.org im Auftrag von tim Rowledge tim@rowledge.org Gesendet: Donnerstag, 2. September 2021 01:33:31 An: The general-purpose Squeak developers list Betreff: [squeak-dev] Json dictionary tree accessing
When driving JS libraries from Seaside it is often necessary to build a tree of dictionaries to encode settings - for example, using vega-lite to display charts & graphs.
I currently have a very simple case that ends up rather verbose - |barchart| barchart := Dictionary new. barchart at: #'$schema' put: 'https://vega.github.io/schema/vega-lite/v5.json'; at: #description put: 'A simple bar chart'; at: #width put: 'container'; at: #usermeta put: (Dictionary new at: #embedOptions put: (Dictionary new at: #export put: true; at: #compiled put: false; at: #editor put: false; yourself); yourself); at: #mark put: (Dictionary new at: #type put: #bar; at: #tooltip put: true; yourself); at: #encoding put: (Dictionary new at: #x put: (Dictionary new at: #field put: 'label'; at: #type put: #nominal; at: #axis put: (Dictionary new at: #labelAngle put: -45; yourself); at: #title put: #Group; yourself); at: #y put: (Dictionary new at: #field put: 'value'; at: #type put: #quantitative; at: #title put: #Value; yourself); yourself); at: #height put:'container'. ^barchart
... and that's without any actual data!
If I want to do something like change the label angle I end up with ugliness like (((chartSpec at: #encoding) at: #x) at: #axis) at:#labelAngle put: axisLabellingAngle.
I feel sure I've seen this done better but I can't find anything with my current searches. It wouldn't be too hard to implement a method to do #atPath: #(encoding x axis labelAngle) put: thingy but if it's been done cleanly by somebody I'd much rather just load it.
Somebody please point me to the ludicrously obvious answer any idiot should have spotted immediately...
tim -- tim Rowledge; tim@rowledge.org; http://www.rowledge.org/tim Strange OpCodes: AG: Add Gibberish
Hi
I think you can do two tings: Model it out or mock with JsonObject
On 2. Sep 2021, at 01:33, tim Rowledge tim@rowledge.org wrote:
When driving JS libraries from Seaside it is often necessary to build a tree of dictionaries to encode settings - for example, using vega-lite to display charts & graphs.
I currently have a very simple case that ends up rather verbose - |barchart| barchart := Dictionary new. barchart at: #'$schema' put: 'https://vega.github.io/schema/vega-lite/v5.json'; at: #description put: 'A simple bar chart'; at: #width put: 'container'; at: #usermeta put: (Dictionary new at: #embedOptions put: (Dictionary new at: #export put: true; at: #compiled put: false; at: #editor put: false; yourself); yourself); at: #mark put: (Dictionary new at: #type put: #bar; at: #tooltip put: true; yourself); at: #encoding put: (Dictionary new at: #x put: (Dictionary new at: #field put: 'label'; at: #type put: #nominal; at: #axis put: (Dictionary new at: #labelAngle put: -45; yourself); at: #title put: #Group; yourself); at: #y put: (Dictionary new at: #field put: 'value'; at: #type put: #quantitative; at: #title put: #Value; yourself); yourself); at: #height put:'container'. ^barchart
... and that's without any actual data!
If I want to do something like change the label angle I end up with ugliness like (((chartSpec at: #encoding) at: #x) at: #axis) at:#labelAngle put: axisLabellingAngle.
I feel sure I've seen this done better but I can't find anything with my current searches. It wouldn't be too hard to implement a method to do #atPath: #(encoding x axis labelAngle) put: thingy but if it's been done cleanly by somebody I'd much rather just load it.
Somebody please point me to the ludicrously obvious answer any idiot should have spotted immediately...
Soo, if you mock it like that:
|barchart| barchart := JsonObject new. barchart at: '$schema' put: 'https://vega.github.io/schema/vega-lite/v5.json'; description: 'A simple bar chart'; width: 'container'; usermeta: (JsonObject new embedOptions: (JsonObject new export: true; compiled: false; editor: false; yourself); yourself); mark: (JsonObject new type: #bar; tooltip: true; yourself); encoding: (JsonObject new x: (JsonObject new field: 'label'; type: #nominal; axis: (JsonObject new labelAngle: -45; yourself); title: #Group; yourself); y: (JsonObject new field: 'value'; type: #quantitative; title: #Value; yourself); yourself); height: 'container'. ^barchart
you can easily say
barchart encoding x axis lableAngle: axisLabellingAngle
The other thing is: Make your Bar chart spec a class, as well as the metadata options etc.pp. Then implement #jsonWriteOn: (see JsonDummyTestObject >> #jsonWriteOn: for an example)
Best regards -Tobias
On Thu, 2 Sep 2021, Tobias Pape wrote:
Hi
I think you can do two tings: Model it out or mock with JsonObject
On 2. Sep 2021, at 01:33, tim Rowledge tim@rowledge.org wrote:
When driving JS libraries from Seaside it is often necessary to build a tree of dictionaries to encode settings - for example, using vega-lite to display charts & graphs.
I currently have a very simple case that ends up rather verbose - |barchart| barchart := Dictionary new. barchart at: #'$schema' put: 'https://vega.github.io/schema/vega-lite/v5.json'; at: #description put: 'A simple bar chart'; at: #width put: 'container'; at: #usermeta put: (Dictionary new at: #embedOptions put: (Dictionary new at: #export put: true; at: #compiled put: false; at: #editor put: false; yourself); yourself); at: #mark put: (Dictionary new at: #type put: #bar; at: #tooltip put: true; yourself); at: #encoding put: (Dictionary new at: #x put: (Dictionary new at: #field put: 'label'; at: #type put: #nominal; at: #axis put: (Dictionary new at: #labelAngle put: -45; yourself); at: #title put: #Group; yourself); at: #y put: (Dictionary new at: #field put: 'value'; at: #type put: #quantitative; at: #title put: #Value; yourself); yourself); at: #height put:'container'. ^barchart
... and that's without any actual data!
If I want to do something like change the label angle I end up with ugliness like (((chartSpec at: #encoding) at: #x) at: #axis) at:#labelAngle put: axisLabellingAngle.
I feel sure I've seen this done better but I can't find anything with my current searches. It wouldn't be too hard to implement a method to do #atPath: #(encoding x axis labelAngle) put: thingy but if it's been done cleanly by somebody I'd much rather just load it.
Somebody please point me to the ludicrously obvious answer any idiot should have spotted immediately...
Soo, if you mock it like that:
|barchart| barchart := JsonObject new. barchart at: '$schema' put: 'https://vega.github.io/schema/vega-lite/v5.json'; description: 'A simple bar chart'; width: 'container'; usermeta: (JsonObject new embedOptions: (JsonObject new export: true; compiled: false; editor: false; yourself); yourself); mark: (JsonObject new type: #bar; tooltip: true; yourself); encoding: (JsonObject new x: (JsonObject new field: 'label'; type: #nominal; axis: (JsonObject new labelAngle: -45; yourself); title: #Group; yourself); y: (JsonObject new field: 'value'; type: #quantitative; title: #Value; yourself); yourself); height: 'container'. ^barchart
Starting from Json-ul.48, you don't need those #yourself sends when constructing json objects, so you can save those and the barchart variable as well:
^JsonObject new at: '$schema' put: 'https://vega.github.io/schema/vega-lite/v5.json'; description: 'A simple bar chart'; width: 'container'; usermeta: (JsonObject new embedOptions: (JsonObject new export: true; compiled: false; editor: false)); mark: (JsonObject new type: #bar; tooltip: true); encoding: (JsonObject new x: (JsonObject new field: 'label'; type: #nominal; axis: (JsonObject new labelAngle: -45); title: #Group); y: (JsonObject new field: 'value'; type: #quantitative; title: #Value)); height: 'container'
Levente
Hi Levente
On 2. Sep 2021, at 12:01, Levente Uzonyi leves@caesar.elte.hu wrote:
Starting from Json-ul.48, you don't need those #yourself sends when constructing json objects, so you can save those and the barchart variable as well:
nice, I actually just copy-replaced in tim's code, but that one is actually even more concise.
^JsonObject new at: '$schema' put: 'https://vega.github.io/schema/vega-lite/v5.json'; description: 'A simple bar chart'; width: 'container'; usermeta: (JsonObject new embedOptions: (JsonObject new export: true; compiled: false; editor: false)); mark: (JsonObject new type: #bar; tooltip: true); encoding: (JsonObject new x: (JsonObject new field: 'label'; type: #nominal; axis: (JsonObject new labelAngle: -45); title: #Group); y: (JsonObject new field: 'value'; type: #quantitative; title: #Value)); height: 'container'
Levente
Best regards -Tobias
I love this way of using dNU: to build as you go.
The Parser fights back by getting all hissy about the unknown messages. Yes, one can handle this manually and so on in a browser, or I suppose filing in the code.
On 2021-09-02, at 3:01 AM, Levente Uzonyi leves@caesar.elte.hu wrote:
^JsonObject new at: '$schema' put: 'https://vega.github.io/schema/vega-lite/v5.json'; description: 'A simple bar chart'; width: 'container'; usermeta: (JsonObject new embedOptions: (JsonObject new export: true; compiled: false; editor: false));
tim -- tim Rowledge; tim@rowledge.org; http://www.rowledge.org/tim Strange OpCodes: PUS: PUrge System
I didn't test it, but this reduces the cruft and should avoid the compiler messages.
|barchart| barchart := { #'$schema' -> 'https://vega.github.io/schema/vega-lite/v5.json'. #description -> 'A simple bar chart'. #usermeta -> ({ #embedOptions -> (export -> true. compiled -> false. editor: false } as: JsonObject). #width -> 'container' } as: JsonObject
On Thu, Sep 2, 2021 at 4:14 PM tim Rowledge tim@rowledge.org wrote:
I love this way of using dNU: to build as you go.
The Parser fights back by getting all hissy about the unknown messages. Yes, one can handle this manually and so on in a browser, or I suppose filing in the code.
On 2021-09-02, at 3:01 AM, Levente Uzonyi leves@caesar.elte.hu wrote:
^JsonObject new at: '$schema' put: '
https://vega.github.io/schema/vega-lite/v5.json';
description: 'A simple bar chart'; width: 'container'; usermeta: (JsonObject new embedOptions: (JsonObject new export: true; compiled: false; editor: false));
tim
tim Rowledge; tim@rowledge.org; http://www.rowledge.org/tim Strange OpCodes: PUS: PUrge System
squeak-dev@lists.squeakfoundation.org