In this article, we shall see how to create a Bar Chart using HighCharts and bind dynamic values using SPServices GetListItems.
Before starting make sure you have all these reference files.
  • jquery-1.10.2.js
  • highcharts-custom.js
  • exporting.js
  • jquery.SPServices-0.7.2.min.js
Using SPservices - GetListItem:
  1. //Getting Current Site URL
  2. var CsiteUrl = _spPageContextInfo.webServerRelativeUrl;
  3. //Getting the CAML Queries
  4. var ViewFields = "<ViewFields><FieldRef Name='Selection' /><FieldRef Name='Integer' /><FieldRef Name='Int' /></ViewFields>";
  5. var FilteredValue = "<Query><Where><IsNotNull><FieldRef Name='Selection' /></IsNotNull></Where></Query>";
  6. //Declaring 2 arrays - Categories and Series for passing the values to chart.
  7. var ogetCountries = [];
  8. var oPopulateData = [];
  9. //Writing the GetListItem SPServices
  10. $().SPServices
  11. ({
  12. operation: "GetListItems",
  13. async: false,
  14. webURL: CsiteUrl,
  15. listName: "PieChart",
  16. CAMLViewFields: ViewFields,
  17. CAMLQuery: FilteredValue,
  18. completefunc: function(xData, Status)
  19. {
  20. // alert(xData.responseText+ " " + Status);
  21. $(xData.responseXML).SPFilterNode("z:row").each(function()
  22. {
  23. oData = $(this).attr("ows_Population")
  24. oPlaces = $(this).attr("ows_Country");
  25. oPopulateData.push(oData); //Storing the Population values in the Array
  26. ogetCountries.push(oPlaces); //Storing the Countries values in the Array
  27. });
  28. }
  29. });
Once you have pushed the entire values into the Array (oPopulateData & ogetCountries) you shall start the Charting process.
In this example, I have used the HighCharts (http://www.highcharts.com - Simple Bar Chart) format.
Below is the format which you can use for creating the BAR Chart.
I've used the Div for displaying the BarChart.
  1. <div id="flot-placeholder" style="width:550px;height:400px;margin:0 auto"></div>
  2. $('#flot-placeholder').highcharts({
  3. chart: {
  4. type: 'bar'
  5. },
  6. title: {
  7. text: 'Historic World Population by Region'
  8. },
  9. subtitle: {
  10. text: 'Source: <a href="https://en.wikipedia.org/wiki/World_population">Wikipedia.org</a>'
  11. },
  12. xAxis: {
  13. categories: ogetCountries, //This is where you are going to pass your Dynamic Text which comes on the X-Axis
  14. title: {
  15. text: null
  16. }
  17. },
  18. yAxis: {
  19. min: 0,
  20. title: {
  21. text: 'Population (millions)',
  22. align: 'high'
  23. },
  24. labels: {
  25. overflow: 'justify'
  26. }
  27. },
  28. tooltip: {
  29. valueSuffix: ' millions'
  30. },
  31. plotOptions: {
  32. bar: {
  33. dataLabels: {
  34. enabled: true
  35. }
  36. }
  37. },
  38. legend: {
  39. layout: 'vertical',
  40. align: 'right',
  41. verticalAlign: 'top',
  42. x: -40,
  43. y: 80,
  44. floating: true,
  45. borderWidth: 1,
  46. backgroundColor: ((Highcharts.theme && Highcharts.theme.legendBackgroundColor) || '#FFFFFF'),
  47. shadow: true
  48. },
  49. credits: {
  50. enabled: false
  51. },
  52. series: [{
  53. name: 'Year 2015',
  54. data: JSON.parse("["+ oPopulateData +"]") //This is the place where the Bar gets displayed. So, the input should be Integer and not charcters.
  55. }]
  56. });
Once completed, you will see the result similar to this image.