Click here to Skip to main content
15,888,322 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi friends,
Could anyone help me regarding below coding it is not updating/loading data to Chart.
Value of Series (LineSeries) will fetch from database dynamically, below are just for testing/demonstration purpose...


I am using UserControls for loading in to Main Form

I want load data into chart OnForm_Load()

When I click another tab/button to load another UserControl, and again click the Dashoboard the Chart background changes to black

What I have tried:

C#
void getMonthDetails()
        {
            var months = new[] { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };

            DateTime dt = DateTime.Now;

            int mm = dt.Date.Month;
            int yy = dt.Date.Year;

            string monthsArranged = "";
            for (int i = mm; i <= 11; i++)
            {
                monthsArranged += ("\"" + months[i] + "/" + (yy - 1) + "\", ");
            }

            for (int i = 0; i <= mm - 1; i++)
            {
                monthsArranged += ("\"" + months[i] + "/" + (yy) + "\", ");
            }


            cartesianChart1.Series = new SeriesCollection
            {
                new LineSeries
                {
                    Title = "Book Issued",
                    Values = new ChartValues<double> {504, 813, 895, 402, 687, 895, 467, 1087, 975, 542, 687, 465 }
                },
                new LineSeries
                {
                    Title = "Book Returned",
                    Values = new ChartValues<double> {306, 407, 703, 704, 796, 1023, 745, 685, 535, 1044, 596, 654},
                }
            };

            cartesianChart1.AxisX.Add(new Axis
            {
                Title = "Month",
                Labels = new[] { monthsArranged.TrimEnd(',', ' ') },
                LabelsRotation = 90
            });

            cartesianChart1.AxisY.Add(new Axis
            {
                Title = "Books",
            });

            cartesianChart1.LegendLocation = LegendLocation.Bottom;

        }


      private void UserControl1_Load(object sender, EventArgs e)
      {
            getMonthDetails();
      }
Posted
Updated 18-Sep-17 21:04pm
v3
Comments
Graeme_Grant 19-Sep-17 2:04am    
Using a larger font size in bold letter is considered the same as text in all capitals - it is shouting and considered RUDE. Please correct this.
[no name] 19-Sep-17 2:09am    
Sir, Is it fine, now ?
[no name] 19-Sep-17 2:42am    
The main problem lies here....

cartesianChart1.AxisX.Add(new Axis
{
Title = "Month",
Labels = new[] { monthsArranged.TrimEnd(',', ' ') },
LabelsRotation = 90
});

1 solution

This link shows you how to do it correctly:

Live Charts - WinForm Basic Line Chart sample with code[^]
 
Share this answer
 
Comments
[no name] 19-Sep-17 2:30am    
I have used the same procedure/technique but my questions are:
1. I need it to be updated on OnForm_Load (UserControl1_Load), which does not load and show data in Chart
2. When I click another tab/button (to open another userControl), and try to re-open the userControl having Chart, background color of chart changes to black.
Graeme_Grant 19-Sep-17 2:34am    
If you go to their github, load the sample, it runs. I'm not sure what you are doing to cause your problems.

I use the WPF version with no issues. This is at the core of the WinForm version. I don't work on WinForm projects normally, so I don't know how reliable the Winform host version is. [update] Here is a Winform live-charting version that I wrote earlier this year: WfRollingSD.zip - Google Drive[^] - just tested and works. You will need to restore the Nuget packages to rebuild and run.

Best contact the Author of the control.
[no name] 19-Sep-17 2:44am    
Okay I will but I thought there would be someone here who can help me...
I did nothing strange but just want to load the data (twelve months, months of current year and previous year)
Thanks mate.....
Graeme_Grant 19-Sep-17 2:47am    
Just posted an updated correction - it was a Winform sample, not WPF, that I wrote. Read above (follow the instructions), download, and try it.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900