Using Charts in C# Visual Studio 2012

+2 Chris Blum · September 15, 2014
Hello everyone.
I'm having trouble making text labels on the x axis of a line chart.

For the X Axis I want to have different arrays depending on the course. For example, course A is chosen, it loads the values from a[0] array with text such as "Lesson 1", "Lesson 2" etc.
The Y Axis will summarize a number of flight hours. Each lessons will have a certain number of hours which needs to be added up to change the y value for each x value.

I've tried looking on the web but am having trouble finding anything that works.

I'm willing to use direct code or or the property controls.

Thank you very much!!
-Chris Blum

Here is an example of the axises
20


15


10


5

0
   "lesson1"        "lesson 2"         "lesson 3"

Post a Reply

Replies

Oldest  Newest  Rating
+2 Eugene Botma · September 15, 2014
Can you post what you've tried so far?
+1 Chris Blum · September 15, 2014

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

using System.Windows.Forms;

using System.Windows.Forms.DataVisualization.Charting;

namespace chartTestHours

{

public partial class Form1 : Form

{

public Form1()

{

InitializeComponent();

}

private void button1_Click(object sender, EventArgs e)

{

string[] xaxis = new string[10];

chart1.ChartAreas[0].AxisX.Minimum = 0;

chart1.ChartAreas[0].AxisX.Maximum = 10;

chart1.ChartAreas[0].AxisX.LabelStyle.Format = "101b";

//chart1.ChartAreas[1].AxisX.LabelStyle.Format = "103b";

chart1.Titles.Add("Flight Hours");





xaxis[0] = "";

xaxis[1] = "Lesson 1";

xaxis[2] = "Lesson 2";

xaxis[3] = "Lesson 3";

xaxis[4] = "Lesson 4";

xaxis[5] = "Lesson 5";

xaxis[6] = "Lesson 6";

xaxis[7] = "Lesson 7";

xaxis[8] = "Lesson 8";

xaxis[9] = "Lesson 9";

Series S1 = new Series();

S1.Points.AddXY(0, 3);

S1.Points.AddXY(1, 4);

S1.Points.AddXY(2, 6);

S1.Points.AddXY(3, 8);

S1.Points.AddXY(4, 10);

S1.Points.AddXY(5, 12);

S1.Points.AddXY(6, 14);

S1.Points.AddXY(7, 16);

int count = 0;

foreach (CustomLabel lbl in chart1.ChartAreas[0].AxisX.CustomLabels)

{

lbl.Text = xaxis[count];

count++;

}

chart1.Series.Add(S1);





}

}

}

This almost gets me where I need to be. The X Axis will post numbers instead of the lesson number from my xaxis array.
The LabelStyle.Format will work but only for the 0 index, if I try to add another it will throw an error (that's why that line is commented out).
When I run this particular code I get 101b, 111b, 121b, etc on the x axis and I can't figure out why.
Thanks for taking a look!
+1 Eugene Botma · September 16, 2014
You almost had the right idea with chart1.ChartAreas[0].AxisX.LabelStyle.Format = "101b";, but you misunderstood how it works.

This is much like string.format();, where the first argument is the index of your point index (like you said), so you can just format it to show lesson x.
 chart1.ChartAreas[0].AxisX.LabelStyle.Format = "Lesson {0}";

This will display : "lesson 0  lesson 1  lesson 2....."

An alternative way to set the label, is to do it for each point.


S1.Points[0].AxisLabel = "Lesson 1";
 S1.Points[1].AxisLabel = "Lesson 2";
//for each point in S1

//or rather, after all of the points have been added
for (int i = 1; i <= S1.Points.Count; i++)
 {
     S1.Points.AxisLabel = "Lesson " + i.ToString();
 }





//chart1.ChartAreas[1].AxisX.LabelStyle.Format = "103b"; is going to crash the program because you only have 1 chart area and you're trying to set the axis format for a second one.

Also, all of the following code is useless and does absolutely nothing that i can see.

string[] xaxis = new string[10];
xaxis[0] = "";
            
 xaxis[1] = "Lesson 1";

 xaxis[2] = "Lesson 2";

 xaxis[3] = "Lesson 3";

 xaxis[4] = "Lesson 4";

 xaxis[5] = "Lesson 5";

 xaxis[6] = "Lesson 6";

 xaxis[7] = "Lesson 7";

 xaxis[8] = "Lesson 8";

 xaxis[9] = "Lesson 9";

int count = 0;

 foreach (CustomLabel lbl in chart1.ChartAreas[0].AxisX.CustomLabels)

 {

 lbl.Text = xaxis[count];

 count++;

 }
0 Chris Blum · September 16, 2014
Thanks man you're the best. This is working exactly how I want it to. Now I just need to tinker around with adding in custom names for each of our 20 or so different courses :)
0 Chris Blum · September 16, 2014
I've updated my code but I'm only getting 6 points on the X axis opposed to the 30 labels I've written out.
Is there a code to make sure all my labels appear on the x axis?

Also, I'll be plotting multiple series, 1 for the student, 1 for what the syllabus expects to be on time, 1 for 10% behind syllabus and 1 for 30% behind syllabus and I think this would look a lot better as a line graph. How do I change the type of graph?

Thanks again! Here is my current code for the chart.

private void button1_Click(object sender, EventArgs e)

{

chart1.ChartAreas[0].AxisX.Minimum = 0;

chart1.ChartAreas[0].AxisX.Maximum = 30;

//chart1.ChartAreas[0].AxisX.LabelStyle.Format = "101b";

//chart1.ChartAreas[1].AxisX.LabelStyle.Format = "103b";

chart1.Titles.Add("Flight Hours");





Series S1 = new Series();

S1.Points.AddXY(0, 3);

S1.Points.AddXY(1, 4);

S1.Points.AddXY(2, 6);

S1.Points.AddXY(3, 8);

S1.Points.AddXY(4, 10);

S1.Points.AddXY(5, 12);

S1.Points.AddXY(6, 14);

S1.Points.AddXY(7, 16);

S1.Points.AddXY(8, 16);

S1.Points.AddXY(9, 16);

S1.Points.AddXY(10, 16);

S1.Points.AddXY(11, 16);

S1.Points.AddXY(12, 16);

S1.Points.AddXY(13, 16);

S1.Points.AddXY(14, 16);

S1.Points.AddXY(15, 16);

S1.Points.AddXY(16, 16);

S1.Points.AddXY(17, 16);

S1.Points.AddXY(18, 16);

S1.Points.AddXY(19, 16);

S1.Points.AddXY(20, 16);

S1.Points.AddXY(21, 16);

S1.Points.AddXY(22, 16);

S1.Points.AddXY(23, 16);

S1.Points.AddXY(24, 16);

S1.Points.AddXY(25, 16);

S1.Points.AddXY(26, 16);

S1.Points.AddXY(27, 16);

S1.Points.AddXY(28, 16);



chart1.Series.Add(S1);

S1.Points[0].AxisLabel = "101b";

S1.Points[1].AxisLabel = "103b";

S1.Points[2].AxisLabel = "104";

S1.Points[3].AxisLabel = "106";

S1.Points[4].AxisLabel = "107";

S1.Points[5].AxisLabel = "108";

S1.Points[6].AxisLabel = "110";

S1.Points[7].AxisLabel = "111";

S1.Points[8].AxisLabel = "112";

S1.Points[9].AxisLabel = "113";

S1.Points[10].AxisLabel = "114";

S1.Points[11].AxisLabel = "116";

S1.Points[12].AxisLabel = "117b";

S1.Points[13].AxisLabel = "118";

S1.Points[14].AxisLabel = "119";

S1.Points[15].AxisLabel = "120";

S1.Points[16].AxisLabel = "121";

S1.Points[17].AxisLabel = "123";

S1.Points[18].AxisLabel = "124";

S1.Points[19].AxisLabel = "125";

S1.Points[20].AxisLabel = "127";

S1.Points[21].AxisLabel = "128b";

S1.Points[22].AxisLabel = "129b";

S1.Points[23].AxisLabel = "130";

S1.Points[24].AxisLabel = "132";

S1.Points[25].AxisLabel = "134";

S1.Points[26].AxisLabel = "135";

S1.Points[27].AxisLabel = "136b";

S1.Points[28].AxisLabel = "137b";



}

}

