9

I want to create a Line chart with multiple datasets dynamically in the chart.js library.

I am able to assign the data dynamically. But I want to create the datasets itself dynamically. I saw the link below:

How to add the elements dynamically from the html table for chart.js

and tried this :

var datasetValue = [];
for (var j = 0; j < count; j++) {
datasetValue[j] = [
{
fillColor: 'rgba(220,220,220,0.5)',
strokeColor :'rgba(220,220,220,1)' ,
title :'2013',
data : [Math.round(Math.random() * 100),Math.round(Math.random() * 100)-10]
}]
}

var mydata = {
datasets : datasetValue
}

Here the count value is 3 and I want to display the 3 lines in the chart and the count value will vary. Even though the chart line color and title will be the same, I want to display the line first and will change the rest once this is solved.

I tried to access the data like so :

alert("Datasets:"+mydata.datasets[0].data);

It should show the data of the first dataset but it's showing undefined.

However in the below case:

var mydata1 = {
labels : ["January","February","March","April","May","June","July"],
datasets : [
    {
        fillColor : "rgba(220,220,220,0.5)",
        strokeColor : "rgba(220,220,220,1)",
        data : [95,53,99,73,27,82,40],
        title : "2014"
    },
    {
        fillColor : "rgba(151,187,205,0.5)",
        strokeColor : "rgba(151,187,205,1)",
        data : [35,43,59,31,50,66,55],
        title : "2013"
    }
 ]
}

alert("Datasets:"+mydata1.datasets[0].data);

I am able to get the data of first dataset. Could anyone please give me the solution?

2 Answers 2

14

I think you are looking for something like the following solution. http://jsfiddle.net/5m63232a/

var datasetValue = [];
var count = 10;
for (var j=0; j<count; j++) {
    datasetValue[j] = {
        fillColor: 'rgba(220,220,220,0.5)',
        strokeColor :'rgba(220,220,220,1)',
        title :'2013',
        data : [Math.round(Math.random() * 100),Math.round(Math.random() * 100)-10]
    }
}
var mydata = {
    datasets : datasetValue
}
alert("Datasets: "+mydata.datasets[0].data);
Sign up to request clarification or add additional context in comments.

2 Comments

Hi Bear GRiZZLY Xi just now i got this solution anyway very very thank you for your quick reply.
@Suresh care to share solution you 'got'?
6

Create service to fetch json formt like :

labels : ["January","February","March","April","May","June","July"],
        datasets : [
            {
                label: "My First dataset",
                fillColor : "rgba(220,220,220,0.2)",
                strokeColor : "rgba(220,220,220,1)",
                pointColor : "rgba(220,220,220,1)",
                pointStrokeColor : "#fff",
                pointHighlightFill : "#fff",
                pointHighlightStroke : "rgba(220,220,220,1)",
                data : [randomScalingFactor(),randomScalingFactor(),randomScalingFactor(),randomScalingFactor(),randomScalingFactor(),randomScalingFactor(),randomScalingFactor()]
            },
            {
                label: "My Second dataset",
                fillColor : "rgba(151,187,205,0.2)",
                strokeColor : "rgba(151,187,205,1)",
                pointColor : "rgba(151,187,205,1)",
                pointStrokeColor : "#fff",
                pointHighlightFill : "#fff",
                pointHighlightStroke : "rgba(151,187,205,1)",
                data : [randomScalingFactor(),randomScalingFactor(),randomScalingFactor(),randomScalingFactor(),randomScalingFactor(),randomScalingFactor(),randomScalingFactor()]
            }
        ]  

Then add following javascript code

var j = [];
$.ajax({
    type: 'GET',
    url: 'http://' + window.location.host.toString() + "/path",
    dataType: 'json',
    success: function (data) {
       j = data;
    },
    async: false
 });
 var chartData = {
     labels: j.labels,
     datasets: j.datasets
 };
 window.onload = function () {
     var ctx = document.getElementById("canvas").getContext("2d");
     window.myLine = new Chart(ctx).Line(chartData, {
         responsive: true,
         animation: true,
         tooltipFillColor: "rgba(0,0,0,0.8)",
         multiTooltipTemplate: "<%= datasetLabel %> - <%=value %>"
     });
 }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.