0

I want to display a stacked chart using chartnew.js in aspx page . I got all relevant data from webmethods. The data for chart is in the format

var classdata = {
                    labels: ["English", "French", "Science", "Maths", "SS", "IT","Computer","Arts"],
                    datasets: [

                        {
                            data: [3, 3, 2, 7, 1, 3, 4, 9],
                            fillColor: "#B1CD4F",
                            title: "EXCELLENT"
                        }, {
                            data: [5, 2, 10, 4, 2, 8, 4, 2],
                            fillColor: "#4663EA",
                            title: "FAIR"
                        }, {
                            data: [6, 5, 4, 4, 0, 7, 4, 2],
                            fillColor: "#B812E0",
                            title: "GOOD"
                        }, {
                            data: [0, 0, 3, 0, 0, 1, 0, 1],
                            fillColor: "#4173D1",
                            title: "NEEDS IMPROVEMENT"
                        }, {
                            data: [2, 0, 2, 0, 1, 1, 1, 2],
                            fillColor: "#55E949",
                            title: "SATISFACTORY"
                        }, {
                            data: [7, 6, 2, 8, 3, 3, 10, 7],
                            fillColor: "#4DD1E7",
                            title: "VERY GOOD"
                        }]
                }

I got all these data from two webmethods. I took it in two separate variables. That is the labels values to a variable named "datalables" and the dataset values to variable named "data".

Now i want to combine these two variables and create the data for chart. I tried like the following, but it is not working ie the chart is not displaying.

var classdata = {labels: [datalabels], datasets: [data] };

How can i combine these two variables to make the chart? Someone please help.

The complete code is here.

function LoadClassChart() {
        $.ajax({
            type: "POST",
            url: "Home.aspx/GetClassChart",
            data: JSON.stringify({ Grade: $('select[id$=BatchDropDownList1]').val(), Term: $('select[id$=TermDropDownList1]').val() }),
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function (r) {
                $(".classdivstyle").css("backgroundImage", "url(/Images/back.png)");

                var data;
                try {
                    data = r.d;
                }
                catch (e) {
                    if (e instanceof SyntaxError) {
                        alert(e.message);
                    }
                    else {
                        throw (e);
                    }
                }
                var el = document.getElementById("<%= classcanvas.ClientID %>");
                var ctx = el.getContext('2d');

                var classdata = {
                    labels: [datalabels],
                    datasets: [data]
                };

                var newopts = {
                    inGraphDataShow: true,
                    inGraphDataFontColor: 'white',
                    inGraphDataFontSize: 14,
                    inGraphDataFontStyle: 'bold',
                    highLight: true,
                    highLightSet: { fillColor: "red", inGraphDataFontColor: "black", inGraphDataFontSize: 18 },
                    highLightFullLine: true,
                    datasetFill: true,
                    scaleFontSize: 16,
                    canvasBorders: true,
                    graphTitle: $('select[id$=TermDropDownList1]').val() + " Performance",
                    graphTitleFontFamily: "'Segoe UI'",
                    graphTitleFontSize: 22,
                    graphTitleFontColor: "#666",
                    graphSubTitle: "Class Teacher : ",
                    legend: true,
                    yAxisLabel: "Students",
                    xAxisLabel: "Subjects",
                    yAxisUnit: "Nos.",
                    annotateDisplay: true,
                    dynamicDisplay: true
                }
                $(".classdivstyle").css("backgroundImage", "none");
                var myBarChart = new Chart(ctx).StackedBar(classdata, newopts);
            },
            failure: function (response) {
                alert('There was an error in loading chart.');
            }
        });
    }
shaiju vj
  • 1
  • 1
  • 1

1 Answers1

1

remove square brackets in classdata variable as follows

var classdata = { labels: datalabels, datasets: data };

I'm guessing datalabels and data are returning as an arrays.

