4

I try to display pie chart according to date .. when I select value from drop-down and dates from calendar, first I try to display data in alert box.. so data is successfully shown in alert box, and now I want to show data in pie chart, and I try this but chart does not display:

UPDATE CODE


    <script type="text/javascript">


     var strArray = [['sfdsdfLi', 9], ['Kiwsdfi', 3], ['Mixesdfd nuts', 1], ['Oranges', 6], ['Grapes (bunch)', 1]];
     $(function () {
         $('[ID*=search_data]').on('click', function () {
             var from = $('[ID*=fromdate]').val();
             var to = $('[ID*=todate]').val();
             var reg = $('[id*=regiondrop] option:selected')[0].value; // changed this to .val()
             var obj = {};
             obj.fdate = from;
             obj.tdate = to;
             obj.region = reg;
             GetData(obj);
             return false;
         });
     });
     function GetData(obj) {

         $.ajax({
             type: "POST",
             contentType: "application/json; charset=utf-8",
             url: "WebForm1.aspx/GetVo",
             data: JSON.stringify(obj),

             dataType: "json",
             async: true,
             cache: false,
             success: function (result) {

                 alert(result.d);
                 alert('u');
                 // start
                 strArray = result.d;
                 var myarray = eval(strArray);
                 DreawChart(myarray);

                 alert('uu');

                  } ,
                  error: function (result) {
                   alert("Error");
                 }
         });
}
function DreawChart(result) {
    $('#container').highcharts({
        chart: {
            type: 'pie',
            options3d: {
                enabled: true,
                alpha: 45
            }
        },
        title: {
            text: 'Contents of Highsoft\'s weekly fruit delivery'
        },
        subtitle: {
            text: '3D donut in Highcharts'
        },
        plotOptions: {
            pie: {
                innerSize: 100,
                depth: 45
            }
        },
        series: [{
            name: 'Delivered amount',
            data: result


        }]
    });

}
 </script>
SUPER_USER
  • 275
  • 3
  • 16
  • 1
    The problem is that you use strings and eval function. Replace that with the JSON. So in your back-end return JSON and use the $.getJSON() in JS. You avoid problem with types of data. Meantime validate your code by jslint, because there some syntax errors. – Sebastian Bochan Jun 28 '16 at 08:14
  • i also remove eval and write this .. name: 'Delivered amount', colorByPoint: true, data: JSON.parse(myarray) but this also not work – SUPER_USER Jun 28 '16 at 08:20
  • and how i replace with json? – SUPER_USER Jun 28 '16 at 08:23
  • like this ? data: JSON.stringify({ region: $('#regiondrop').val(), fdate: $('#fromdate').val(), tdate: $('#todate').val() }), – SUPER_USER Jun 28 '16 at 08:24
  • @SebastianBochan ????? – SUPER_USER Jun 30 '16 at 05:18
  • Your app / script in this url WebForm1.aspx/GetVo shoudl return JSON and then you should use $.getJSON(), not use eval / stringify etc. – Sebastian Bochan Jun 30 '16 at 07:41
  • @SebastianBochan GETVO return data like this [['DPSB',1],['mv',11],['PSB',8]] .. for test i get chart with same data on simple button click but when i use drop-down and calendar value(from-date & to date) and then when i click on button then i successfully get data in alert box but i did not get chart – SUPER_USER Jun 30 '16 at 07:48

1 Answers1

4

Here's an updated version of your fiddle with the working chart: https://jsfiddle.net/brightmatrix/bkbdna6d/12/

Four items to note:

1) You had your Highcharts libraries called before your jQuery scripts were called. They should be ordered as follows:

<link href="//code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css">
<link href="/resources/demos/style.css">
<script src="//code.jquery.com/jquery-1.10.2.js"></script>
<script src="//code.jquery.com/ui/1.11.4/jquery-ui.js"></script>
<!-- moved the Highcharts scripts AFTER the jQuery scripts are called -->
<script src="https://code.highcharts.com/highcharts.js"></script>
<script src="https://code.highcharts.com/modules/exporting.js"></script>

2) You were missing an ID for your dropdown menu, so the value wasn't being retrieved by your first function.

In your HTML:

<select id="regiondrop">  <!-- added id here; needed in your first function call  -->
    <option value="UK">UK</option>
    <option value="France">France</option>
    <option value="London">London</option>
    <option value="Paris">Paris</option>
</select>

In your Javascript code:

$(function () {
    $('[ID*=search_data]').on('click', function () {
        var from = $('[ID*=fromdate]').val();
        var to = $('[ID*=todate]').val();
        var reg = $('[ID*=regiondrop]').val();      // changed this to .val()
        var obj = {};
        obj.fdate = from;
        obj.tdate = to;
        obj.region = reg;
        GetData(obj);
        return false;
    });
});

