manuel-rubio

manuel-rubio

I’m excited to introduce you to Plotto, a chart library created in 100% Elixir that generates SVG and PNG images. Yes, it’s not only generating SVG for interactive use in combination with LiveView, but you can also generate directly to PNG with the same image and send it vía email, Telegram, Slack, or whatever you need.

Plotto is a plot library, 100% Elixir, that’s focused on generating beautiful SVG charts and exporting the same chart to PNG when it’s needed — including the PNG rasterizer and TrueType font renderer, no external binaries or NIFs required.

It is very useful when you are developing a website and need to integrate SVG charts. Chart data items accept arbitrary HTML/SVG attributes (phx-click, data-*, etc.), so if you are using Phoenix LiveView you can attach events, actions, and feedback to individual bars/points — without Plotto depending on Phoenix or LiveView in any way.

If you need to export or generate PNG charts for email, PDF, or sending via Telegram, Slack, Mattermost, etc., Plotto.to_png/1 renders the same chart to a PNG binary, anti-aliased and with full Unicode text support (including accented characters like á, é, ñ) via a bundled DejaVu Sans font.

Charts can also show an optional title and a legend (one color swatch + name row per series), positioned in any of the four corners — see :title and :legend in Plotto.BarChart or Plotto.LineChart. Negative values and mixed positive/negative domains are fully supported with an automatic zero baseline.

It was created to be introduced in the project Conta.

Showing Posts 1 to 8

alvinkatojr

alvinkatojr

You are killling it, man! I can’t wait to dive into the source. This and press both look amazing! Good work!

MarcusRiemer

MarcusRiemer

Oh, this is very interesting, thank you for your contribution! I’m currently rendering my own charts for (effectively) a distribution of outcomes for a game with dice:

Could I switch to Plotto for this? I skimmed the examples and didn’t see an option to put labels above the bars, but maybe I’m missing something. The colours in my example change whenever the distribution switches from “target damaged” to “target defeated”. I think for that I would have to pass two seperate series?

manuel-rubio

manuel-rubio OP

I’m using both in Conta :wink:

manuel-rubio

manuel-rubio OP

Done! New version 0.4.0 supports that:

categories = ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "> 10"]

# 0..4 en la primera serie (rosa), 5..> 10 en la segunda serie (azul)
first_values = [9.3, 6.7, 8.4, 10.1, 12.0, 0, 0, 0, 0, 0, 0]
second_values = [0, 0, 0, 0, 0, 14.0, 9.4, 8.5, 7.2, 5.7, 8.6]

data = [
  %{
    name: "0-4 Range",
    data: Enum.zip_with(categories, first_values, fn cat, val -> %{label: cat, value: val} end)
  },
  %{
    name: "5+ Range",
    data: Enum.zip_with(categories, second_values, fn cat, val -> %{label: cat, value: val} end)
  }
]

pink = "#F06292"
blue = "#4E79A7"

chart =
  Plotto.BarChart.new!(
    data,
    mode: :stacked,
    colors: [pink, blue],
    label: fn item ->
      if item.value > 0 do
        "#{item.value}%"
      end
    end,
    width: 700,
    height: 400
  )

svg = Plotto.to_svg!(chart)
png = Plotto.to_png!(chart)

File.write!("two_color_bars.svg", svg)
File.write!("two_color_bars.png", png)

Here you go. The proof:

MarcusRiemer

MarcusRiemer

I started integrating Plotto and just stumbled over this: Plotto.LineChart — plotto v0.4.0

data uses the same multi-series shape as Plotto.BarChart, but today only the first series is drawn — full multi-series line rendering (multiple lines) is planned for a future release

Unluckily for me I do have line charts that look like this:

I could find another way to represent same-faction series than dashing, but the multi-series line chart have been exactly the chart I wanted to move to plotto in order to be able to render them for og:images.

Any ETA on that “future release”? :innocent:

manuel-rubio

manuel-rubio OP

Of course, but I don’t understand exactly what do you expect and what’s the issue? :stuck_out_tongue:

manuel-rubio

manuel-rubio OP

Done! New version 0.5.0

