Question Assign value

kattungen

Member
Joined
Sep 8, 2019
Messages
8
Programming Experience
Beginner
Hello!

I am using A graphing tool for c# called Live Charts and would like to assign values to the y-axis based on an array I already have. So It would basically look like this:

Code:
  new LineSeries
                {
                    Title = "Species 1",
                    Values = new ChartValues<int>{1,2,3,4}
                }
Where Values should be assigned to an array called Info.

Also how do I make the x-axis be dependent to a variable. So that if I want it to be 100 units long I wont have to do it by hand?

Code:
 cartesianChart1.AxisX.Add(new Axis
            {
                Title = "Day",
Labels = new[] { "0","1", "2", "3", "4", "5", "6", "7", "8", "9", "10" }
            });
Thanks in advance!
 

Skydiver

Well-known member
Joined
Apr 6, 2019
Messages
365
Location
Virginia Beach, VA
Programming Experience
10+
The following:
Code:
var series = new LineSeries
{
    Title = "Species 1",
    Values = new ChartValues<int>{1,2,3,4}
}
is called Object Initialization and Collection Initialization.

It is just syntactic sugar that is equivalent to:
Code:
var values = new ChartValues();
values.Add(1);
values.Add(2);
values.Add(3);
values.Add(4);

var series = new LineSeries();
series.Title = "Species 1";
series.Values = value;
Just apply your C# knowledge and about how loops and arrays work and you could simply call [il]Add()[/il] multiple times.

The documentation for Live Charts seems to be a bit lacking in some areas, but from the source code looks like ChartValues has a constructor that takes an IEnumerable[/il][/URL]. Since arrays, are [icode]IEnumerable, you can simply pass it in: new ChartValues<int>(myIntArray).
 

kattungen

Member
Joined
Sep 8, 2019
Messages
8
Programming Experience
Beginner
Doesn't work :/


Code:
            new LineSeries
                {
                    Title = "Species 1",
                    var values = new ChartValues(); values.Add(1); values.Add(2); values.Add(3); values.Add(4); var series = new LineSeries(); series.Title = "Species 1"; series.Values = value;
                }
               
            
            };
 

Skydiver

Well-known member
Joined
Apr 6, 2019
Messages
365
Location
Virginia Beach, VA
Programming Experience
10+
You have to break it out as multiple lines like I had above. Object initializers only support assignments, not statements.
 

kattungen

Member
Joined
Sep 8, 2019
Messages
8
Programming Experience
Beginner
That was just bad typing on my part in the post. The real code looks like this:

Code:
 new LineSeries
{
Title = "Species 1",
var values = new ChartValues();
values.Add(1);
values.Add(2);
values.Add(3);
            values.Add(4);
            var series = new LineSeries();
series.Title = "Species 1";
series.Values = value;
        }
[code]
And generates a lot of errors as well. Such as: 

Error CS1003 Syntax error, ','

and

Error CS0116 A namespace cannot directly contain members such as fields or methods
 

Skydiver

Well-known member
Joined
Apr 6, 2019
Messages
365
Location
Virginia Beach, VA
Programming Experience
10+
Try:
Code:
var values = new ChartValues<int>();
values.Add(1);
values.Add(2);
values.Add(3);
values.Add(4);

new LineSeries
{
    Title = "Species 1",
    Values = values
};
OR

Code:
var values = new ChartValues();
values.Add(1);
values.Add(2);
values.Add(3);
values.Add(4);

var series = new LineSeries();
series.Title = "Species 1";
series.Values = values;
 

kattungen

Member
Joined
Sep 8, 2019
Messages
8
Programming Experience
Beginner
None of the above works

