0

I have the following code -

C#

string data = string.Format("[{0}]", string.Join(",", listOfInts));
string script = string.Format("chart('{0}','{1}','{2}','{3}','{4}','{5}','{6}');",
startYear, startMonth, startDay, startHour, startMinute, chartTitle, data);

 ScriptManager.RegisterStartupScript(this, GetType(), "chart", script, true);

Javascript

series: [{
             type: 'area',
             name: 'Count:',
             pointInterval:  3600 * 1000,
             pointStart: Date.UTC(startYear, startMonth, startDay, startHour, startMinute),
             data: intsAsJSArray

        }]

Where inrtAsJSArray = "[1,2,3,4,5,6,7]".

This breaks the graphing function and I have realised that it is due to data being in the wrong format.

However if I hard code the data section like -

series: [{
             type: 'area',
             name: 'Count:',
             pointInterval:  3600 * 1000,
             pointStart: Date.UTC(startYear, startMonth, startDay, startHour, startMinute),
             data: [1,2,3,4,5,6,7]

        }]

This works, how can I reconfigure intASJarray to be accepted into the series?

Ebikeneser
  • 2,582
  • 13
  • 57
  • 111

3 Answers3

1

you can parse the string to an array

var str = "[1,2,3,4,5,6,7]";

var trim = str.replace(/[\[\]]/g, "");

var data = trim.split(",");

data = data.map(function (d) {
    return +d;
});

console.log(data);

here is the demo jsfiddle

Hasan Fathi
  • 5,610
  • 4
  • 42
  • 60
qiu-deqing
  • 1,323
  • 7
  • 13
0

I'm not familiar with RegisterStartupScript but the crux of it is that your intASJarray ends up a javascript string instead of an array.

Try:

string script = string.Format("chart('{0}','{1}','{2}','{3}','{4}','{5}',{6});",
   startYear, startMonth, startDay, startHour, startMinute, chartTitle, data);

Notice I've dropped the quotes on the string format {6} argument. You might need to do this for the data parts as well.

Mark
  • 106,305
  • 20
  • 172
  • 230
0

If you target browsers that support JSON.parse(), you can parse the string like this:

  series: [{
         type: 'area',
         name: 'Count:',
         pointInterval:  3600 * 1000,
         pointStart: Date.UTC(startYear, startMonth, startDay, startHour, startMinute),
         data: JSON.parse(intsAsJSArray)

    }]

Working sample: http://jsfiddle.net/MnsuS/

Community
  • 1
  • 1
Hamund
  • 706
  • 11
  • 19