Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add embeddable charts #268

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,20 @@ Use the column name `target` to draw a line for goals. [Example](https://blazer.
SELECT date_trunc('week', created_at), COUNT(*) AS new_users, 100000 AS target FROM users GROUP BY 1
```

### Embedding a Chart outside of the Blazer Rails Engine

Queries can be found using the ActiveRecord API. E.g. `Blazer::Query.find(5)`

Once you have a query, you can render a chart in any view:
```erb
<%= render "blazer/queries/query", query: query %>
```

If your chart has options for variables, you can pass those in using the `params` keyword:
```erb
<%= render "blazer/queries/query", query: query, params: { ... } %>
```

## Dashboards

Create a dashboard with multiple queries. [Example](https://blazer.dokkuapp.com/dashboards/1-dashboard-demo)
Expand Down
36 changes: 1 addition & 35 deletions app/assets/stylesheets/blazer/application.css
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
*= require ./selectize
*= require ./github
*= require ./daterangepicker
*= require ./chart
*= require_self
*/

Expand Down Expand Up @@ -81,21 +82,6 @@ input.search:focus {
background-color: red;
}

.chart {
height: 300px;
text-align: center;
display: flex;
justify-content: center;
flex-direction: column;
}

.chart > .results-container {
height: 300px;
border: solid 1px #ddd;
overflow: scroll;
text-align: left;
}

.dashboard {
font-weight: bold;
}
Expand Down Expand Up @@ -185,22 +171,6 @@ input.search:focus {
display: none;
}

.chart-container {
clear: both;
}

.chart-container h4 {
text-align: center;
}

.chart-container h4 a {
color: inherit;
}

.query-error {
color: red;
}

.small-form {
margin-right: auto;
margin-left: auto;
Expand All @@ -217,10 +187,6 @@ h1, h2, h3, h4, p, hr, .table, .navbar, #header, .alert, .form-group {
margin-bottom: 15px;
}

.double-margin, .chart-container {
margin-bottom: 30px;
}

h1 {
font-size: 24px;
}
Expand Down
35 changes: 35 additions & 0 deletions app/assets/stylesheets/blazer/chart.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@

.chart-container {
clear: both;
}

.chart-container h4 {
text-align: center;
}

.chart-container h4 a {
color: inherit;
}

.query-error {
color: red;
}

.chart-container {
margin-bottom: 30px;
}

.chart {
height: 300px;
text-align: center;
display: flex;
justify-content: center;
flex-direction: column;
}

.chart > .results-container {
height: 300px;
border: solid 1px #ddd;
overflow: scroll;
text-align: left;
}
24 changes: 24 additions & 0 deletions app/views/blazer/queries/_query.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<% unless defined?(@blazer_query_assets_loaded) && @blazer_query_assets_loaded %>
<%= stylesheet_link_tag "blazer/chart" %>
<%= javascript_include_tag "blazer/application" %>
<% @blazer_query_assets_loaded = true %>
<% end %>

<div class="chart-container">
<h4><%= link_to query.friendly_name, blazer.query_path(query, local_assigns[:params] || {}), target: "_blank" %></h4>
<div id="chart-<%= query.id %>" class="chart">
<p class="text-muted">Loading...</p>
</div>
</div>

<script>
<%= blazer_js_var "rootPath", blazer.root_path %>
<%= blazer_js_var "data", {statement: query.statement, query_id: query.id, data_source: query.data_source, only_chart: true} %>

runQuery(data, function (data) {
$("#chart-<%= query.id %>").html(data)
$("#chart-<%= query.id %> table").stupidtable(stupidtableCustomSettings)
}, function (message) {
$("#chart-<%= query.id %>").addClass("query-error").html(message)
});
</script>
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Taken from https://github.com/ankane/blazer/blob/master/app/views/blazer/dashboards/show.html.erb#L35-L50

I chose not to use this fragment there as I also wanted to embed some stylesheet_link_tag and javascript_include_tag and we had a call to rootPath as well.

6 changes: 6 additions & 0 deletions lib/blazer/engine.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,14 @@ class Engine < ::Rails::Engine
isolate_namespace Blazer

initializer "blazer" do |app|
ActiveSupport.on_load :action_view do
include Blazer::BaseHelper
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is required so we can call blazer_js_var.
I originally has this in my app, but I didn't like the feeling of this extra step. I wanted to get to a simple API of just a render call + activerecord call.

I don't think this is a concern, especially as we have prefixed all methods with blazer_.

Can anyone think of trouble this could cause?

end

if defined?(Sprockets) && Sprockets::VERSION >= "4"
app.config.assets.precompile << "blazer/application.js"
app.config.assets.precompile << "blazer/application.css"
app.config.assets.precompile << "blazer/chart.css"
app.config.assets.precompile << "blazer/glyphicons-halflings-regular.eot"
app.config.assets.precompile << "blazer/glyphicons-halflings-regular.svg"
app.config.assets.precompile << "blazer/glyphicons-halflings-regular.ttf"
Expand All @@ -15,6 +20,7 @@ class Engine < ::Rails::Engine
else
# use a proc instead of a string
app.config.assets.precompile << proc { |path| path =~ /\Ablazer\/application\.(js|css)\z/ }
app.config.assets.precompile << proc { |path| path =~ /\Ablazer\/chart\.css\z/ }
app.config.assets.precompile << proc { |path| path =~ /\Ablazer\/.+\.(eot|svg|ttf|woff|woff2)\z/ }
app.config.assets.precompile << proc { |path| path == "blazer/favicon.png" }
end
Expand Down