如何自定义MVC图表图例?

时间:2016-06-15 13:53:07

标签: c# .net model-view-controller charts mschart

我画了一张MVC图表。 enter image description here

只是想知道如何实现循环系列名称。 enter image description here

请帮忙。我的代码如下:

 public ActionResult CreateBar()
        {

            // Create OnPremise and Azure Output Models
            var objOnPremiseOutPut = new OnPremisesOutputModel();
            var objAzureOutPut = new AzureCostOutputModel();

            // Get the data from the TempData. Check :: What if the TemData is empty ??
            objOnPremiseOutPut = (OnPremisesOutputModel)TempData["TotalOnPremiseCost"];
            objAzureOutPut = (AzureCostOutputModel)TempData["TotalAzureCost"];

            // Create a new chart Object
            var chart = new Chart();

            // Set Dimensions of chart
            chart.Width = 500;
            chart.Height = 350;

            // Set Color and Background color properties
            chart.BackColor = Color.WhiteSmoke;//Color.FromArgb(255, 255, 255);
            chart.BorderlineDashStyle = ChartDashStyle.Solid;
            chart.BackSecondaryColor = Color.White;
            //chart.BackGradientStyle = GradientStyle.TopBottom;
            chart.BorderlineWidth = 0;
            chart.Palette = ChartColorPalette.BrightPastel;
            chart.BorderlineColor = Color.FromArgb(26, 59, 105);
            chart.RenderType = RenderType.BinaryStreaming;
            chart.BorderSkin.SkinStyle = BorderSkinStyle.None;
            chart.AntiAliasing = AntiAliasingStyles.All;
            chart.TextAntiAliasingQuality = TextAntiAliasingQuality.Normal;

            // Set the Title and Legends for chart
            chart.Titles.Add(CreateTitle());
            chart.Legends.Add(new Legend("Costs Distribution")
            {
                BackColor = Color.Transparent,
                Font = new Font("Trebuchet MS", 8.25f, FontStyle.Bold,
                GraphicsUnit.Point),
                IsTextAutoFit = false

            });


            // Clear any existing series and add the new series
            chart.Series.Clear();
            chart.Series.Add(new Series());
            chart.Series.Add(new Series());
            chart.Series.Add(new Series());
            chart.Series.Add(new Series());

            foreach (Series s in chart.Series)
            {
                s.ChartType = SeriesChartType.StackedColumn;
            }
            //var totalStorage = objAzureOutPut.GeoReplicatedStorageAnnualExpense+objAzureOutPut.LocallyRedundantStorageAnnualExpense
            //chart.Series["Default"].Points[0].AxisLabel = "On Premises";
            //chart.Series["Default"].Points[1].AxisLabel = "Azure";
            chart.Series[0].Points.Add(new DataPoint(0, objOnPremiseOutPut.TotalOnPremServerExpense));
            chart.Series[1].Points.Add(new DataPoint(0, objOnPremiseOutPut.TotalOnPremStorageCapacity));
            chart.Series[2].Points.Add(new DataPoint(0, objOnPremiseOutPut.TotalOnPremNetworkExpense));
            chart.Series[3].Points.Add(new DataPoint(0, objOnPremiseOutPut.TotalOnPremITExpense));

            //chart.Series[0].AxisLabel = "Azure";
            chart.Series[0].Points.Add(new DataPoint(1, objAzureOutPut.DiscVMPricingAnnualExpense));
            chart.Series[1].Points.Add(new DataPoint(1, objAzureOutPut.TotalStorageExpense));
            chart.Series[2].Points.Add(new DataPoint(1, objAzureOutPut.Zone1EgressAnnualExpense));
            chart.Series[3].Points.Add(new DataPoint(1, objAzureOutPut.AdminExpense));

            // Name the series
            chart.Series["Series1"].Name = "Server";
            chart.Series["Series2"].Name = "Storage";
            chart.Series["Series3"].Name = "Network";
            chart.Series["Series4"].Name = "IT";
            chart.Series["IT"].MarkerStyle = MarkerStyle.Circle;
            chart.Series["IT"].MarkerSize = 5;

            // Create Memorysteam to dump the image
            MemoryStream ms = new MemoryStream();
            CreateChartArea(chart).SaveImage(ms);
            return File(ms.GetBuffer(), @"image/png");
        }

        public Title CreateTitle()
        {
            Title title = new Title();
            title.Text = "Cost Comparison";
            title.ShadowColor = Color.FromArgb(32, 0, 0, 0);
            title.Font = new Font("Trebuchet MS", 14F, FontStyle.Bold);
            title.ShadowOffset = 3;
            title.ForeColor = Color.FromArgb(26, 59, 105);
            return title;
        }
        public Chart CreateChartArea(Chart chart)
        {
            ChartArea chartArea = new ChartArea();
            chartArea.Name = "Cost Comaparison";
            chartArea.BackColor = Color.WhiteSmoke;

            // X Axis interval
            chartArea.AxisX.Interval = 1;

            // Set the Custom Labebls
            CustomLabel onPremXLabel = new CustomLabel(-0.5, 0.5, "On Premise", 0, LabelMarkStyle.None);
            CustomLabel azureXLabel = new CustomLabel(0.75, 1.25, "Azure", 0, LabelMarkStyle.None);
            chartArea.AxisX.CustomLabels.Add(onPremXLabel);
            chartArea.AxisX.CustomLabels.Add(azureXLabel);


            //chartArea.AxisY.Title = "Costs (in $)";
            chartArea.AxisY.LabelStyle.Format = "C";
            //chartArea.AxisY.TitleFont = new Font("Verdana,Arial,Helvetica,sans-serif",
            //            12F, FontStyle.Bold);
            //chartArea.AxisX.LabelStyle.Font =
            new Font("Verdana,Arial,Helvetica,sans-serif",
                     8F, FontStyle.Regular);
            chartArea.AxisY.LabelStyle.Font =
               new Font("Verdana,Arial,Helvetica,sans-serif",
                        8F, FontStyle.Regular);
            chartArea.AxisY.LineColor = Color.FromArgb(64, 64, 64, 64);
            chartArea.AxisX.LineColor = Color.FromArgb(64, 64, 64, 64);
            chartArea.AxisY.MajorGrid.LineColor = Color.FromArgb(64, 64, 64, 64);
            chartArea.AxisX.MajorGrid.LineColor = Color.FromArgb(64, 64, 64, 64);

            chart.ChartAreas.Add(chartArea);
            chart.ChartAreas["Cost Comaparison"].AxisY.MajorGrid.Enabled = false;
            chart.ChartAreas["Cost Comaparison"].AxisX.MajorGrid.Enabled = false;
            chartArea.AxisX.MinorTickMark.Enabled = false;
            chartArea.AxisX.MajorTickMark.Enabled = false;
            return chart;
        }    

1 个答案:

答案 0 :(得分:1)

处理CustomizeLegend事件,如下所示:

    private void Chart1_CustomizeLegend(object sender, CustomizeLegendEventArgs e)
    {
        foreach (LegendItem li in e.LegendItems)
        {
            li.ImageStyle = LegendImageStyle.Marker;
            li.MarkerStyle = MarkerStyle.Circle;
            li.MarkerSize = 72;
        }
    }

enter image description here

编辑:在Controller中,在创建图表之后,在将其返回View之前,注册处理事件,如下所示:

            chart1.CustomizeLegend += Chart1_CustomizeLegend;