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

x
  • 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;
            chart.Series.Add(series);

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

            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.DataBind();

            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);
            chart.ChartAreas.Add(chartArea);
            string filename = "C:\\image\\" + chartImageName + ".png";
            chart.SaveImage(filename, ChartImageFormat.Png);
     }
    }

Open in new window

ChartModification.png
0
Mike Eghtebas
Asked:
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

0
 
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.

Thanks,

Mike
0
 
Robert SchuttSoftware EngineerCommented:
ha ha, no that was the "2. force show all labels" part ;-)
0
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:

http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/Q_28491538.html

Regards,

Mike
0
 
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...
0
 
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.
0
 
Mike EghtebasDatabase and Application DeveloperAuthor Commented:
Thank you.

Mie
0

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