Code Monkey home page Code Monkey logo

chartkick-ex's Introduction

Chartkick Build Status

Create beautiful Javascript charts with one line of Elixir. No more fighting with charting libraries!

See it in action, you can find the example phoenix app that generates that page here.

Works with Phoenix, plain Elixir and most browsers.

Any feedback, suggestions or comments please open an issue or PR.

Charts

All charts expect a JSON string.

raw_data = [[175, 60], [190, 80], [180, 75]]
data = Poison.encode!(raw_data)
# or if you are using Jason
data = Jason.encode!(raw_data)

Line chart

Chartkick.line_chart data

Pie chart

Chartkick.pie_chart data

Column chart

Chartkick.column_chart data

Bar chart

Chartkick.bar_chart data

Area chart

Chartkick.area_chart data

Scatter chart

Chartkick.scatter_chart data

Geo chart

Chartkick.geo_chart Poison.encode!("[[\"United States\",44],[\"Germany\",23]]")
# or if you are using Jason
Chartkick.geo_chart Jason.encode!("[[\"United States\",44],[\"Germany\",23]]")

Timeline

Chartkick.timeline "[
  [\"Washington\", \"1789-04-29\", \"1797-03-03\"],
  [\"Adams\", \"1797-03-03\", \"1801-03-03\"],
  [\"Jefferson\", \"1801-03-03\", \"1809-03-03\"]
]"

Say Goodbye To Timeouts

Make your pages load super fast and stop worrying about timeouts. Give each chart its own endpoint.

Chartkick.line_chart "/path/to/chart.json"

And respond with data as JSON.

Options

ℹī¸ This implementation aims to support all options that chartkick.js supports. If there are any missing, please open an issue or a PR.

Id, width and height

Chartkick.line_chart data, id: "users-chart", height: "500px", width: "50%"

Min and max values

Chartkick.line_chart data, min: 1000, max: 5000

min defaults to 0 for charts with non-negative values. Use nil to let the charting library decide.

Colors

Chartkick.line_chart data, colors: ["pink", "#999"]

Stacked columns or bars

Chartkick.column_chart data, stacked: true

Discrete axis

Chartkick.line_chart data, discrete: true

Axis titles

Chartkick.line_chart data, xtitle: "Time", ytitle: "Population"

Straight lines between points instead of a curve

Chartkick.line_chart data, curve: false

Hide points

Chartkick.line_chart data, points: false

Show or hide legend

Chartkick.line_chart data, legend: false

Specify legend position

Chartkick.line_chart data, legend: "bottom"

Donut chart

Chartkick.pie_chart data, donut: true

Prefix, useful for currency - Chart.js, Highcharts

Chartkick.line_chart data, prefix: "$"

Suffix, useful for percentages - Chart.js, Highcharts

Chartkick.line_chart data, suffix: "%"

Set a thousands separator - Chart.js, Highcharts

Chartkick.line_chart data, thousands: ","

Set a decimal separator - Chart.js, Highcharts

Chartkick.line_chart data, decimal: ","

Show insignificant zeros, useful for currency - Chart.js, Highcharts

Chartkick.line_chart data, round: 2, zeros: true

Show a message when data is empty

Chartkick.line_chart data, messages: %{ empty: "My message.."}

Refresh data from a remote source every n seconds

Chartkick.line_chart data, refresh: 60

You can pass options directly to the charting library with:

Chartkick.line_chart data, library: %{ backgroundColor: "#eee" }

See the documentation for Google Charts and Highcharts for more info.

To customize datasets in Chart.js, use:

Chartkick.line_chart data, dataset: %{ borderWidth: 10 }

Data

Pass data as a JSON string.

Chartkick.pie_chart "{\"Football\" => 10, \"Basketball\" => 5}"
Chartkick.pie_chart "[[\"Football\", 10], [\"Basketball\", 5]]"

For multiple series, use the format

Chartkick.line_chart "[
  {name: \"Series A\", data: []},
  {name: \"Series B\", data: []}
]"

Times can be a time, a timestamp, or a string (strings are parsed)

Chartkick.line_chart "{
  1368174456 => 4,
  \"2013-05-07 00:00:00 UTC\" => 7
}"

Installation

You need to set JSON encoder in your config file. This encoder is used to encode options passed to Chartkick.

# config.exs
config :chartkick, json_serializer: Jason

By default when you render a chart it will return both the HTML-element and JS that initializes the chart. This will only work if you load Chartkick in the <head> tag. You can chose to render the JS & HTML separately using the only: :html or only: :script option. Note that if you use those options you need to pass id otherwise it wont work.

line_chart data, id: "my-line-chart", only: :html
line_chart data, id: "my-line-chart", only: :script

For Google Charts, use:

<script src="//www.google.com/jsapi"></script>
<script src="path/to/chartkick.js"></script>

If you prefer Highcharts, use:

<script src="/path/to/highcharts.js"></script>
<script src="path/to/chartkick.js"></script>

Localization

To specify a language for Google Charts, add:

<script>
  var Chartkick = {"language": "de"};
</script>

before the javascript files.

No Elixir? No Problem

Check out

History

View the changelog

Chartkick follows Semantic Versioning

Contributing

Everyone is encouraged to help improve this project. Here are a few ways you can help:

chartkick-ex's People

Contributors

arathunku avatar buren avatar dependabot-preview[bot] avatar dependabot[bot] avatar dzfranklin avatar fklement avatar nbernardes avatar sevab avatar sigu avatar treble37 avatar vikger 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  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

chartkick-ex's Issues

Crashing in production

The graph works okay in my local environment but in production, it crashes.

Error

function UUID.uuid4/0 is undefined (module UUID is not available)

Poison dependency need to be overriden

I get an error because the Poison in my mix.lock is a different version than the one required by Chartkick. I have tried multiple things like unlocking the dependency and still not luck.

  chartkick (version 0.0.2) requires ~> 1.5
  mix.lock specifies 3.1.0

** (Mix) Hex dependency resolution failed, change the version requirements of your dependencies or unlock them (by using mix deps.update or mix deps.unlock). If you are unable to resolve the conflicts you can try overriding with {:dependency, "~> 1.0", override: true}```

Support deferring js

I'd prefer to render the rest of the page first, load Chartkick, and then render the charts. This would remove the requirement to load Chartkick in the head.

I think this could by changing the generated js to something like

(function() {
  function render() {
    // .. existing generated js
  }

  if (window.Chartkick !== undefined) {
    render()
  } else {
    window.addEventListener("chartkick-loaded", render);
  }
}(();

and also changing chartkick.js to emit that event.

Project development

This project are abandoned?
plus I have a question:
1)This project is integrated with phoenix framework??

Change font color

Thanks for your great job
I am wondering, if there is a way to change the font color?
I am using black background and the text is hard to read

Another question: Is there a way to put a text at the top left corner of a line chart?

Thanks again

Uncaught ReferenceError: Chartkick is not defined error

Hi @buren , I am trying to make this work in Elixir/Phoenix 1.4 and getting this error

Uncaught ReferenceError: Chartkick is not defined error

I followed README to add below to my mix.exs file:

{:chartkick, "~>0.4.0"}

and added below to my template file.

data = Poison.encode!([[175, 60], [190, 80], [180, 75]])
Chartkick.line_chart data

Did I miss something? Sorry, I am a newbie.

Mix release is broken when chartkick is included

The following error message is displayed when starting a released elixir app that includes chartkick:

** (UndefinedFunctionError) function UUID.uuid4/0 is undefined (module UUID is not available)

To properly include the dependencies inside chartkick, logger should be defined under extra_applications instead of `applications, as the latter redefines the default behaviour of starting dependent applications.

The background is described in detail here:
https://www.amberbit.com/blog/2019/8/23/mix-release-and-missing-dependencies/

Not working inside LiveView phoenix

Great package. Thanks
When I put it on a regular view it works fine but what I put it in a liveView render it display the chart for a split of a sec and then just put "Loading..."
I believe that the LiveView is taking control but I don't know how to solve it

Livebook support

Hi there! Thanks a lot for the library. Do you happen to know if it's possible to use chartkick-ex with livebook?

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.