How to use Flot canvas plugin - html5-canvas

I am building a Rails 3.2.11 app with Flot. I want to render my Flot charts in PDFs using Prawn. My Flot charts render in the browser fine. I can create Prawn PDFs fine (though not with chart images yet).
I want to use the Flot canvas plugin to render my axes, etc. on the canvas so they are included when I Ajax the image data to the server using the .getCanvas() and .toDataURL() methods, but I can’t seem to get it to work.
I am using jquery.flot.min.js 0.8.0 and jquery.flot.canvas.min.js (no version indicated). In the Chrome console I see that both are loading.
My Coffeescript looks like this:
temp_plot = $.plot $("#barchart"), [
data: $("#barchart").data("bardata")
bars:
show: true
barWidth: (365/12)*24*60*60*1000*0.8
align: 'center'
],
xaxis:
mode: "time"
timeformat: "%b"
tickSize: [1, "month"]
yaxis:
position: 'right'
labelWidth: '40'
reserveSpace: true
canvas: true
barchart_canvas = temp_plot.getCanvas()
I am able to see the Ajax payload and it is indeed the Flot chart canvas, just without the axes, etc. I appreciate any advice. Thanks!
Just to be extra clear, code as Javascript looks like this:
var barchart_canvas, temp_plot;
temp_plot = $.plot($("#barchart"), [
{
data: $("#barchart").data("bardata"),
bars: {
show: true,
barWidth: (365 / 12) * 24 * 60 * 60 * 1000 * 0.8,
align: 'center'
}
}
], {
xaxis: {
mode: "time",
timeformat: "%b",
tickSize: [1, "month"]
},
yaxis: {
position: 'right',
labelWidth: '40',
reserveSpace: true
},
canvas: true
});
barchart_canvas = temp_plot.getCanvas();
UPDATE - SUCCESS
I updated Flot to 0.8.1 and included the jquery.flot.canvas.js file that came in the 0.8.1 .zip archive. (I was getting some strange rendering behavior using plugins from Flot 0.8.0 with jquery.flot.js 0.8.1, so watch out for that.)
Now my axes render on the canvas. Great! My thanks to the Flot gods!