data = [
  %{
    name: "Elektrogriff (Gewitterbote)",
    dashed: true,
    color: "#9B59B6",
    data: [
      %{label: "1", value: 12},
      %{label: "2", value: 49},
      %{label: "3", value: 74},
      %{label: "4", value: 87},
      %{label: "5", value: 98}
    ]
  },
  %{
    name: "Sturmbogen (Wüstenjägerin)",
    color: "#8E44AD",
    data: [
      %{label: "1", value: 0},
      %{label: "2", value: 12},
      %{label: "3", value: 26},
      %{label: "4", value: 49},
      %{label: "5", value: 63},
      %{label: "6", value: 75},
      %{label: "7", value: 80},
      %{label: "8", value: 96}
    ]
  },
  %{
    name: "Jagddolch (Wüstenjägerin)",
    color: "#4A235A",
    data: [
      %{label: "1", value: 0},
      %{label: "2", value: 1},
      %{label: "3", value: 5},
      %{label: "4", value: 12},
      %{label: "5", value: 25},
      %{label: "6", value: 27},
      %{label: "7", value: 40},
      %{label: "8", value: 50},
      %{label: "9", value: 52},
      %{label: "10", value: 63}
    ]
  }
]

chart =
  Plotto.LineChart.new!(
    data,
    legend: :right_top,
    stroke_width: 3,
    width: 700,
    height: 450
  )

svg = Plotto.to_svg!(chart)
png = Plotto.to_png!(chart)

svg_path = Path.join(__DIR__, "weapons_chart.svg")
png_path = Path.join(__DIR__, "weapons_chart.png")

File.write!(svg_path, svg)
File.write!(png_path, png)

IO.puts("Successfully generated:")
IO.puts(" - SVG: #{svg_path}")
IO.puts(" - PNG: #{png_path}")

MarcusRiemer

MarcusRiemer

This is wonderful! I had some time yesterday and put Plotto to use at Wüstenjägerin gegen Gewitterbote · Summoners Compendium (linked page shows embedded SVG, is in german and is a technically unreleased feature that may still be buggy) and as png for the og:image:

I made minor CSS tweaks to show labels and axis even in darkmode:

/* Relies on Daisy UI, not portable */
.plotto-embed .plotto-axis {
  stroke: var(--color-base-300);
}

/* Maybe worth porting over: bridges the gap between HTML color for text and svg */
.plotto-embed .plotto-label,
.plotto-embed .plotto-legend,
.plotto-embed text {
  fill: currentColor;
}

Asking you for features has been so scarily effective, that I am almost hesitant to propose more. Please don’t feel pressured, these are things that would delight me, but I am very happy as is:

  1. Change the legends orientation to horizontal when being plotted under or over the graph. The current layout works well, but I do see this as an option to reduce empty whitespace by plotting my up to 4 options below the graph in a horizontal layout.
  2. Configuration option to add target values for the y axis. In my case I would like to always go to 100.
  3. Configuration option to add a suffix to the values (y-axis label and <title> on circles), in my case this would be a %.
  4. Configuration option to show horizontal guidelines with the y axis labels.
— All posts loaded —

Where Next? Top

Trending in Announcing Top

wojtekmach
Hey everyone! Req is an HTTP client for Elixir that I’ve been working on for quite some time. There is already a lot of HTTP clients out...
New
handnot2
Samly can be used to enable SAML 2.0 Single Sign On in a Plug/Phoenix application. This library uses Erlang esaml to provide plug enabl...
New
woylie
Flop is an Elixir library that applies filtering, ordering and pagination parameters to your Ecto queries. offset-based pagination with...
New
MRdotB
I needed to reuse React components from my Chrome extension in my Phoenix/LiveView backend. I noticed that for Svelte/Vue, there are live...
New
garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New
marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
New
fuelen
Hi all! I want to present a small library which provides a mix task for generating an Entity-Relationship Diagram for Ecto schemas. You...
New

Other Trending Topics Top

mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
AstonJ
This showed up on my feed.. anyone heard of it? Just hype? Ox Alpha is a reasoning model designed for coding, sustained ag...
New
sergio
It’s not that it’s vocabulary is too advanced. It’s something worse. I get lost trying to follow even a paragraph written by Claude. It’...
New
sorenone
Today we’re releasing Oban for Python. Not an Oban client in Python. Not a pythonx wrapper embedded in Elixir. Nope, it’s a fully operati...
New
akoutmos
@hugobarauna, Dr. Dimitrios Koutmos (my brother) and I (Alex Koutmos) have been hard at work on writing a book on how you can use Elixir ...
New
pferriby
Introductory paragraph I’ll be looking for a keen junior or someone that has a couple of years experience in the real world (so you’ve be...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews