ASP.Net MVC图表控件 - 创建多个图表

时间:2013-09-20 19:00:31

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

我想在我的控制器中动态创建多个图表。当我在我的每个实例中实例化一个新的图表控件时,它会呈现一个空白图表。我想知道可以做些什么来允许我使用下面的代码在一个页面上创建和填充多个图表。

 public ActionResult BuildChart()
    {
        var chart = new System.Web.UI.DataVisualization.Charting.Chart()
        {
            Width = 576,
            Height = 100,
            BackColor = System.Drawing.Color.White,
        };

        GetPersonas();
        GetSenarioVoting();
        List<CIEToolRole.Models.ScenarioModel> scenarios = GetSenarioVotingResults();

        int i = 0;

       foreach (ScenarioModel scenario in scenarios)
       {
           chart = new System.Web.UI.DataVisualization.Charting.Chart()
           {
               Width = 576,
               Height = 100,
               BackColor = System.Drawing.Color.White,
           };

           chart.ID = "Chart" + i;

           Series mySeries = new Series();
           mySeries.ChartType = SeriesChartType.Column;

           mySeries.BorderWidth = 1;
           mySeries.Palette = ChartColorPalette.Chocolate;
           mySeries.Name = "series" + i;

               List<int> voteVal = scenario.Votes.Select(p => p.Value).ToList();

               foreach (int vote in voteVal)
               {
                   DataPoint point = new DataPoint();
                   point.YValues = new double[] { vote };
                   mySeries.Points.Add(point);
               }


           chart.Series.Add(mySeries);

           i++;
       }

        ChartArea ca1 = new ChartArea("ca1");
        ca1.BackColor = System.Drawing.Color.LightGray;
        ca1.AxisY.LineColor = System.Drawing.Color.LightBlue;
        chart.ChartAreas.Add(ca1);

        var imgStream = new System.IO.MemoryStream();
        chart.SaveImage(imgStream, ChartImageFormat.Png);
        imgStream.Seek(0, System.IO.SeekOrigin.Begin);

        return File(imgStream, "image/png");
    } 

1 个答案:

答案 0 :(得分:1)

经过一些试验和错误后,我能够提出一个不需要部分查看的解决方案。

控制器

 public ActionResult BuildChart(int scenarioID)
    {
        var chart = new System.Web.UI.DataVisualization.Charting.Chart()
        {
            Width = 576,
            Height = 100,
            BackColor = System.Drawing.Color.White,
        };



        GetPersonas();
        GetSenarioVoting();
        List<modelstuff.Models.ScenarioModel> scenarios = GetSenarioVotingResults();

        int x = 0;

        var scenarioById = scenarios.FirstOrDefault(p => p.ID == scenarioID).Votes;

        Series mySeries = new Series();
        mySeries.ChartType = SeriesChartType.Column;

        mySeries.BorderWidth = 1;
        mySeries.Palette = ChartColorPalette.Chocolate;
        // Making a unqiue series ID
        Guid guido = new Guid();
        mySeries.Name = guido.ToString();

        foreach (int vote in scenarioById.Values)
        {
            DataPoint point = new DataPoint();

            point.YValues = new double[] { vote };
            mySeries.Points.Add(point);
        }

        chart.Series.Add(mySeries);

        ChartArea ca1 = new ChartArea("ca1");
        ca1.AxisX.LabelStyle.Enabled = false;
        ca1.AxisY.LabelStyle.Enabled = false;
        ca1.BackColor = System.Drawing.Color.LightGray;
        ca1.AxisY.LineColor = System.Drawing.Color.LightBlue;
        chart.ChartAreas.Add(ca1);

        var imgStream = new System.IO.MemoryStream();
        chart.SaveImage(imgStream, ChartImageFormat.Png);
        imgStream.Seek(0, System.IO.SeekOrigin.Begin);

        return File(imgStream, "image/png");
    }

查看

 foreach (modelstuff.Models.ScenarioModel scenario in ViewBag.Scenarios)
               { %>
               <li class="<%= i % 2 == 0 ? "row" : "altRow" %>" data-       rowid="<%= "scenario" + scenario.ID%>">
                <div class="chartcol1">
                    <span class="charttoolTipTitle"><%= scenario.Name%></span>
                </div>
                <div class="chartcol">
                    <%= Html.Chart(
                    "BuildChart",
                    "Home",
                    new 
                    {
                        scenarioID = scenario.ID
                    },
                    new
                    {

                        style = "width: 576px; height:100px;",

                    }) %>