ASP .NET MVC图表助手添加标记

时间:2012-06-25 21:06:30

标签: asp.net-mvc helper asp.net-charts

我有一个柱形图(每个区域的总点数)。我想在列顶部显示实际点。我相信这被称为标记? 这是我的代码:

var myChart = new Chart(width: 800, height: 500, theme: myTheme)
.AddTitle("Region Report")
.SetXAxis("Region")
.SetYAxis("Total Points")  
.AddSeries("RegionReport", chartType: "Column",
xValue: Model, xField: "Region",
yValues: Model, yFields: "TotalPoints")
.Write();

1 个答案:

答案 0 :(得分:4)

如果要显示列值(或任何数据值)的顶部标记,则需要先创建一个系列,然后使用 IsValueShownAsLabel = true 显示特定系列价值。

您编写上述控制器功能的方式,您将无法显示它,因为Chart Helper不支持它。

以下是使用Series时如何执行此操作的示例。将以下功能添加到控制器:

    public ActionResult ShowChart()
    {
        Bitmap image = new Bitmap(500, 50);
        Graphics g = Graphics.FromImage(image);
        System.Web.UI.DataVisualization.Charting.Chart myChart = new System.Web.UI.DataVisualization.Charting.Chart();
        myChart.Width = 600;
        myChart.Height = 300;
        myChart.ChartAreas.Add("xSeries").BackColor = System.Drawing.Color.FromArgb(64, System.Drawing.Color.White);
        // create a couple of series  
        myChart.Series.Add("xSeries");
        // add points to xSeries  
        myChart.Series["xSeries"].Points.AddY(83);
        myChart.Series["xSeries"].Points.AddY(49);
        myChart.Series["xSeries"].Points.AddY(94);
        myChart.Series["xSeries"].Points.AddY(65);
        // add points to ySeries  
        myChart.Series["xSeries"].IsValueShownAsLabel = true;
        myChart.BackColor = Color.Transparent;
        MemoryStream imageStream = new MemoryStream();
        myChart.SaveImage(imageStream, ChartImageFormat.Png);
        myChart.TextAntiAliasingQuality = TextAntiAliasingQuality.SystemDefault;
        Response.ContentType = "image/png";
        imageStream.WriteTo(Response.OutputStream);
        g.Dispose();
        image.Dispose();
        return null;
    }

然后在您的视图中将其调用如下:

<img src="@Url.Action("ShowChart")" alt="MyChart" />