Please note: The Google Image Charts used in this solution is deprecated and will stop working April 20, 2015.
v1.54 fixes one incompatibility with SP 2013. I have not fully tested this so please let me know if you find any bugs.
July 01. 2013 v1.53: The sort order of answers in chart now reflects the order given in the setup. I have removed some script references so that you now must refer spjs-utility.js – get it here
You must update the CEWP code to include reference to spjs-utility.js
15.12.2011 v1.5.1: Bugfix in the file “PollForSharePoint.js” – line 238. The property “listBaseUrl” were missing – thanks to Jaap Dijkma for finding this bug.
04.12.2011 I have updated the solution to support putting the list in a central location. See “listBaseUrl” below.
22.10.2011 Updated the code to support free input rather than predefined answers – as requested by Claudia:
Hi, thank you for this wonderful piece of code, I think it’s great and we use it on our intranet :).
I wondered if it is possible to use a single line of text for free input instead of radiobuttons with preset answers?
We like to use the poll for ‘brandtagging’ so that people can type keywords.
Just wondering…..
thanks! Claudia
I thought this was a clever idea and have implemented this in v1.4. See updated CEWP code and description below.
17.08.2011 Updated the code with these changes:
- Lets you view the results and come back to answer.
- Tidied up the code.
- The charts now supports https (Thanks to Google).
- Removed the poll question from the chart title to preserve the formatting.
01.03.2011 Updated the code to fix a bug when “singleResponse” is true. Thanks to “Al”. I have also included two more settings in the “argObj”: qStyle and aStyle. These sets the CSS style of question and answers. See CEWP code below.
28.11.2010 Updated the code to support special characters in the poll question and answers. Thanks to Christophe for the tip.
This code lets you generate polls using JavaScript/jQuery only. No need for server side installed WebParts.
Poll
Result with column chart
Result with bar chart
Result with pie chart
Result with “table chart”
Already answered
Not active
Finished
The charts are generated using Google Chart Tools / Image Charts (aka Chart API).
Create a custom list with the following fields
- Answer: Single line of text
- Question: Single line of text
Name it anything you like, but keep the display name fairly simple (no special characters) as you will use the display name in the CEWP code.
CEWP code
The CEWP code below refers jQuery from Google. If you have a local copy of jQuery you can change the script src. You find the code for the file “PollForSharePoint.js” at the bottom of the page. In this example the file “PollForSharePoint.js” is placed in a document library named “Javascript”.
Place this code where you want the poll to appear:
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script> <script type="text/javascript" src="/test/English/Javascript/Poll/spjs-utility.js"></script> <script type="text/javascript" src="/test/English/Javascript/Poll/PollForSharePoint.js"></script> <script type="text/javascript"> buildQueryWP({pollAnswerListName:'PollA', listBaseUrl:L_Menu_BaseUrl, id:'myPoll_01-03-2011', // Allowed characters id a-z, 0-9 - and _ start:'10/22/2011', // format: mm/dd/yyyy end:'11/10/2011', // format: mm/dd/yyyy singleResponse:false, q:"What is your<br />favorite junk food?", qStyle:'font-size:small;font-weight:bold;color:gray', aStyle:'font-size:xx-small', a:['Pizza','Hot dog','Hamburger','Neither of them'], // Leave empty for free input color:['32CD32','FFCC11','FF3300','C0C0C0'], forceLowerCaseAnswer:false, // Group result by lowercase chart:'col', // table, bar, col or pie height:100, width:450}); </script>
You can control these values in the top of the file “PollForSharePoint.js”:
var submitLinkText = 'Submit'; var backLinkText = 'Back'; var showResultBtnText = 'Show result'; var pollNotActiveText = 'The poll is not active prior to {0}'; var pollEndedText = 'The poll ended on {0}'; var alreadyRespondedText = 'You answered: '; var totalResponsesLabel = "Total responses: ";
Object attributes explained
- pollAnswerListName: DisplayName or GUID of the list that stores the answers
- listBaseUrl: The baseUrl of the site. This is like “/sites/hr” when the list is located in the site “hr” under “/sites”. Use L_Menu_BaseUrl (or omit the property) for current site. New in v1.5
- id: The unique id of the poll. All poll answers are stored in a list and this id is used to separate each poll
- start: Start date in the format mm/dd/yyyy
- end: End date in the format mm/dd/yyyy
- singleResponse: true for one reply per user, false for unlimited number of replies
- q: Poll question. To have a linefeed in the question, use <br>
- qStyle: CSS syntax style
- aStyle: CSS syntax style
- a: Answers in an array format. To use free input and not predefined answers, leave the array empty.
- color: Colors for the chart in an array format. This must have the same length as the previous parameter – one color for each answer
- forceLowerCaseAnswer: Primarily for use with free input to avoid getting two “series” when the only difference are uppercase characters. New in v1.4
- chart: “bar” for bar chart, “col” for column chart, “pie” for pie chart or “table” for a plain table.
- height: Height in pixels
- width: Width in pixels
Regarding free input
If you leave the attribute “a” as an empty array, the user can supply free text as “answer”. When using free input, the result are automatically presented as a table.
Download code
The code for the file “PollForSharePoint.js”:
Download code from this location
Ask if anything is unclear.
Alexander