Link to home
Start Free TrialLog in
Avatar of Mike Eghtebas
Mike EghtebasFlag for United States of America

asked on

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
ASKER CERTIFIED SOLUTION
Avatar of Robert Schutt
Robert Schutt
Flag of Netherlands image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Mike Eghtebas

ASKER

Thank you Robert.

I guess the following line was a bounces:

chartArea.AxisX.Interval = 1;

or you have answered my next question.

Thanks,

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

https://www.experts-exchange.com/questions/28491538/BorderSkinStyle-FrameThin1-apply-to-chart-column-c.html

Regards,

Mike
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...
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.
Thank you.

Mie