2014-03-05 3 views
1

enter image description hereMVC 차트 (범례) 변경 색상

4 가지 색상을 변경하고 싶습니다. 나는 클래스 System.Web.Helpers.ChartTheme을 안다. 그러나 나는 그 클래스에 의해서만 테마를 바꿀 수있다.

차트 색상 자체를 변경하려면 어떻게해야합니까? 이에 대해 자세히 살펴 갖는

답변

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());

+0

하지만'Palette'와'PaletteCustomColors'와 같은 속성을 가지고 있지 않은'System.Web.Helpers.Chart' 클래스에서 작업하고 있습니다 : ( – levi

+1

@levi? 내 예제에서는 System.Web.Helpers도 사용하고 있습니다. . 게시 한 코드를 사용할 수 있습니까? 다시 시도하십시오.이 작동하는 경우 upvote :-) – Dave3of5

+1

당신은 좋습니다. 그것은 작동합니다. 너 내 상향 투표가 이미있다. – levi