Click here to Skip to main content
15,896,063 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
Hi I am trying to pass the dynamic data for jquery charts. But am not able to plot the chart by that data is not binding to the chart...it is showing like [Object object]..
Any one help me please...
I have tried this..

XML
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>


<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <title>Flot Examples: AJAX</title>
    <link href="scripts/examples.css" rel="stylesheet" type="text/css" />

    <!--[if lte IE 8]><script language="javascript" type="text/javascript" src="../../excanvas.min.js"></script><![endif]-->
    <script language="javascript" type="text/javascript" src="scripts/jquery.js"></script>
    <script language="javascript" type="text/javascript" src="scripts/jquery.flot.js"></script>
    <script type="text/javascript">



    $(function() {

        var source = {};

        $.ajax({
            type: 'POST',
            //data:'format=json',
            dataType: 'jsonp',
            async: false,
            url: 'http://localhost:30082/restfrchrst.svc/GetChartdetails',
            //cache: false,
            contentType: 'application/json',
            data: "",
            success: function (data) {
                alert(data);
                source = data;
                //source = $.parseJSON(data);

            },
            error: function (err) {
                alert(err);
            }


        });
        function fn()
        {
            var retdata;
            var arr1 = [];
            retdata = source;
            arr1.push(retdata);
        }

        var plot = $.plot("#placeholder", [
            //{ data: sin, label: "sin(x)"},
            //{ data: cos, label: "cos(x)"}
           { data:  arr1}
        ], {
            series: {
                lines: {
                    show: true
                },
                points: {
                    show: true
                }
            },
            grid: {
                hoverable: true,
                clickable: true
            },
            yaxis: {
                min: 200,
                max: 600
            }

        });

        $("<div id='tooltip'></div>").css({
            position: "absolute",
            display: "none",
            border: "1px solid #fdd",
            padding: "2px",
            "background-color": "#fee",
            opacity: 0.80
        }).appendTo("body");

        $("#placeholder").bind("plothover", function (event, pos, item) {

            if ($("#enablePosition:checked").length > 0) {
                var str = "(" + pos.x.toFixed(2) + ", " + pos.y.toFixed(2) + ")";
                $("#hoverdata").text(str);
            }

            if ($("#enableTooltip:checked").length > 0) {
                if (item) {
                    var x = item.datapoint[0].toFixed(2),
                        y = item.datapoint[1].toFixed(2);

                    $("#tooltip").html(item.series.label + " of " + x + " = " + y)
                        .css({top: item.pageY+5, left: item.pageX+5})
                        .fadeIn(200);
                    }
                else {
                    $("#tooltip").hide();
                }
            }
        });

        $("#placeholder").bind("plotclick", function (event, pos, item) {
            if (item) {
                $("#clickdata").text(" - click point " + item.dataIndex + " in " + item.series.label);
                plot.highlight(item.series, item.datapoint);
            }
        });

        // Add the Flot version string to the footer

        $("#footer").prepend("Flot " + $.plot.version + " &ndash; ");
    });

    </script>
</head>
<body>
    <div id="header">
        <h2>Interactivity</h2>
    </div>

    <div id="content">

        <div class="demo-container">
            <div id="placeholder" class="demo-placeholder"></div>
        </div>

        <p>One of the goals of Flot is to support user interactions. Try pointing and clicking on the points.</p>

        <p>
            <label><input id="enablePosition" type="checkbox" checked="checked"></input>Show mouse position</label>
            <span id="hoverdata"></span>
            <span id="clickdata"></span>
        </p>

        <p>A tooltip is easy to build with a bit of jQuery code and the data returned from the plot.</p>

        <p><label><input id="enableTooltip" type="checkbox" checked="checked"></input>Enable tooltip</label></p>

    </div>

    <div id="footer">
        Copyright &copy; 2007 - 2013 IOLA and Ole Laursen
    </div>

</body>
</html>




public class restfrchrst : Irestfrchrst
    {
        SqlConnection con = new SqlConnection("Data Source=10.90.90.100;Initial Catalog=SGT_Test;User ID=sgtuser;Password=sgtuser");
        List c = new List();
        public List GetChartdetails() 
        {
            con.Open();
            SqlCommand cmd = new SqlCommand("select * from charts",con);
            SqlDataReader dr = cmd.ExecuteReader();
            if (dr.HasRows)
            {
                while (dr.Read())
                {
                    c.Add(new chart { marks = Convert.ToInt32(dr[0]), progress = Convert.ToDecimal(dr[1]) });
                }
            }
            con.Close();
            return c;
        }

svc.cs 

isvc is:
 public interface Irestfrchrst
    {
        [OperationContract]
        [WebGet(UriTemplate = "GetChartdetails", ResponseFormat = WebMessageFormat.Json)]
        
        List GetChartdetails();
    }
    [DataContract]

    public class chart
    {
          [DataMember]
        public int marks { get; set; }
           [DataMember]
        public decimal progress { get; set; }
    } 
}
Posted
Updated 1-Feb-14 1:21am
v2
Comments
JoCodes 1-Feb-14 6:31am    
Whats the error?
Member 10324774 1-Feb-14 6:43am    
no error but not plotting the data. I have written alert(data); then it is showing [Object object] [Object object] [Object object] [Object object] [Object object] [Object object] like this but don't know y?

