Skip to content

Instantly share code, notes, and snippets.

@bmiles
Forked from davefp/LICENSE.md
Last active August 29, 2015 14:13
Show Gist options
  • Save bmiles/81f0a3e00ddb1f371fbd to your computer and use it in GitHub Desktop.
Save bmiles/81f0a3e00ddb1f371fbd to your computer and use it in GitHub Desktop.
Chart Plotting with Chartist in Dashing

##Preview

Description

Simple Dashing widget (and associated job) to display charts using Chartist.js.

##Dependencies

Chartist.js is required.

Drop the Chartist-js .js and .css files (get them from the Chartist-js) into the dashing asset directories.

##Usage

To use this widget, copy charts.html, charts.coffee, and charts.scss into the /widgets/charts directory. Put the charts.rb file in your /jobs folder.

To include the widget in a dashboard, add the following snippet to the dashboard layout file:

    <li data-row="1" data-col="2" data-sizex="4" data-sizey="2">
      <div data-id="chart" class="chartist" data-chart-type="Line" data-view="Charts" data-title="Line"></div>
    </li>

##Settings

Set the data-chart-type tag to either "Line", "Bar" or "Pie". And the data-view tag to "Charts".

class Dashing.Charts extends Dashing.Widget
ready: ->
container = $(@node).parent()
# Gross hacks. Let's fix this.
width = (Dashing.widget_base_dimensions[0] * container.data("sizex")) + Dashing.widget_margins[0] * 2 * (container.data("sizex") - 1)
height = (Dashing.widget_base_dimensions[1] * container.data("sizey"))
chartType = @node.dataset.chartType
data=@get('data')
options= {
width: width,
height: height
}
console.log($(@node).children("div.ct-chart")[0])
switch chartType
when "Bar"
@chart = new Chartist.Bar($(@node).children("div.ct-chart")[0], data, options)
when "Line"
@chart = new Chartist.Line($(@node).children("div.ct-chart")[0], data, options)
when "Pie"
@chart = new Chartist.Pie($(@node).children("div.ct-chart")[0], data, options)
else
@chart = new Chartist.Line($(@node).children("div.ct-chart")[0], data, options)
onData: (data) ->
if @chart
@chart.update(data.data)
<h1 class="title" data-bind="title"></h1>
<div class="ct-chart"></div>
<p class="more-info" data-bind="moreinfo"></p>
# Populate the graph with some random points
points = []
(1..5).each do |i|
points << rand(50)
end
last_points = points.last
## Job sends data object.
# Format of data object is.
# data: {
# labels: ["foo","bar", "baz", "bat", "nyan"],
# series: [
# [1,2,3,4,5],
# points.collect { |n| n * 2 }
# ]
# }
SCHEDULER.every '2s' do
points.shift
last_points += 1
points << rand(50)
poinst2 = points * 2
send_event('chart', {
data: {
labels: ["foo","bar", "baz", "bat", "nyan"],
series: [
[1,2,3,4,5],
points.collect { |n| n * 2 }
]
}
})
end
// ----------------------------------------------------------------------------
// Sass declarations
// ----------------------------------------------------------------------------
$background-color: #EADBC4;
$title-color: rgba(255, 255, 255, 0.7);
$moreinfo-color: rgba(255, 255, 255, 0.3);
$tick-color: rgba(0, 0, 0, 0.4);
// ----------------------------------------------------------------------------
// Widget-graph styles
// ----------------------------------------------------------------------------
.widget-charts {
background-color: $background-color;
position: relative;
svg {
position: absolute;
left: 0px;
top: 0px;
}
.title, .value {
position: relative;
z-index: 99;
}
.title {
color: $title-color;
}
.more-info {
color: $moreinfo-color;
font-weight: 600;
font-size: 20px;
margin-top: 0;
}
}

The MIT License (MIT)

Copyright (c) 2015 Ben Miles

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment