在ms图表数据点中设置自定义标签

时间:2014-09-04 06:39:16

标签: asp.net asp.net-mvc-3 mschart

我正在使用ms图表在asp.net mvc3中创建图表!

我创建了图表。 现在我想在特定数据点上设置一些文本。例如在(185,90)  这是我的代码

公共ActionResult GetGraph()         {

        System.Web.UI.DataVisualization.Charting.Chart Chart2 = new System.Web.UI.DataVisualization.Charting.Chart();


        Chart2.Width = 800;
        Chart2.Height = 600;

        var series = new Series();
        series.ChartArea = "ca1";

        var area = new ChartArea("ca1");
        area.Area3DStyle.Enable3D = false;
        area.AxisX.Title = "Height";
        area.AxisX.IsStartedFromZero = true;
        area.AxisX.Maximum = 200;
        area.AxisX.Minimum = 110;
        //area.AxisX.Interval = 10;
        area.AxisY.Minimum = 10;
        area.AxisY.Maximum = 110;
        area.AxisY.Interval = 10;
        area.AxisY.TitleFont = new Font("Arial,Helvetica,sans-serif",
          10F, FontStyle.Regular); 
        area.AxisX.TitleFont = new Font("Arial,Helvetica,sans-serif",
                    10F, FontStyle.Regular); 
        area.AxisY.Title = "Weight";
       // area.InnerPlotPosition.Height = 10;
        area.AxisX.IsLabelAutoFit = false;
        area.AxisY.IsLabelAutoFit = false;
           new Font("Arial,Helvetica,sans-serif",
                    10F, FontStyle.Regular);
        area.AxisY.LabelStyle.Font =
           new Font("Arial,Helvetica,sans-serif",
                    10F, FontStyle.Regular);

        area.AxisY.LineColor = Color.FromArgb(64, 64, 64, 64);
        area.AxisX.LineColor = Color.FromArgb(64, 64, 64, 64);
        area.AxisY.MajorGrid.LineColor = Color.FromArgb(64, 64, 64, 64);
        area.AxisX.MajorGrid.LineColor = Color.FromArgb(64, 64, 64, 64);

        area.Position.Auto = false;
        area.Position.X = 1;
        area.Position.Y = 1;
        area.Position.Width = 100;
        area.Position.Height = 100;
        area.InnerPlotPosition.X = 10;
        area.InnerPlotPosition.Y = 5;
        area.InnerPlotPosition.Width = 85;
        area.InnerPlotPosition.Height = 85;
        Chart2.ChartAreas.Add(area);
        Chart2.ChartAreas.Add("curve 15").BackColor = System.Drawing.Color.FromArgb(64, System.Drawing.Color.White);
        Chart2.Series.Add("curve 15").ChartType = SeriesChartType.Line;
        Chart2.Series.Add("curve 18.5").ChartType = SeriesChartType.Line;
        Chart2.Series.Add("curve22").ChartType = SeriesChartType.Line;
        Chart2.Series.Add("curve25").ChartType = SeriesChartType.Line;
        Chart2.Series.Add("curve30").ChartType = SeriesChartType.Line;
        Chart2.Series.Add("bmi30").ChartType = SeriesChartType.FastPoint;
        Chart2.Series["bmi30"].Points.AddXY(185,96);
        Chart2.Series["bmi30"].IsValueShownAsLabel = true;
        Chart2.Series["bmi30"].Label = "BMI";
        Chart2.Series["curve 15"].Color = Color.Blue;
        Chart2.Series["curve 15"].BorderWidth = 2;
        Chart2.Series["curve 15"].Points.AddXY(120, 21);
        Chart2.Series["curve 15"].Points.AddXY(130, 25);
        Chart2.Series["curve 15"].Points.AddXY(140, 27);
        Chart2.Series["curve 15"].Points.AddXY(150, 32.5);
        Chart2.Series["curve 15"].Points.AddXY(160, 37.5);
        Chart2.Series["curve 15"].Points.AddXY(170, 42);
        Chart2.Series["curve 15"].Points.AddXY(185, 48);
        Chart2.Series["curve 15"].Points.Last().AxisLabel = "lllcc";
        Chart2.Series["curve 18.5"].Color = Color.Red;
        Chart2.Series["curve 18.5"].BorderWidth = 2;
        Chart2.Series["curve 18.5"].Points.AddXY(120, 26);
        Chart2.Series["curve 18.5"].Points.AddXY(130, 31);
        Chart2.Series["curve 18.5"].Points.AddXY(140, 36);
        Chart2.Series["curve 18.5"].Points.AddXY(150, 41);
        Chart2.Series["curve 18.5"].Points.AddXY(160, 48);
        Chart2.Series["curve 18.5"].Points.AddXY(170, 50.5);
        Chart2.Series["curve 18.5"].Points.AddXY(185, 63);

        Chart2.Series["curve22"].Color = Color.Chocolate;
        Chart2.Series["curve22"].BorderWidth = 2;
        Chart2.Series["curve22"].Points.AddXY(120, 30.5);
        Chart2.Series["curve22"].Points.AddXY(130,36);
        Chart2.Series["curve22"].Points.AddXY(140, 42);
        Chart2.Series["curve22"].Points.AddXY(150,49);
        Chart2.Series["curve22"].Points.AddXY(160, 51);
        Chart2.Series["curve22"].Points.AddXY(170, 61);
        Chart2.Series["curve22"].Points.AddXY(185,71);

        Chart2.Series["curve25"].Color = Color.Yellow;
        Chart2.Series["curve25"].BorderWidth = 2;
        Chart2.Series["curve25"].Points.AddXY(120,36);
        Chart2.Series["curve25"].Points.AddXY(130, 41);
        Chart2.Series["curve25"].Points.AddXY(140,48.5);
        Chart2.Series["curve25"].Points.AddXY(150,56);
        Chart2.Series["curve25"].Points.AddXY(160,58);
        Chart2.Series["curve25"].Points.AddXY(170,71);
        Chart2.Series["curve25"].Points.AddXY(185,81);

        Chart2.Series["curve30"].Color = Color.Green;
        Chart2.Series["curve30"].BorderWidth = 2;
        Chart2.Series["curve30"].Points.AddXY(120, 42);
        Chart2.Series["curve30"].Points.AddXY(130, 50);
        Chart2.Series["curve30"].Points.AddXY(140, 58);
        Chart2.Series["curve30"].Points.AddXY(150, 66);
        Chart2.Series["curve30"].Points.AddXY(160, 76);
        Chart2.Series["curve30"].Points.AddXY(170, 85);
        Chart2.Series["curve30"].Points.AddXY(185, 96);
        Chart2.BackColor = Color.Transparent;
        MemoryStream imageStream = new MemoryStream();
        Chart2.SaveImage(imageStream, ChartImageFormat.Png);
        Chart2.TextAntiAliasingQuality = TextAntiAliasingQuality.SystemDefault;
        Response.ContentType = "image/png";
        imageStream.WriteTo(Response.OutputStream);

         return PartialView("_GetGraph");
    }

我尝试使用链接  Adding text , image to asp.net chart control 但标签没有显示。 我也想用0,120,130开始x,...... 和y轴到0,20,30 可能吗?任何有帮助的帮助

1 个答案:

答案 0 :(得分:0)

首先应创建一个数据点,然后将其添加到系列中。例如:

DataPoint dp = new DataPoint();
dp4.Font = dp_font;
dp4.XValue = 185;
dp4.SetValueY(90);
dp4.Label = "Label text";

Series["bmi30"].Points.Add(dp)