1 solution

Please add the document.ready() method is as below.

XML
<script type="text/javascript">
$(document).ready(function() {
    //your code here
});
</script>


UPDATE


C#
[Serializable]
public class chart
  {
        [DataMember]
        public int marks { get; set; }

        [DataMember]
        public decimal progress { get; set; }
    }


Try to use associative array :

C#
var value = { "aaa": "111", "bbb": "222", "ccc": "333" };
var blkstr = [];
$.each(value, function(idx2,val2) {
     var str = idx2 + ":" + val2;
     blkstr.push(str);
});
alert(blkstr.join(", "));


For more info : Associative array
 
Share this answer
 
v3
Comments
Member 10324774 1-Feb-14 6:57am    
no change..not binding the data..
Sampath Lokuge 1-Feb-14 7:02am    
Can you put the code snippet for 'GetChartdetails' method ?
Member 10324774 1-Feb-14 7:17am    
public class restfrchrst : Irestfrchrst
{
SqlConnection con = new SqlConnection("Data Source=10.90.90.100;Initial Catalog=SGT_Test;User ID=sgtuser;Password=sgtuser");
List<chart> c = new List<chart>();
public List<chart> GetChartdetails()
{
con.Open();
SqlCommand cmd = new SqlCommand("select * from charts",con);
SqlDataReader dr = cmd.ExecuteReader();
if (dr.HasRows)
{
while (dr.Read())
{
c.Add(new chart { marks = Convert.ToInt32(dr[0]), progress = Convert.ToDecimal(dr[1]) });
}
}
con.Close();
return c;
}

svc.cs

isvc is:
public interface Irestfrchrst
{
[OperationContract]
[WebGet(UriTemplate = "GetChartdetails", ResponseFormat = WebMessageFormat.Json)]

List<chart> GetChartdetails();
}
[DataContract]

public class chart
{
[DataMember]
public int marks { get; set; }
[DataMember]
public decimal progress { get; set; }
}
}
Sampath Lokuge 1-Feb-14 7:27am    
Please check my 'UPDATE' section.Add [Serializable] attribute too.
Member 10324774 1-Feb-14 8:11am    
hi
I updated my code , wcf rest service data is taken in an array but not able to plot chart...
please check..
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>


<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Flot Examples: AJAX</title>
<link href="scripts/examples.css" rel="stylesheet" type="text/css" />

<!--[if lte IE 8]><script language="javascript" type="text/javascript" src="../../excanvas.min.js"></script><![endif]-->
<script language="javascript" type="text/javascript" src="scripts/jquery.js"></script>
<script language="javascript" type="text/javascript" src="scripts/jquery.flot.js"></script>
<script type="text/javascript">



$(document).ready(function () {

var source = {};

$.ajax({
type: 'POST',
//data:'format=json',
dataType: 'jsonp',
async: false,
url: 'http://localhost:30082/restfrchrst.svc/GetChartdetails',
//cache: false,
contentType: 'application/json',
data: "",
success: function (data) {

var arrayReturn = data;
for (var i = 0, len = arrayReturn.length; i < len; i++) {

var chart = arrayReturn[i];
var arr = [];
arr.push([chart.marks, chart.progress]);
//alert(arr);
//alert(chart.progress);
}
//source = $.parseJSON(data);

},
error: function (err) {
alert(err);
}


});

var plot = $.plot("#placeholder", [{ data: arr }], {
series: {
lines: {
show: true
},
points: {
show: true
}
},
grid: {
hoverable: true,
clickable: true
},
yaxis: {
min: 200,
max: 600
}

});



$("<div id='tooltip'></div>").css({
position: "absolute",
display: "none",
border: "1px solid #fdd",
padding: "2px",
"background-color": "#fee",
opacity: 0.80
}).appendTo("body");

$("#placeholder").bind("plothover", function (event, pos, item) {

if ($("#enablePosition:checked").length > 0) {
var str = "(" + pos.x.toFixed(2) + ", " + pos.y.toFixed(2) + ")";
$("#hoverdata").text(str);
}

if ($("#enableTooltip:checked").length > 0) {
if (item) {
var x = item.datapoint[0].toFixed(2),
y = item.datapoint[1].toFixed(2);

$("#tooltip").html(item.series.label + " of " + x + " = " + y)
.css({top: item.pageY+5, left: item.pageX+5})
.fadeIn(200);
}
else {
$("#tooltip").hide();
}
}
});

$("#placeholder").bind("plotclick", function (event, pos, item) {
if (item) {
$("#clickdata").text(" - click point " + item.dataIndex + " in " + item.series.label);
plot.highlight(item.series, item.datapoint);
}
});

// Add the Flot version string to the footer

$("#footer").prepend("Flot " + $.plot.version + " – ");
});
});
</script>
</head>
<body>
<div id="header">

Interactivity


</d

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900