0

I am trying to add the data into below function using jQuery (or highcharts). The question is how to embed the data into the javascript code without using eval since I will have to write all the code as string?

function pie(data)
{
  $(function () {
    $('#renderingdiv').highcharts({
        chart: {
            plotBackgroundColor: null,
            plotBorderWidth: null,
            plotShadow: false
        },
        title: {
            text: 'Browser market shares at a specific website, 2010'
        },
        tooltip: {
            pointFormat: '{series.name}: <b>{point.percentage:.1f}%</b>'
        },
        plotOptions: {
            pie: {
                allowPointSelect: true,
                cursor: 'pointer',
                dataLabels: {
                    enabled: true,
                    color: '#000000',
                    connectorColor: '#000000',
                    format: '<b>{point.name}</b>: {point.percentage:.1f} %'
                }
            }
        },
        series: [**data**]
    });
});
};

data looks like:

{\
            type: 'pie',\
            name: 'Statuses',\
            data: [\
                [WSCH,   377]\
        ,\
                [WMATL,   4]\
        ,\
                [WAPPR,   349]\
        ,\
                [NCOMP,   3]\
        ,\
                [INPRG,   56]\
        ,\
                [COMP,   18]\
        ,\
                [CLOSE,   697]\
        ,\
                [APPR,   420]\
        \
            ]\
        }

Any idea please?

10
  • you have any framework behind it ? Commented Sep 4, 2013 at 23:08
  • Any reason on why you dont want to use eval() ? just asking out of curiosity Commented Sep 4, 2013 at 23:09
  • 4
    Is data supposed to be a string literal? Then use JSON.parse instead of eval. If not, what are all those backslashes doing there? And how did you got them there in the first place? Remove them and you could just use the markup as a JS object literal. Commented Sep 4, 2013 at 23:14
  • 1
    @john: yes, special framework. The question, in general, how to embed a variable in the code without using eval? Commented Sep 4, 2013 at 23:18
  • @Raghu: eval is not preferable as data may contain some characters plus it will require lot of coding to break lines with \. Commented Sep 4, 2013 at 23:19

1 Answer 1

1

This quick and dirty function converts your data to valid JSON and returns an object.

function parseData(data) {    
    data = data
        // remove \+line endings
        .replace(/\[\n\r]+/g, '') 
        // insert double quotes for keys
        .replace(/([\[{,])\s*(\w+)([,:])/g, '$1"$2"$3') 
        // replace values single quotes with double
        .replace(/(:)\s*'(\w+)\s*'/g, '$1"$2"'); 
    return JSON.parse(data);
}

Of course you should improve this to handle corner cases.

DEMO: http://jsfiddle.net/qq98D/2/ (result in console output)

But this is just a workaround. The real solution is to change the server output to return valid JSON.

Result (JSON encoded):

{"type":"pie","name":"Statuses","data":[["WSCH",377],["WMATL",4],["WAPPR",349],["NCOMP",3],["INPRG",56],["COMP",18],["CLOSE",697],["APPR",420]]}

Sign up to request clarification or add additional context in comments.

1 Comment

Yes but my issue is not with converting to JSON. I need the other format of data as it was shown above. I want a mechanism of adding the data string variable (which may include itself some js code) into the function as if it is part of the pie function code

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.