How to display a graph in Ruby on Rails (LazyHighChart)

I'm just starting to learn programming, so it may be a poor article, but I would like to write such an article as a memorandum and practice for myself. I look forward to working with you.

What is Lazy High Chart?

Simply put, it is a gem that displays a graph. It looks like a gem that makes it possible to use a JavaScript graph library called HighCharts in Ruby. I will write it early, but I am not sure yet how to change the display method of the graph itself, so this time I will write only the implementation.

Introducing Gem

gem 'lazy_high_charts'

First, write this in the Gemfile and bundle install

In addition to this in application.js

//= require highcharts/highcharts
//= require highcharts/highcharts-more
//= require highcharts/highstock

Let's add. Now you are ready.

Display graph in View

First is the description on the View side.

<%= high_chart('my-first-chart', @chart) %>

The graph will be drawn where this description is made.

Controller side settings

Here, I decide the type of graph (bar graph, pie graph, line graph, etc.), but as I mentioned earlier, I don't know the details yet, so I will write the description I implemented for the time being.

  def create
    @post = Post.new(post_params)

    if @post.save
-----------------------------------------------------#Description for displaying a graph
    @chart = LazyHighCharts::HighChart.new('graph') do |f|
      f.title(text: @post.select.name)
      f.xAxis(categories: ["Soldier", "Wizard", "Monk", "Fighter", "Gunner"])
      f.series(name: "", yAxis: 1, data: [@post.vs_soldier_id,@post.vs_wizard_id,@post.vs_monk_id,@post.vs_fighter_id,@post.vs_gunner_id])
      f.yAxis [
        {title: {text: "", margin: 0} },
        {title: {text: ""}, opposite: true},
      ]
      f.legend(align: 'none')
      f.chart({defaultSeriesType: "column"})
    end
-------------------------------------------------------------------------

    else
      render "new"
    end
  end

It is a description when the value acquired by the strong parameter from the new action is displayed by the create action.

This time, a bar graph is displayed. f.yAxis f.legend f.series I'm not sure about these three because I didn't need them in this implementation (sorry)

f.title As written, the value you enter here will be the title of the graph.

f.xAxis The value on the horizontal axis of the graph. This time it was decided here, so I put a character string in it.

And in the f.series data: [] I will put the data here. (I did a lot of variables to put values here, but it was hard for beginners ...)

f.chart({defaultSeriesType: "column"}) It seems that the "column" here is the type of graph.

that's all.

There are still many things I don't understand and it has become a messy article, but I would like to do my best to write good things little by little. Then ~

Recommended Posts

How to display a graph in Ruby on Rails (LazyHighChart)
[Ruby on Rails] How to display error messages
[Ruby on Rails] How to install Bootstrap in Rails
[Ruby on Rails] How to write enum in Japanese
[Ruby On Rails] How to reset DB in Heroku
How to use Ruby on Rails
Apply CSS to a specific View in Ruby on Rails
[Ruby on Rails] How to use CarrierWave
How to insert a video in Rails
[Ruby on Rails] How to use redirect_to
[Ruby on Rails] How to use kaminari
[Rails] How to create a graph using lazy_high_charts
How to add / remove Ruby on Rails columns
How to display a web page in Java
How to implement a like feature in Rails
How to easily create a pull-down in Rails
(Ruby on Rails6) Creating data in a table
How to make a follow function in Rails
[Ruby on Rails] How to use session method
Change from SQLite3 to PostgreSQL in a new Ruby on Rails project
How to build a Ruby on Rails environment using Docker (for Docker beginners)
How to implement gem "summer note" in wysiwyg editor in Ruby on Rails
How to build a Ruby on Rails development environment with Docker (Rails 6.x)
How to debug the processing in the Ruby on Rails model only on the console
How to build a Ruby on Rails development environment with Docker (Rails 5.x)
How to set the display time to Japan time in Rails
How to implement a like feature in Ajax in Rails
[Ruby on Rails] How to change the column name
How to launch another command in a Ruby program
How to display a browser preview in VS Code
How to run npm install on all projects in Lerna
How to display a graph in Ruby on Rails (LazyHighChart)
[How to insert a video in haml with Rails]
How to write a date comparison search in Rails
[Rails 6] How to set a background image in Rails [CSS]
[Rails] How to load JavaScript in a specific view
(Ruby on Rails6) How to create models and tables
[Rails] How to display an image in the view
How to resolve errors that occur in the "Ruby on Rails" integration test
How to implement image posting function using Active Storage in Ruby on Rails
How to deploy jQuery on Rails
Difficulties in building a Ruby on Rails environment (Windows 10) (SQLite3)
[rails] How to display db information
How to iterate infinitely in Ruby
[Rails] How to write in Japanese
[Ruby On Rails] How to use simple_format to display the entered text with line breaks
[Ruby on Rails] Quickly display the page title in the browser
How to deploy Bootstrap on Rails
Ruby on Rails Japanese-English support i18n
From Ruby on Rails error message display to Japanese localization
How to install Bootstrap in Ruby
Rails on Tiles (how to write)
How to introduce jQuery in Rails 6
How to deploy a Rails application on AWS (article summary)
[Rails] How to display information stored in the database in view
How to install Swiper in Rails
[Ruby On Rails / HTML] How to display the cursor (caret) at an appropriate place when displaying a web page
[Ruby On Rails] How to retrieve and display column information of multiple records linked to a specific id at once
I want to add a browsing function with ruby on rails
[rails] How to display parent information in child view in nested relationships
How to automatically operate a screen created in Java on Windows
(Ruby on Rails6) Creating a database and displaying it in a view