Chart.js

Chart.js integration, Bootstrap theming aware. In order to limit the scope of the matestack-ui-bootstrap gem, Chart.js components are not part of this gem. The below shown example should enable you to easily integrate Chart.js (or any other chart library) according to your needs!

Ruby component

app/matestack/components/chart_js.rb

class Components::ChartJs < Matestack::Ui::VueJsComponent
  vue_name "chart-js-component"

  requires :type
  requires :datasets
  optional :labels
  optional :height
  optional :width
  optional :display_legend
  optional :display_x_axes
  optional :display_y_axes
  optional :cutout_percentage
  optional class: { as: :bs_class }

  # injected into vue.js components
  def vue_props
    {}.tap do |props|
      props[:type] = context.type
      props[:datasets] = context.datasets
      props[:labels] = context.labels
      props[:display_legend] = !display_legend.nil? ? context.display_legend : false
      props[:display_x_axes] = !display_x_axes.nil? ? context.display_x_axes : true
      props[:display_y_axes] = !display_y_axes.nil? ? context.display_y_axes : true
      props[:display_y_axes] = !display_y_axes.nil? ? context.display_y_axes : true
      props[:cutout_percentage] = !context.cutout_percentage.nil? ? context.cutout_percentage : 70
    end
  end

  def response
    div class: "chart-container #{context.bs_class}",  style: "width: 100%; height: 100%;" do
      plain "<canvas ref='chart'></canvas>".html_safe
    end
  end

end

Vue.js component

If not already installed, do:

Do not forget to import the following file into you application pack

app/matestack/components/chart_js.js

Usage

Example 1: Bar chart

Example 2: Doughnut chart

Example 3: Line chart

Example 4: Pie chart

Last updated