123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258 |
- //[Dashboard Javascript]
- //Project: Riday Admin - Responsive Admin Template
- //Primary use: Used only for the main dashboard (index.html)
- // ------------------------------
- var Widgetschart = function() {
-
-
- // 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() {
- _sparklinesWidget("#sparklines_color", "line", 30, 200, "basis", 750, 2000, "rgba(255,255,255,0.75)");
-
- }
- }
- }();
- // Initialize module
- // ------------------------------
- // When content loaded
- document.addEventListener('DOMContentLoaded', function() {
- Widgetschart.init();
- });
|