score:1
Accepted answer
you can use set, its avoid duplicate values:
let ar = new set();
data.map((item) => {
ar.add(item.items.selecteditem);
});
ar = array.from(ar); //convert set to array
edit: ok, for that i think its better to use:
let ar = {};
for(let i=0; i<data.length; i++){
if(ar[data[i].items.selecteditem] === undefined)
ar[data[i].items.selecteditem] = 1;
else
ar[data[i].items.selecteditem]++;
}
let data_labels = object.keys(ar);
let data_ = object.keys(ar).map(key => ar[key]);
and replace:
data={{
labels: data_labels,
datasets: [
{
data: data_,
backgroundcolor: ["red", "yellow", "green", "blue", "pink"],
bordercolor: ["rgba(255, 99, 132, 1)"],
borderwidth: 1
}
]
}}
and for the backgroundcolor array you need to check the length of ar, and make an array of this size with different colors if you want.
Source: stackoverflow.com
Related Query
- How can I pass the data into the chartjs label?
- How can I load multiple Chartjs charts with different data on the same page?
- How can I change the label name when you hover your data in Chart.js?
- How to get the database data into ChartJS using codeigniter
- How can I put my label on the right hand side of my chart in Chartjs
- How to pass sql query data onto the datasets field of chartjs using nodejs (ejs)
- How to feed hour and minute data into chartJS
- Chart.js canvas, how can I swap data without the previous data affecting my hover events?
- How can I display the xAxes and yAxes data in the tooltip, Chart JS?
- How to Change the Label Strike-Through with light gray on a ChartJS Doughnut?
- Chartjs in Reactjs - how can I update data dynamically?
- How can I get the active label element on click in ChartJS?
- ChartJS - How to increase the maximum degree of label rotation on x-axis?
- how can i send a list of numbers from views.py to a chartjs file im using for the front?
- Angular 2: How to pass my API data to graph and Display the Graph with data
- How can I prevent the hover data from displaying in Chart.JS?
- Chart.JS: How can I only display data labels when the bar width is big enough for the text?
- How can I show a subset of data on pie pieces in Chart.JS while still displaying the superset when hovering?
- How to set the chartjs bar graph scale to the highest value in the result data
- How to reuse a Chartjs Chart component in with different Data and get past the **Canvas is already in use** error?
- How can I change the cursor on pie chart segment hover in ChartJS 3?
- Dynamically pass the JSON data in chartjs
- how labels in chartjs can make data invisible
- ChartJs how can I move bar into new group?
- In ChartJS how do I change the color of a label in the legend?
- how can i remove the grid lines in chartJs
- Chart.JS: How can I only display data labels when the doughnut size is big enough for the text?
- How to set the gap between data items in a chartjs chart
- How can I pass my sqlite data to django, and make chart?
- How can i do so that my chartjs updates everytime data is inserted or by time interval?
More Query from same tag
- Chart.js polar chart
- Change height of chartjs dynamically
- Custom background limits for Doughnut (Gauge)
- Chart JS - X-Axis base on timezone
- Passing Labels from PHP to Chart.js
- How to get hundreds of JS object properties into arrays to use as chart.js axes/data
- Chart js legend are being cut off if the bar height is equal to port height - chart js
- How to create an angular 7 component?
- How i can localize days and month name in ChartJS 3.x?
- Chartjs doughnut chart for conditional data
- Add HTML to label of bar chart - chart js
- The scale on the axis doesn't automatically narrow
- Chart.js formatting data values
- How to push an array into an Object which is inside of another array?
- How to inject “Chart.js” to my module?
- Why does nuxt give me this error with vue-chartjs?
- Chart.js Example Code not working
- Why must I create my VueJS application before using ChartJS?
- Vue Chart.js -- can I give a line chart a default value for missing data?
- How to have ticks on x axis in 15 min increments on chartJS?
- Why is the html page only displaying one of the charts?
- How to improve PDF rasterization quality in PhantomJS?
- Try to change style in current month in chartJS
- Getting rid of Chart.js canvas subpixels
- Dotted Line in ChartJS
- Do not draw line on condition in ChartJS line chart
- Weather graph using Angular 2, chart.js and json data
- Unable to change y-axis value dynamically based on data for Stacked bar chart in reactjs
- ChartJS - Uncaught SyntaxError: Unexpected number
- chartjs-plugin-zoom not working with my React project