javascript - JSON-Object as string: Use of concat(), eval() and/or JSON.parse()? -


What do I do: I display a diagram using the high and best code style Want to

Problem: My code is not debugable / because I store it in the variable.

OK, I have a Javascript object that contains all the information I need to use later (categories, value of Y axis and series itself, which is divided into positive, neutral and negative).

  var testDia = {name: 'testname', 'chartcategories': ['golf', 'polo', 'paas'], 'chartreseries': {'positive': [[y : 341, url: 'http: //golf.com? Q = positively '}, {y: 487, Url:' http: //polo.com? Q = positiv '}, {y: 180, url:' http: //passat.com? Q = positiv '}],' Neutral ': [[y: 12], Url:' http: //golf.com? Q = neutral '}, {y: 3, url:' http: //polo.com? Q = neutral '}, {y: 9, url:' http: //passat.com?q=neutral '}],' negative ': [[y: 222, url:' http: //golf.com? Q = negative '}, {y: 115, url:' http: //polo.com? Q = negative '}, [Y: 321, url:' http: //passat.com? Q = negative '}]}}  

My view is loop on categories and different from positive, neutral and negative strings which I later evaluate in higher chairs.

  var allPositiveData = ''; Var allNeutralData = ''; Var allNegativeData = ''; {Var diaPositive = series ['positive'] [i] ['y'] for (var i = 0; i & lt; categories; length; i ++); Var diaNeutral = series ['neutral'] [i] ['y']; Var diaNegative = series ['negative'] [i] ['y']; UrlPositive = Series ['Positive'] [i] ['url']; UrlNeutral = Series ['Neutral'] [i] ['url']; UrlNegative = Series ['Negative'] [i] ['url']; AllPositiveData + = "{'y':" + diaPositive + ", 'url': '" + urlPositive + "'},"; AllNeutralData + = "{'y':" + diagonal + ", 'url': '" + urlNeutral + "'},"; AllNegativeData + = "{'y':" + ડાયનેગેટિવ + ", 'url': '" + urlNegative + "'},"; } // end all of the loop positiveData = eval ("[" + allPositiveData.slice (0, -2) + "]"); AllNeutralData = eval ("[" + allNeutralData.slice (0, -2) + "]"); AllNegativeData = eval ("[" + allNegativeData.slice (0, -2) + "]");  

High-Setup

  newChart = new highchart. Chart ({chartereTo: 'container', type: 'column'}, [. ..] // Excluding the remaining setup series: [{Name: 'positive', data: sub posititude}, {name: Neutral ', data: allNeutralData}, {name:' Negative ', data: allNegativeData}]});  

I think there are a few ways to achieve what I want, but I want to know a better (perhaps object-oriented) solution.

I think you are trying to do something which you Have done already.

You want to display three series from your JSON, for related categories, then for:

Your code:

  var categories = TestDia ['chart categories']; Var series = testDia ['charteries'];  

Great! Now use that variable:

  newChart = new highcharts.Chart ({chart: {rendereTo: portletContainer, type: 'column'}, ... xXx: {categories: categories, labels : {Rotation: -45, align: 'right',}}, ... series: [{name: 'positive', data: chain.postitive // ​​testidhyart.series is same}, {name: 'neutral ', Data: series.neutral}, {name:' negative ', data: series.negative}]});  

More work demo:


Comments