touying-typ / touying

Touying is a powerful package for creating presentation slides in Typst.
https://touying-typ.github.io/
MIT License
533 stars 13 forks source link

Uncovering parts of chart / plot #57

Closed namednil closed 1 month ago

namednil commented 1 month ago

Touying is amazing - thank you for creating it!

How would you uncover parts of a chart in multiple steps, e.g. first only show the bars for 15-24, then add the bars for 25-29 etc.

#import "@preview/touying:0.4.2": *
#import "@preview/cetz:0.2.2"

#let cetz-canvas = touying-reducer.with(reduce: cetz.canvas, cover: cetz.draw.hide.with(bounds: true))

#let s = themes.default.register(aspect-ratio: "16-9")

// Extract methods
#let (init, slides, touying-outline, alert, speaker-note) = utils.methods(s)
#show: init

// Extract slide functions
#let (slide, empty-slide) = utils.slides(s)
#show: slides

#let data2 = (
                 (  ([15-24], 18.0, 20.1, 23.0, 17.0),
  ([25-29], 16.3, 17.6, 19.4, 15.3),
  ([30-34], 14.0, 15.3, 13.9, 18.7),
  ([35-44], 35.5, 26.5, 29.4, 25.8),
  ([45-54], 25.0, 20.6, 22.4, 22.0),
  ([55+],   19.9, 18.2, 19.2, 16.4),)
)

#cetz-canvas({
  cetz.draw.set-style(legend: (fill: white))
  cetz.chart.columnchart(mode: "clustered",
                 size: (20, 9),
                 label-key: 0,
                 value-key: (..range(1, 5)),
                 bar-width: .8,
                 y-tick-step: 5,
                data2,
                 labels: ([Low], [Medium], [High], [Very high]),
                 legend: "legend.inner-north-east",)
})

And could you do the same to uncover the lines of a plot, e.g. show the first line, show the second line, then show the arrow

#cetz-canvas({
  import cetz.plot
  import cetz.draw: *
  plot.plot(size: (10,10), name: "plot",
  x-tick-step: none, y-tick-step: none, {
  plot.add(((0,0), (1,1), (2,.5), (4,3)))
  plot.add(((0,2), (1,1), (2, 1.5), (4,1.5)))
  plot.add-anchor("pt", (1,1))
  })
  (pause,)
  line("plot.pt", ((), "|-", (0,1.5)), mark: (start: ">"), name: "line")
  content("line.end", [Here], anchor: "north", padding: .1)
}
)
OrangeX4 commented 1 month ago

First one:

#slide(
  repeat: data2.len(),
  self => [
    #cetz-canvas({
      cetz.draw.set-style(legend: (fill: white))
      cetz.chart.columnchart(
        mode: "clustered",
        size: (20, 9),
        label-key: 0,
        value-key: (..range(1, 5),),
        bar-width: .8,
        y-tick-step: 5,
        data2.slice(0, self.subslide) + (([], 0, 0, 0, 0),) * (data2.len() - self.subslide),
        labels: ([Low], [Medium], [High], [Very high]),
        legend: "legend.inner-north-east",
      )
    })
  ],
)

Second one:

#slide(self => [
  #let (only, uncover) = utils.methods(self)

  #cetz-canvas({
    import cetz.plot
    import cetz.draw: *
    plot.plot(
      size: (10, 10),
      name: "plot",
      x-tick-step: none,
      y-tick-step: none,
      {
        only("1-", plot.add(((0, 0), (1, 1), (2, .5), (4, 3))))
        only("2-", plot.add(((0, 2), (1, 1), (2, 1.5), (4, 1.5))))
        plot.add-anchor("pt", (1, 1))
      },
    )
    (pause,) * 2
    line("plot.pt", ((), "|-", (0, 1.5)), mark: (start: ">"), name: "line")
    content("line.end", [Here], anchor: "north", padding: .1)
  })
])
namednil commented 1 month ago

Thank you so much for the quick response! It works like a charm, and repeating slides for this makes a lot of sense!

Just for reference, the second one can also be handled more similarly to the first one:

#slide(
  repeat:3,
  self => [
  #let (only, uncover) = utils.methods(self)

  #cetz-canvas({
    import cetz.plot
    import cetz.draw: *
    plot.plot(
      size: (10, 10),
      name: "plot",
      x-tick-step: none,
      y-tick-step: none,
      {
        only("1-", plot.add(((0, 0), (1, 1), (2, .5), (4, 3))))
        only("2-", plot.add(((0, 2), (1, 1), (2, 1.5), (4, 1.5))))
        plot.add-anchor("pt", (1, 1))
      },
    )
    only("3-", {
    line("plot.pt", ((), "|-", (0, 1.5)), mark: (start: ">"), name: "line")
    content("line.end", [Here], anchor: "north", padding: .1) })
  })
])