7
votes

I am using Chart JS for the first time and I wanted to show multiple data points for the same label. Can I do this without creating multiple datasets each time. Also, I will know the number of datasets only on run time.

Use case: For each point on x-axis(label) plot multiple points on y-axis

Right now I am doing something like this

 var ctx = $(element);
            var myChart = new Chart(ctx, {
                type: graphType,
                data: {
                    labels: labels,
                    datasets: [
                        {
                            label: labelTeacher,
                            fill: false,
                            lineTension: 0.1,
                            backgroundColor: "rgba(100,149,237,0.5)",
                            borderColor: "rgba(100,149,237,1)",
                            borderCapStyle: 'butt',
                            borderDash: [],
                            borderDashOffset: 0.0,
                            borderJoinStyle: 'miter',
                            pointBorderColor: "rgba(58,95,205,1)",
                            pointBackgroundColor: "#fff",
                            pointBorderWidth: 1,
                            pointHoverRadius: 5,
                            pointHoverBackgroundColor: "rgba(100,149,237,1)",
                            pointHoverBorderColor: "rgba(220,220,220,1)",
                            pointHoverBorderWidth: 2,
                            pointRadius: 1,
                            pointHitRadius: 10,
                            data: [65, 59, 90, 81, 56, 55, 40]
                        },
                        {
                            label: labelVedantu,
                            fill: false,
                            lineTension: 0.1,
                            backgroundColor: "rgba(255,165,0,0.5)",
                            borderColor: "rgba(255,165,0,1)",
                            borderCapStyle: 'butt',
                            borderDash: [],
                            borderDashOffset: 0.0,
                            borderJoinStyle: 'miter',
                            pointBorderColor: "rgba(255,165,0,1)",
                            pointBackgroundColor: "rgba(255,255,255,1)",
                            pointBorderWidth: 1,
                            pointHoverRadius: 5,
                            pointHoverBackgroundColor: "rgba(255,165,0,1)",
                            pointHoverBorderColor: "rgba(250,228,196,1)",
                            pointHoverBorderWidth: 2,
                            pointRadius: 1,
                            pointHitRadius: 10,   
                            data: [28, 48, 40, 19, 96, 27, 100]
                        }
                    ]
                },

Let us say I have multiple data arrays for the same label set, is there a way to do it without adding a new dataset each time

1

1 Answers

0
votes

When you say 'show', are you referring to in how the chart renders, or in the "tooltips" that appear when you hover over the chart?

If the latter, check out the mode:'index' setting for options/tooltips, as described here:

http://www.chartjs.org/docs/latest/general/interactions/modes.html#interaction-modes