3) As Sebastian Bochan noted, the values for your chart were being read as a string, not an array. This is how it should appear. Notice there are no quotes around the entire array.

// removed quotes from this line to make it a regular array, not a string
var strArray = [['sfdsdfLi', 9],['Kiwsdfi', 3],['Mixesdfd nuts', 1],['Oranges', 6],['Grapes (bunch)', 1]];

Now, once you click the "Search Data" button, the donut chart appears:

enter image description here

4) Upon further review, I think the JSON.stringify(obj) line is what's causing your data not to appear. According to the Mozilla Developer network:

The JSON.stringify() method converts a JavaScript value to a JSON string, optionally replacing values if a replacer function is specified, or optionally including only the specified properties if a replacer array is specified.

(see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify)

That's the key problem I see: your data is being returned as a string, not an array, so your chart isn't getting the data in the right format. Instead, try using JSON.parse():

$.ajax({
    type: "POST",
    contentType: "application/json; charset=utf-8",
    url: "WebForm1.aspx/GetVo",  // <--- this is a relative URL and will not work in your fiddle; update your source code with the full URL instead
    data: JSON.stringify(obj),   // <--- change this to JSON.parse(obj)
    dataType: "json",
    async: true,
    cache: false,
    // ... the rest of your code ...

This question elsewhere on Stack Overflow might be helpful for you: Difference between JSON.stringify and JSON.parse

I hope all of this is helpful. If you have any questions, please leave a comment and I'll be glad to edit my answer as needed.

Community
  • 1
  • 1
Mike Zavarello
  • 3,514
  • 4
  • 29
  • 43
  • @SUPER_USER Thanks for your patience. The ASP code you added unfortunately doesn't help me with seeing the actual data returned to your `ajax` call. What I need is the full URL of `"WebForm1.aspx/GetVo"`. That way, I can run the data through a `getJSON()` function and see if there is anything there that's preventing your chat from drawing. – Mike Zavarello Jul 02 '16 at 09:44
  • full url means ? @Mike Zavarello – SUPER_USER Jul 02 '16 at 09:48
  • @SUPER_USER For example, I need something like `http://www.mywebsite.com/WebForm1.aspx/GetVo`. Or, any other way to see the real data you're getting back. Your sample was helpful, but you had asked about parsing the data using `getJSON()`, as Sebastian Bochen mentioned in their comments. – Mike Zavarello Jul 02 '16 at 09:50
  • CHECK my updated script code when i run this script code then pop up appear with error message.. whereas data2 return me correct data – SUPER_USER Jul 02 '16 at 09:51
  • @SUPER_USER I've updated my answer; see item #4 about `JSON.stringify()`. I think that may help you further. – Mike Zavarello Jul 02 '16 at 10:01
  • now i getting my data in alert box but still chart not display :( and please check my latest fiddle – SUPER_USER Jul 02 '16 at 10:05
  • @SUPER_USER I've reviewed your updated fiddles. The sample in your comment above works fine, and always has. In your code ... this one: https://jsfiddle.net/bkbdna6d/5/ ... you should try changing `JSON.stringify()` to `JSON.parse()`, Please know that this **will not work** in your fiddle right now with your live data because you're pulling data from a **relative** URL `WebForm1.aspx/GetV` ... you should try changing this in your **actual** web code where you plan to use this chart. – Mike Zavarello Jul 02 '16 at 10:14
  • have you edit something in this fiddle ?jsfiddle.net/bkbdna6d/5 – SUPER_USER Jul 02 '16 at 10:16
  • when i try JSON.parse() then nothing happens. even alert box is also not appear – SUPER_USER Jul 02 '16 at 10:24
  • 1
    @SUPER_USER My friend, I've tried my very best to help answer your question to the best of my ability, but I don't think there is any more that I can do. I've updated my answer a final time with a code edit at item #4. You are more than welcome to remove the bounty if you feel what I've written does not resolve your issue. – Mike Zavarello Jul 02 '16 at 10:26
  • 3
    @SUPER_USER: When someone tells you they're done helping you, you do not respond to them further. First of all, it's futile; second, it's exceedingly rude of you to continue bothering them about an issue they no longer wish to be concerned with. Plus you had an entire *week* to wait for a suitable answer to come along - if you weren't satisfied with this answer, why did you award the bounty right away? This has been a colossal waste of time not just for you, but for Mike, and to continue demanding their attention *two weeks after the fact* is extraordinarily selfish of you. – BoltClock Jul 14 '16 at 12:03