c# chart control

internal class ChartStyle
    {
        internal static void RemoveZeroDataPointLabel(Chart chart)
        {
            foreach (Series series in chart.Series)
            {
                foreach (DataPoint dp in series.Points)
                {
                    if (dp.YValues[0].ToString() == "0")
                    {
                        dp.IsValueShownAsLabel = false;
                        dp.Label = " ";
                    }
                }
            }
        }

        internal static void DrawString(object sender, PaintEventArgs e, string text)
        {
            Font font = new Font("微软雅黑", 15F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));

            PictureBox pb = sender as PictureBox;

            float sumY = 0;

            foreach (char c in text)
            {
                string line = new string(c, 1);
                SizeF size = e.Graphics.MeasureString(line, font);
                sumY += size.Height;
            }

            float y = (pb.Height - sumY) / 2;
            y = 10;

            foreach (char c in text)
            {
                string line = new string(c, 1);
                SizeF size = e.Graphics.MeasureString(line, font);
                sumY += size.Height;

                e.Graphics.DrawString(line, font, Brushes.White, (pb.Width - size.Width) / 2, y);
                y += size.Height;
            }
        }

        internal static void chartPickRatio_PostPaint(object sender, ChartPaintEventArgs e)
        {
            e.ChartGraphics.Graphics.SmoothingMode = SmoothingMode.AntiAlias;

            Chart chart = sender as Chart;
            if (chart.Series.Count > 0 && chart.Series[0].Points.Count > 0)
            {
                Series series = chart.Series[0];
                double value = series.Points[0].XValue;
                if (series.Label == "0")
                {
                    value = 0;
                }

                string msg = value.ToString("P0");

                Font font = new Font("Arial", 25);
                SizeF size = e.ChartGraphics.Graphics.MeasureString(msg, font);
                e.ChartGraphics.Graphics.DrawString(msg, font, Brushes.Red,
                    chart.Width / 2 - size.Width / 2,
                    chart.Height / 2 - size.Height / 2);
            }
        }

        /// <summary>
        /// Set Chart ScrollBarStyle
        /// </summary>
        /// <param name="chart"></param>
        internal static void SetChartScrollBarStyle(Chart chart)
        {
            chart.ChartAreas[0].AxisX.ArrowStyle = AxisArrowStyle.Lines;
            //chart.ChartAreas[0].AxisY.ArrowStyle = AxisArrowStyle.Triangle;

            chart.ChartAreas[0].AxisX.ScrollBar.BackColor = Color.Transparent;
            chart.ChartAreas[0].AxisX.ScrollBar.LineColor = chart.ChartAreas[0].AxisX.ScrollBar.ButtonColor = Color.FromArgb(214, 214, 204);

            chart.ChartAreas[0].AxisY.TitleForeColor = Color.White;
            chart.ChartAreas[0].AxisY.TitleFont = new Font("微软雅黑", 16F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
            chart.ChartAreas[0].AxisY.TextOrientation = TextOrientation.Stacked;

            chart.ChartAreas[0].AxisY2.TitleForeColor = Color.White;
            chart.ChartAreas[0].AxisY2.TitleFont = new Font("微软雅黑", 16F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));

            if (chart.Tag != "NO")
            {
                chart.ChartAreas[0].InnerPlotPosition.Auto = false;
                chart.ChartAreas[0].InnerPlotPosition.Height = 84.41489F;
                chart.ChartAreas[0].InnerPlotPosition.Width = 85.34175F;
                chart.ChartAreas[0].InnerPlotPosition.X = 9.65825F;
                chart.ChartAreas[0].InnerPlotPosition.Y = 2.79255F;

                //3, 0, 94.8, 97
                chart.ChartAreas[0].InnerPlotPosition.Height = 90;
                chart.ChartAreas[0].InnerPlotPosition.Width = 90;
                chart.ChartAreas[0].InnerPlotPosition.X = 5;
                chart.ChartAreas[0].InnerPlotPosition.Y = 5;

                //chart.ChartAreas[0].InnerPlotPosition.Height = 50;
                //chart.ChartAreas[0].InnerPlotPosition.Width = 50;
                //chart.ChartAreas[0].InnerPlotPosition.X = 15;
                //chart.ChartAreas[0].InnerPlotPosition.Y = 15;
            }

            //chart.ChartAreas[0].InnerPlotPosition.Height = 94.8f;
            //chart.ChartAreas[0].InnerPlotPosition.Height = 90.8f;
            //chart.ChartAreas[0].InnerPlotPosition.Width = 97;
            //chart.ChartAreas[0].InnerPlotPosition.X = 3;
            //chart.ChartAreas[0].InnerPlotPosition.Y = 0;

            chart.ChartAreas[0].AxisY.MajorTickMark.TickMarkStyle = TickMarkStyle.None;
            chart.ChartAreas[0].AxisY2.MajorTickMark.TickMarkStyle = TickMarkStyle.None;
            chart.ChartAreas[0].AxisX.LabelStyle.Enabled = true;

            if (chart.Tag != "NO" && chart.ChartAreas[0].AxisX.ScrollBar.Enabled)
            {
                if (chart.Height > 20)
                {
                    float ratio = 20.0f/(float) chart.Height;
                    chart.ChartAreas[0].InnerPlotPosition.Height -= (ratio*100);
                }
            }

            chart.ChartAreas[0].AxisX.ScrollBar.ButtonStyle = ScrollBarButtonStyles.SmallScroll;
            chart.ChartAreas[0].AxisX.ScrollBar.ButtonColor = Color.White;
        }

        internal static void SetChartScrollBarStyleInForm(Form control)
        {
            List<Control> controlList = new List<Control>();
            GetChildren(controlList, control);

            foreach (Control control1 in controlList)
            {
                if (control1 is Chart)
                {
                    SetChartScrollBarStyle((Chart)control1);
                }
            }
        }

        public static void GetChildren(List<Control> controlList, Control control)
        {
            controlList.Add(control);

            foreach (Control control1 in control.Controls)
            {
                GetChildren(controlList, control1);
            }
        }
    }

 

posted @ 2013-05-13 10:43  louiskoo  阅读(1855)  评论(0编辑  收藏  举报