Solved

Need More Efficient Javascript Code

Posted on 2014-03-15
3
263 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
Comment Utility
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
Comment Utility
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
Comment Utility
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
This article discusses how to create an extensible mechanism for linked drop downs.
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…

772 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

11 Experts available now in Live!

Get 1:1 Help Now