score:1

if your aim is to take the values from an array and have them appear along the bottom of the bar chart so that each array value is the label for a bar then you need to set the data.labels value, not the data.datasets.label value.

for example, this basic chart is taken from the chart.js documentation for bar chart data structure and shows how to use an array of month names to label the bars. notice that the label bars go into the data.labels node.

var data = {
    labels: ["january", "february", "march", "april", "may", "june", "july"],
    datasets: [
        {
            label: "my first dataset",
            backgroundcolor: "rgba(255,99,132,0.2)",
            bordercolor: "rgba(255,99,132,1)",
            borderwidth: 1,
            hoverbackgroundcolor: "rgba(255,99,132,0.4)",
            hoverbordercolor: "rgba(255,99,132,1)",
            data: [65, 59, 80, 81, 56, 55, 40],
        }
    ]
};

if you programmatically create an array with one label for each point of data then it might look something like this:

var chartconfig = {};

for (score = 0; score < maxscore; ++score) {
    chartconfig.scorelabels[score] = score;
    chartconfig.scoredata[score] = howmanyachievedscore(score);
}

var data = {
    labels: chartconfig.scorelabels,
    datasets: [
        {
            label: "number of players who achieved score",
            backgroundcolor: "rgba(255,99,132,0.2)",
            bordercolor: "rgba(255,99,132,1)",
            borderwidth: 1,
            hoverbackgroundcolor: "rgba(255,99,132,0.4)",
            hoverbordercolor: "rgba(255,99,132,1)",
            data: chartconfig.scoredata,
        }
    ]
};

you don't have to create the labels and data values inside a single object, but it's usually tidier if you can group your chart configuration data into one object so that you can pass it from one function to another with one parameter.

the data.datasets.label value does something different, providing text which appears in the chart legend and in tooltips which appear when you hover over a bar.


Related Query

More Query from same tag