A basic pie chart example would be like this:
var data = [
{ label: "social networks", data: 38.7, color: "#68BC31"},
{ label: "search engines", data: 24.5, color: "#2091CF"},
{ label: "ad campaigns", data: 8.2, color: "#AF4E96"},
{ label: "direct traffic", data: 18.6, color: "#DA5430"},
{ label: "other", data: 10, color: "#FEE074"}
]
$('#piechart-placeholder').css({'width':'90%' , 'min-height':'150px'});
var my_chart = $.plot('#piechart-placeholder', data, {
series: {
pie: {
show: true,
tilt: 0.8,
highlight: {
opacity: 0.25
},
stroke: {
color: '#fff',
width: 2
},
startAngle: 2
}
},
legend: {
show: true,
position: position || "ne",
labelBoxBorderColor: null,
margin:[-30,15] //some offsetting
},
grid: {
hoverable: true,
clickable: true
}
})
Chart tooltip example:
//pie chart tooltip example
//create the tooltip once
var $tooltip = $("<div class='tooltip top in'><div class='tooltip-inner'></div></div>").hide().appendTo('body');
var lastIndex = null;
$('#piechart-placeholder').on('plothover', function (event, pos, item) {
if(item) {
if (lastIndex != item.seriesIndex) {
lastIndex = item.seriesIndex;
var tooltip_text = item.series['label'] + " : " + item.series['percent']+'%';
$tooltip.show().children(0).text(tooltip_text);
//or
//$tooltip.find('.tooltip-inner').text(tooltip_text);
}
$tooltip.css({top:pos.pageY + 10, left:pos.pageX + 10});
} else {
$tooltip.hide();
lastIndex = null;
}
});
Another basic example:
var d1 = [];//dataset 1 (random data)
for (var i = 0; i < Math.PI * 2; i += 0.5) {
d1.push([i, Math.sin(i)]);
}
var d2 = [];//dataset 2 (random data)
for (var i = 0; i < Math.PI * 2; i += 0.5) {
d2.push([i, Math.cos(i)]);
}
//we put the chart inside #sales-charts element
$('#sales-charts').css({'width':'100%' , 'height':'220px'});
var my_chart = $.plot("#sales-charts", [
{ label: "Domains", data: d1 },
{ label: "Hosting", data: d2 }
], {
hoverable: true,
shadowSize: 0,
series: {
lines: { show: true },
points: { show: true }
},
xaxis: {
tickLength: 0
},
yaxis: {
ticks: 10,
min: -2,
max: 2,
tickDecimals: 3
},
grid: {
backgroundColor: { colors: [ "#fff", "#fff" ] },
borderWidth: 1,
borderColor:'#555'
}
});