Code:
cartesianChart1.Series = new SeriesCollection
            {

var values = new ChartValues();
values.Add(1);
values.Add(2);
values.Add(3);
            values.Add(4);
            var series = new LineSeries();
series.Title = "Species 1";
            series.Values = values;
It generates many errors. One of them is: Using the generic type 'ChartValues<T>' required 1 type arguments.
 

Skydiver

Well-known member
Joined
Apr 6, 2019
Messages
365
Location
Virginia Beach, VA
Programming Experience
10+
You cannot put multiple statements inside object initializers. So lines 4-11 need to go before line 1.

Out of curiosity, what book or tutorial are you using to learn C#? It seems like you just jumped straight into the language without going through the learning curve of learning parts of the language, and how to read and interpret error messages.
 

Sheepings

Senior Programmer
Joined
Sep 5, 2018
Messages
345
Location
UK
Programming Experience
10+
That's because you haven't a clue what you're doing. Before you try moving forward, I recommend you read some of Jon Skeet's books : C# In Depth C# in Depth: 9781933988368: Computer Science Books @ Amazon.com - you can find a few other editions here : Jon Skeet

It is one of my biggest pet-hates to see someone skipping fundamental chapters, and then expecting anyone to help them. If anyone helps you from here, they will not actually be helping you, but carrying you. And while I am not trying to discourage support on the forums, as I would never do that on learning platforms such as these. But in my opinion, It is rather pointless for anyone to help you at this point without you firstly fully understanding some of the basic utilisation concepts of the language.
 

kattungen

Member
Joined
Sep 8, 2019
Messages
8
Programming Experience
Beginner
Alright but I still get an error message: Using the generic type 'ChartValues<T>' required 1 type arguments.
Code:
 var values = new ChartValues();
values.Add(1);
values.Add(2);
values.Add(3);
values.Add(4);
var series = new LineSeries();
series.Title = "Species 1";
series.Values = values;
cartesianChart1.Series = new SeriesCollection
            {
               


            };
I just want to get this working, please.
 

Skydiver

Well-known member
Joined
Apr 6, 2019
Messages
365
Location
Virginia Beach, VA
Programming Experience
10+
Try passing in the type int as the generic type parameter.

What is baffling to me is that the error message is telling you exactly what is wrong. Don't you read the error message and try to research what it means and how to fix it?
 

Sheepings

Senior Programmer
Joined
Sep 5, 2018
Messages
345
Location
UK
Programming Experience
10+
Help yourself by learning the basics. I've given you some good books to read up there. What use is it if we do it for you? You learn nothing.

And frankly if you can't understand what you're being shown above or what the error message is telling you, that's your fault for skipping the basics, in which case I will show know remorse or sympathy.

An argument is expected to be passed between ChartValues / ( ), so next ask yourself what is an object type, and what type<T> of parameters could it be looking for?

Logically look and think : You have a class called ChartValues which that class requires a type<T> which is meant to be a collection of that type. So where are you specifying the type in new ChartValues(); ?
 

kattungen

Member
Joined
Sep 8, 2019
Messages
8
Programming Experience
Beginner
Alright the code runs but the output is not what I was expecting.

Code:
   var values = new ChartValues<int>();
values.Add(1);
values.Add(2);
values.Add(3);
values.Add(4);
var series = new LineSeries();
series.Title = "Species 1";
series.Values = values;
cartesianChart1.Series = new SeriesCollection
            {

new LineSeries
                {
                }
            };
It doesnt add the points and the axis has very small numbers instead of integers.
And also the function should be called "Species 1".
 

Skydiver

Well-known member
Joined
Apr 6, 2019
Messages
365
Location
Virginia Beach, VA
Programming Experience
10+
That's because you did all that work on lines 1-8 to create and initialize a LineSeries, but decided to ignore it. You create yet another empty LineSeries on lines12-14.

Please take time to learn the language.
 

Sheepings

Senior Programmer
Joined
Sep 5, 2018
Messages
345
Location
UK
Programming Experience
10+
The compiler prefers when you do more on one line than having multiple lines essentially performing the same action for each line.
Switch:
   var values = new ChartValues<int>();
values.Add(1);
values.Add(2);
values.Add(3);
values.Add(4);
For This:
ChartValues<int> values = new ChartValues<int>() { 1, 2, 3, 4 };
When you create your LineSeries series; use the one you created. You clearly don't understand the use of the new keyword operator. Why aren't you using the LineSeries on line 6 instead of trying to instantiate a new LineSeries where you failed to declare the object type on line 12?

I don't mean to be patronising, you you really need to step back and pick up a book on C# basic programming 101.
 

kattungen

Member
Joined
Sep 8, 2019
Messages
8
Programming Experience
Beginner
I still get the same result

Code:
var values = new ChartValues<int>();
            var series = new LineSeries();
            values.Add(1);
            values.Add(2);
            values.Add(3);
            values.Add(4);
            
            series.Title = "Species 1";
            series.Values = values;
            cartesianChart1.Series = new SeriesCollection { };
 

Sheepings

Senior Programmer
Joined
Sep 5, 2018
Messages
345
Location
UK
Programming Experience
10+
15 posts on this nonsense. Pick up a book and learn. It's not like you don't have an excuse. MSDN is free and at your disposal, so use it to better understand what you're being shown. The failure here is your lack of understanding and willingness to learn, and you shouldn't be looking for people to do the work for you. That's not how it works around here.
 

kattungen

Member
Joined
Sep 8, 2019
Messages
8
Programming Experience
Beginner
Well, you are one helpful person. I just want to get this one thing working, not learn the entire language.
 

Sheepings

Senior Programmer
Joined
Sep 5, 2018
Messages
345
Location
UK
Programming Experience
10+
Yes I am very helpful. I've enough profiles on various code forums to prove that. Thank you.

What you fail to understand is that we are not obliged to do the work for you. And while we can and will sit here all day correcting your mistakes, its' not actually going to benefit anyone, is it? And it certainly doesn't benefit your lack of understanding for the language. That's something you need to work on yourself. So on that note; you need to understand that there is no point in us explaining to you how something works over and over, which you don't and won't understand because you skipped class on the basic fundamentals of the language in play.

Further note, you've already been shown what you need to do, and you should show a little effort by indulging your curiosity and doing your own research into the advice you've already received.

Lastly, I never said you need to learn the whole language. I said you need to learn the fundamental principles of which surround the basics of the language; which thus gives you a better understanding with the above code. Lets remind you, this is also a learning forum and not a code writing service. ;)
 

Skydiver

Well-known member
Joined
Apr 6, 2019
Messages
365
Location
Virginia Beach, VA
Programming Experience
10+
I am using A graphing tool for c# called Live Charts
I just want to get this one thing working, not learn the entire language.
If you are using a graphing tool for C#, but don't want to learn C#, then I highly suggest picking a different graphing tool that will just let you manipulate the settings/data directly, or that uses some kind of markup.

Right now, it's like you deciding to pick up some wood, saw, hammer, measuring tape, nails, and varnish to make a bookshelf. You then go post a question on a woodworking forum asking how to build a bookshelf, but you are not interesting in learning any woodworking or wood finishing skills. If your goal is just to create a bookshelf, then get a ready to build bookshelf kit from Ikea or Target, where are the pieces are already pre-cut and finished and all you need to do is screw or nail some pieces together.
 
Top Bottom