Giter Site home page Giter Site logo

plotex's Introduction

Plotex

Pure Elixir library for producing simple plots time-series plots. Currently it only supports SVG which can be used Phoenix static or LiveView pages.

See units tests for more examples of producing SVG graphs. The SVG can be styled using CSS.

Changes

  • v0.4.0 Dropped CLDR builtin support for default Elixir Calendar and renamed a few things and bug fixes
  • v0.3.0 switch SVG generation to use markers -- should significantly reduce bandwith and svg elements
  • v0.2.2 fixed multi-graph support, added test output-dual.html
  • v0.2.1 added optional support for :cldr_datetime in addition to :calendar for datetime
  • v0.2.0 refactored some of the options
  • v0.1.1 added hex package
  • v0.1.1 has support for NaiveDateTime (easier to convert to user local time on the fly)
  • v0.1.0 has basic plotting functionality included

Features

Supports creating axis and scaling for both numeric and DateTime/NaiveDateTime series from Elixir Streams or Enums. Scaling and sizing can be modified with CSS used for styling everything else including font sizes.

Graph generation is designed to be modular.

Future Features

  • The API and handline of the plot gutters need to be polished
  • Better better support for changing aspect ratios
  • Legends
  • Add introspective abilities

Installation

def deps do
  [
    {:plotex, "~> 0.1.0"}
  ]
end

Example

defmodule ExampleSVG.Graph

  alias Plotex.Output.Options

  @doc " Create Plotex Graph "
  def plot() do
      xdata = [
        ~U[2019-05-20T05:04:12.836Z],
        ~U[2019-05-20T05:13:17.836Z],
        ~U[2019-05-20T05:21:23.836Z],
        ~U[2019-05-20T05:33:25.836Z]
      ]
      ydata = [0.1, 0.25, 0.15, 0.1]
      graph_data = {xdata, ydata}

      plt = Plotex.plot(
        [ graph_data ],
        xaxis: [kind: :datetime, ticks: 5, padding: 0.05] 
      )
      
      Logger.warn("svg plotex cfg: #{inspect plt, pretty: true}")
      
      plt
  end

  def render(socket) do
      plt = plot()
      
      # These options aren't really documented, but 
      # the plotex_test.ex contains most of the basic
      # usages. 
      svg_str =
        plt |>
        Plotex.Output.Svg.generate(
          %Options{
            xaxis: %Options.Axis{
            label: %Options.Item{rotate: 35, dy: '2.5em'}},
          width: 140,
          height: 105
        })
        |> Phoenix.HTML.safe_to_string()

      assigns = [svg_str: svg_str]

      ~L"""
      <html>
        <head>
          <style>
            #{Plotex.Output.Svg.default_css()}
          </style>
        </head>
        <body>
          <%= @svg_str %>
        </body>
      </html>
      """
  end
end

Example DateTime Output

Example DateTime Output Example Dual DateTime Output

Note, SVG uses a "graphics" coordinate system where the X-Y origin are centered on the top-left. Most graphing configurations assume the X-Y origin is in the bottom left. The SVG output adjusts this by setting the Y origin to range from -100..0 and adds a negative sign to the Y-data axis. This turns out to be the simplest general way to adjust the SVG origin.

TODO

  • The configuration API needs to be expanded upon.
  • Needs work in simplifying adjusting the axis gutter widths and adjusting the ratio (this works but is very manual).
  • Documentation!
  • Would like to remove the dependency on Calendar and TZData dependency.
  • PR's welcome.

plotex's People

Contributors

elcritch avatar k-cross avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

plotex's Issues

Missing `Date` support

For the X axis, it supports NaiveDateTime and DateTime, but not Date, instead it's crashing because it's trying to subtract two date structures due to the def vals(a, _units), do: a fallback online 51 in view_range.ex. If it can't do a subtraction on the types then it should probably fail here instead of crashing when trying to subtract two maps.

CSS Stroke is not set

In the CSS the stroke is not set, so it uses what's on the page, which if is non-default then it makes the text unreadable. The stroke value should be set on the entire svg back to an expected value (default is the value none, which is what I'm manually adding now).

Calendar module is marked as optional, but appears to be required

As per title, the calendar dependency is marked as {:calendar, "~> 1.0.0", optional: true},, however I can't seem to find a use that doesn't require it, always getting the exception of function Calendar.Strftime.strftime/2 is undefined (module Calendar.Strftime is not available). I think elixir has that functionality in it now as well (or at least has a PR for it), though not too hard to do it manually with Elixir's existing std functionality.

README.md is incorrect on `Plotex.Output.Svg.generate`

In the README.md it has the line Plotex.Output.Svg.generate(plt, xaxis: [rotate: 35, dy: '2.5em'], yaxis: []), however it fails because the second argument can't be a keyword list but rather must be an Options struct, which isn't mentioned of how to use in the readme at all.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.