Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Question: Add legend to columnchart clustered #240

Closed
iilyak opened this issue Oct 4, 2023 · 4 comments · Fixed by #337
Closed

Question: Add legend to columnchart clustered #240

iilyak opened this issue Oct 4, 2023 · 4 comments · Fixed by #337
Labels

Comments

@iilyak
Copy link

iilyak commented Oct 4, 2023

Currently the bars within the group do not have labels and legend is not provided. It is hard to tell that the bars are actually represent p99, p95, min, max

@iilyak
Copy link
Author

iilyak commented Oct 4, 2023

I was able to add legend, but it would be really cool to have this functionality in CeTZ.

Screenshot 2023-10-04 at 11 35 23 AM
#let legend = (
  "p99": (stroke: black, fill: rgb("#3D5A80")),
  "p95": (stroke: black, fill: rgb("#98C1D9")),
  "min": (stroke: black, fill: rgb("#E0FBFC")),
  "max": (stroke: black, fill: rgb("#EE6C4D")),
)

#let legend-item(point, name, style) = {
  import cetz.draw: *
  content(
    (point.at(0)+2, point.at(1)+0), (point.at(0)+5, point.at(1)+0), 
    par(justify: false)[#name],
    frame: "rect",
    padding: .5em,
    fill: style.fill
  )
}

#let style(index) = legend.values().at(index)

#cetz.canvas({
  cetz.chart.columnchart(
    mode: "clustered",
    y-label: "ms",
    size: (auto, 5),
    label-key: 0,
    value-key: (..range(1, 5  )),
    bar-width: 0.9,
    bar-style: style,
    data3
  )

  let y = 4
  for (key, style) in legend {
    legend-item((4, y), key, style)
    y = y + 0.5
  }
})

Specifically what I don't like in the above solution is manual coordinates management.

@johannes-wolf
Copy link
Member

I want to add a legend function for charts and plots, but I am not yet sure what a good API could look like.

@reitermarkus
Copy link

what a good API could look like.

Maybe a value-labels option, since this adds labels to each value, and a legend option to specify the position of those labels, e.g. legend: none | "left" | "right" | "bottom".

@johannes-wolf
Copy link
Member

Implemented with #337.

Preview:
grafik

The chart API will use the plot API and therefore uses the same legend mechanism.
You can freely move the legend around or use some predefined anchors, see plot.plot for details.

@fenjalien fenjalien linked a pull request Nov 22, 2023 that will close this issue
johannes-wolf added a commit that referenced this issue Dec 7, 2023
Make the chart API a wrapper around `plot.plot({})`.

Fixes #306.
Fixes #240.
Fixes #341.

Also fixes some bugs:
- Axis grid for top/bottom axis did not work
- New axis style "scientific-auto" that only draws set axes
- New plot API `add-bar` for plotting bar/columncharts
- Improved chart styling
- Streamlines the boxwhisker style with the other chart styles
- Move boxwhisker styling attributes to style dictionary
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants