Click here to Skip to main content
15,891,372 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have JSON string array in the following format:

JavaScript
{
   [
      {
         "BillingMonth":"11",
         "BillingYear":"2016",
         "Volume":"72",
         "BillingMonthName":"November",
         "BillingProduct":"Product1"
      },
      {
         "BillingMonth":"11",
         "BillingYear":"2016",
         "Volume":"617",
         "BillingMonthName":"November",
         "BillingProduct":"Product2"
      },
      {
         "BillingMonth":"11",
         "BillingYear":"2016",
         "Volume":"50",
         "BillingMonthName":"November",
         "BillingProduct":"Product3"
      },
      {
         "BillingMonth":"11",
         "BillingYear":"2016",
         "Volume":"617",
         "BillingMonthName":"November",
         "BillingProduct":"Product4"
      },
      {
         "BillingMonth":"12",
         "BillingYear":"2016",
         "Volume":"72",
         "BillingMonthName":"December",
         "BillingProduct":"Product1"
      },
      {
         "BillingMonth":"12",
         "BillingYear":"2016",
         "Volume":"72",
         "BillingMonthName":"December",
         "BillingProduct":"Product2"
      },
      {
         "BillingMonth":"12",
         "BillingYear":"2016",
         "Volume":"72",
         "BillingMonthName":"December",
         "BillingProduct":"Product3"
      },
      {
         "BillingMonth":"12",
         "BillingYear":"2016",
         "Volume":"72",
         "BillingMonthName":"December",
         "BillingProduct":"Product4"
      }
   ]
}

There Are diffrent products(Product1,Product2,Product3,Product4) sold in months of november and december each.

I have to create one stackcolumn chart or stackedBar chart in canvas js with legends carrying products name.

I want to prepare four diffrent datapoints array to assign to display one column for each month november and december with diffrent products in diffrent colour holding in one column dynamically.

Please help me to write javascript to achieve the scenario.

Thanks!!!

What I have tried:

JavaScript
Result = JSON.parse(Result.d);
                    var dataPoints = [];
                    var dataPointsnew = [];
                    var dataPointsNew1 = [];
                    var dataPointsNew2 = [];
                    for (var i = 0; i <= Result.length - 1; i++) {
                        var series = new Array(dataPoints.push({ x: Result[i].BillingMonthName + '-' + Result[i].BillingYear, y: Result[i].Volume }));
                    }

                    dataPointsnew = dataPoints.chunk(8);

                    dataPointsNew1 = dataPointsnew[0][1];

                    dataPointsNew2 = dataPointsnew[1];
Posted
Updated 3-Mar-17 18:37pm
v3

1 solution

Try This code. It Will Work.

JavaScript
$(document).ready(function () {
        $.ajax({
            type: "POST",
            url: "Pagename/Data",
            data: "{}",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function (r) {
                var Result = r.d;
                var Remove1 = Result.substring(2, Result.length); // Remove First { [
                var Remove2 = Remove1.slice(2, Remove1.length); // Remove Last ] }
                var split1 = Remove2.split('},'); //Split By },

                var dataPoints = [];
                for (var i = 0; i <= split1.length - 1; i++) {

                    var InnerSplit = split1[i].split(','); //Split by , for Billing details 
                    var TempBillingName = InnerSplit[3].split(':'); //split for billing name ' 0-'BillingMonthName': 1 -'November''
                    var BillingName = TempBillingName[1]; // 'November' MonthName
                    var TempBillingYear = InnerSplit[1].split(':');
                    var BillingYear = TempBillingYear[1]; //Month Year
                    var TempVolumn = InnerSplit[2].split(':');
                    var Volumn = TempVolumn[1]; //Volumn

                    var series = new Array(dataPoints.push({ x: BillingName + '-' + BillingYear, y: Volumn })); // Finaly Data Add in dataPionts
                }
            }
        });
    });
 
Share this answer
 
v3

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