Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 716
  • Last Modified:

C# chart help...

The following chart needs 3 minor modifications I cannot handle:

1. Change font-size for the data-points.
2. Force to show all of the x-axis labels (it is skipping some).
3. Add a chart title.

Please see the attached image.

Thank you for your comments in advance.
 protected void createMonthlyChartImages(string YYYY, int RegionID, string Region, int Metric_ID, int MedCtrID, string MedCenter, string chartImageName)
        var filePathName = "~\\image\\" + chartImageName + ".png";
        if (!File.Exists(Server.MapPath(filePathName)))
            Session["Metric_ID"] = Metric_ID;
            Session["YYYY"] = YYYY;
            Session["MedCtr_ID"] = MedCtrID;

            SqlDataSource mySqlDataSource = new SqlDataSource();
            mySqlDataSource.ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["myConn"].ConnectionString;
            mySqlDataSource.SelectCommand = "spMonthlyChart";
            mySqlDataSource.SelectCommandType = SqlDataSourceCommandType.StoredProcedure;
            mySqlDataSource.SelectParameters.Add(new SessionParameter("Metric_ID", DbType.Int32, "Metric_ID"));
            mySqlDataSource.SelectParameters.Add(new SessionParameter("YYYY", DbType.String, "YYYY"));
            mySqlDataSource.SelectParameters.Add(new SessionParameter("MedCtr_ID", DbType.Int32, "MedCtr_ID"));

            Chart chart = new Chart();

            Series series = new Series("Default");
            series.ChartType = SeriesChartType.Column;

            Series series2 = new Series("Default2");
            series2.ChartType = SeriesChartType.Line;
            series2.Color = System.Drawing.Color.Red;
            series2.BorderWidth = 1; // this is actually the line width...

            ChartArea chartArea = new ChartArea();
            Axis yAxis = new Axis(chartArea, AxisName.Y);
            Axis xAxis = new Axis(chartArea, AxisName.X);

            series.XValueMember = "ColName"; // X
            series.YValueMembers = "ColValue"; // Y1
            series2.XValueMember = "ColName"; // X
            series2.YValueMembers = "Target"; // Y2
            chart.DataSource = mySqlDataSource;

            chart.Series["Default"].Color = System.Drawing.Color.Green;
            chart.Series["Default"].Points[0].Color = System.Drawing.Color.Red;
            chart.Series["Default"].Points[chart.Series["Default"].Points.Count - 1].Color = System.Drawing.Color.Orange;
            series.IsValueShownAsLabel = true; // 3 show value
            series.LabelForeColor = System.Drawing.Color.Black;
            chartArea.AxisX.MajorGrid.Enabled = false; // 1 don't show grid
            chartArea.AxisY.MajorGrid.Enabled = false;// 1 don't show grid
            chartArea.AxisY.LabelStyle.Enabled = false; // 2 remove y labels
            chartArea.AxisX.LabelStyle.Angle = 90; // 4 rotate x labels
            chartArea.AxisY.MajorTickMark.Enabled = false; //5.remove tick marks and add border
            chartArea.BorderDashStyle = ChartDashStyle.Solid; //6. add border
            chartArea.BorderWidth = 3;
            chartArea.BorderColor = System.Drawing.Color.Black; //6. add border

            chart.Series["Default"].Color = System.Drawing.Color.Green;// Blue;
            chart.Series["Default"].BackGradientStyle = GradientStyle.TopBottom;
            chart.Series["Default"].BackSecondaryColor = System.Drawing.Color.DarkGreen;// DarkBlue;
            chart.Series["Default"].Points[0].Color = System.Drawing.Color.Red;
            chart.Series["Default"].Points[0].BackGradientStyle = GradientStyle.TopBottom;
            chart.Series["Default"].Points[0].BackSecondaryColor = System.Drawing.Color.DarkRed;
            chart.Series["Default"].Points[chart.Series["Default"].Points.Count - 1].Color = System.Drawing.Color.Orange;
            chart.Series["Default"].Points[chart.Series["Default"].Points.Count - 1].BackGradientStyle = GradientStyle.TopBottom;
            chart.Series["Default"].Points[chart.Series["Default"].Points.Count - 1].BackSecondaryColor = System.Drawing.Color.Yellow;

            chartArea.BackGradientStyle = GradientStyle.HorizontalCenter;
            chartArea.BackColor = System.Drawing.Color.SlateGray;
            chartArea.BackSecondaryColor = System.Drawing.Color.White;
           // chartArea.Position.X = 0;
          //  chartArea.Position.Y = 0;

            chart.Width = new Unit(1200, UnitType.Pixel);
            chart.Height = new Unit(600, UnitType.Pixel);
            string filename = "C:\\image\\" + chartImageName + ".png";
            chart.SaveImage(filename, ChartImageFormat.Png);

Open in new window

Mike Eghtebas
Mike Eghtebas
  • 4
  • 3
1 Solution
Robert SchuttSoftware EngineerCommented:
Try this (somewhere around line 70):
                series.Font = new Font("Verdana", 16f);
                chartArea.AxisX.Interval = 1;
                chart.Titles.Add(string.Format("Chart {0} - {1}", YYYY, MedCtrID));
                chart.Titles[0].Alignment = ContentAlignment.TopLeft;
                chart.Titles[0].Font = new Font("Verdana", 20f);

Open in new window

Mike EghtebasDatabase and Application DeveloperAuthor Commented:
Thank you Robert.

I guess the following line was a bounces:

chartArea.AxisX.Interval = 1;

or you have answered my next question.


Robert SchuttSoftware EngineerCommented:
ha ha, no that was the "2. force show all labels" part ;-)
Veeam Disaster Recovery in Microsoft Azure

Veeam PN for Microsoft Azure is a FREE solution designed to simplify and automate the setup of a DR site in Microsoft Azure using lightweight software-defined networking. It reduces the complexity of VPN deployments and is designed for businesses of ALL sizes.

Mike EghtebasDatabase and Application DeveloperAuthor Commented:
I am totally lost. This is my first project and I love it as you suffer through my not knowing much (LOL) and teaching me.

Is there a site where you one can learn c# chart by trying thing? Similar to http://www.w3schools.com/

Pretty soon, I will get hang of it though. I wonder it you could possibly add some comment to:



Robert SchuttSoftware EngineerCommented:
A website to learn C# by trying, no I haven't come across that. I mostly just use IntelliSense en "Add Watch" in Visual Studio to learn about the structure of objects. The MSDN site is usually very good when it comes to the documentation of classes, methods and properties. But in this case I have struggled with finding good examples showing the relations between the various parts of the chart like labels and axes. It's mainly been trial and error...
Robert SchuttSoftware EngineerCommented:
I just remembered at some point it gave good insight just into what is possible with the Chart object by downloading the samples from Microsoft: http://code.msdn.microsoft.com/Samples-Environments-for-b01e9c61/view/SamplePack#content

Your new question I'm afraid I have no answer, hopefully somebody with more experience regarding skins/styles will step in. All I see is setting a normal border, not that nice one.
Mike EghtebasDatabase and Application DeveloperAuthor Commented:
Thank you.


Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now