2013-12-20 79 views
0

您好我正在从代码后面向silverlight列表图表添加系列。点击按钮事件我首先清除系列,然后动态添加系列。现在我发现每次颜色随机变化,无论系列中是否有变化。 说第一次点击事件两个系列添加了图表并有颜色1和颜色2。下一次,即使正好添加了两个系列(在清除以前的系列之后的偏离过程),我可能会得到颜色3和颜色3.同样的事情我在不同的图表类型中观察到。如何获得一致的颜色,而无需在模板中绑定颜色。我正在使用silverlight 5.0版 我可以获得相同颜色的唯一方法是在以下情况。假设我在模板中定义了n个(比如5个)颜色,并且每次都精确地添加n个系列。现在,如果我第一次添加4,它会显示第一个4色。现在下一次,如果我加4,它会显示从2到5.这意味着它周期性地尝试显示其中定义的所有颜色。Silverlight图表颜色在每次刷新时都会更改

+0

您可以创建多个样式'DataPoint',并明确将它们设置为系列'DataPointStyle'广告载体。 – vorrtex

回答

0

我解决了这个问题,在代码后面重新创建图表,而不是在XAML中使用绑定。

Chart chartUsers = new Chart(); 
chartUsers.Palette = palette; 
chartUsers.SetValue(Grid.RowProperty, 0); 
chartUsers.SetValue(Grid.ColumnProperty, 0); 
chartUsers.Title = Resources["DashboardUsersTitle"].ToString(); 
chartUsers.Style = (Style)Application.Current.Resources["ChartStyle1"]; //my style 
chartUsers.Height = 250; 
gridFirstRow.Children.Add(chartUsers); 

现在你可以创建系列,轴和绑定的东西给他们。

您可以将自定义颜色添加到调色板,像这样(例如):

private void CreateChartPalette() 
    { 
     List<Color> colors = new List<Color>(); 
     colors.Add(Color.FromArgb(255, 88, 113, 165)); 
     colors.Add(Color.FromArgb(255, 78, 149, 51));//dark green 
     colors.Add(Color.FromArgb(255, 32, 153, 161));//green-blue 
     colors.Add(Color.FromArgb(255, 201, 152, 0));//orange 
     colors.Add(Color.FromArgb(255, 166, 22, 22));//ref 
     colors.Add(Color.FromArgb(255, 196, 192, 85));//yellow 
     colors.Add(Color.FromArgb(255, 86, 168, 117));//light green 
     colors.Add(Color.FromArgb(255, 117, 49, 49));//dark red 
     colors.Add(Color.FromArgb(255, 195, 146, 0));//orange 2 
     colors.Add(Color.FromArgb(255, 128, 126, 86));//dark yellow 
     colors.Add(Color.FromArgb(255, 86, 121, 128));//dark blue 
     colors.Add(Color.FromArgb(255, 58, 115, 25));//dark green 
     colors.Add(Color.FromArgb(255, 126, 147, 152));//grey-blue 
     colors.Add(Color.FromArgb(255, 111, 169, 133));//light green 2 
     colors.Add(Color.FromArgb(255, 97, 55, 96));//purple 
     colors.Add(Color.FromArgb(255, 250, 229, 166));//yellow 
     colors.Add(Color.FromArgb(255, 72, 109, 117));//dark blue 2 

     palette = new ResourceDictionaryCollection(); 

     Random rand = new Random(); 

     for (int i = 0; i < colors.Count; i++) 
     { 
      LinearGradientBrush lgb = new LinearGradientBrush(); 
      lgb.StartPoint = new Point(0, 0); 
      lgb.EndPoint = new Point(0.5, 1); 

      GradientStop gsBegin = new GradientStop(); 
      gsBegin.Color = Color.FromArgb(128, colors[i].R, colors[i].G, colors[i].B); 
      gsBegin.Offset = 0; 

      GradientStop gsEnd = new GradientStop(); 
      gsEnd.Color = colors[i]; 
      gsEnd.Offset = 1; 

      lgb.GradientStops.Add(gsBegin); 
      lgb.GradientStops.Add(gsEnd); 

      Style style = new Style(typeof(DataPoint)); 
      style.Setters.Add(new Setter(BackgroundProperty, lgb)); 

      ResourceDictionary dictionary = new ResourceDictionary(); 
      dictionary.Add("DataPointStyle", style); 

      palette.Add(dictionary); 
     } 
    } 
相关问题