Playing with Mustache and Google Chart

Size
3,289 Kb
Views
30,360

How do I make an playing with mustache and google chart?

Let's try Mustache and Google Chart.. What is a playing with mustache and google chart? How do you make a playing with mustache and google chart? This script and codes were developed by Renaud Tertrais on 12 August 2022, Friday.

Playing with Mustache and Google Chart Previews

Playing with Mustache and Google Chart - Script Codes HTML Codes

<!DOCTYPE html>
<html >
<head> <meta charset="UTF-8"> <title>Playing with Mustache and Google Chart</title> <link rel="stylesheet" href="css/style.css">
</head>
<body> <!-- JS -->
<script src="http://code.jquery.com/jquery-latest.js"></script>
<script src="http://documentcloud.github.io/underscore/underscore-min.js"></script>
<link href="http://getbootstrap.com/dist/css/bootstrap.css" rel="stylesheet" type="text/css" />
<script src="http://getbootstrap.com/dist/js/bootstrap.js"></script> <script src="http://cdnjs.cloudflare.com/ajax/libs/mustache.js/0.7.2/mustache.min.js"></script>
<script type="text/javascript" src="https://www.google.com/jsapi"></script>
<!-- HTML -->
<div id="main">
<form id="add-form"> <input type="text" class="form-control" placeholder="name" name="name" required pattern="\w{3,}"> <div class="btn-group" data-toggle="buttons"> <label class="btn btn-default active"> <input type="radio" name="gender" value="man" checked="checked">Man </label> <label class="btn btn-default"> <input type="radio" name="gender" value="woman"> Woman </label>
</div> <input type="number" class="form-control" style="width:60px" placeholder="25" name="age" required> <input type="submit" class="btn btn-primary" value="add"/> </form> <hr> <div id="view"> <h2>{{friends.length}} friends</h2> <table class="table table-striped table-bordered"> <thead> <tr> <th>Name</th> <th>Gender</th> <th>Age</th> </tr> </thead> <tbody id="friends-list"> <!-- {{#friends}} --> <tr> <td>{{name}}</td> <td class="{{gender}}">{{gender}}</td> <td>{{age}}</td> </tr> <!-- {{/friends}} --> </tbody> </table> </div> <div class="row"> <div class="col-xs-12 col-sm-6"> <h2>Gender :</h2> <div id="chart-gender" class="chart"> <em>Doesn't work in edit mode, go <a href="http://codepen.io/eMaj/full/mvhHb">full page</a> to see the chart.</em> </div> </div> <div class="col-xs-12 col-sm-6"> <h2>Age :</h2> <div id="chart-age" class="chart"> <em>Doesn't work in edit mode, go <a href="http://codepen.io/eMaj/full/mvhHb">full page</a> to see the chart.</em> </div> </div> </div>
</div> <script src='http://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js'></script> <script src="js/index.js"></script>
</body>
</html>

Playing with Mustache and Google Chart - Script Codes CSS Codes

#main{ padding:20px; background-color: #EEF; height: 100%; position:absolute; width: 100%;
}
table{ background-color: #fff;
}
table .man{ color:#36C;
}
table .woman{ color:#C31;
}
form .form-control{ display:inline-block; width:inherit;
}
.chart{ min-height:200px;
}

Playing with Mustache and Google Chart - Script Codes JS Codes

// init data
var data = {};
data.friends = [];
data.friends.push({name:'Paul',gender:'man',age:25});
data.friends.push({name:'John',gender:'man',age:18});
data.friends.push({name:'Lucie',gender:'woman',age:14});
//google chart
google.load("visualization", "1", {packages:["corechart"]});
var charts = {};
charts.gender = {};
charts.age = {};
charts.gender.options = { backgroundColor:'none', animation:{ duration: 1000 }, chartArea:{ left:0, top:0, width:"100%", height:"100%" }
};
charts.age.options = { backgroundColor:'none', animation:{ duration: 1000 }, chartArea:{ left:0, top:0, width:"100%", height:"80%" }
};
// init chart function
function initChart(){ //gender charts.gender.chart = new google.visualization.PieChart($('#chart-gender')[0]); //age charts.age.chart = new google.visualization.ColumnChart($('#chart-age')[0]); // call render render();
}
// init Events function
function initEvents(){ $("#add-form").on("submit",function(e){ e.preventDefault(); data.friends.push({ name:$("[name=name]").val(), gender:$("[name=gender]:checked").val(), age:$("[name=age]").val() }); $("[name=name],[name=age]").val(''); render(); });
}
// init Views function
function initViews(){ $view = $("#view"); $view.data().template = $view.html();
}
// render function
function render(){ // gender chart charts.gender.data = new google.visualization.DataTable(); charts.gender.data.addColumn('string','gender'); _.each(data.friends,function(friend){ charts.gender.data.addRow([friend.gender]); }); charts.gender.chart.draw(charts.gender.data, charts.gender.options); //age chart charts.age.data = new google.visualization.DataTable(); charts.age.data.addColumn('number','age'); charts.age.data.addColumn('number','number'); var j = []; /* for(var i = 0 ; i<75 ; i++){ j.push([i,0]); } _.each(data.friends,function(friend){ if(j[friend.age]) j[friend.age][1]++; else j[friend.age] = [friend.age,1]; });*/ j = _.countBy(data.friends,function(friend){ return friend.age; }); var k = []; _.each(j,function(value,key){ k.push([parseInt(key),parseInt(value)]); }); charts.age.data.addRows(k); charts.age.chart.draw(charts.age.data, charts.age.options); // mustache $view = $("#view"); $view.html(Mustache.to_html($view.data().template,data));
}
// onload
$(function(){ initViews(); initEvents(); google.setOnLoadCallback(initChart);
});
Playing with Mustache and Google Chart - Script Codes
Playing with Mustache and Google Chart - Script Codes
Home Page Home
Developer Renaud Tertrais
Username renaudtertrais
Uploaded August 12, 2022
Rating 3
Size 3,289 Kb
Views 30,360
Do you need developer help for Playing with Mustache and Google Chart?

Find the perfect freelance services for your business! Fiverr's mission is to change how the world works together. Fiverr connects businesses with freelancers offering digital services in 500+ categories. Find Developer!

Renaud Tertrais (renaudtertrais) Script Codes
Create amazing love letters with AI!

Jasper is the AI Content Generator that helps you and your team break through creative blocks to create amazing, original content 10X faster. Discover all the ways the Jasper AI Content Platform can help streamline your creative workflows. Start For Free!