DDS Charts

In this example below you will see how to do a DDS Charts with some HTML / CSS and Javascript

Thumbnail
This awesome code was written by TVR, you can see more from this user in the personal repository.
You can find the original code on Codepen.io
Copyright TVR ©
  • HTML
  • CSS
  • JavaScript
    <div id="lineChart"></div>
<div id="pieChart"></div>
<div id='trendTooltip'></div>


/*Downloaded from https://www.codeseek.co/TVR/dds-charts-vWeKwm */
    body {
  font-family: verdana;
}
#lineChart {
   display: inline-block;
   height: 200px;
   width: 800px;
}
#pieChart {
   display: inline-block;
   height: 200px;
   width: 200px;
}

#trendTooltip {
    position: absolute;
    display: none;
    padding: 5px 7px;
    color: white;
    text-align: center;
}
.trendValue {
    font-size: 16px;
}
.trendLabel {
    font-size: 8px;
}


/*Downloaded from https://www.codeseek.co/TVR/dds-charts-vWeKwm */
    var lineData = [{
			label: "Purchases",
         color: '#8CA14D',
			data: [[1990, 10000], [1991, 11300], [1992, 9900], [1993, 9600], [1994, 9500], [1995, 9500], [1996, 9900], [1997, 9300], [1998, 9200], [1999, 9200], [2000, 9500], [2001, 9600], [2002, 9300], ['Q4', 9400], ['Q4', 9790]]
		}, {
			label: "Dispensations",
         color: '#3E8FAF',
			data: [[1990, 9700], [1991, 12100], [1992, 10300], [1993, 11300], [1994, 11700], [1995, 10600], [1996, 12800], [1997, 10800], [1998, 10300], [1999, 9400], [2000, 8700], [2001, 9000], [2002, 8900], ['Q4', 10100], ['Q4', 9800]]
}];

$.plot('#lineChart', lineData, {
   series: {
      lines: {
         show: true
      },
      points: {
         show: true
      }
   },
   legend: {
      noColumns: 2
   },
   grid: { borderWidth: 0, labelMargin: 20, hoverable: true }
});

var pieData = [
   { label: "Purchases", color: '#8CA14D', data: 37455 },
   { label: "Dispensations", color: '#3E8FAF', data: 994500 }
];

$.plot('#pieChart', pieData, {
    series: {
        pie: {
            innerRadius: 1/2,
            show: true,
            label: {
               show: true,
               formatter: function (label, series) {
		            return '<div style="text-align:center; color:' + series.color + '">' + abbreviateNumber(series.data[0][1]) + '<div style="font-size:8px">' + series.label + '</div></div>';
	            },
               radius: 1/4
            }
         }
      },
      legend: {
         show: false
      }
});
   
$('#lineChart').bind("plothover", function (event, pos, item) {
   if (item) {
      var data = item.series.data[item.dataIndex],
          tooltip = $('#trendTooltip'),
          label = '<div class="trendLabel">' + item.series.label + '</div>',
          value = '<div class="trendValue">' + abbreviateNumber(data[1]) + '</div>';
      console.log(data);
      $("#trendTooltip").html(value + label)
         .css({top: item.pageY-(tooltip.height()+20), left: item.pageX-(tooltip.width()/2)-5, 'background-color': item.series.color })
         .show();
   } else {
      $("#trendTooltip").hide();
   }
});

function abbreviateNumber(num, fixed) {
  if (num === null) { return null; } // terminate early
  if (num === 0) { return '0'; } // terminate early
  fixed = (!fixed || fixed < 0) ? 0 : fixed; // number of decimal places to show
  var b = (num).toPrecision(2).split("e"), // get power
      k = b.length === 1 ? 0 : Math.floor(Math.min(b[1].slice(1), 14) / 3), // floor at decimals, ceiling at trillions
      c = k < 1 ? num.toFixed(0 + fixed) : (num / Math.pow(10, k * 3) ).toFixed(1 + fixed), // divide by power
      d = c < 0 ? c : Math.abs(c), // enforce -0 is 0
      e = d + ['', 'K', 'M', 'B', 'T'][k]; // append power
  return e;
}

Comments