【问题标题】:Silverlight chart colours change on every refreshSilverlight 图表颜色在每次刷新时都会发生变化
【发布时间】:2013-12-20 17:50:05
【问题描述】:

您好,我正在从后面的代码向 silverlight 柱形图添加系列。单击按钮事件时,我首先清除系列,然后动态添加系列。现在我发现每次颜色都会随机变化,而不管系列是否发生变化。 说在第一次点击事件中添加了两个系列,图表有颜色 1 和颜色 2。下次即使恰好添加了两个系列(在清除现有的先前系列之后当然会),我可能会得到颜色 3 和颜色 3。我在不同的图表类型中观察到的情况相同。如何在不绑定模板颜色的情况下获得一致的颜色。我正在使用 Silverlight 5.0 版 我可以获得相同颜色的唯一方法是在以下情况下。假设我在模板中定义了 n 个(比如 5 个)颜色,并且每次都添加了 n 个系列。现在,如果我第一次添加 4,它会显示第 4 种颜色。现在下次如果我添加 4,它将显示从 2 到 5。这意味着它会循环尝试显示其中定义的所有颜色。

【问题讨论】:

  • 您可以为DataPoint 创建多个样式并将它们显式设置为系列的DataPointStyle 属性。

标签: silverlight charts


【解决方案1】:

我通过在后面的代码中重新创建图表解决了这个问题,而不是在 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);
        }
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-06
    • 2020-05-19
    • 2012-11-29
    • 2016-06-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多