//[Dashboard Javascript]
//Project: Riday Admin - Responsive Admin Template
//Primary use: Used only for the main dashboard (index.html)
// ------------------------------
var Widgetschart = function() {
// Simple bar charts
var _barChartWidget = function(element, barQty, height, animate, easing, duration, delay, color, tooltip) {
if (typeof d3 == 'undefined') {
console.warn('Warning - d3.min.js is not loaded.');
return;
}
// Initialize chart only if element exsists in the DOM
if(element) {
// Basic setup
// ------------------------------
// Add data set
var bardata = [];
for (var i=0; i < barQty; i++) {
bardata.push(Math.round(Math.random() * 10) + 10);
}
// Main variables
var d3Container = d3.select(element),
width = d3Container.node().getBoundingClientRect().width;
// Construct scales
// ------------------------------
// Horizontal
var x = d3.scale.ordinal()
.rangeBands([0, width], 0.3);
// Vertical
var y = d3.scale.linear()
.range([0, height]);
// Set input domains
// ------------------------------
// Horizontal
x.domain(d3.range(0, bardata.length));
// Vertical
y.domain([0, d3.max(bardata)]);
// Create chart
// ------------------------------
// Add svg element
var container = d3Container.append('svg');
// Add SVG group
var svg = container
.attr('width', width)
.attr('height', height)
.append('g');
//
// Append chart elements
//
// Bars
var bars = svg.selectAll('rect')
.data(bardata)
.enter()
.append('rect')
.attr('class', 'd3-random-bars')
.attr('width', x.rangeBand())
.attr('x', function(d,i) {
return x(i);
})
.style('fill', color);
// Tooltip
// ------------------------------
// Initiate
var tip = d3.tip()
.attr('class', 'd3-tip')
.offset([-10, 0]);
// Show and hide
if(tooltip == "hours" || tooltip == "goal" || tooltip == "members") {
bars.call(tip)
.on('mouseover', tip.show)
.on('mouseout', tip.hide);
}
// Daily meetings tooltip content
if(tooltip == "hours") {
tip.html(function (d, i) {
return "
" +
"
" + d + "
" +
"
meetings" +
"
" + i + ":00" + "
" +
"
";
});
}
// Statements tooltip content
if(tooltip == "goal") {
tip.html(function (d, i) {
return "" +
"
" + d + "
" +
"
statements" +
"
" + i + ":00" + "
" +
"
";
});
}
// Online members tooltip content
if(tooltip == "members") {
tip.html(function (d, i) {
return "" +
"
" + d + "0" + "
" +
"
members" +
"
" + i + ":00" + "
" +
"
";
});
}
// Bar loading animation
// ------------------------------
// Choose between animated or static
if(animate) {
withAnimation();
} else {
withoutAnimation();
}
// Animate on load
function withAnimation() {
bars
.attr('height', 0)
.attr('y', height)
.transition()
.attr('height', function(d) {
return y(d);
})
.attr('y', function(d) {
return height - y(d);
})
.delay(function(d, i) {
return i * delay;
})
.duration(duration)
.ease(easing);
}
// Load without animateion
function withoutAnimation() {
bars
.attr('height', function(d) {
return y(d);
})
.attr('y', function(d) {
return height - y(d);
});
}
// Resize chart
// ------------------------------
// Call function on window resize
$(window).on('resize', barsResize);
// Call function on sidebar width change
$(document).on('click', '.sidebar-control', barsResize);
// Resize function
//
// Since D3 doesn't support SVG resize by default,
// we need to manually specify parts of the graph that need to
// be updated on window resize
function barsResize() {
// Layout variables
width = d3Container.node().getBoundingClientRect().width;
// Layout
// -------------------------
// Main svg width
container.attr("width", width);
// Width of appended group
svg.attr("width", width);
// Horizontal range
x.rangeBands([0, width], 0.3);
// Chart elements
// -------------------------
// Bars
svg.selectAll('.d3-random-bars')
.attr('width', x.rangeBand())
.attr('x', function(d,i) {
return x(i);
});
}
}
};
// Simple sparklines
var _sparklinesWidget = function(element, chartType, qty, chartHeight, interpolation, duration, interval, color) {
if (typeof d3 == 'undefined') {
console.warn('Warning - d3.min.js is not loaded.');
return;
}
// Initialize chart only if element exsists in the DOM
if(element) {
// Basic setup
// ------------------------------
// Define main variables
var d3Container = d3.select(element),
margin = {top: 0, right: 0, bottom: 0, left: 0},
width = d3Container.node().getBoundingClientRect().width - margin.left - margin.right,
height = chartHeight - margin.top - margin.bottom;
// Generate random data (for demo only)
var data = [];
for (var i=0; i < qty; i++) {
data.push(Math.floor(Math.random() * qty) + 5);
}
// Construct scales
// ------------------------------
// Horizontal
var x = d3.scale.linear().range([0, width]);
// Vertical
var y = d3.scale.linear().range([height - 5, 5]);
// Set input domains
// ------------------------------
// Horizontal
x.domain([1, qty - 3]);
// Vertical
y.domain([0, qty]);
// Construct chart layout
// ------------------------------
// Line
var line = d3.svg.line()
.interpolate(interpolation)
.x(function(d, i) { return x(i); })
.y(function(d, i) { return y(d); });
// Area
var area = d3.svg.area()
.interpolate(interpolation)
.x(function(d,i) {
return x(i);
})
.y0(height)
.y1(function(d) {
return y(d);
});
// Create SVG
// ------------------------------
// Container
var container = d3Container.append('svg');
// SVG element
var svg = container
.attr('width', width + margin.left + margin.right)
.attr('height', height + margin.top + margin.bottom)
.append("g")
.attr("transform", "translate(" + margin.left + "," + margin.top + ")");
// Add mask for animation
// ------------------------------
// Add clip path
var clip = svg.append("defs")
.append("clipPath")
.attr('id', function(d, i) { return "load-clip-" + element.substring(1); });
// Add clip shape
var clips = clip.append("rect")
.attr('class', 'load-clip')
.attr("width", 0)
.attr("height", height);
// Animate mask
clips
.transition()
.duration(1000)
.ease('linear')
.attr("width", width);
//
// Append chart elements
//
// Main path
var path = svg.append("g")
.attr("clip-path", function(d, i) { return "url(#load-clip-" + element.substring(1) + ")"; })
.append("path")
.datum(data)
.attr("transform", "translate(" + x(0) + ",0)");
// Add path based on chart type
if(chartType == "area") {
path.attr("d", area).attr('class', 'd3-area').style("fill", color); // area
}
else {
path.attr("d", line).attr("class", "d3-line d3-line-medium").style('stroke', color); // line
}
// Animate path
path
.style('opacity', 0)
.transition()
.duration(500)
.style('opacity', 1);
// Set update interval. For demo only
// ------------------------------
setInterval(function() {
// push a new data point onto the back
data.push(Math.floor(Math.random() * qty) + 5);
// pop the old data point off the front
data.shift();
update();
}, interval);
// Update random data. For demo only
// ------------------------------
function update() {
// Redraw the path and slide it to the left
path
.attr("transform", null)
.transition()
.duration(duration)
.ease("linear")
.attr("transform", "translate(" + x(0) + ",0)");
// Update path type
if(chartType == "area") {
path.attr("d", area).attr('class', 'd3-area').style("fill", color);
}
else {
path.attr("d", line).attr("class", "d3-line d3-line-medium").style('stroke', color);
}
}
// Resize chart
// ------------------------------
// Call function on window resize
$(window).on('resize', resizeSparklines);
// Call function on sidebar width change
$(document).on('click', '.sidebar-control', resizeSparklines);
// Resize function
//
// Since D3 doesn't support SVG resize by default,
// we need to manually specify parts of the graph that need to
// be updated on window resize
function resizeSparklines() {
// Layout variables
width = d3Container.node().getBoundingClientRect().width - margin.left - margin.right;
// Layout
// -------------------------
// Main svg width
container.attr("width", width + margin.left + margin.right);
// Width of appended group
svg.attr("width", width + margin.left + margin.right);
// Horizontal range
x.range([0, width]);
// Chart elements
// -------------------------
// Clip mask
clips.attr("width", width);
// Line
svg.select(".d3-line").attr("d", line);
// Area
svg.select(".d3-area").attr("d", area);
}
}
};
//
// Return objects assigned to module
//
return {
init: function() {
_barChartWidget("#chart_bar_basic", 24, 50, true, "elastic", 1200, 50, "#3e8ef7", "members");
_sparklinesWidget("#sparklines_basic", "area", 30, 50, "basis", 750, 2000, "#0bb2d4");
}
}
}();
// Initialize module
// ------------------------------
// When content loaded
document.addEventListener('DOMContentLoaded', function() {
Widgetschart.init();
});
// donut chart
$('.donut').peity('donut');
// ------------------------------
// Basic bar chart
// ------------------------------
// based on prepared DOM, initialize echarts instance
var myChart = echarts.init(document.getElementById('basic-bar'));
// specify chart configuration item and data
var option = {
// Setup grid
grid: {
left: '1%',
right: '2%',
bottom: '3%',
containLabel: true
},
// Add Tooltip
tooltip : {
trigger: 'axis'
},
legend: {
data:['Site A','Site B']
},
toolbox: {
show : true,
feature : {
magicType : {show: true, type: ['line', 'bar']},
restore : {show: true},
saveAsImage : {show: true}
}
},
color: ["#689f38", "#389f99"],
calculable : true,
xAxis : [
{
type : 'category',
data : ['Jan','Feb','Mar','Apr','May','Jun','July','Aug','Sept','Oct','Nov','Dec']
}
],
yAxis : [
{
type : 'value'
}
],
series : [
{
name:'Site A',
type:'bar',
data:[7.2, 5.3, 6.1, 32.1, 23.1, 89.2, 158.4, 178.1, 36.4, 22.7, 7.1, 9.4],
markPoint : {
data : [
{type : 'max', name: 'Max'},
{type : 'min', name: 'Min'}
]
},
markLine : {
data : [
{type : 'average', name: 'Average'}
]
}
},
{
name:'Site B',
type:'bar',
data:[19.4, 7.9, 8.9, 27.9, 24.8, 88.1, 167.8, 197.5, 47.1, 16.7, 7.1, 1.5],
markPoint : {
data : [
{name : 'The highest year', value : 182.2, xAxis: 7, yAxis: 183, symbolSize:18},
{name : 'Year minimum', value : 2.3, xAxis: 11, yAxis: 3}
]
},
markLine : {
data : [
{type : 'average', name : 'Average'}
]
}
}
]
};
// use configuration item and data specified to show chart
myChart.setOption(option);