更改MVC图表(图例)颜色

时间:2014-03-05 13:43:35

标签: c# asp.net-mvc charts styles legend

enter image description here

我想改变那4种颜色。我知道班级System.Web.Helpers.ChartTheme,但我只能改变该班级的主题。

如何更改图表颜色?

1 个答案:

答案 0 :(得分:1)

仔细看看这个我觉得这就是你想要的:

http://truncatedcodr.wordpress.com/2012/09/18/system-web-helpers-chart-custom-themes/

如果你举例我可以在这里给出。

编辑:这就是我所做的:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Drawing;
using System.Web.UI.DataVisualization.Charting;
using System.Text;
using System.Xml;

namespace MyMvcApplication
{
    public class Theme
    {
        public static string GetTheme()
        {
            ChartArea ca = new System.Web.UI.DataVisualization.Charting.ChartArea("Default");
            var chart = new System.Web.UI.DataVisualization.Charting.Chart();
            chart.BackColor = Color.Azure;
            chart.BackGradientStyle = GradientStyle.TopBottom;
            chart.BackSecondaryColor = Color.White;
            chart.BorderColor = Color.FromArgb(26, 59, 105);
            chart.BorderlineDashStyle = ChartDashStyle.Solid;
            chart.BorderWidth = 2;
            chart.Palette = ChartColorPalette.None;
            chart.PaletteCustomColors = new Color[] { Color.Lime, Color.Red, 
        Color.Orange, Color.Yellow, Color.Green, Color.Blue, Color.Purple,
         Color.Black };
            chart.ChartAreas.Add(new ChartArea("Default")
            {
                BackColor = Color.FromArgb(64, 165, 191, 228),
                BackGradientStyle = GradientStyle.TopBottom,
                BackSecondaryColor = Color.White,
                BorderColor = Color.FromArgb(64, 64, 64, 64),
                BorderDashStyle = ChartDashStyle.Solid,
                ShadowColor = Color.Transparent,
                Area3DStyle = new ChartArea3DStyle()
                {
                    LightStyle = LightStyle.Simplistic,
                    Enable3D = true,
                    Inclination = 5,
                    IsClustered = true,
                    IsRightAngleAxes = true,
                    Perspective = 5,
                    Rotation = 0,
                    WallWidth = 0
                }
            });
            chart.Legends.Add(new Legend("All")
                {
                    BackColor = Color.Transparent,
                    Font = new Font("Trebuchet MS", 8.25f, FontStyle.Bold,
                    GraphicsUnit.Point),
                    IsTextAutoFit = false
                }
                );
            chart.BorderSkin.SkinStyle = BorderSkinStyle.Emboss;

            var cs = chart.Serializer;
            cs.IsTemplateMode = true;
            //cs.Content = SerializationContents.Appearance;
            cs.Format = SerializationFormat.Xml;
            var sb = new StringBuilder();
            XmlWriterSettings settings = new XmlWriterSettings();
            settings.OmitXmlDeclaration = true;
            using (XmlWriter xw = XmlWriter.Create(sb, settings))
            {
                cs.Save(xw);
            }
            string theme = sb.ToString();
            return theme;
        }
    }
}

将PaletteCustomColors更改为您想要的颜色。您还可以使用各种样式设置。

像这样使用:

Chart myChart = new Chart(width: 600, height: 400, theme: MyMvcApplication.Theme.GetTheme());