Getting StartedPerformance !!!Chart DataBar and Column ChartsLine and Area ChartsPie ChartsScatter and Bubble ChartsRadar and Polar ChartsFinancial ChartsCombinational ChartsDynamic ChartsWorking with Chart AxesChart FeaturesCustomizing ChartClient-Side Events |
This sample demonstrates how to change (add, remove) chart data points.
To change the chart data, you can just change the data array and call the .jqChart("update") method. The jqChart will handle the rest. For detailed implementation, please take a look at the HTML code tab.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>
Change Data Points Example - HTML5 jQuery Chart Plugin by jqChart
</title>
<link rel="stylesheet" type="text/css" href="../css/jquery.jqChart.css" />
<link rel="stylesheet" type="text/css" href="../css/jquery.jqRangeSlider.css" />
<link rel="stylesheet" type="text/css" href="../themes/le-frog/jquery-ui-1.8.20.css" />
<script src="../js/jquery-1.5.2.min.js" type="text/javascript"></script>
<script src="../js/jquery.jqChart.min.js" type="text/javascript"></script>
<script src="../js/jquery.jqRangeSlider.min.js" type="text/javascript"></script>
<!--[if IE]><script lang="javascript" type="text/javascript" src="../js/excanvas.js"></script><![endif]-->
<script lang="javascript" type="text/javascript">
var background = {
type: 'linearGradient',
x0: 0,
y0: 0,
x1: 0,
y1: 1,
colorStops: [{ offset: 0, color: '#d2e6c9' },
{ offset: 1, color: 'white'}]
};
var fillStyle = {
type: 'linearGradient',
x0: 0,
y0: 0,
x1: 1,
y1: 0,
colorStops: [{ offset: 0, color: '#65c2e8' },
{ offset: 0.49, color: '#55b3e1' },
{ offset: 0.5, color: '#3ba6dc' },
{ offset: 1, color: '#2794d4'}]
};
$(document).ready(function () {
$('#jqChart').jqChart({
title: { text: 'Change Data Points' },
border: { strokeStyle: '#6ba851' },
background: background,
series: [
{
title: 'Series',
type: 'column',
data: [20, 50, 40, 60, 70],
fillStyle: fillStyle
}
]
});
$('#buttonAddPoint').click(function () {
// get current series
var series = $('#jqChart').jqChart('option', 'series');
// get the data from the first series
var data = series[0].data;
// add new data item
data.push(Math.round(Math.random() * 100));
// update (redraw) the chart
$('#jqChart').jqChart('update');
});
$('#buttonRemovePoint').click(function () {
// get current series
var series = $('#jqChart').jqChart('option', 'series');
// get the data from the first series
var data = series[0].data;
// remove the last data item
data.splice(data.length - 1, 1);
// update (redraw) the chart
$('#jqChart').jqChart('update');
});
});
</script>
</head>
<body>
<div id="jqChart" style="width: 500px; height: 300px;">
</div>
<br />
<input id="buttonAddPoint" type="button" value="Add new point" />
<input id="buttonRemovePoint" type="button" value="Remove last point" />
<br />
</body>
</html>
|
Follow us on Twitter