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], });

        }
7
  • 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? Commented Feb 15, 2016 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 Commented Feb 15, 2016 at 13:03
  • Possible duplicate of Razor MVC Populating Javascript array with Model Array Commented Feb 15, 2016 at 13:13
  • @JimAho it's not duplicate Commented Feb 15, 2016 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? Commented Feb 15, 2016 at 13:19

0

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.