modify chart c#

Mike Eghtebas
Mike Eghtebas used Ask the Experts™
on
I have good solutions from Robert Schutt at:

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

In my posts,I have used two different chart samples. Now I am having a bit trouble consolidating the solutions.

Question How can I consolidate solution 1 through 6 with the code below it?

Thank you.
            // 1 don't show grid*********************************************************
            chartImage.ChartAreas[0].AxisX.MajorGrid.Enabled = false;
           chartImage.ChartAreas[0].AxisY.MajorGrid.Enabled = false;
           chartImage.ChartAreas[0].BorderWidth = 1;

           // 2 remove y labels?********************************************************
            chartImage.ChartAreas[0].AxisY.LabelStyle.Enabled = false;

            // 3 show value*************************************************************
            chartImage.Series[0].IsValueShownAsLabel = true;
            chartImage.Series[0].LabelForeColor = System.Drawing.Color.Blue;

            // 4 rotate x labels**********************************************************
           chartImage.ChartAreas[0].AxisX.LabelStyle.Angle = 90;

           //5.remove tick marks and add border*************************************
           chartImage.ChartAreas[0].AxisY.MajorTickMark.Enabled = false;

           //6. add border************************************************************
           chartImage.ChartAreas[0].BorderDashStyle = ChartDashStyle.Solid;
           chartImage.ChartAreas[0].BorderColor = System.Drawing.Color.DeepPink;

The above solutions (1-6) to be included with:
       double[] yValues = { 15, 60, 12, 13 };   //for column chart
        double[] yValues2 = { 30, 32, 30, 0 };   //for line
        string[] xValues = { "September", "October", "November", "December" };

        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 = 5; // 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);

        chart.Series["Default"].Points.DataBindXY(xValues, yValues);
        chart.Series["Default2"].Points.DataBindXY(xValues, yValues2);
        chart.Series["Default"].Color = System.Drawing.Color.Blue; // change default if you want to
        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;

        chart.ChartAreas.Add(chartArea);

        chart.Width = new Unit(300, UnitType.Pixel);
        chart.Height = new Unit(200, UnitType.Pixel);
        string filename = "C:\\image\\Chart.png";
        chart.SaveImage(filename, ChartImageFormat.Png);

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Software Engineer
Commented:
Well the main thing would be that you can only set properties on the series and chartAreas after they have been created. In this case it's probably nicer to use the variables you create for that instead of the indexed properties of the Chart object. Question is if both series need to have a label shown which might overlap (see the comment in the code below). All in all it should be something like this:
            double[] yValues = { 15, 60, 12, 13 };   //for column chart
            double[] yValues2 = { 30, 32, 30, 0 };   //for line
            string[] xValues = { "September", "October", "November", "December" };

            Chart chart = new Chart();

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

            // 3 show value*************************************************************
            series.IsValueShownAsLabel = true;
            series.LabelForeColor = System.Drawing.Color.Blue;

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

            // also show value label for series2?
            //series2.IsValueShownAsLabel = true;
            //series2.LabelForeColor = System.Drawing.Color.Purple;

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

            chart.Series["Default"].Points.DataBindXY(xValues, yValues);
            chart.Series["Default2"].Points.DataBindXY(xValues, yValues2);
            chart.Series["Default"].Color = System.Drawing.Color.Blue; // change default if you want to
            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;

            // 1 don't show grid*********************************************************
            chartArea.AxisX.MajorGrid.Enabled = false;
            chartArea.AxisY.MajorGrid.Enabled = false;
            chartArea.BorderWidth = 1;

            // 2 remove y labels?********************************************************
            chartArea.AxisY.LabelStyle.Enabled = false;

            // 4 rotate x labels**********************************************************
            chartArea.AxisX.LabelStyle.Angle = 90;

            //5.remove tick marks and add border*************************************
            chartArea.AxisY.MajorTickMark.Enabled = false;

            //6. add border************************************************************
            chartArea.BorderDashStyle = ChartDashStyle.Solid;
            chartArea.BorderColor = System.Drawing.Color.DeepPink;

            chart.ChartAreas.Add(chartArea);

            chart.Width = new Unit(300, UnitType.Pixel);
            chart.Height = new Unit(200, UnitType.Pixel);
            string filename = "C:\\image\\Chart.png";
            chart.SaveImage(filename, ChartImageFormat.Png);

Open in new window

Mike EghtebasDatabase and Application Developer

Author

Commented:
Thank you.

There is yet another question turning these charts to dynamic data source. If interested please see:
http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/Q_28490316.html


Regards,

Mike

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial