C# chart help...

Posted on 2014-08-05
Last Modified: 2014-08-06
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

Question by:Mike Eghtebas
    LVL 35

    Accepted Solution

    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

    LVL 33

    Author Comment

    by:Mike Eghtebas
    Thank you Robert.

    I guess the following line was a bounces:

    chartArea.AxisX.Interval = 1;

    or you have answered my next question.


    LVL 35

    Expert Comment

    by:Robert Schutt
    ha ha, no that was the "2. force show all labels" part ;-)
    LVL 33

    Author Comment

    by:Mike Eghtebas
    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

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


    LVL 35

    Expert Comment

    by:Robert Schutt
    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...
    LVL 35

    Expert Comment

    by:Robert Schutt
    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:

    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.
    LVL 33

    Author Comment

    by:Mike Eghtebas
    Thank you.


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    What Should I Do With This Threat Intelligence?

    Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

    Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
    Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
    Sending a Secure fax is easy with eFax Corporate ( First, Just open a new email message.  In the To field, type your recipient's fax number You can even send a secure international fax — just include t…
    In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

    779 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    14 Experts available now in Live!

    Get 1:1 Help Now