Your options look okay, assuming (I'm not familiar with CoffeeScript) that last bit is supposed to be missing curly braces.
Your Flot version can't be 1.1, though; the latest is 0.8.1, and jquery.flot.canvas.min.js was introduced in 0.8.0. So you're using either the wrong version, or some Flot-derivative that may not support the canvas plugin.
Note that the canvas plugin currently only affects the axes; the legend is still rendered in HTML. Complete support for rendering everything on canvas will come in 0.9.

Related

Fractional classes with Tailwind CSS inside a HAML file

I am trying to use the w-2/3 class from Tailwind CSS with HAML in a Rails .html.haml file. The forward slash is causing Rails (or HAML) to throw an exception and I don't know how to format it so it's accepted.
Is there a way to use the w-2/3 etc classes or will I have to go back to using .html.erb?
You will have to explicitly add the class, something like
%div{:class => "w-2/3"}
Note you can combine this with the shorthand syntax for classes if you want, e.g.
.foo{:class => "w-2/3"}
The class names used in Tailwind can be overwritten. This might help if you tend to use these classes frequently and don't want to write the extended version (%div{class: 'w-1/2'} or %div(class="w-1/2").
To overwrite the width classes to use _ instead of /, use the following configuration in your tailwind.config.js:
module.exports = {
theme: {
extend: {},
width: (theme) => ({
auto: 'auto',
...theme('spacing'),
'1_2': '50%',
'1_3': '33.333333%',
'2_3': '66.666667%',
'1_4': '25%',
'2_4': '50%',
'3_4': '75%',
'1_5': '20%',
'2_5': '40%',
'3_5': '60%',
'4_5': '80%',
'1_6': '16.666667%',
'2_6': '33.333333%',
'3_6': '50%',
'4_6': '66.666667%',
'5_6': '83.333333%',
'1_12': '8.333333%',
'2_12': '16.666667%',
'3_12': '25%',
'4_12': '33.333333%',
'5_12': '41.666667%',
'6_12': '50%',
'7_12': '58.333333%',
'8_12': '66.666667%',
'9_12': '75%',
'10_12': '83.333333%',
'11_12': '91.666667%',
full: '100%',
screen: '100vw',
}),
}
}
Obviously, this duplicates information from Tailwind and might make framework upgrades more cumbersome.

How to use an npm package (chart.js) once installed?

I'm making a practice Laravel site, and I've installed chart.js via npm install.
This is a dumb questions but now how do I use it (or anything installed via npm) from here?
The files are installed in the node modules folder. Am I supposed to reference them files using tags in my page headers?
If I do:
<script type="text/javascript" src="/node_modules/chart.js/dist/Chart.min.js"></script>
Then it works, I can get a chart to appear. But this doesn't feel right. Isn't npm supposed to take care of all that automatically?
What you're doing in your code snippet is importing the package for global use. NPM is more of a package manager (Node Package Manager) that installs packages and manages dependencies.
So to answer your question, no NPM doesn't usually handle that automatically. What you're doing is one way of accessing/importing the package once its installed. Another way would be https://docs.npmjs.com/using-npm-packages-in-your-projects .
Laravel makes use of webpack aka Laravel Mix where you can copy files as well. For example jQuery:
mix.copy('node_modules/jquery/dist/jquery.min.js', 'public/js/jquery.min.js');
Then on the page where you need to js/css you can make use of stacks where you can
#push('js')
<script src="/js/Chart.bundle.min.js"></script>
#endpush
to include your script. Just run npm run dev on development and npm run prod if you are ready to upload. If you use version control, don't forget to add files copied by Laravel Mix in your public css and js folders.
Chart.js does not seem to be friendly for npm users, in version 3 you need to register chart elements, because of tree shaking. This is an example with chart.js#3.6.1
HTML
<div style="width: 600px;">
<canvas id="myChart"></canvas>
</div>
JS
import { Chart, LinearScale, BarElement, BarController, CategoryScale } from 'chart.js';
Chart.register(LinearScale, BarElement, BarController, CategoryScale);
const ctx = document.getElementById('myChart');
const myChart = new Chart(ctx, {
type: 'bar',
data: {
labels: ['Red', 'Blue', 'Yellow', 'Green', 'Purple', 'Orange'],
datasets: [{
label: '# of Votes',
data: [12, 19, 3, 5, 2, 3],
borderWidth: 1
}]
},
options: {
scales: {
y: {
beginAtZero: true
}
}
}
});
The result

ag-grid does not work properly with angular 1.x

I am trying to use ag-grid for one of my project work and was trying to configure it with webpack & Angular 1.6
i have configured it as follow
Module
var agGrid = require('ag-grid');
agGrid.initialiseAgGridWithAngular1(angular);
module.exports = angular.module('transModule', ['agGrid'])
.component('transComponent', transComponent)
.name;
Controller
var columnDefs = [
{headerName: "Make", field: "make"},
{headerName: "Model", field: "model"},
{headerName: "Price", field: "price"}
];
var rowData = [
{make: "Toyota", model: "Celica", price: 35000},
{make: "Ford", model: "Mondeo", price: 32000},
{make: "Porsche", model: "Boxter", price: 72000}
];
$scope.gridOptions = {
columnDefs: columnDefs,
rowData: rowData
};
html
<div ag-grid="gridOptions" class="ag-fresh" style="height: 100%;"></div>
but when i use i, it displays as follow
then i tried adding the stylesheets as follow
require('ag-grid/dist/styles/ag-grid.css');
require('ag-grid/dist/styles/theme-fresh.css');
yet again it the table want render properly and it will show as follow
is there anything um missing?? I would much appreciate if you could give me some headsup??
I noticed in ag-grid's package.json it was referenced main.js as entry point, and I actually found the whole lib folder content loaded in the Source tab of Chrome DevTools.
This was due to the way I was requiring ag-grid:
var agGrid = require('ag-grid');
// get ag-Grid to create an Angular module and register the ag-Grid directive
agGrid.initialiseAgGridWithAngular1(angular);
var myApp = 'myApp';
module.exports = myApp;
angular
.module(myApp, [
'agGrid'
])
Even if the "get-started" docs don't list a Webpack based solution, they do say to include the dist/ag-grid.js file or one of the minified/noStyle versions, so I changed the first line like this:
var agGrid = require('ag-grid/dist/ag-grid.min.js');

Cytoscape web 2 arbor layout

I've been trying to use the arbor layout. I've tried
layout: {
name:"arbor"
}
and
layout: {
name:"arbor",
liveUpdate: true,
ready: undefined,
maxSimulationTime: 4000,
fit: true,
padding: [ 50, 50, 50, 50 ],
ungrabifyWhileSimulating: true,
repulsion: undefined,
stiffness: undefined,
friction: undefined,
gravity: true,
fps: undefined,
precision: undefined,
nodeMass: undefined,
edgeLength: undefined,
stepSize: 1,
stableEnergy: function( energy ){
var e = energy;
return (e.max <= 7) || (e.mean <= 5);
}
}
In both cases the firebug console reports
arbor is not defined
http://localhost/WS/BioJS/biojs/src/main/resources/dependencies/cytoscape/jquery.cytoscapeweb.layout.arbor.js
Line 76
I'm I missing something?
is there a working example of the arbor layout usage?
You need to include arbor.js in a script tag in your HTML. The file jquery.cytoscapeweb.layout.arbor.js just interfaces arbor with Cytoscape Web. Make sure you use the version of arbor.js bundled in the Cytoscape Web ZIP if you need IE support -- arbor doesn't provide this out of the box.
The reason why arbor.js isn't embedded in jquery.cytoscapeweb.layout.arbor.js is because arbor.js needs to be in its own script tag in order for web workers to work properly. If you concatenate and minify arbor.js with your app's other scripts, arbor's path finding can break or arbor's webworkers could conflict with other webworkers in your app.

vertical scroller html app webos

Hi I have an app that is basically a html page.
I have a problem though as the html page is longer than the viewable screen and the page wont scroll.
Ive added this div:
<div id="scrollerId" style="width:320px; height:100px" x-mojo-element="Scroller">
<div >scrolling content</div>
</div>
but it doesn't do anything.
Please can someone help explain how to add one. or if i need to add anything to my javascript file or anything else ?
source/helloworld.js
enyo.kind({
name: "HelloWorld",
kind: enyo.VFlexBox,
components: [
{kind: "PageHeader", components: [
{content: "Page Header"}
]},
{flex: 1, kind: "Pane", components: [
{flex: 1, kind: "Scroller", components: [
//Insert your components here
]}
]},
{kind: "Toolbar", components: [
]}
]
});
Im a newbie to webos dev so go easy on me.
It might help to know what device(s) you're targeting. You've got a mix of a Mojo app and an Enyo app there, it looks like. Mojo is for the phones. If you're targeting the TouchPad, you should probably switch entirely to Enyo.
For the Mojo scroller to work in webOS you need to enable it as follows:
this.controller.setupWidget("myScroller",
this.attributes = {
},
this.model = {
scrollbars: true,
mode: "free"
});
You can read more about scrollers in Mojo here:
http://webos101.com/Scroller
However, I think you want an Enyo scroller so you get rid of the HTML in your app and use the method described above by XRay Enabler.
It is possible to use JavaScript functions to pull in content from a DIV in your HTML into an Enyo kind. Here's an example using jQuery:
this.$.myContent.setContent($("#someDiv").html());
Keep in mind you'll have to set allowHtml to true to allow HTML content.
First of all welcome to Enyo and webOS! Try to remember that Enyo is your framework that will create the elements of your HTML (the app). You generally do not manipulate it (HTML) directly.
As a simple example, you can create your content after the kind 'HelloWorld' has been rendered:
** your previous code **
{flex: 1, kind: "Scroller", components: [
//Insert your components here
{content: "", name:"myContent"}
]}
]},
{kind: "Toolbar", components: []}
],
create: function() {
this.inherited(arguments);
},
rendered: function() {
this.$.myContent.setContent("I can now add Content!");
}
});
Notice the added content container called myContent in the Scoller. Also, remove the previously created div's in your HTML file.
The content is then added in the rendered function.

Resources