As per documentation of Datatables.net https://datatables.net/examples/advanced_init/length_menu.html
i need to write some setting to data table implementation to my thmyeleaf template,
<script th:inline="javascript">
$(document).ready(function() {
$('#example').DataTable( {
"lengthMenu": [[10, 25, 50, [[${rowTotal}]]], [10, 25, 50, "All"]]
});
});
</script>
But on rendering,it's giving me such error.
org.thymeleaf.exceptions.TemplateProcessingException: Could not parse as expression: "10, 25, 50, -1], [10, 25, 50, 'All'" (template: "customer/customerlist" - line 237, col 21)
And on reading this documentation on thymeleaf,it is being said
When inlining, if the expression between [[...]] is not a valid Standard Expression, it is output without modification, including the double-brackets.
https://github.com/thymeleaf/thymeleaf/issues/22.
How to i solve this problem?
Hmm, I didn't know that was how it works in thymeleaf 2. The easiest change would just be to format it differently. Something like this:
<script th:inline="javascript">
$(document).ready(function() {
$('#example').DataTable({
"lengthMenu": [
[10, 25, 50, -1],
[10, 25, 50, "All"]
]
});
});
</script>
If that doesn't suit you, you could split the datatable definition into it's own javascript block and use th:inline="none" or move it to it's own external javascript file. (Why are you using th:inline="javascript" in this case anyways?).
Related
I have an issue with Typescript, i'm trying to apply a style on a HTMLElement, here is the code :
styleChoice(element:HTMLElement){
console.log(element);
element.style.background="rgba(228, 48, 48, 0.2)";
element.style.borderRadius="40px";
}
test(){
console.log(document.getElementById('test'));
this.styleChoice(document.getElementById('test'));
}
I don't understand why is not working.
You should use the Renderer2 for this to work. It will be a good option. Don't use the document directly in the app, check here for more details.
export class DemoComponent {
constructor(
private element: ElementRef,
private renderer: Renderer2,
){
}
styleChoice(element:HTMLElement){
console.log(element);
//element.style.background="rgba(228, 48, 48, 0.2)";
renderer.setStyle(el, 'background', 'red');
renderer.setStyle(el, 'borderRadius', '40px');
// element.style.borderRadius="40px";
}
test(){
console.log(document.getElementById('test'));
this.styleChoice(document.getElementById('test'));
}
}
You can check here for renderer2 documentation.
This is not an Angular way, an Angular way is to use a variable and [ngStyle]
e.g.
//in .ts
style={
"background":"rgba(228, 48, 48, 0.2)",
"borderRadius":"40px"
}
//in .html
<some-element [ngStyle]="style">...</some-element>
//or directly
<some-element [ngStyle]="{'background':'rgba(228, 48, 48, 0.2)',
'borderRadius':'40px'}">
...
</some-element>
You can change the style according a variable, e.g.
variable=true;
<some-element [ngStyle]="variable?style:null">...</some-element>
If only want to change one property you can use [style.property]
//in .ts
backgroundColor="rgba(228, 48, 48, 0.2)"
//in .html
<some-element [style.background]="backgroundColor">...</some-element>
I am trying to display a simple chart following the guide here:
http://jtblin.github.io/angular-chart.js/
I am using sails.js, angular, bootstrap and angular-chart.js
When I load the app, I get a blank image where a chart should be. I see this error in chrome dev tools:
TypeError: (intermediate value)[type] is not a function
at createChart (angular-chart.js:175)
at Object.fn (angular-chart.js:118)
at Scope.$get.Scope.$digest (angular.js:14308)
at Scope.$get.Scope.$apply (angular.js:14571)
at bootstrapApply (angular.js:1455)
at Object.invoke (angular.js:4203)
at doBootstrap (angular.js:1453)
at bootstrap (angular.js:1473)
at angularInit (angular.js:1367)
at angular.js:26304
Please forgive me but I don't know why that error is popping.
I have tried endless amounts of variations of the code below and feel this version is the closest I have gotten to a working sample.
View:
<head>
<link rel="stylesheet" href="/styles/angular-chart.css">
</head>
<body ng-app="DashboardModule" ng-controller="BarCtrl">
<canvas id="bar" class="chart chart-bar" data="data" labels="labels"></canvas>
<script src="/js/dependencies/sails.io.js"></script>
<script src="/js/dependencies/angular.js"></script>
<script src="/js/dependencies/Chart.Core.js"></script>
<script src="/js/dependencies/angular-chart.js"></script>
<script src="/js/private/dashboard/DashboardModule.js"></script>
<script src="/js/private/dashboard/DashboardController.js"></script>
</body>
Module:
angular.module('DashboardModule', ['chart.js']);
Controller:
angular.module('DashboardModule').controller("BarCtrl", function ($scope) {
$scope.labels = ['2006', '2007', '2008', '2009', '2010', '2011', '2012'];
$scope.series = ['Series A', 'Series B'];
$scope.data = [
[65, 59, 80, 81, 56, 55, 40],
[28, 48, 40, 19, 86, 27, 90]
];
});
I think the missing part was adding these two dependencies perhaps:
-- chart.js
-- angular-chart.js
working version is here: https://jsfiddle.net/cwLja39j/
FYI: for jsfiddle, github files can be added as "External Resources" by running them through rawgit.com first.
Markup:
<div ng-app="DashboardModule" ng-controller="BarCtrl">
<canvas id="bar" class="chart chart-bar" data="data" labels="labels"></canvas>
</div>
JS :
angular.module("DashboardModule", ["chart.js"]).controller("BarCtrl", function ($scope) {
$scope.labels = ['2006', '2007', '2008', '2009', '2010', '2011', '2012'];
$scope.series = ['Series A', 'Series B'];
$scope.data = [
[65, 59, 80, 81, 56, 55, 40],
[28, 48, 40, 19, 86, 27, 90]
];
});
(used Chrome, no errors seen in console)
It may be version issue ,Use Chart.js Version: 1.1.1
My question is I want to integrate a d3.js visualization to my markdown rather than a link pointing to the visualization on external website. Is there a way to achieve that?
To accomplish adding nonlocal javascript such as d3.v3.min.js to our Rmd, there are a couple ways to do it. If you are looking to include local copy of d3, it is much easier.
This is my favorite way. If for some reason, you would like to see the others, I will be happy to show them. Note: I am still experimenting.
---
title: "rmarkdown example with external js"
output:
html_document:
self_contained: false
keep_md: true
includes:
in_header: "header_include_d3.html"
---
Let's create a very basic d3 graph using data from R. since the graph is d3, we will need the d3.js file for the graph to render.
```{r results='asis'}
cat('
<script>
d3.select("body").append("p").text("d3 made me")
</script>
')
```
<script>
// from https://www.dashingd3js.com/svg-paths-and-d3js
//The data for our line
var lineData = [ { "x": 1, "y": 5}, { "x": 20, "y": 20},
{ "x": 40, "y": 10}, { "x": 60, "y": 40},
{ "x": 80, "y": 5}, { "x": 100, "y": 60}];
//This is the accessor function we talked about above
var lineFunction = d3.svg.line()
.x(function(d) { return d.x; })
.y(function(d) { return d.y; })
.interpolate("linear");
//The SVG Container
var svgContainer = d3.select("body").append("svg")
.attr("width", 200)
.attr("height", 200);
//The line SVG Path we draw
var lineGraph = svgContainer.append("path")
.attr("d", lineFunction(lineData))
.attr("stroke", "blue")
.attr("stroke-width", 2)
.attr("fill", "none");
</script>
then in the same directory as this .Rmd file, save this
<script src = "http://d3js.org/d3.v3.min.js"></script>
into a file I called header_include_d3.html or whatever name you would like. If you change the name, just be sure to change the reference in the includes in the yaml of your Rmd.
As I said before, this is much easier if you have d3.js locally that you would like to use.
Also, <script src='...'></script> inside the body will work if you are not particular about have your js in the header. In that case, just include it anywhere in the Rmd.
You have now the R2D3 package that allows that!
Rmardown is one way of including D3 visualisation in R
https://rstudio.github.io/r2d3/articles/publishing.html#r-markdown
I have created a table in Google Visualization which uses the following code:
<html>
<head>
<script type='text/javascript' src='https://www.google.com/jsapi'></script>
<script type='text/javascript'>
google.load('visualization', '1', {packages:['table']});
google.setOnLoadCallback(drawTable);
function drawTable() {
var cssClassNames = {
'headerRow': 'headerRow',
'tableRow': 'tableRow'};
var options = {'allowHtml': true, 'cssClassNames': cssClassNames, 'alternatingRowStyle': true};
var data = new google.visualization.DataTable();
data.addColumn('string', 'Username');
data.addColumn('number', 'Won');
data.addColumn('number', 'Lost');
data.addColumn('number', 'Win/Loss Ratio');
data.addColumn('number', 'Goals For');
data.addColumn('number', 'Goals Against');
data.addColumn('number', 'Score');
data.addRows([
['Mike', 22, 13, 0.63, 65, 30, 600],
['Andy', 25, 10, 0.71, 60, 18, 630],
['Steve', 5, 20, 0.20, 10, 50, 475],
['Chris', 40, 10, 0.80, 120, 20, 670],
['Jake', 15, 15, 0.50, 70, 50, 525],
]);
var table = new google.visualization.Table(document.getElementById('table_div'));
table.draw(data, {showRowNumber: true});
}
</script>
</head>
<body>
<div id='table_div'></div>
</body>
</html>
I wish to be able to change the formatting of the header row and table rows using CSS, I can't figure out how to do this though. I have read through the Configuration Options but being a relative new comer to coding this hasn't helped and need it clearly explained step by step.
Specifically what do I add to the above code to tell the chart to use my custom CSS. And what would I put in my main? CSS stylesheet. Not sure whether it would be (for the header) #headerRow { } or .headerRow { }.
For your information this is being inserted through Wordpress via a custom field if that makes any difference.
If I haven't made myself clear enough in the question, please follow up. Cheers.
UPDATE:
#asgallant - still not working when changing to headerCell and tableCell.
My current code is:
HTML/Javascript:
<html>
<head>
<script type='text/javascript' src='https://www.google.com/jsapi'></script>
<script type='text/javascript'>
google.load('visualization', '1', {packages:['table']});
google.setOnLoadCallback(drawTable);
function drawTable() {
var cssClassNames = {
headerCell: 'headerCell',
tableCell: 'tableCell'};
var options = {'allowHtml': true, 'cssClassNames': cssClassNames, 'alternatingRowStyle': true};
var data = new google.visualization.DataTable();
data.addColumn('string', 'Username',{style: 'background-color:red;'});
data.addColumn('number', 'Won');
data.addColumn('number', 'Lost');
data.addColumn('number', 'Win/Loss Ratio');
data.addColumn('number', 'Goals For');
data.addColumn('number', 'Goals Against');
data.addColumn('number', 'Score');
data.addRows([
['Mike', 22, 13, 0.63, 65, 30, 600],
['Andy', 25, 10, 0.71, 60, 18, 630],
['Steve', 5, 20, 0.20, 10, 50, 475],
['Chris', 40, 10, 0.80, 120, 20, 670],
['Jake', 15, 15, 0.50, 70, 50, 525],
]);
var table = new google.visualization.Table(document.getElementById('table_div'));
table.draw(data, {showRowNumber: true});
}
</script>
</head>
<body>
<div id='table_div'></div>
</body>
</html>
CSS:
#table_div table {
color: #000;
margin-top: 10px;
}
.headerRow {
color: #000;
}
The table_div allows me to edit the table as a whole, so the current CSS code has an effect but when I add background-color: #ff0000; for example, it has no effect. Same with .headerRow . The background is being taken from https://ajax.googleapis.com/ajax/static/modules/gviz/1.0/table/table.css and doesn't want to be overrode.
Any idea why this could be?
If absolutely necessary I'd be happy to disable the Google CSS entirely and do it purely off my own CSS sheet.
Once you create the CSS, I found this to be the key to getting it to work:
var cssClasses = {headerRow: 'tblHeaderClass',hoverTableRow: 'tblHighlightClass'};
var options = {showRowNumber: false, allowHTML: true, 'cssClassNames':cssClasses};
table.draw(data,options);
force the directive cssClassNames to string with "ticks" and be sure to use the object you declared to define whe CSS classes to use for the table element names exposed.
Take a quick peak at https://developers.google.com/chart/interactive/docs/gallery/table for more information, but you should be able to link in your own style sheet in the head tag, then reference the class in JS. The only thing I'm not certain is whether you can create custom CSS files with Wordpress, but this would be the general idea:
HTML:
<link href="css/myCustomCss.css" rel="stylesheet">
Javascript:
var cssClassNames = {headerRow: 'myAwesomeClass'};
CSS:
.myAwesomeClass {
font-size: 24px;
}
I'm also new with Google Charts, but have explored so many of the options that I have a pretty good understanding to how to stylize your table.
See this link for proper Google example:
https://developers.google.com/chart/interactive/docs/examples?hl=fr
Ultimately, what you do is assign a class name to the table row. After you can assign any CSS code you want. There is the alternative where you can use inline CSS, but I do not recommend it.
Inline style is as follows
[{v: 'cell string', p: {'style': 'font-weight: bold;'}}],
You can also add your formatting, or anything else.
If you need further clarification, do not hesitate to reply to my thread.
Have you tried !important at the end of your CSS property?
Example CSS:
.headerRow {
color: #000!important;
}
The !important property will always be applied no matter where that rule appears in the CSS.
Has anyone been able to incorporate Charts and graphs into Sencha Touch?
If so, an example would be appreciated.
Thanks
Sencha Touch Charts has just been released
I was under the impression that Raphael (http://raphaeljs.com/) would eventually be incorporated into Sencha Touch for its' graphing component (http://g.raphaeljs.com/). Until then, you can pretty easily just include the extra Raphael .js files and make graphs that way. Something like:
<script src="sencha-touch-1.0/sencha-touch-debug.js" type="text/javascript" charset="utf-8"></script>
<!-- Raphael JS -->
<script src="raphael/raphael-min.js" type="text/javascript" charset="utf-8"></script>
<script src="raphael/g.raphael-min.js" type="text/javascript" charset="utf-8"></script>
<script src="raphael/g.pie-min.js" type="text/javascript" charset="utf-8"></script>
<script src="raphael/g.line-min.js" type="text/javascript" charset="utf-8"></script>
<script src="raphael/g.bar-min.js" type="text/javascript" charset="utf-8"></script>
<script type="text/javascript" charset="utf-8">
Ext.setup({
onReady: function()
{
// Set up main panel!
var tabPanel = new Ext.Panel
({
fullscreen: true,
html: '<div id="graph"></div>'
});
// Try to draw a graph!
var r = Raphael('graph');
r.g.txtattr.font = '12px Helvetica, Arial, sans-serif';
r.g.text(150, 250, "Demo chart with title");
r.g.linechart(10, 10, 300, 220, [[1, 2, 3, 4, 5, 6, 7],[3.5, 4.5, 5.5, 6.5, 7, 8]], [[12, 32, 23, 15, 17, 27, 22], [10, 20, 30, 25, 15, 28]], {nostroke: false, axis: "0 0 1 1", symbol: "o", smooth: true});
}
});
</script>
Take a look at http://code.google.com/p/oppo-touching/. Someone already moved charting to Snecha Touch. Also there is news that next version of Sencha Touch will include charting.
they now have some examples. they should help http://dev.sencha.com/deploy/touch-charts-beta/examples/
Here's the code for a sample chart in Sencha Touch
var SampleLineChart = new Ext.chart.Chart({
fullscreen : true,
iconCls: 'line',
cls: 'chartpanel',
theme: 'Energy',
interactions: [ 'reset',
{
type: 'panzoom',
axes: {
right: {}
}
},
{
type: 'iteminfo',
gesture: 'tap',
listeners: {
show: function(interaction, item, panel) {
// Ext.dispatch({
// controller : 'Users',
// action : 'popupInfoAbtChart',
// data : {item:item, panel:panel}
// });
}
}
}],
animate: false,
store: EnergyApp.stores.ChartStore, //choose for consumption
axes: [{
type: 'Numeric',
position: 'right',
minimum: 0,
majorTickSteps : 10,
minorTickSteps : 5,
fields: ['generatedpv', 'buildcons','excessPV'],
title: 'Y-axis title'
},
{
type: 'Category',
position: 'bottom',
fields: ['day'],
title: 'X-axis title',
label: {
rotate: {
degrees: 45
}
}
}],
legend: {
position: Ext.is.Phone ? 'left' : 'top'
},
series: [{
type: 'line',
highlight: false,
showMarkers: true,
fill: false,
smooth: true,
axis: 'right',
xField: 'day',
yField: 'generatedpv',
title: 'Field 1
},
{
type: 'line',
highlight: false,
showMarkers: true,
fill: false,
smooth: true,
axis: 'right',
xField: 'day',
yField: 'buildcons',
title: 'Field 2
}],
listeners: {
afterrender: function(me) {
me.on('beforerefresh', function() {
if (me.ownerCt.getActiveItem().id !== me.id) {
return false;
}
}, me);
}
}
});
For more code examples take a look at the EnergyApp Example in the Sencha-Touch-Charts example folder. Its been depicted quite well
Here is a link to a Sencha forum with some examples of how to incorporate a chart into an existing Sencha Touch 2.0 application:
http://www.sencha.com/forum/showthread.php?190053-How-to-Integrate-Touch-2-Charts-into-an-existing-Sencha-Touch-2.0-Final-application.
The package with the chart api for Sencha exists ( http://dev.sencha.com/deploy/touch-charts-beta/examples/) but appears very hard to integrate in the sencha touch solution (files dependency, function not defined in some version of the sencha touch package).
The solution I found is to install the trial version of Sencha Architect which already include the graph api, create a mobile project (touch project) and package it. Then I have a whole package with the right dependencies and I can reuse it without depending on Sencha Architect.