score:0
Accepted answer
this function will return the data you need for chartjs:
function getcountdata(choices, results) {
var counts = {};
var data = [];
choices.foreach(function(c) {
counts[c] = 0;
});
results.foreach(function(r) {
counts[r] += 1;
});
object.keys(counts).foreach(function(c) {
data.push(counts[c]);
});
return data;
}
var choices = ['answer1', 'answer2', 'answer3', 'answer4', 'answer5'];
var results = ["answer1", "answer1", "answer1", "answer1", "answer1", "answer2", "answer2", "answer2"];
console.log(getcountdata(choices, results));
you can use the above function with chartjs like this:
data: {
labels: choices,
datasets: [{
label: ': votes',
data: getcountdata(choices, results),
// ...
}]
},
score:0
this fonction can count your answers based on your answers and results :
function dynamiccounter(answers,results){
return answers.map( (a) => {
var nb = 0;
results.foreach( (r) => { if(a === r)nb++ } );
return { [a] : nb }
})
}
if you want only the results to give to chartjs (as data) :
function results(answers,results){
return answers.map( (a) => {
var nb = 0;
results.foreach( (r) => { if(a === r)nb++ } );
return nb;
})
}
here a working jsfiddle https://jsfiddle.net/wanhe6rs/1/
Source: stackoverflow.com
Related Query
- Adding data to charts.js dynamically from tables
- Chartjs random colors for each part of pie chart with data dynamically from database
- Update Chart js chart dynamically in angular 2 from data getting from nodejs service
- ChartJs - displaying data dynamically from back-end
- Chart.js and Angular 8 - Dynamically updating Chart.js labels and data from *ngfor
- Visualization of charts using real time data from MSSQL with node.js webserver
- Angular6 and ng2-charts does not display any charts when i fill data from webservice
- chartjs how to update dynamically data from database(Chartjs cant get the data)
- How to add data dynamically to primevue Line chart from vuejs3?
- How to create a charts in Razor Pages pulling data from the database?
- Calling data from outside of Chartjs code
- I need a PHP query to grab data from the database and use in my bar charts for results
- Changing chart data dynamically with C# from SQL database
- getting additional value fields from data source for dx.chartjs doughnut chart
- Multiple Chart.js charts on a single page, from 1 function but different data
- How to adding data in loop and display chart dynamically like in live (chart.js & typescript & angular)?
- how to add multiple data in chart js dynamically from JSON
- Get data from SQLite as an array and generate charts with the data using Chart.js in an HTML
- How to Push data dynamically from firebase to bar graph in angular
- Adding data to Chart.js line graph from array
- Angular 13, Chart.js, ng2-chart Update data from API dynamically
- Display API data in line charts dynamically
- How do I fetch data from 2 tables and display into 1 chartjs
- Adding Chart.js line chart to Jinja2/Flask html page from JS file
- How can I sort data from highest to lowest in chart.js
- Move tooltip further from data point for Chart.js?
- Show data dynamically in line chart - ChartJS
- React ChartJS prevent new data from being added into state after it's redrawn?
- Chart.js - Getting data from database using mysql and php
- How to use 'time' (data from database, data type: timestamp ) for plotting graph in Chart JS
More Query from same tag
- Ajax call to trigger a controller function
- ChartJS tooltip scrollbar to prevent data overflow?
- Bar labels in Legend
- How to install Chart.js without a library?
- Need help adding decimals to yaxis values
- Remove custom Chart Js tool tip colour square
- Chart.js t.ticks.map is not a function
- How to display that data was unavaible during some time in a Chart.js (or any other library's) timeline graph
- How to add the value for each label to pie legend
- Chart.js: Don't stretch axes beyond chart
- Chart.js - Tooltips overlapped by bar values
- create Chart using Chartjs and PHP
- Separating list of data in Ajax
- Chart.js is not rendered until zoom in in angular 8
- Is it possible to print a chart with vue-chartjs?
- Angular .subscribe executes at the end after the ngOnInit
- ChartJs - Round borders on a doughnut chart with multiple datasets
- Pass variable from function js to other function js
- Sending 2 fetched result into AJAX
- Access a chart in a different function
- Access data arrays from c# method inside JavaScript inside .cshtml file
- Disable Charts.js animations in Polymer?
- Can't load multiple charts onto a page when looping through dummy array
- how to setting chart js customization
- Can't get Chart.js to run in a Vue.js component
- chart.js time x-axis - start ticks at specific time
- Chartjs removing labels and begin number
- how to create line chart using chart.js in angular 2+
- Is there a way to store a chart as an image directly to server using Chart.js?
- Deleting and recreating an element with React