0

I try to get this format

[
    {
        name: 'Apple',
        data: [
            { name: 'April', y: 35 },
            { name: 'June', y: 1 }
        ]
    },
    {
        name: 'Orange',
        data: [
            { name: 'April', y: 7 },
            { name: 'July', y: 11 },
            { name: 'June', y: 7 },
            { name: 'May', y: 6 }
        ]
    }, 
    {
        name: 'Mango',
        data: [
            { name: 'July', y: 1 }
        ]
    }, 
    {
        name: 'Grapes', data: [
            { name: 'June', y: 1 }
        ]
    }, 
    {
        name: 'Cheery',
        data: [
            { name: 'June', y: 1 }
        ]
    }
];

So i try this code to get required above format

var mdata1 = new TrackDataEntities1().spdatasumry(frDate, to_Date, RegNo)
            .Select(s => new { s.Name }).ToArray().Distinct();

foreach (var c in mdata1) {
    voil.Add(c.Name);
}

var sdata = new TrackDataEntities1().spdatasumry(frDate, to_Date, RegNo)
            .Select(s => new {s.Name,s.Month,s.total }).ToArray();

string data2 = "[";
foreach(string vo in voil) {
    data2 += "{name:'" + vo + "',";
    data2 += "data:[";

    foreach(string mo in vmonths) {
        decimal tv = 0;

        try {
            tv = sdata.Single(t => t.Month == mo && t.Name == vo).total;
        } catch {
            tv = 0;
        }

        if (tv > 0) {
            data2 += "{name:'" + mo + "',y:" + tv + "},";
        }
    }
    data2 += "]},";
}

data2 = data2.Remove(data2.Length - 1);
data2 += "]";

but above code show format like this

In below code there is comma in that part {name:'June',y:1},]}, so i want to remove 2nd last ,]}, comma to get correct format

"[{name:'Apple',data:[{name:'April',y:35},{name:'June',y:1},]},
{name:'Orange',data:[{name:'April',y:7},{name:'July',y:11},{name:'June',y:7},{name:'May',y:6},]},
{name:'Mango',data:[{name:'July',y:1},]},
{name:'Grapes',data:[{name:'June',y:1},]},
{name:'Cheery',data:[{name:'June',y:1},]}]"
Grzegorz Blachliński
  • 5,132
  • 10
  • 13
user6628729
  • 323
  • 1
  • 7
  • 25
  • can you update the tags and remove highcharts from it? – Rahul Sharma Aug 24 '16 at 10:44
  • Here you can find info how you can check if you have last element in foreach loop: http://stackoverflow.com/questions/1070244/how-to-determine-the-first-and-last-iteration-in-a-foreach-loop you may change added strings depending if their are last. You can also again use data2 = data2.Remove(data2.Length - 1); just before you will add "]" bracket to your data. – Grzegorz Blachliński Aug 24 '16 at 10:57

1 Answers1

0

You are adding an extra comma at each node which is wrong. You will have to skip adding comma in last node something like this:

var i = 0;
foreach(string vo in voil) {
    data2 += "{name:'" + vo + "',";
    data2 += "data:[";

    var j = 0;
    foreach(string mo in vmonths) {
        decimal tv = 0;

        try {
            tv = sdata.Single(t => t.Month == mo && t.Name == vo).total;
        } catch {
            tv = 0;
        }

        if (tv > 0) {
            j++;
            if(vmonths.Length == j){
                data2 += "{name:'" + mo + "',y:" + tv + "}";
            }else{
                data2 += "{name:'" + mo + "',y:" + tv + "},";
            }
        }
    }
    i++;

    if(voil.Length == i){
        data2 += "]}";
    }else{
        data2 += "]},";
    }
}
d.coder
  • 1,988
  • 16
  • 23
  • this show error on length ... Error 3 'System.Collections.Generic.List' does not contain a definition for 'Length' and no extension method 'Length' accepting a first argument of type 'System.Collections.Generic.List' could be found (are you missing a using directive or an assembly reference?) – user6628729 Aug 24 '16 at 11:45
  • I am not sure what is the alternative method to get `list` size/length/count. May be you can easily find it by googling it. I just suggested you the required changes in your piece of code. – d.coder Aug 24 '16 at 11:50