Solved

Need More Efficient Javascript Code

Posted on 2014-03-15
3
264 Views
Last Modified: 2014-03-15
I have an application that builds 3 AmCharts, each with three series apiece.  I currently have an AJAX call that returns:
[{"CashPrice":"190.30","strDate":"2014-03-15","ChartSet":"1","ChartSeries":"1","LocName":""},{"CashPrice":"190.82","strDate":"2014-03-15","ChartSet":"1","ChartSeries":"2","LocName":""},{"CashPrice":"191.53","strDate":"2014-03-15","ChartSet":"1","ChartSeries":"3","LocName":""},{"CashPrice":"191.51","strDate":"2014-03-22","ChartSet":"1","ChartSeries":"3","LocName":""},{"CashPrice":"190.80","strDate":"2014-03-22","ChartSet":"1",

Open in new window


Where ChartSet is the chart (1-3) and the ChartSeries is the series for chart[x].  AmCharts requires the data to be in this format:
[            {
                    "series1": "xxx",
                    "series2": xxx,
                    "series3": xxx,
                    "date": "date"
                },
                {
                    "series1": "xxx",
                    "series2": xxx,
                    "series3": xxx,
                    "date": "date"
                },....
}

Open in new window


Currently I loop through the return data and put the data into nine different arrays (3 charts x 3 Series):
 
$.each(chartData, function (index, array) {
                  var chartSet = parseInt(array.ChartSet);
                  var series = array.ChartSeries;

                  //Chart 1
                  if (chartSet == 1) {
                      if (series == 1) {
                          arrChart1_Series1.push({
                              date: array.strDate,
                              value: array.CashPrice
                          });
                      } // End Series 1
...Code repeated 8 more times with different array names

Open in new window


Than I have to combine the corresponding arrays (three for each chart) into one array:
 //Chart 1
              for(var i=0; i<arrChart1_Series1.length; i++){
                  chartData1.push({
                      series1: arrChart1_Series1[i].value,
                      series2:arrChart1_Series2[i].value,
                      series3: arrChart1_Series3[i].value,
                      date: arrChart1_Series1[i].date
                  });
...Code Repeated two more times

Open in new window

This obvious takes a lot of time (approx. 6 seconds) and there has to be a better way to do it.  Any body have any better idea's?
0
Comment
Question by:badrhino
  • 2
3 Comments
 
LVL 82

Accepted Solution

by:
leakim971 earned 500 total points
ID: 39931534
Yes, I've an idea but you're not going to like it.
I'm not sure why people want to give such type of task to my lovely language.
Why not using your server with the hundred of Xeon 8 cores this job?

So my idea is : ask the server to send you the json in the right format.
0
 
LVL 1

Author Comment

by:badrhino
ID: 39931630
Leakim,

I agree that what I'm trying to do isn't right (that's why I'm here :) ).  If you don't see a better approach in javascript to this , I'll do it server side.   I typically do server side code so I didn't know if I was missing something obvious...
0
 
LVL 82

Expert Comment

by:leakim971
ID: 39931635
No, I'm not here to judge.
Sometimes you can't have the right format (json from third party) so there's not other ways to format at the client side. But when you can, just do it.
It always the same process, do a loop over the data and create the new chart.
I don't see no better approach which really make a difference with your current code.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

947 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now