Damitha Shyamantha
  • 514
  • 1
  • 4
  • 9
  • Thank you, I tried the code, but the chart is not displaying. – shaiju vj Nov 23 '16 at 08:39
  • can you post the content of r.d in data = r.d; :) – Damitha Shyamantha Nov 23 '16 at 08:43
  • This is the value in r.d - `{fillColor : "#A6DD4A",strokeColor : "#979491",data : [12,8,4,1,13,11],title: "EXCELLENT"},{fillColor : "#E3D954",strokeColor : "#CEDC51",data : [6,2,4,6,0,4],title: "FAIR"},{fillColor : "#364C06",strokeColor : "#C90DA8",data : [2,5,7,9,1,2],title: "GOOD"},{fillColor : "#68F351",strokeColor : "#CAA55D",data : [0,0,0,1,0,1],title: "NEEDS IMPROVEMENT"},{fillColor : "#ED585E",strokeColor : "#D59EAC",data : [3,8,8,6,9,5],title: "VERY GOOD"}` – shaiju vj Nov 23 '16 at 08:53
  • where is datalabels variable defined, is it accessible in ajax success – Damitha Shyamantha Nov 23 '16 at 09:04
  • it is outside of all the functions, i can see the values of datalabels when i do `alert(datalabels)` – shaiju vj Nov 23 '16 at 09:10
  • When i put the values directly in a variable like this it is working `var classdata = { labels: ["en", "fr", "hi", "mar", "math", "sc"], datasets: [ { fillColor: "#A6DD4A", strokeColor: "#979491", data: [12, 8, 4, 1, 13, 11], title: "EXCELLENT" }, { fillColor: "#E3D954", strokeColor: "#CEDC51", data: [6, 2, 4, 6, 0, 4], title: "FAIR" }, { fillColor: "#364C06", strokeColor: "#C90DA8", data: [2, 5, 7, 9, 1, 2], title: "GOOD" }, { fillColor: "#68F351", strokeColor: "#CAA55D", data: [0, 0, 0, 1, 0, 1], title: "NEEDS IMPROVEMENT" } ] }` – shaiju vj Nov 23 '16 at 09:13
  • try this darasers:JSON.parse(data) – Damitha Shyamantha Nov 23 '16 at 09:27
  • do it like this, var data1 = JSON.parse(data); then add this to darasers:data1. because sometime js get confused when creating a object and we convert properties inside. – Damitha Shyamantha Nov 23 '16 at 09:59
  • i got exception on parse - Unexpected tocken f in JSON at position 1. what is wrong in this data? {fillColor : "#8907B7",strokeColor : "#D028CD",data : [11,8,6,1,10,3],title: "EXCELLENT"}, {fillColor : "#C41C8F",strokeColor : "#984C1E",data : [0,3,0,8,3,3],title: "FAIR"} – shaiju vj Nov 23 '16 at 10:16
  • are you returning the data as a array? I guess this is the problem. to be valid json it should be look like following. [{fillColor : "#A6DD4A",strokeColor : "#979491",data : [12,8,4,1,13,11],title: "EXCELLENT"},{fillColor : "#E3D954",strokeColor : "#CEDC51",data : [6,2,4,6,0,4],title: "FAIR"},{fillColor : "#364C06",strokeColor : "#C90DA8",data : [2,5,7,9,1,2],title: "GOOD"},{fillColor : "#68F351",strokeColor : "#CAA55D",data : [0,0,0,1,0,1],title: "NEEDS IMPROVEMENT"},{fillColor : "#ED585E",strokeColor : "#D59EAC",data : [3,8,8,6,9,5],title: "VERY GOOD"}]. you are missing [] around json array. – Damitha Shyamantha Nov 24 '16 at 03:10
  • [] is also there. i merged the two variables from the cs code and now it is like `labels: ["label1","label2",...], datasets: [ ... dataset values ...]` now how do i use it to create the chart? i tried `var classdata =r.d; var myBarChart = new Chart(ctx).StackedBar(classdata, newopts);` still no chart is displaying – shaiju vj Nov 24 '16 at 05:50