I've found online that I need to set the axis interval to 1 but I can't find an example of the code.
I've been trying things like...
chart1.Points.AxisLabel.(no interval value here)
chart1.AxisLabel.(can't find interval value)
S1.AxisLabel.(etc etc)
I don't know where or how to set the interval value.
+1 Eugene Botma · September 16, 2014
Think logically about how charts are constructed and then break them up into parts.

on the outside, you have the chart object. 
Each chart can have multiple chart areas where different charts are plotted.
each chart area can have multiple series'. the scale of the chart is determined by the series, but the scale remains constant for a group of series that are drawn on the same chart. Thus the interval is probably on the chart area. since you should be able to change the interval for the x and y axis independently, each axis should have its own setting. and what do you know:

chart1.ChartAreas[0].AxisX.Interval = 1;

Same thing for how to change the chart type.
You can have a line, dot and bar graph on the same graph, so that already skips past the graph object and the chart area. The type is probably define by the series, so try

S1.ChartType = SeriesChartType.Whateveryouneed;//this chart type enum is pretty big.
//It Has a lot of possible chart types

Edit: a pretty good way to find these things are just to sit and go through the inteliisense options. If' it's not where you expect it, look at what other options there are and which of them might make sense to contain what you're looking for.
0 Chris Blum · September 16, 2014
Once again, Thank you Eugene.

For anyone who may read this later, I'm in the process of making this a line chart instead of a bar chart.
Here is the code
S1.ChartType = SeriesChartType.Line;

However, this is putting a line on top of the bars I already have. I will experiment with it more and update the post when it's ready.


*EDIT*
The bar chart has gone and I have just a line with all the X axis labeled now.

Now I'm trying to populate it with some data and running into an issue manipulating the numbers since they are strings.
I'm trying to convert them to floats but I keep getting exceptions.


float placeHolder = 0;

String stringPlaceHolder = "";

float listPlaceHolder = 0;

for (int i = 0; i < Variables.listIPHours.Count; i++) {

//String studentNameString = studentNameObject.ToString();

if (Variables.listIPHours != null)

{

stringPlaceHolder = Variables.listIPHours;

listPlaceHolder = float.Parse(stringPlaceHolder);

placeHolder = listPlaceHolder + placeHolder;

S1.Points.AddXY(i, placeHolder);

}

}

The error I'm getting is "input string is not in a correct format" on the line that is trying to set the value of listPlaceHolder = float.Parse(stringPlaceHolder);.
  • 1

C#

107,154 followers
About

C# is an object-oriented language designed for improving the development of web applications.

Links
Moderators