0

I have problem with adding points to chart (CanvasJS).

here how I store points manually but I need to add it dynamically

var dps = [{ x: 1, y: 10 }, { x: 2, y: 10 }, { x: 3, y: 10 }, { x: 4, y: 10 }, { x: 5, y: 10 }, { x: .., y: ..}, ... .... ...];   //dataPoints.

How Can I add to dps points which are in my ViewData["XPoints"] and ViewData["YPoints"] ?

I was thinking about foreach but I don't know how to use it.

@foreach (var pk in ViewData["XPoints"] as List<int>)
{ 
  @pk
}  

EDIT:

Ok maybe it's not so clear.

I have got two ViewData with X and Y points. ViewData.["XPoints"] and ViewData.["YPoints"] which are storing numbers.

Now I have to make an array like DPS using this numbers.

var DPS = [{x: .., y: ...},{x: .., y: ...},{x: .., y: ...},{x: .., y: ...},.....].

How Can I achieve it?

EDIT2

I solved it, here is How I did it

 var myArrayX = [];
        var myArrayY = [];
        var arry = [];
        @foreach (var st in ViewData["XPoints"] as List<int>)
        {
        @:myArrayX.push(@st);
                    }

        @foreach (var st in ViewData["YPoints"] as List<int>)
        {
        @:myArrayY.push(@st);
                        }

        for (var i = 0; i < myArrayX.length; i++) {
            arry.push({ x: myArrayX[i], y: myArrayY[i], });

        }
Icet
  • 678
  • 2
  • 13
  • 31
  • It's not very clear what you want to accomplish, try to be a bit more detailed. You have a list of coordinate points in C# and want that translated into a list in JavaScript to be processed on the client side? – Jim Aho Feb 15 '16 at 12:59
  • Ok I have got ViewData with some numbers. I want to create array like dps and use in this array numbers from my ViewData.XPoints and ViewData.YPoints. @JimAho – Icet Feb 15 '16 at 13:03
  • Possible duplicate of [Razor MVC Populating Javascript array with Model Array](http://stackoverflow.com/questions/23781034/razor-mvc-populating-javascript-array-with-model-array) – Jim Aho Feb 15 '16 at 13:13
  • @JimAho it's not duplicate – Icet Feb 15 '16 at 13:16
  • To me it seems you're essentially trying to populate a JavaScript array with some values from the server in a Razor page. This is very much the same problem as is explained in the link I provided. It's not neccessarily the exact same format on the data, but the main problem is the same? – Jim Aho Feb 15 '16 at 13:19
  • I just don't know how to add my numbers from ViewDatas to array so the array should looks like [{x: .., y: ...},{x: .., y: ...},{x: .., y: ...},{x: .., y: ...},.....]. @JimAho – Icet Feb 15 '16 at 13:23
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/103489/discussion-between-icet-and-jim-aho). – Icet Feb 15 '16 at 13:26

0 Answers0